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 ( import (
"bytes" "bytes"
"fmt"
"testing" "testing"
) )
@ -186,17 +187,20 @@ func TestNoName(t *testing.T) {
for i, test := range tests { 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 { mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
t.Errorf("%d: unexpected error <%s>", i, err)
continue
}
if err = mpr.Parse(); err != ErrMissingDataName { if err != nil {
t.Errorf("%d: expected the error <%s>, got <%s>", i, ErrMissingDataName, err) t.Errorf("unexpected error <%s>", err)
continue 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 { 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 { if err != nil {
t.Errorf("%d: unexpected error <%s>", i, err) t.Errorf("unexpected error <%s>", err)
continue return
} }
if err = mpr.Parse(); err != ErrNoHeader { if err = mpr.Parse(); err != ErrNoHeader {
t.Errorf("%d: expected the error <%s>, got <%s>", i, ErrNoHeader, err) t.Errorf("expected the error <%s>, got <%s>", ErrNoHeader, err)
continue return
} }
})
} }

View File

@ -1,6 +1,7 @@
package builtin_test package builtin_test
import ( import (
"fmt"
"testing" "testing"
"git.xdrm.io/go/aicra/typecheck/builtin" "git.xdrm.io/go/aicra/typecheck/builtin"
@ -74,10 +75,12 @@ func TestAny_AlwaysTrue(t *testing.T) {
} }
for i, value := range values { for i, value := range values {
if !checker(value) { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
t.Errorf("%d: expect value to be valid", i) if !checker(value) {
t.Fail() t.Errorf("expect value to be valid")
} t.Fail()
}
})
} }
} }

View File

@ -1,6 +1,7 @@
package builtin_test package builtin_test
import ( import (
"fmt"
"math" "math"
"testing" "testing"
@ -38,18 +39,21 @@ func TestInt_AvailableTypes(t *testing.T) {
} }
for _, test := range tests { 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 checker == nil {
if test.Handled { if test.Handled {
t.Errorf("expect %q to be handled", test.Type) t.Errorf("expect %q to be handled", test.Type)
t.Fail()
}
return
} }
continue
}
if !test.Handled { if !test.Handled {
t.Errorf("expect %q NOT to be handled", test.Type) 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 { for i, test := range tests {
if checker(test.Value) { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if !test.Valid { if checker(test.Value) {
t.Errorf("%d: expect value to be invalid", i) 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() 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 package builtin_test
import ( import (
"fmt"
"testing" "testing"
"git.xdrm.io/go/aicra/typecheck/builtin" "git.xdrm.io/go/aicra/typecheck/builtin"
@ -52,18 +53,20 @@ func TestString_AvailableTypes(t *testing.T) {
} }
for _, test := range tests { 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 checker == nil {
if test.Handled { if test.Handled {
t.Errorf("expect %q to be handled", test.Type) t.Errorf("expect %q to be handled", test.Type)
}
return
} }
continue
}
if !test.Handled { if !test.Handled {
t.Errorf("expect %q NOT to be handled", test.Type) 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 { for i, test := range tests {
if checker(test.Value) { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if !test.Valid { if checker(test.Value) {
t.Errorf("%d: expect value to be invalid", i) if !test.Valid {
t.Fail() t.Errorf("expect value to be invalid")
t.Fail()
}
return
} }
continue if test.Valid {
} t.Errorf("expect value to be valid")
if test.Valid { t.Fail()
t.Errorf("%d: expect value to be valid", i)
t.Fail()
} }
})
} }
} }
@ -128,25 +133,27 @@ func TestString_FixedLength(t *testing.T) {
} }
for i, test := range tests { for i, test := range tests {
checker := builtin.NewString().Checker(test.Type) t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if checker == nil { checker := builtin.NewString().Checker(test.Type)
t.Errorf("%d: expect %q to be handled", i, test.Type) if checker == nil {
t.Fail() t.Errorf("expect %q to be handled", test.Type)
continue
}
if checker(test.Value) {
if !test.Valid {
t.Errorf("%d: expect value to be invalid", i)
t.Fail() 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 { for i, test := range tests {
checker := builtin.NewString().Checker(test.Type) t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if checker == nil { checker := builtin.NewString().Checker(test.Type)
t.Errorf("%d: expect %q to be handled", i, test.Type) if checker == nil {
t.Fail() t.Errorf("expect %q to be handled", test.Type)
continue
}
if checker(test.Value) {
if !test.Valid {
t.Errorf("%d: expect value to be invalid", i)
t.Fail() 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()
}
})
} }
} }