MCPcopy Index your code
hub / github.com/felixge/httpsnoop / Tests

Method Tests

codegen/main.go:249–303  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

247}
248
249func (b *Build) Tests() *Generator {
250 ifaces := b.Interfaces
251 // @TODO dedupe
252 // subIfaces has all interfaces except http.ResponseWriter
253 subIfaces := ifaces[1:]
254
255 var g Generator
256 // Package header
257 b.writeHeader(&g)
258 g.Printf("import (\n")
259 g.Printf(`"net/http"` + "\n")
260 g.Printf(`"io"` + "\n")
261 g.Printf(`"testing"` + "\n")
262 g.Printf(")\n")
263 g.Printf("\n")
264
265 // TestWrap func
266 g.Printf("func TestWrap(t *testing.T) {\n")
267 combinations := 1 << uint(len(subIfaces))
268 for i := 0; i < combinations; i++ {
269 fields := make([]string, 0, len(subIfaces))
270 fields = append(fields, "http.ResponseWriter")
271 expected := make([]bool, len(ifaces))
272 expected[0] = true
273 for j, iface := range subIfaces {
274 ok := i&(1<<uint(len(subIfaces)-j-1)) > 0
275 expected[j+1] = ok
276 if ok {
277 fields = append(fields, iface.Name)
278 }
279 }
280 g.Printf("// combination %d/%d\n", i+1, combinations)
281 g.Printf("{\n")
282 g.Printf(`t.Log("%s")`+"\n", strings.Join(fields, ", "))
283 g.Printf("inner := struct{\n%s\n}{}\n", strings.Join(fields, "\n"))
284 g.Printf("w := Wrap(inner, Hooks{})\n")
285 for i, iface := range ifaces {
286 g.Printf("if _, ok := w.(%s); ok != %t {\n", iface.Name, expected[i])
287 g.Printf("t.Error(\"unexpected interface\");\n")
288 g.Printf("}\n")
289 }
290 g.Printf(`
291if w, ok := w.(Unwrapper); ok {
292 if w.Unwrap() != inner {
293 t.Error("w.Unwrap() failed")
294 }
295} else {
296 t.Error("Unwrapper interface not implemented")
297}`)
298 g.Printf("}\n")
299 g.Printf("\n")
300 }
301 g.Printf("}\n")
302 return &g
303}
304
305type Interfaces []*Interface
306

Callers 1

MustBuildMethod · 0.95

Calls 2

writeHeaderMethod · 0.95
PrintfMethod · 0.95

Tested by

no test coverage detected