chore(template): add tests for roundSeconds

This commit is contained in:
Jan De Dobbeleer 2022-01-28 10:37:05 +01:00 committed by Jan De Dobbeleer
parent eef50268a9
commit c94e5e6efb
2 changed files with 50 additions and 4 deletions

View file

@ -37,10 +37,10 @@ func secondsRound(seconds interface{}) string {
var (
second = 1
minute = 60
hour = minute * 60
day = hour * 24
month = day * 30
year = day * 365
hour = 3600
day = 86400
month = 2629800
year = 31560000
)
var builder strings.Builder
writePart := func(unit int, name string) {

View file

@ -0,0 +1,46 @@
package template
import (
"oh-my-posh/environment"
"oh-my-posh/mock"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRoundSeconds(t *testing.T) {
cases := []struct {
Case string
Expected string
Template string
ShouldError bool
}{
{Case: "int - 1 second", Expected: "1s", Template: "{{ secondsRound 1 }}"},
{Case: "double - 1 second", Expected: "1s", Template: "{{ secondsRound 1.1 }}"},
{Case: "int - 1 minute", Expected: "1m", Template: "{{ secondsRound 60 }}"},
{Case: "int - 2 minutes 30 seconds", Expected: "2m 30s", Template: "{{ secondsRound 150 }}"},
{Case: "int - 1 day 2 minutes 30 seconds", Expected: "1d 2m 30s", Template: "{{ secondsRound 86550 }}"},
{Case: "double - 1 day 2 minutes 30 seconds", Expected: "1d 2m 30s", Template: "{{ secondsRound 86550.555 }}"},
{Case: "int - 1 month 1 day 2 minutes 30 seconds", Expected: "1mo 1d 2m 30s", Template: "{{ secondsRound 2716350 }}"},
{Case: "int - 1 year 1 month 1 day 2 minutes 30 seconds", Expected: "1y 1mo 1d 2m 30s", Template: "{{ secondsRound 34276350 }}"},
{Case: "error", Expected: "", Template: "{{ secondsRound foo }}", ShouldError: true},
}
env := &mock.MockedEnvironment{}
env.On("TemplateCache").Return(&environment.TemplateCache{
Env: make(map[string]string),
})
for _, tc := range cases {
tmpl := &Text{
Template: tc.Template,
Context: nil,
Env: env,
}
text, err := tmpl.Render()
if tc.ShouldError {
assert.Error(t, err)
continue
}
assert.Equal(t, tc.Expected, text, tc.Case)
}
}