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