gen_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build generate
  5. // +build generate
  6. package triegen_test
  7. // The code in this file generates captures and writes the tries generated in
  8. // the examples to data_test.go. To invoke it, run:
  9. // go test -tags=generate
  10. //
  11. // Making the generation code a "test" allows us to link in the necessary test
  12. // code.
  13. import (
  14. "log"
  15. "os"
  16. "os/exec"
  17. )
  18. func init() {
  19. const tmpfile = "tmpout"
  20. const dstfile = "data_test.go"
  21. f, err := os.Create(tmpfile)
  22. if err != nil {
  23. log.Fatalf("Could not create output file: %v", err)
  24. }
  25. defer os.Remove(tmpfile)
  26. defer f.Close()
  27. // We exit before this function returns, regardless of success or failure,
  28. // so there's no need to save (and later restore) the existing genWriter
  29. // value.
  30. genWriter = f
  31. f.Write([]byte(header))
  32. Example_build()
  33. ExampleGen_build()
  34. if err := exec.Command("gofmt", "-w", tmpfile).Run(); err != nil {
  35. log.Fatal(err)
  36. }
  37. os.Remove(dstfile)
  38. os.Rename(tmpfile, dstfile)
  39. os.Exit(0)
  40. }
  41. const header = `// This file is generated with "go test -tags generate". DO NOT EDIT!
  42. // +build !generate
  43. package triegen_test
  44. `
  45. // Stubs for generated tries. These are needed as we exclude data_test.go if
  46. // the generate flag is set. This will clearly make the tests fail, but that
  47. // is okay. It allows us to bootstrap.
  48. type trie struct{}
  49. func (t *trie) lookupString(string) (uint8, int) { return 0, 1 }
  50. func (t *trie) lookupStringUnsafe(string) uint64 { return 0 }
  51. func newRandTrie(i int) *trie { return &trie{} }
  52. func newMultiTrie(i int) *trie { return &trie{} }