assertSentinelNotDeclared parses the generated output as a package body and fails if it is not valid Go or if injectSentinel appears as a top-level declared identifier (i.e. the payload escaped its comment or string literal and became real source).
(t *testing.T, out string)
| 136 | // top-level declared identifier (i.e. the payload escaped its comment or |
| 137 | // string literal and became real source). |
| 138 | func assertSentinelNotDeclared(t *testing.T, out string) { |
| 139 | t.Helper() |
| 140 | src := "package p\n" + out |
| 141 | fset := token.NewFileSet() |
| 142 | file, err := parser.ParseFile(fset, "gen.go", src, parser.ParseComments) |
| 143 | require.NoError(t, err, "generated output must parse as valid Go:\n%s", src) |
| 144 | |
| 145 | for _, name := range topLevelDeclNames(file) { |
| 146 | assert.NotEqual(t, injectSentinel, name, |
| 147 | "injected identifier became a real top-level declaration:\n%s", src) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // topLevelDeclNames returns every identifier declared at file scope. |
| 152 | func topLevelDeclNames(file *ast.File) []string { |
no test coverage detected