From af44a86a60ea815ca7100edc34db8acbdcc2cccf Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Tue, 15 Jun 2021 15:55:12 +0300 Subject: [PATCH] all: imp docs & tests --- HACKING.md | 15 ++++++-- internal/home/duration_test.go | 66 +++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/HACKING.md b/HACKING.md index 83e8ac1be0b..7bff9e772d2 100644 --- a/HACKING.md +++ b/HACKING.md @@ -197,8 +197,19 @@ attributes to make it work in Markdown renderers that strip "id". --> ### Formatting - * Decorate `break`, `continue`, `fallthrough`, `return`, and other function - exit points with empty lines unless it's the only statement in that block. + * Decorate `break`, `continue`, `fallthrough`, `return`, and other terminating + statements with empty lines unless it's the only statement in that block. + + * Group `require.*` blocks together with the presceding related statements, + but separate from the following `assert.*` and unrelated requirements. + + ```go + val, ok := valMap[key] + require.True(t, ok) + require.NotNil(t, val) + + assert.Equal(t, expected, val) + ``` * Use `gofumpt --extra -s`. diff --git a/internal/home/duration_test.go b/internal/home/duration_test.go index afbff16d508..6453634280b 100644 --- a/internal/home/duration_test.go +++ b/internal/home/duration_test.go @@ -56,50 +56,58 @@ const ( // 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) { - // PtrMap + t.Run("pointers_map", func(t *testing.T) { + require.NotNil(t, m.PtrMap) - require.NotNil(t, m.PtrMap) + fromPtrMap, ok := m.PtrMap["dur"] + require.True(t, ok) + require.NotNil(t, fromPtrMap) - fromPtrMap, ok := m.PtrMap["dur"] - require.True(t, ok) - require.NotNil(t, fromPtrMap) + assert.Equal(t, d, *fromPtrMap) + }) - // PtrSlice + t.Run("pointers_slice", func(t *testing.T) { + require.Len(t, m.PtrSlice, 1) - require.Len(t, m.PtrSlice, 1) + fromPtrSlice := m.PtrSlice[0] + require.NotNil(t, fromPtrSlice) - fromPtrSlice := m.PtrSlice[0] - require.NotNil(t, fromPtrSlice) + assert.Equal(t, d, *fromPtrSlice) + }) - // PtrArray + t.Run("pointers_array", func(t *testing.T) { + fromPtrArray := m.PtrArray[0] + require.NotNil(t, fromPtrArray) - fromPtrArray := m.PtrArray[0] - require.NotNil(t, fromPtrArray) + assert.Equal(t, d, *fromPtrArray) + }) - // PtrValue + t.Run("pointer_value", func(t *testing.T) { + require.NotNil(t, m.PtrValue) - require.NotNil(t, m.PtrValue) + assert.Equal(t, d, *m.PtrValue) + }) - // Map + t.Run("map", func(t *testing.T) { + fromMap, ok := m.Map["dur"] + require.True(t, ok) - var fromMap Duration - fromMap, ok = m.Map["dur"] - require.True(t, ok) + assert.Equal(t, d, fromMap) + }) - // Slice + t.Run("slice", func(t *testing.T) { + require.Len(t, m.Slice, 1) - require.Len(t, m.Slice, 1) + assert.Equal(t, d, m.Slice[0]) + }) - // Value Check + t.Run("array", func(t *testing.T) { + assert.Equal(t, d, m.Array[0]) + }) - assert.Equal(t, d, *fromPtrMap) - assert.Equal(t, d, *fromPtrSlice) - assert.Equal(t, d, *m.PtrValue) - assert.Equal(t, d, *fromPtrArray) - assert.Equal(t, d, fromMap) - assert.Equal(t, d, m.Slice[0]) - assert.Equal(t, d, m.Value) - assert.Equal(t, d, m.Array[0]) + t.Run("value", func(t *testing.T) { + assert.Equal(t, d, m.Value) + }) } // defaultTestDur is the default time.Duration value to be used throughout the tests of