MCPcopy Index your code
hub / github.com/coder/coder / testAuditFunctionWithSwitch

Function testAuditFunctionWithSwitch

enterprise/audit/table_internal_test.go:92–108  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

90// testAuditFunctionWithSwitch is a helper function to test that a function has
91// a typed switch statement that includes all the types in expectedTypes.
92func 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.

Callers 1

TestAuditableResourcesFunction · 0.85

Calls 4

ContainsFunction · 0.92
findSwitchTypesFunction · 0.85
HelperMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected