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