@@ -55,3 +55,46 @@ func TestFormat(t *testing.T) {
5555 }
5656 }
5757}
58+
59+ func TestDuration (t * testing.T ) {
60+ t .Parallel ()
61+
62+ ns := New ()
63+
64+ for i , test := range []struct {
65+ unit interface {}
66+ num interface {}
67+ expect interface {}
68+ }{
69+ {"nanosecond" , 10 , 10 * time .Nanosecond },
70+ {"ns" , 10 , 10 * time .Nanosecond },
71+ {"microsecond" , 20 , 20 * time .Microsecond },
72+ {"us" , 20 , 20 * time .Microsecond },
73+ {"µs" , 20 , 20 * time .Microsecond },
74+ {"millisecond" , 20 , 20 * time .Millisecond },
75+ {"ms" , 20 , 20 * time .Millisecond },
76+ {"second" , 30 , 30 * time .Second },
77+ {"s" , 30 , 30 * time .Second },
78+ {"minute" , 20 , 20 * time .Minute },
79+ {"m" , 20 , 20 * time .Minute },
80+ {"hour" , 20 , 20 * time .Hour },
81+ {"h" , 20 , 20 * time .Hour },
82+ {"hours" , 20 , false },
83+ {"hour" , "30" , 30 * time .Hour },
84+ } {
85+ result , err := ns .Duration (test .unit , test .num )
86+ if b , ok := test .expect .(bool ); ok && ! b {
87+ if err == nil {
88+ t .Errorf ("[%d] Duration didn't return an expected error, got %v" , i , result )
89+ }
90+ } else {
91+ if err != nil {
92+ t .Errorf ("[%d] Duration failed: %s" , i , err )
93+ continue
94+ }
95+ if result != test .expect {
96+ t .Errorf ("[%d] Duration got %v but expected %v" , i , result , test .expect )
97+ }
98+ }
99+ }
100+ }
0 commit comments