summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/text/internal/triegen/gen_test.go
blob: 831627d7a059d83aace161c28199900312f7eb2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build generate

package triegen_test

// The code in this file generates captures and writes the tries generated in
// the examples to data_test.go. To invoke it, run:
// 		go test -tags=generate
//
// Making the generation code a "test" allows us to link in the necessary test
// code.

import (
	"log"
	"os"
	"os/exec"
)

func init() {
	const tmpfile = "tmpout"
	const dstfile = "data_test.go"

	f, err := os.Create(tmpfile)
	if err != nil {
		log.Fatalf("Could not create output file: %v", err)
	}
	defer os.Remove(tmpfile)
	defer f.Close()

	// We exit before this function returns, regardless of success or failure,
	// so there's no need to save (and later restore) the existing genWriter
	// value.
	genWriter = f

	f.Write([]byte(header))

	Example_build()
	ExampleGen_build()

	if err := exec.Command("gofmt", "-w", tmpfile).Run(); err != nil {
		log.Fatal(err)
	}
	os.Remove(dstfile)
	os.Rename(tmpfile, dstfile)

	os.Exit(0)
}

const header = `// This file is generated with "go test -tags generate". DO NOT EDIT!
// +build !generate

package triegen_test
`

// Stubs for generated tries. These are needed as we exclude data_test.go if
// the generate flag is set. This will clearly make the tests fail, but that
// is okay. It allows us to bootstrap.

type trie struct{}

func (t *trie) lookupString(string) (uint8, int) { return 0, 1 }
func (t *trie) lookupStringUnsafe(string) uint64 { return 0 }

func newRandTrie(i int) *trie  { return &trie{} }
func newMultiTrie(i int) *trie { return &trie{} }