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 "testing" 8 9 func eq(a, b []string) bool { 10 if len(a) != len(b) { 11 return false 12 } 13 for i := 0; i < len(a); i++ { 14 if a[i] != b[i] { 15 return false 16 } 17 } 18 return true 19 } 20 21 func TestPragmaFields(t *testing.T) { 22 23 var tests = []struct { 24 in string 25 want []string 26 }{ 27 {"", []string{}}, 28 {" \t ", []string{}}, 29 {`""""`, []string{`""`, `""`}}, 30 {" a'b'c ", []string{"a'b'c"}}, 31 {"1 2 3 4", []string{"1", "2", "3", "4"}}, 32 {"\n\t\n", []string{"", ""}}, 33 {`"1 2 " 3 " 4 5"`, []string{`"1 2 "`, `3`, `" 4 5"`}}, 34 {`"1""2 3""4"`, []string{`"1"`, `"2 3"`, `"4"`}}, 35 {`12"34"`, []string{`12`, `"34"`}}, 36 {`12"34 `, []string{`12`}}, 37 } 38 39 for _, tt := range tests { 40 got := pragmaFields(tt.in) 41 if !eq(got, tt.want) { 42 t.Errorf("pragmaFields(%q) = %v; want %v", tt.in, got, tt.want) 43 continue 44 } 45 } 46 } 47 48 func TestPragcgo(t *testing.T) { 49 50 var tests = []struct { 51 in string 52 want string 53 }{ 54 {`go:cgo_export_dynamic local`, "cgo_export_dynamic local\n"}, 55 {`go:cgo_export_dynamic local remote`, "cgo_export_dynamic local remote\n"}, 56 {`go:cgo_export_dynamic local' remote'`, "cgo_export_dynamic 'local''' 'remote'''\n"}, 57 {`go:cgo_export_static local`, "cgo_export_static local\n"}, 58 {`go:cgo_export_static local remote`, "cgo_export_static local remote\n"}, 59 {`go:cgo_export_static local' remote'`, "cgo_export_static 'local''' 'remote'''\n"}, 60 {`go:cgo_import_dynamic local`, "cgo_import_dynamic local\n"}, 61 {`go:cgo_import_dynamic local remote`, "cgo_import_dynamic local remote\n"}, 62 {`go:cgo_import_dynamic local remote "library"`, "cgo_import_dynamic local remote library\n"}, 63 {`go:cgo_import_dynamic local' remote' "lib rary"`, "cgo_import_dynamic 'local''' 'remote''' 'lib rary'\n"}, 64 {`go:cgo_import_static local`, "cgo_import_static local\n"}, 65 {`go:cgo_import_static local'`, "cgo_import_static 'local'''\n"}, 66 {`go:cgo_dynamic_linker "/path/"`, "cgo_dynamic_linker /path/\n"}, 67 {`go:cgo_dynamic_linker "/p ath/"`, "cgo_dynamic_linker '/p ath/'\n"}, 68 {`go:cgo_ldflag "arg"`, "cgo_ldflag arg\n"}, 69 {`go:cgo_ldflag "a rg"`, "cgo_ldflag 'a rg'\n"}, 70 } 71 72 for _, tt := range tests { 73 got := pragcgo(tt.in) 74 if got != tt.want { 75 t.Errorf("pragcgo(%q) = %q; want %q", tt.in, got, tt.want) 76 continue 77 } 78 } 79 } 80