github.com/haraldrudell/parl@v0.4.176/ptime/duration_test.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package ptime
     7  
     8  import (
     9  	"math"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestDuration(t *testing.T) {
    15  	type args struct {
    16  		d time.Duration
    17  	}
    18  	tests := []struct {
    19  		name           string
    20  		args           args
    21  		wantPrintableD string
    22  	}{
    23  		{"ns", args{time.Duration(math.Round(math.Pi * 100))}, "314ns"},     // 314ns
    24  		{"µs", args{time.Duration(math.Round(math.Pi * 1e5))}, "314µs"},     // 314.159µs
    25  		{"ms", args{time.Duration(math.Round(math.Pi * 1e8))}, "314ms"},     // 314.159265ms
    26  		{"10s", args{time.Duration(math.Round(math.Pi * 1e9))}, "3.1s"},     // 3.141592654s
    27  		{"s", args{time.Duration(math.Round(math.Pi * 1e10))}, "31s"},       // 31.415926536s
    28  		{"min", args{time.Duration(math.Round(math.Pi * 1e11))}, "5m14s"},   // 5m14.159265359s
    29  		{"h", args{time.Duration(math.Round(math.Pi * 2e13))}, "17h27m"},    // 17h27m11.853071796s
    30  		{"days", args{time.Duration(math.Round(math.Pi * 1e14))}, "3d15h"},  // 87h15m59.265358979s
    31  		{"months", args{time.Duration(math.Round(math.Pi * 1e15))}, "36d"},  // 872h39m52.653589793s
    32  		{"years", args{time.Duration(math.Round(math.Pi * 1e17))}, "3636d"}, // 87266h27m45.358979328s
    33  		{"negative", args{time.Duration(-time.Second / 3)}, "-333ms"},       // 87266h27m45.358979328s
    34  	}
    35  	for _, tt := range tests {
    36  		t.Run(tt.name, func(t *testing.T) {
    37  			actual := Duration(tt.args.d)
    38  			t.Logf("%s Duration(%s) → %s", tt.name, tt.args.d.String(), actual)
    39  			if actual != tt.wantPrintableD {
    40  				t.Errorf("Duration() = %q, want %q", actual, tt.wantPrintableD)
    41  				t.Fail()
    42  			}
    43  		})
    44  	}
    45  }