-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
148 lines (129 loc) · 4.25 KB
/
main.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The following enables go generate to generate the doc.go file.
//go:generate go run $JIRI_ROOT/release/go/src/v.io/x/lib/cmdline/testdata/gendoc.go . -help
package main
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"v.io/v23/vdl"
"v.io/v23/vdl/vdltest"
"v.io/v23/vom"
"v.io/x/lib/cmdline"
"v.io/x/lib/textutil"
"v.io/x/ref/lib/vdl/codegen"
"v.io/x/ref/lib/vdl/codegen/vdlgen"
)
var cmdGen = &cmdline.Command{
Runner: cmdline.RunnerFunc(runGen),
Name: "vomtestgen",
Short: "generates test data for the vomtest package",
Long: `
Command vomtestgen generates test cases for the vomtest package. The following
file is generated:
pass81_gen.vdl - Golden file containing passing test cases for version 81.
Do not run this tool manually. Instead invoke it via:
$ jiri run go generate v.io/v23/vom/vomtest
`,
}
func main() {
cmdline.Main(cmdGen)
}
func runGen(_ *cmdline.Env, _ []string) error {
allCanonical := vdltest.AllPassFunc(func(e vdltest.Entry) bool {
return e.IsCanonical
})
writePassFile("pass81", vdltest.ToEntryValues(allCanonical))
return nil
}
// This tool is only used to generate test cases for the vdltest package, so the
// strategy is to panic on any error, to make the code simpler.
func panicOnError(err error) {
if err != nil {
panic(err)
}
}
func createFile(name string) (*os.File, func()) {
file, err := os.Create(name)
panicOnError(err)
return file, func() { panicOnError(file.Close()) }
}
func writef(w io.Writer, format string, args ...interface{}) {
_, err := fmt.Fprintf(w, format, args...)
panicOnError(err)
}
func writeHeader(w io.Writer) {
writef(w, `// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file was auto-generated by v.io/v23/vom/vomtest/internal/vomtestgen
// Run the following to re-generate:
// $ jiri run go generate v.io/v23/vom/vomtest
package vomtest
`)
}
func writePassFile(constName string, entries []vdltest.EntryValue) {
const version vom.Version = vom.Version81
fileName := constName + "_gen.vdl"
// We skip all random entries for now, since they may contain multiple set and
// map elements, which would cause non-deterministic output.
//
// TODO(toddw): Determine a better strategy.
var filtered []vdltest.EntryValue
for _, e := range entries {
if !strings.HasPrefix(e.Label, "Random") {
filtered = append(filtered, e)
}
}
skipped := len(entries) - len(filtered)
entries = filtered
fmt.Printf("Writing %s:\t%d entries (%d skipped)\n", fileName, len(entries), skipped)
file, cleanup := createFile(fileName)
defer cleanup()
writeHeader(file)
imports := codegen.Imports{
{Path: "v.io/v23/vdl/vdltest", Local: "vdltest"},
}
writef(file, "%s\n\n", vdlgen.Imports(imports))
comment := textutil.PrefixLineWriter(file, "// ")
panicOnError(vdltest.PrintEntryStats(comment, entries...))
panicOnError(comment.Flush())
writef(file, "\nconst %[1]s = []vdlEntry {\n", constName)
for _, e := range entries {
source := vdlgen.TypedConst(e.Source, "v.io/v23/vom/vomtest", imports)
vomDump, hexType, hexValue := toVomHex(version, e.Source)
writef(file, `%[1]s
{
%#[2]q,
%#[3]q,
%[3]s,
0x%[4]x, %[5]q, %[6]q,
},
`, vomDump, e.Label, source, byte(version), hexType, hexValue)
}
writef(file, "}\n")
}
func toVomHex(version vom.Version, value *vdl.Value) (_, _, _ string) {
var buf, bufType bytes.Buffer
encType := vom.NewVersionedTypeEncoder(version, &bufType)
enc := vom.NewVersionedEncoderWithTypeEncoder(version, &buf, encType)
panicOnError(enc.Encode(value))
versionByte, _ := buf.ReadByte() // Read the version byte.
if bufType.Len() > 0 {
bufType.ReadByte() // Remove the version byte.
}
vomBytes := append([]byte{versionByte}, bufType.Bytes()...)
vomBytes = append(vomBytes, buf.Bytes()...)
dump, err := vom.Dump(vomBytes)
panicOnError(err)
const pre = "\t// "
vomDump := pre + strings.Replace(dump, "\n", "\n"+pre, -1)
if strings.HasSuffix(vomDump, "\n"+pre) {
vomDump = vomDump[:len(vomDump)-len("\n"+pre)]
}
return vomDump, fmt.Sprintf("%x", bufType.Bytes()), fmt.Sprintf("%x", buf.Bytes())
}