2022-01-26 06:54:36 -08:00
|
|
|
package segments
|
2021-12-11 08:31:58 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2024-07-03 00:04:11 -07:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
2023-01-05 12:57:38 -08:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
2024-07-24 04:17:25 -07:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
2024-07-03 00:04:11 -07:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
2022-12-28 08:30:48 -08:00
|
|
|
|
2021-12-11 08:31:58 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-03 00:04:11 -07:00
|
|
|
testify_ "github.com/stretchr/testify/mock"
|
2021-12-11 08:31:58 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWTTrackedTime(t *testing.T) {
|
|
|
|
cases := []struct {
|
2024-07-30 03:37:02 -07:00
|
|
|
Case string
|
|
|
|
Seconds int
|
|
|
|
Expected string
|
|
|
|
Template string
|
|
|
|
Error error
|
2021-12-11 08:31:58 -08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
Case: "nothing tracked",
|
|
|
|
Seconds: 0,
|
|
|
|
Expected: "0s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "25 minutes",
|
|
|
|
Seconds: 1500,
|
|
|
|
Expected: "25m",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "2 hours",
|
|
|
|
Seconds: 7200,
|
|
|
|
Expected: "2h",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "2h 45m",
|
|
|
|
Seconds: 9900,
|
|
|
|
Expected: "2h 45m",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "negative number",
|
|
|
|
Seconds: -9900,
|
|
|
|
Expected: "2h 45m",
|
|
|
|
},
|
|
|
|
{
|
2024-07-30 03:37:02 -07:00
|
|
|
Case: "no cache 2h 45m",
|
|
|
|
Seconds: 9900,
|
|
|
|
Expected: "2h 45m",
|
2021-12-11 08:31:58 -08:00
|
|
|
},
|
|
|
|
{
|
2024-07-30 03:37:02 -07:00
|
|
|
Case: "api error",
|
|
|
|
Seconds: 2,
|
|
|
|
Expected: "0s",
|
|
|
|
Error: errors.New("api error"),
|
2021-12-11 08:31:58 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2024-07-03 00:04:11 -07:00
|
|
|
env := &mock.Environment{}
|
2023-03-11 01:05:29 -08:00
|
|
|
response := fmt.Sprintf(`{"cumulative_total": {"seconds": %.2f, "text": "x"}}`, float64(tc.Seconds))
|
2021-12-11 08:31:58 -08:00
|
|
|
|
2022-01-07 10:41:58 -08:00
|
|
|
env.On("HTTPRequest", FAKEAPIURL).Return([]byte(response), tc.Error)
|
2021-12-11 08:31:58 -08:00
|
|
|
|
2024-07-03 00:04:11 -07:00
|
|
|
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
2024-07-24 04:17:25 -07:00
|
|
|
env.On("Flags").Return(&runtime.Flags{})
|
2021-12-11 08:31:58 -08:00
|
|
|
|
2024-07-03 00:04:11 -07:00
|
|
|
env.On("TemplateCache").Return(&cache.Template{
|
2022-08-16 11:55:08 -07:00
|
|
|
Env: map[string]string{"HELLO": "hello"},
|
|
|
|
})
|
|
|
|
|
2022-01-26 05:10:18 -08:00
|
|
|
w := &Wakatime{
|
2022-01-26 04:53:35 -08:00
|
|
|
props: properties.Map{
|
2024-07-30 03:37:02 -07:00
|
|
|
URL: FAKEAPIURL,
|
2021-12-11 08:31:58 -08:00
|
|
|
},
|
|
|
|
env: env,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.ErrorIs(t, tc.Error, w.setAPIData(), tc.Case+" - Error")
|
2022-01-26 05:26:56 -08:00
|
|
|
assert.Equal(t, tc.Expected, renderTemplate(env, w.Template(), w), tc.Case+" - String")
|
2021-12-11 08:31:58 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-16 11:55:08 -07:00
|
|
|
|
|
|
|
func TestWTGetUrl(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
Expected string
|
|
|
|
URL string
|
|
|
|
ShouldError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Case: "no template",
|
|
|
|
Expected: "test",
|
|
|
|
URL: "test",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "template",
|
|
|
|
URL: "{{ .Env.HELLO }} world",
|
|
|
|
Expected: "hello world",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "error",
|
|
|
|
URL: "{{ .BURR }}",
|
|
|
|
ShouldError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2024-07-03 00:04:11 -07:00
|
|
|
env := &mock.Environment{}
|
2022-08-16 11:55:08 -07:00
|
|
|
|
2024-07-03 00:04:11 -07:00
|
|
|
env.On("Error", testify_.Anything)
|
|
|
|
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
|
|
|
env.On("TemplateCache").Return(&cache.Template{
|
2022-08-16 11:55:08 -07:00
|
|
|
Env: map[string]string{"HELLO": "hello"},
|
|
|
|
})
|
2024-07-24 04:17:25 -07:00
|
|
|
env.On("Flags").Return(&runtime.Flags{})
|
2022-08-16 11:55:08 -07:00
|
|
|
|
|
|
|
w := &Wakatime{
|
|
|
|
props: properties.Map{
|
|
|
|
URL: tc.URL,
|
|
|
|
},
|
|
|
|
env: env,
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := w.getURL()
|
|
|
|
|
|
|
|
if tc.ShouldError {
|
|
|
|
assert.Error(t, err, tc.Case)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
assert.Equal(t, tc.Expected, got, tc.Case)
|
|
|
|
}
|
|
|
|
}
|