Skip to content

Commit

Permalink
Refine test suite (#74)
Browse files Browse the repository at this point in the history
* local tc

* another range local variable

* rewrite comment

* move call to parallelize test

* gofmt

* early instantiate units regex cache

* add call to parallel test

* remove t.Parallel()

* gomft

* remove update cache
  • Loading branch information
mfleader authored Feb 5, 2024
1 parent 57122a4 commit a481c35
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ on:
branches:
- main
pull_request:
env:
go_version: 1.21.6
jobs:
golangci-lint:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Pin Golang version for linting
uses: actions/setup-go@v5
with:
go-version: ${{ env.go_version }}
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand All @@ -24,7 +30,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.21.6
go-version: ${{ env.go_version }}
- name: Set up gotestfmt
uses: GoTestTools/gotestfmt-action@v2
- uses: actions/cache@v4
Expand Down Expand Up @@ -67,7 +73,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.21.6
go-version: ${{ env.go_version }}
- uses: actions/cache@v4
with:
path: |
Expand Down
15 changes: 10 additions & 5 deletions schema/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,22 @@ var boolTestSerializationCases = map[string]struct {

func TestBoolSerializationCycle(t *testing.T) {
for name, tc := range boolTestSerializationCases {
// When executed in parallel, referencing tc from the
// outer scope will not produce the proper value, so we need
// to bind it to a variable, localTC, scoped inside
// the loop body.
localTC := tc
t.Run(name, func(t *testing.T) {
var boolType schema.Bool = schema.NewBoolSchema()
output, err := boolType.Unserialize(tc.input)
output, err := boolType.Unserialize(localTC.input)
if err != nil {
if tc.expectedError {
if localTC.expectedError {
return
}
t.Fatalf("Failed to unserialize %v: %v", tc.input, err)
t.Fatalf("Failed to unserialize %v: %v", localTC.input, err)
}
if output != tc.output {
t.Fatalf("Unexpected unserialize output for %v: %v", tc.input, output)
if output != localTC.output {
t.Fatalf("Unexpected unserialize output for %v: %v", localTC.input, output)
}

if err := boolType.Validate(output); err != nil {
Expand Down
34 changes: 22 additions & 12 deletions schema/units_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ func TestUnitsParseInt(t *testing.T) {
}

for testCase, testData := range testMatrix {
// When executed in parallel, referencing testData from the
// outer scope will not produce the proper value, so we need
// to bind it to a variable, localTestData, scoped inside
// the loop body.
localTestData := testData
t.Run(testCase, func(t *testing.T) {
result, err := testData.units.ParseInt(testData.input)
result, err := localTestData.units.ParseInt(localTestData.input)
if err != nil {
t.Fatal(err)
}
if result != testData.expected {
t.Fatalf("Result mismatch, expected: %d, got: %d", testData.expected, result)
if result != localTestData.expected {
t.Fatalf("Result mismatch, expected: %d, got: %d", localTestData.expected, result)
}
formatted := testData.units.FormatShortInt(result)
if formatted != testData.input {
t.Fatalf("Formatted result doesn't match input, expected: %s, got: %s", testData.input, formatted)
formatted := localTestData.units.FormatShortInt(result)
if formatted != localTestData.input {
t.Fatalf("Formatted result doesn't match input, expected: %s, got: %s", localTestData.input, formatted)
}
})
}
Expand All @@ -70,17 +75,22 @@ func TestUnitsParseFloat(t *testing.T) {
}

for testCase, testData := range testMatrix {
// When executed in parallel, referencing testData from the
// outer scope will not produce the proper value, so we need
// to bind it to a variable, localTestData, scoped inside
// the loop body.
localTestData := testData
t.Run(testCase, func(t *testing.T) {
result, err := testData.units.ParseFloat(testData.input)
result, err := localTestData.units.ParseFloat(localTestData.input)
if err != nil {
t.Fatal(err)
}
if result != testData.expected {
t.Fatalf("Result mismatch, expected: %f, got: %f", testData.expected, result)
if result != localTestData.expected {
t.Fatalf("Result mismatch, expected: %f, got: %f", localTestData.expected, result)
}
formatted := testData.units.FormatShortFloat(result)
if formatted != testData.input {
t.Fatalf("Formatted result doesn't match input, expected: %s, got: %s", testData.input, formatted)
formatted := localTestData.units.FormatShortFloat(result)
if formatted != localTestData.input {
t.Fatalf("Formatted result doesn't match input, expected: %s, got: %s", localTestData.input, formatted)
}
})
}
Expand Down
19 changes: 12 additions & 7 deletions schema/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,39 @@ func performSerializationTest[T any](
) {
t.Helper()
for name, tc := range testCases {
// When executed in parallel, referencing tc from the
// outer scope will not produce the proper value, so we need
// to bind it to a variable, localTC, scoped inside
// the loop body.
localTC := tc
t.Run(name, func(t *testing.T) {
t.Helper()
unserialized, err := typeUnderTest.UnserializeType(tc.SerializedValue)
unserialized, err := typeUnderTest.UnserializeType(localTC.SerializedValue)
if err != nil {
if tc.ExpectError {
if localTC.ExpectError {
return
}
t.Fatal(err)
}
if err := typeUnderTest.ValidateType(unserialized); err != nil {
t.Fatal(err)
}
if !compareUnserialized(unserialized, tc.ExpectUnserializedValue) {
if !compareUnserialized(unserialized, localTC.ExpectUnserializedValue) {
t.Fatalf(
"Unexpected unserialized value, expected: %v, got: %v",
tc.ExpectUnserializedValue,
localTC.ExpectUnserializedValue,
unserialized,
)
}
serialized, err := typeUnderTest.SerializeType(unserialized)
if err != nil {
t.Fatal(err)
}
if !compareSerialized(serialized, tc.ExpectedSerializedValue) {
if !compareSerialized(serialized, localTC.ExpectedSerializedValue) {
t.Fatalf(
"Serialized value mismatch, expected: %v (%T), got: %v (%T)",
tc.ExpectedSerializedValue,
tc.ExpectedSerializedValue,
localTC.ExpectedSerializedValue,
localTC.ExpectedSerializedValue,
serialized,
serialized,
)
Expand Down

0 comments on commit a481c35

Please sign in to comment.