mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-10 04:54:03 -08:00
chore(template): add tests for roundSeconds
This commit is contained in:
parent
eef50268a9
commit
c94e5e6efb
|
@ -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) {
|
||||
|
|
46
src/template/round_test.go
Normal file
46
src/template/round_test.go
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue