oh-my-posh/src/segment_owm_test.go

276 lines
6.9 KiB
Go
Raw Normal View History

2021-08-15 12:11:02 -07:00
package main
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
const (
OWMAPIURL = "http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key"
)
func TestOWMSegmentSingle(t *testing.T) {
2021-08-15 12:11:02 -07:00
cases := []struct {
Case string
JSONResponse string
ExpectedString string
ExpectedEnabled bool
2021-11-02 23:33:15 -07:00
Template string
2021-08-15 12:11:02 -07:00
Error error
}{
{
Case: "Sunny Display",
JSONResponse: `{"weather":[{"icon":"01d"}],"main":{"temp":20}}`,
ExpectedString: "\ufa98 (20°C)",
2021-08-15 12:11:02 -07:00
ExpectedEnabled: true,
},
2021-11-02 23:33:15 -07:00
{
Case: "Sunny Display",
JSONResponse: `{"weather":[{"icon":"01d"}],"main":{"temp":20}}`,
ExpectedString: "\ufa98 (20°C)",
ExpectedEnabled: true,
Template: "{{.Weather}} ({{.Temperature}}{{.UnitIcon}})",
},
{
Case: "Sunny Display",
JSONResponse: `{"weather":[{"icon":"01d"}],"main":{"temp":20}}`,
ExpectedString: "\ufa98 ",
ExpectedEnabled: true,
Template: "{{.Weather}} ",
},
2021-08-15 12:11:02 -07:00
{
Case: "Error in retrieving data",
JSONResponse: "nonsense",
Error: errors.New("Something went wrong"),
ExpectedEnabled: false,
},
}
for _, tc := range cases {
env := &MockedEnvironment{}
2021-11-26 01:37:33 -08:00
var props properties = map[Property]interface{}{
APIKey: "key",
Location: "AMSTERDAM,NL",
Units: "metric",
CacheTimeout: 0,
2021-08-15 12:11:02 -07:00
}
env.On("doGet", OWMAPIURL).Return([]byte(tc.JSONResponse), tc.Error)
2021-08-15 12:11:02 -07:00
2021-11-02 23:33:15 -07:00
if tc.Template != "" {
2021-11-26 01:37:33 -08:00
props[SegmentTemplate] = tc.Template
2021-11-02 23:33:15 -07:00
}
2021-08-15 12:11:02 -07:00
o := &owm{
props: props,
env: env,
}
enabled := o.enabled()
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)
if !enabled {
continue
}
assert.Equal(t, tc.ExpectedString, o.string(), tc.Case)
}
}
func TestOWMSegmentIcons(t *testing.T) {
2021-08-15 12:11:02 -07:00
cases := []struct {
Case string
IconID string
ExpectedIconString string
}{
{
Case: "Sunny Display day",
2021-08-15 12:11:02 -07:00
IconID: "01d",
ExpectedIconString: "\ufa98",
},
{
Case: "Light clouds Display day",
2021-08-15 12:11:02 -07:00
IconID: "02d",
ExpectedIconString: "\ufa94",
},
{
Case: "Cloudy Display day",
2021-08-15 12:11:02 -07:00
IconID: "03d",
ExpectedIconString: "\ue33d",
},
{
Case: "Broken Clouds Display day",
2021-08-15 12:11:02 -07:00
IconID: "04d",
ExpectedIconString: "\ue312",
},
{
Case: "Shower Rain Display day",
2021-08-15 12:11:02 -07:00
IconID: "09d",
ExpectedIconString: "\ufa95",
},
{
Case: "Rain Display day",
2021-08-15 12:11:02 -07:00
IconID: "10d",
ExpectedIconString: "\ue308",
},
{
Case: "Thunderstorm Display day",
2021-08-15 12:11:02 -07:00
IconID: "11d",
ExpectedIconString: "\ue31d",
},
{
Case: "Snow Display day",
2021-08-15 12:11:02 -07:00
IconID: "13d",
ExpectedIconString: "\ue31a",
},
{
Case: "Fog Display day",
2021-08-15 12:11:02 -07:00
IconID: "50d",
ExpectedIconString: "\ue313",
},
{
Case: "Sunny Display night",
IconID: "01n",
ExpectedIconString: "\ufa98",
},
{
Case: "Light clouds Display night",
IconID: "02n",
ExpectedIconString: "\ufa94",
},
{
Case: "Cloudy Display night",
IconID: "03n",
ExpectedIconString: "\ue33d",
},
{
Case: "Broken Clouds Display night",
IconID: "04n",
ExpectedIconString: "\ue312",
},
{
Case: "Shower Rain Display night",
IconID: "09n",
ExpectedIconString: "\ufa95",
},
{
Case: "Rain Display night",
IconID: "10n",
ExpectedIconString: "\ue308",
},
{
Case: "Thunderstorm Display night",
IconID: "11n",
ExpectedIconString: "\ue31d",
},
{
Case: "Snow Display night",
IconID: "13n",
ExpectedIconString: "\ue31a",
},
{
Case: "Fog Display night",
IconID: "50n",
ExpectedIconString: "\ue313",
},
2021-08-15 12:11:02 -07:00
}
for _, tc := range cases {
env := &MockedEnvironment{}
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, tc.IconID)
expectedString := fmt.Sprintf("%s (20°C)", tc.ExpectedIconString)
2021-08-15 12:11:02 -07:00
env.On("doGet", OWMAPIURL).Return([]byte(response), nil)
2021-08-15 12:11:02 -07:00
o := &owm{
2021-11-26 01:37:33 -08:00
props: map[Property]interface{}{
APIKey: "key",
Location: "AMSTERDAM,NL",
Units: "metric",
CacheTimeout: 0,
},
env: env,
2021-08-15 12:11:02 -07:00
}
assert.Nil(t, o.setStatus())
assert.Equal(t, expectedString, o.string(), tc.Case)
}
// test with hyperlink enabled
for _, tc := range cases {
env := &MockedEnvironment{}
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, tc.IconID)
expectedString := fmt.Sprintf("[%s (20°C)](http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key)", tc.ExpectedIconString)
env.On("doGet", OWMAPIURL).Return([]byte(response), nil)
o := &owm{
2021-11-26 01:37:33 -08:00
props: map[Property]interface{}{
APIKey: "key",
Location: "AMSTERDAM,NL",
Units: "metric",
CacheTimeout: 0,
EnableHyperlink: true,
SegmentTemplate: "[{{.Weather}} ({{.Temperature}}{{.UnitIcon}})]({{.URL}})",
},
env: env,
}
assert.Nil(t, o.setStatus())
assert.Equal(t, expectedString, o.string(), tc.Case)
}
2021-08-15 12:11:02 -07:00
}
func TestOWMSegmentFromCache(t *testing.T) {
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, "01d")
expectedString := fmt.Sprintf("%s (20°C)", "\ufa98")
env := &MockedEnvironment{}
cache := &MockedCache{}
2021-11-26 01:37:33 -08:00
o := &owm{
props: map[Property]interface{}{
2021-09-23 22:36:21 -07:00
APIKey: "key",
Location: "AMSTERDAM,NL",
Units: "metric",
},
2021-11-26 01:37:33 -08:00
env: env,
}
cache.On("get", "owm_response").Return(response, true)
cache.On("get", "owm_url").Return("http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key", true)
cache.On("set").Return()
env.On("cache", nil).Return(cache)
assert.Nil(t, o.setStatus())
assert.Equal(t, expectedString, o.string())
}
func TestOWMSegmentFromCacheWithHyperlink(t *testing.T) {
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, "01d")
expectedString := fmt.Sprintf("[%s (20°C)](http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key)", "\ufa98")
env := &MockedEnvironment{}
cache := &MockedCache{}
2021-11-26 01:37:33 -08:00
o := &owm{
props: map[Property]interface{}{
APIKey: "key",
Location: "AMSTERDAM,NL",
Units: "metric",
EnableHyperlink: true,
2021-11-26 01:37:33 -08:00
SegmentTemplate: "[{{.Weather}} ({{.Temperature}}{{.UnitIcon}})]({{.URL}})",
},
2021-11-26 01:37:33 -08:00
env: env,
}
cache.On("get", "owm_response").Return(response, true)
cache.On("get", "owm_url").Return("http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key", true)
cache.On("set").Return()
env.On("cache", nil).Return(cache)
assert.Nil(t, o.setStatus())
assert.Equal(t, expectedString, o.string())
}