Skip to content

Commit

Permalink
test: add test TestTimeDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant committed Feb 7, 2024
1 parent 5e717d1 commit 76e6068
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion commands/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (d *TimeDuration) String() string {

// s
if d.Duration < time.Minute {
return fmt.Sprintf("%ds", d.Duration.Seconds())
return fmt.Sprintf("%.2fs", d.Duration.Seconds())
}

// m
Expand Down
23 changes: 23 additions & 0 deletions commands/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package commands

import (
"testing"
"time"
)

func TestTimeDuration(t *testing.T) {
// ms
if (&TimeDuration{Duration: 100 * time.Millisecond}).String() != "100ms" {
t.Fatalf("expected 100ms, but got %s", (&TimeDuration{Duration: 100 * time.Millisecond}).String())
}

// s
if (&TimeDuration{Duration: 2*time.Second + 123*time.Millisecond}).String() != "2.12s" {
t.Fatalf("expected 2.12s, but got %s", (&TimeDuration{Duration: 2 * time.Second}).String())
}

// m
if (&TimeDuration{Duration: 2*time.Minute + 12*time.Second}).String() != "2m 12s" {
t.Fatalf("expected 2m 12s, but got %s", (&TimeDuration{Duration: 2 * time.Minute}).String())
}
}

0 comments on commit 76e6068

Please sign in to comment.