Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine test suite #74

Merged
merged 11 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading