-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
99 lines (90 loc) · 2.51 KB
/
Copy pathmain_test.go
File metadata and controls
99 lines (90 loc) · 2.51 KB
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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestMain(t *testing.T) {
args, stdout, stderr := os.Args, os.Stdout, os.Stderr
os.Stdout, _ = os.Open(os.DevNull)
os.Stderr, _ = os.Open(os.DevNull)
defer func() {
exit = os.Exit
os.Args = args
os.Stdout = stdout
os.Stderr = stderr
}()
exit = func(code int) {
panic(code)
}
grammarFile := filepath.Join(t.TempDir(), "grammar.peg")
if err := os.WriteFile(grammarFile, []byte(`start = "a"`), 0o600); err != nil {
t.Fatal(err)
}
cases := []struct {
args []string
code int
}{
{args: nil, code: 3}, // stdin: no match found
{args: []string{"-h"}, code: 0}, // help
{args: []string{"FILE1", "FILE2"}, code: 1}, // want only 1 non-flag arg
{args: []string{"-x"}, code: 3}, // stdin: no match found
{args: []string{"-t", "hx", "-x", grammarFile}},
{args: []string{"-target", "hx", "-x", grammarFile}},
{args: []string{"-target", "hx", "-haxe-use-hxunicode", "-x", grammarFile}},
{args: []string{"-t", "ts", "-x", grammarFile}},
{args: []string{"-t", "cs", "-x", grammarFile}},
{args: []string{"-t", "c", "-x", grammarFile}},
{args: []string{"-t", "c99", "-x", grammarFile}},
{args: []string{"-t", "rust", "-x", grammarFile}},
{args: []string{"-t", "unknown", "-x", grammarFile}, code: 1},
}
for _, tc := range cases {
os.Args = append([]string{"pegtool"}, tc.args...)
got := runMainRecover()
if got != tc.code {
t.Errorf("%q: want code %d, got %d", tc.args, tc.code, got)
}
}
}
func TestParserBuilderForTarget(t *testing.T) {
for _, target := range []string{
"go", "hx", "haxe", "ts", "typescript", "cs", "csharp", "c#", "c", "c99", "rust",
} {
builder, formatGo, err := parserBuilderForTarget(target)
if err != nil {
t.Errorf("%q: unexpected error: %v", target, err)
continue
}
if builder == nil {
t.Errorf("%q: nil builder", target)
}
if formatGo != (target == "go") {
t.Errorf("%q: formatGo = %t", target, formatGo)
}
}
if _, _, err := parserBuilderForTarget("invalid"); err == nil {
t.Fatal("expected invalid target error")
}
}
func TestUsageIncludesTargetFlagAliases(t *testing.T) {
for _, want := range []string{"-t TARGET, -target TARGET", "-haxe-use-hxunicode"} {
if !strings.Contains(usagePage, want) {
t.Fatalf("usage page missing %q", want)
}
}
}
func runMainRecover() (code int) {
defer func() {
if e := recover(); e != nil {
if i, ok := e.(int); ok {
code = i
return
}
panic(e)
}
}()
main()
return 0
}