This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package rfctime
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
|
||||
)
|
||||
|
||||
func TestSecondsF64Basics(t *testing.T) {
|
||||
d := NewSecondsF64(2*time.Second + 500*time.Millisecond)
|
||||
tst.AssertEqual(t, d.Duration(), 2500*time.Millisecond)
|
||||
tst.AssertEqual(t, d.Seconds(), 2.5)
|
||||
tst.AssertEqual(t, d.Milliseconds(), int64(2500))
|
||||
tst.AssertEqual(t, d.Microseconds(), int64(2500000))
|
||||
tst.AssertEqual(t, d.Nanoseconds(), int64(2500000000))
|
||||
tst.AssertEqual(t, d.Minutes(), 2.5/60.0)
|
||||
tst.AssertEqual(t, d.Hours(), 2.5/3600.0)
|
||||
tst.AssertEqual(t, d.String(), (2500 * time.Millisecond).String())
|
||||
}
|
||||
|
||||
func TestSecondsF64JSON(t *testing.T) {
|
||||
type Wrap struct {
|
||||
D SecondsF64 `json:"d"`
|
||||
}
|
||||
|
||||
w1 := Wrap{D: NewSecondsF64(2500 * time.Millisecond)}
|
||||
b, err := json.Marshal(w1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tst.AssertEqual(t, string(b), `{"d":2.5}`)
|
||||
|
||||
var w2 Wrap
|
||||
if err := json.Unmarshal(b, &w2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if math.Abs(float64(w2.D.Duration()-w1.D.Duration())) > float64(time.Microsecond) {
|
||||
t.Errorf("roundtrip mismatch: %v vs %v", w1.D, w2.D)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecondsF64UnmarshalJSONInvalid(t *testing.T) {
|
||||
var d SecondsF64
|
||||
if err := d.UnmarshalJSON([]byte(`"not-a-number"`)); err == nil {
|
||||
t.Errorf("expected error")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user