Skip to content

Commit

Permalink
replace panic with t.Fatal in tests (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Dec 5, 2024
1 parent 159bc9e commit b6bf430
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestChecklist(t *testing.T) {
"5": {"C"},
},
}
checkEnumMembersLiteral("TestChecklist", em)
checkEnumMembersLiteral(t, "TestChecklist", em)

checkRemaining := func(t *testing.T, h checklist, want map[string]struct{}) {
t.Helper()
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestChecklist(t *testing.T) {
"5": {"C"},
},
}
checkEnumMembersLiteral("TestChecklist blank identifier", em)
checkEnumMembersLiteral(t, "TestChecklist blank identifier", em)

var c checklist
c.add(et, em, true)
Expand Down Expand Up @@ -272,7 +272,7 @@ func TestChecklist(t *testing.T) {
"42": {"lowercase"},
},
}
checkEnumMembersLiteral("TestChecklist lowercase", em)
checkEnumMembersLiteral(t, "TestChecklist lowercase", em)

t.Run("include", func(t *testing.T) {
var c checklist
Expand Down
14 changes: 7 additions & 7 deletions enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import (

// checkEnumMembersLiteral checks that an enumMembers literal is correctly
// defined in tests.
func checkEnumMembersLiteral(id string, v enumMembers) {
func checkEnumMembersLiteral(t *testing.T, id string, v enumMembers) {
var count int
for _, names := range v.ValueToNames {
count += len(names)
}
if len(v.Names) != len(v.NameToPos) {
panic(fmt.Sprintf("%s: wrong lengths: %d != %d (test definition bug)", id, len(v.Names), len(v.NameToPos)))
t.Fatalf("%s: wrong lengths: %d != %d (test definition bug)", id, len(v.Names), len(v.NameToPos))
}
if len(v.Names) != len(v.NameToValue) {
panic(fmt.Sprintf("%s: wrong lengths: %d != %d (test definition bug)", id, len(v.Names), len(v.NameToValue)))
t.Fatalf("%s: wrong lengths: %d != %d (test definition bug)", id, len(v.Names), len(v.NameToValue))
}
if len(v.Names) != count {
panic(fmt.Sprintf("%s: wrong lengths: %d != %d (test definition bug)", id, len(v.Names), count))
t.Fatalf("%s: wrong lengths: %d != %d (test definition bug)", id, len(v.Names), count)
}
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func TestFindEnums(t *testing.T) {
cfg := &packages.Config{Mode: packages.NeedTypesInfo | packages.NeedTypes | packages.NeedSyntax}
pkgs, err := packages.Load(cfg, "./testdata/src/enum")
if err != nil {
panic(err)
t.Fatal(err)
}
return pkgs[0]
}()
Expand Down Expand Up @@ -429,7 +429,7 @@ func checkEnums(t *testing.T, got []checkEnum, pkgOnly bool) {
}

for _, c := range wantPkg {
checkEnumMembersLiteral(c.typeName, c.members)
checkEnumMembersLiteral(t, c.typeName, c.members)
}

wantInner := []checkEnum{
Expand Down Expand Up @@ -486,7 +486,7 @@ func checkEnums(t *testing.T, got []checkEnum, pkgOnly bool) {
}

for _, c := range wantInner {
checkEnumMembersLiteral(c.typeName, c.members)
checkEnumMembersLiteral(t, c.typeName, c.members)
}

want := append([]checkEnum{}, wantPkg...)
Expand Down
4 changes: 2 additions & 2 deletions fact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestEnumMembersFact(t *testing.T) {
},
},
}
checkEnumMembersLiteral("Biome", e.Members)
checkEnumMembersLiteral(t, "Biome", e.Members)
if want := "Tundra,Savanna,Desert"; e.String() != want {
t.Errorf("got %v, want %v", e.String(), want)
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestEnumMembersFact(t *testing.T) {
},
},
}
checkEnumMembersLiteral("Token", e.Members)
checkEnumMembersLiteral(t, "Token", e.Members)
if want := "_,add,sub,mul,quotient,remainder"; e.String() != want {
t.Errorf("got %v, want %v", e.String(), want)
}
Expand Down

0 comments on commit b6bf430

Please sign in to comment.