testAuditFunctionWithSwitch is a helper function to test that a function has a typed switch statement that includes all the types in expectedTypes.
(t *testing.T, pkg *packages.Package, funcName string, expectedTypes []string)
| 90 | // testAuditFunctionWithSwitch is a helper function to test that a function has |
| 91 | // a typed switch statement that includes all the types in expectedTypes. |
| 92 | func testAuditFunctionWithSwitch(t *testing.T, pkg *packages.Package, funcName string, expectedTypes []string) { |
| 93 | t.Helper() |
| 94 | |
| 95 | f, ok := pkg.Types.Scope().Lookup(funcName).(*types.Func) |
| 96 | require.True(t, ok, fmt.Sprintf("expected %s to be a function", funcName)) |
| 97 | switchCases := findSwitchTypes(f) |
| 98 | for _, expected := range expectedTypes { |
| 99 | if !slice.Contains(switchCases, expected) { |
| 100 | t.Errorf("%s switch statement is missing type %q. Include it in the switch case block", funcName, expected) |
| 101 | } |
| 102 | } |
| 103 | for _, sc := range switchCases { |
| 104 | if !slice.Contains(expectedTypes, sc) { |
| 105 | t.Errorf("%s switch statement has unexpected type %q. Remove it from the switch case block", funcName, sc) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // findSwitchTypes is a helper function to find all types a switch statement in |
| 111 | // the function body of f has. |
no test coverage detected