From be84c8617244ddff2903a38ee5dfca53af940aba Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 4 May 2019 10:35:29 +0200 Subject: [PATCH] fix: use t.Run in tests instead of for{} with i --- internal/multipart/reader_test.go | 42 ++++++----- typecheck/builtin/any_test.go | 11 +-- typecheck/builtin/int_test.go | 44 +++++++----- typecheck/builtin/string_test.go | 111 ++++++++++++++++-------------- 4 files changed, 116 insertions(+), 92 deletions(-) diff --git a/internal/multipart/reader_test.go b/internal/multipart/reader_test.go index a726dce..0c4cc02 100644 --- a/internal/multipart/reader_test.go +++ b/internal/multipart/reader_test.go @@ -2,6 +2,7 @@ package multipart import ( "bytes" + "fmt" "testing" ) @@ -186,17 +187,20 @@ func TestNoName(t *testing.T) { for i, test := range tests { - mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - if err != nil { - t.Errorf("%d: unexpected error <%s>", i, err) - continue - } + mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) - if err = mpr.Parse(); err != ErrMissingDataName { - t.Errorf("%d: expected the error <%s>, got <%s>", i, ErrMissingDataName, err) - continue - } + if err != nil { + t.Errorf("unexpected error <%s>", err) + return + } + + if err = mpr.Parse(); err != ErrMissingDataName { + t.Errorf("expected the error <%s>, got <%s>", ErrMissingDataName, err) + return + } + }) } @@ -225,18 +229,20 @@ func TestNoHeader(t *testing.T) { } for i, test := range tests { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) + mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) - if err != nil { - t.Errorf("%d: unexpected error <%s>", i, err) - continue - } + if err != nil { + t.Errorf("unexpected error <%s>", err) + return + } - if err = mpr.Parse(); err != ErrNoHeader { - t.Errorf("%d: expected the error <%s>, got <%s>", i, ErrNoHeader, err) - continue - } + if err = mpr.Parse(); err != ErrNoHeader { + t.Errorf("expected the error <%s>, got <%s>", ErrNoHeader, err) + return + } + }) } diff --git a/typecheck/builtin/any_test.go b/typecheck/builtin/any_test.go index 7ef19d2..de4f260 100644 --- a/typecheck/builtin/any_test.go +++ b/typecheck/builtin/any_test.go @@ -1,6 +1,7 @@ package builtin_test import ( + "fmt" "testing" "git.xdrm.io/go/aicra/typecheck/builtin" @@ -74,10 +75,12 @@ func TestAny_AlwaysTrue(t *testing.T) { } for i, value := range values { - if !checker(value) { - t.Errorf("%d: expect value to be valid", i) - t.Fail() - } + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + if !checker(value) { + t.Errorf("expect value to be valid") + t.Fail() + } + }) } } diff --git a/typecheck/builtin/int_test.go b/typecheck/builtin/int_test.go index be43293..63dc04e 100644 --- a/typecheck/builtin/int_test.go +++ b/typecheck/builtin/int_test.go @@ -1,6 +1,7 @@ package builtin_test import ( + "fmt" "math" "testing" @@ -38,18 +39,21 @@ func TestInt_AvailableTypes(t *testing.T) { } for _, test := range tests { - checker := inst.Checker(test.Type) - - if checker == nil { - if test.Handled { - t.Errorf("expect %q to be handled", test.Type) + t.Run(test.Type, func(t *testing.T) { + checker := inst.Checker(test.Type) + if checker == nil { + if test.Handled { + t.Errorf("expect %q to be handled", test.Type) + t.Fail() + } + return } - continue - } - if !test.Handled { - t.Errorf("expect %q NOT to be handled", test.Type) - } + if !test.Handled { + t.Errorf("expect %q NOT to be handled", test.Type) + t.Fail() + } + }) } } @@ -96,17 +100,19 @@ func TestInt_Values(t *testing.T) { } for i, test := range tests { - if checker(test.Value) { - if !test.Valid { - t.Errorf("%d: expect value to be invalid", i) + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + if checker(test.Value) { + if !test.Valid { + t.Errorf("expect value to be invalid") + t.Fail() + } + return + } + if test.Valid { + t.Errorf("expect value to be valid") t.Fail() } - continue - } - if test.Valid { - t.Errorf("%d: expect value to be valid", i) - t.Fail() - } + }) } } diff --git a/typecheck/builtin/string_test.go b/typecheck/builtin/string_test.go index 0c20579..3a0912f 100644 --- a/typecheck/builtin/string_test.go +++ b/typecheck/builtin/string_test.go @@ -1,6 +1,7 @@ package builtin_test import ( + "fmt" "testing" "git.xdrm.io/go/aicra/typecheck/builtin" @@ -52,18 +53,20 @@ func TestString_AvailableTypes(t *testing.T) { } for _, test := range tests { - checker := inst.Checker(test.Type) + t.Run(test.Type, func(t *testing.T) { + checker := inst.Checker(test.Type) - if checker == nil { - if test.Handled { - t.Errorf("expect %q to be handled", test.Type) + if checker == nil { + if test.Handled { + t.Errorf("expect %q to be handled", test.Type) + } + return } - continue - } - if !test.Handled { - t.Errorf("expect %q NOT to be handled", test.Type) - } + if !test.Handled { + t.Errorf("expect %q NOT to be handled", test.Type) + } + }) } } @@ -91,18 +94,20 @@ func TestString_AnyLength(t *testing.T) { } for i, test := range tests { - if checker(test.Value) { - if !test.Valid { - t.Errorf("%d: expect value to be invalid", i) - t.Fail() + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + if checker(test.Value) { + if !test.Valid { + t.Errorf("expect value to be invalid") + t.Fail() + } + return } - continue - } - if test.Valid { - t.Errorf("%d: expect value to be valid", i) - t.Fail() + if test.Valid { + t.Errorf("expect value to be valid") + t.Fail() - } + } + }) } } @@ -128,25 +133,27 @@ func TestString_FixedLength(t *testing.T) { } for i, test := range tests { - checker := builtin.NewString().Checker(test.Type) - if checker == nil { - t.Errorf("%d: expect %q to be handled", i, test.Type) - t.Fail() - continue - } - - if checker(test.Value) { - if !test.Valid { - t.Errorf("%d: expect value to be invalid", i) + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + checker := builtin.NewString().Checker(test.Type) + if checker == nil { + t.Errorf("expect %q to be handled", test.Type) t.Fail() + return } - continue - } - if test.Valid { - t.Errorf("%d: expect value to be valid", i) - t.Fail() - } + if checker(test.Value) { + if !test.Valid { + t.Errorf("expect value to be invalid") + t.Fail() + } + return + } + if test.Valid { + t.Errorf("expect value to be valid") + t.Fail() + + } + }) } } @@ -187,25 +194,27 @@ func TestString_VariableLength(t *testing.T) { } for i, test := range tests { - checker := builtin.NewString().Checker(test.Type) - if checker == nil { - t.Errorf("%d: expect %q to be handled", i, test.Type) - t.Fail() - continue - } - - if checker(test.Value) { - if !test.Valid { - t.Errorf("%d: expect value to be invalid", i) + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + checker := builtin.NewString().Checker(test.Type) + if checker == nil { + t.Errorf("expect %q to be handled", test.Type) t.Fail() + return } - continue - } - if test.Valid { - t.Errorf("%d: expect value to be valid", i) - t.Fail() - } + if checker(test.Value) { + if !test.Valid { + t.Errorf("expect value to be invalid") + t.Fail() + } + return + } + if test.Valid { + t.Errorf("expect value to be valid") + t.Fail() + + } + }) } }