fix: use t.Run in tests instead of for{} with i

This commit is contained in:
Adrien Marquès 2019-05-04 10:35:29 +02:00
parent 21f48a65ee
commit be84c86172
4 changed files with 116 additions and 92 deletions

View File

@ -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
}
})
}

View File

@ -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()
}
})
}
}

View File

@ -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()
}
})
}
}

View File

@ -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()
}
})
}
}