Skip to content

Commit

Permalink
home: fix docs & naming
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Jun 15, 2021
1 parent af44a86 commit 84df492
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
3 changes: 2 additions & 1 deletion internal/home/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func (d Duration) MarshalText() (text []byte, err error) {
return []byte(d.String()), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface for Duration.
// UnmarshalText implements the encoding.TextUnmarshaler interface for
// *Duration.
func (d *Duration) UnmarshalText(b []byte) (err error) {
defer func() { err = errors.Annotate(err, "unmarshalling duration: %w") }()

Expand Down
28 changes: 14 additions & 14 deletions internal/home/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ const (
`- 1ms`
)

// defaultTestDur is the default time.Duration value to be used throughout the tests of
// Duration.
const defaultTestDur = time.Millisecond

// checkFields verifies m's fields. It expects the m to be unmarshalled from
// one of the constant strings above.
func (m *durationEncodingTester) checkFields(t *testing.T, d Duration) {
Expand Down Expand Up @@ -110,15 +114,11 @@ func (m *durationEncodingTester) checkFields(t *testing.T, d Duration) {
})
}

// defaultTestDur is the default time.Duration value to be used throughout the tests of
// Duration.
const defaultTestDur = time.Millisecond

func TestDuration_MarshalText(t *testing.T) {
d := Duration{defaultTestDur}
dPtr := &d

m := durationEncodingTester{
v := durationEncodingTester{
PtrMap: map[string]*Duration{"dur": dPtr},
PtrSlice: []*Duration{dPtr},
PtrValue: dPtr,
Expand All @@ -132,15 +132,15 @@ func TestDuration_MarshalText(t *testing.T) {
b := &bytes.Buffer{}
t.Run("json", func(t *testing.T) {
t.Cleanup(b.Reset)
err := json.NewEncoder(b).Encode(m)
err := json.NewEncoder(b).Encode(v)
require.NoError(t, err)

assert.JSONEq(t, jsonStr, b.String())
})

t.Run("yaml", func(t *testing.T) {
t.Cleanup(b.Reset)
err := yaml.NewEncoder(b).Encode(m)
err := yaml.NewEncoder(b).Encode(v)
require.NoError(t, err)

assert.YAMLEq(t, yamlStr, b.String(), b.String())
Expand All @@ -156,26 +156,26 @@ func TestDuration_MarshalText(t *testing.T) {

func TestDuration_UnmarshalText(t *testing.T) {
d := Duration{defaultTestDur}
var m *durationEncodingTester
var v *durationEncodingTester

t.Run("json", func(t *testing.T) {
m = &durationEncodingTester{}
v = &durationEncodingTester{}

r := strings.NewReader(jsonStr)
err := json.NewDecoder(r).Decode(m)
err := json.NewDecoder(r).Decode(v)
require.NoError(t, err)

m.checkFields(t, d)
v.checkFields(t, d)
})

t.Run("yaml", func(t *testing.T) {
m = &durationEncodingTester{}
v = &durationEncodingTester{}

r := strings.NewReader(yamlStr)
err := yaml.NewDecoder(r).Decode(m)
err := yaml.NewDecoder(r).Decode(v)
require.NoError(t, err)

m.checkFields(t, d)
v.checkFields(t, d)
})

t.Run("direct", func(t *testing.T) {
Expand Down

0 comments on commit 84df492

Please sign in to comment.