github.com/go-enjin/golang-org-x-text@v0.12.1-enjin.2/internal/triegen/gen_test.go (about)

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