1
2
3
4
5 package noder
6
7 import (
8 "reflect"
9 "runtime"
10 "testing"
11
12 "cmd/compile/internal/syntax"
13 )
14
15 func eq(a, b []string) bool {
16 if len(a) != len(b) {
17 return false
18 }
19 for i := 0; i < len(a); i++ {
20 if a[i] != b[i] {
21 return false
22 }
23 }
24 return true
25 }
26
27 func TestPragmaFields(t *testing.T) {
28 var tests = []struct {
29 in string
30 want []string
31 }{
32 {"", []string{}},
33 {" \t ", []string{}},
34 {`""""`, []string{`""`, `""`}},
35 {" a'b'c ", []string{"a'b'c"}},
36 {"1 2 3 4", []string{"1", "2", "3", "4"}},
37 {"\n☺\t☹\n", []string{"☺", "☹"}},
38 {`"1 2 " 3 " 4 5"`, []string{`"1 2 "`, `3`, `" 4 5"`}},
39 {`"1""2 3""4"`, []string{`"1"`, `"2 3"`, `"4"`}},
40 {`12"34"`, []string{`12`, `"34"`}},
41 {`12"34 `, []string{`12`}},
42 }
43
44 for _, tt := range tests {
45 got := pragmaFields(tt.in)
46 if !eq(got, tt.want) {
47 t.Errorf("pragmaFields(%q) = %v; want %v", tt.in, got, tt.want)
48 continue
49 }
50 }
51 }
52
53 func TestPragcgo(t *testing.T) {
54 type testStruct struct {
55 in string
56 want []string
57 }
58
59 var tests = []testStruct{
60 {`go:cgo_export_dynamic local`, []string{`cgo_export_dynamic`, `local`}},
61 {`go:cgo_export_dynamic local remote`, []string{`cgo_export_dynamic`, `local`, `remote`}},
62 {`go:cgo_export_dynamic local' remote'`, []string{`cgo_export_dynamic`, `local'`, `remote'`}},
63 {`go:cgo_export_static local`, []string{`cgo_export_static`, `local`}},
64 {`go:cgo_export_static local remote`, []string{`cgo_export_static`, `local`, `remote`}},
65 {`go:cgo_export_static local' remote'`, []string{`cgo_export_static`, `local'`, `remote'`}},
66 {`go:cgo_import_dynamic local`, []string{`cgo_import_dynamic`, `local`}},
67 {`go:cgo_import_dynamic local remote`, []string{`cgo_import_dynamic`, `local`, `remote`}},
68 {`go:cgo_import_static local`, []string{`cgo_import_static`, `local`}},
69 {`go:cgo_import_static local'`, []string{`cgo_import_static`, `local'`}},
70 {`go:cgo_dynamic_linker "/path/"`, []string{`cgo_dynamic_linker`, `/path/`}},
71 {`go:cgo_dynamic_linker "/p ath/"`, []string{`cgo_dynamic_linker`, `/p ath/`}},
72 {`go:cgo_ldflag "arg"`, []string{`cgo_ldflag`, `arg`}},
73 {`go:cgo_ldflag "a rg"`, []string{`cgo_ldflag`, `a rg`}},
74 }
75
76 if runtime.GOOS != "aix" {
77 tests = append(tests, []testStruct{
78 {`go:cgo_import_dynamic local remote "library"`, []string{`cgo_import_dynamic`, `local`, `remote`, `library`}},
79 {`go:cgo_import_dynamic local' remote' "lib rary"`, []string{`cgo_import_dynamic`, `local'`, `remote'`, `lib rary`}},
80 }...)
81 } else {
82
83
84 tests = append(tests, []testStruct{
85 {`go:cgo_import_dynamic local remote "lib.a/obj.o"`, []string{`cgo_import_dynamic`, `local`, `remote`, `lib.a/obj.o`}},
86
87 {`go:cgo_import_dynamic local' remote' "library"`, []string{`<unknown position>: usage: //go:cgo_import_dynamic local [remote ["lib.a/object.o"]]`}},
88 }...)
89
90 }
91
92 var p noder
93 var nopos syntax.Pos
94 for _, tt := range tests {
95
96 p.err = make(chan syntax.Error)
97 gotch := make(chan [][]string, 1)
98 go func() {
99 p.pragcgobuf = nil
100 p.pragcgo(nopos, tt.in)
101 if p.pragcgobuf != nil {
102 gotch <- p.pragcgobuf
103 }
104 }()
105
106 select {
107 case e := <-p.err:
108 want := tt.want[0]
109 if e.Error() != want {
110 t.Errorf("pragcgo(%q) = %q; want %q", tt.in, e, want)
111 continue
112 }
113 case got := <-gotch:
114 want := [][]string{tt.want}
115 if !reflect.DeepEqual(got, want) {
116 t.Errorf("pragcgo(%q) = %q; want %q", tt.in, got, want)
117 continue
118 }
119 }
120
121 }
122 }
123
View as plain text