github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/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  	"cmd/compile/internal/syntax"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func eq(a, b []string) bool {
    14  	if len(a) != len(b) {
    15  		return false
    16  	}
    17  	for i := 0; i < len(a); i++ {
    18  		if a[i] != b[i] {
    19  			return false
    20  		}
    21  	}
    22  	return true
    23  }
    24  
    25  func TestPragmaFields(t *testing.T) {
    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  	var tests = []struct {
    53  		in   string
    54  		want []string
    55  	}{
    56  		{`go:cgo_export_dynamic local`, []string{`cgo_export_dynamic`, `local`}},
    57  		{`go:cgo_export_dynamic local remote`, []string{`cgo_export_dynamic`, `local`, `remote`}},
    58  		{`go:cgo_export_dynamic local' remote'`, []string{`cgo_export_dynamic`, `local'`, `remote'`}},
    59  		{`go:cgo_export_static local`, []string{`cgo_export_static`, `local`}},
    60  		{`go:cgo_export_static local remote`, []string{`cgo_export_static`, `local`, `remote`}},
    61  		{`go:cgo_export_static local' remote'`, []string{`cgo_export_static`, `local'`, `remote'`}},
    62  		{`go:cgo_import_dynamic local`, []string{`cgo_import_dynamic`, `local`}},
    63  		{`go:cgo_import_dynamic local remote`, []string{`cgo_import_dynamic`, `local`, `remote`}},
    64  		{`go:cgo_import_dynamic local remote "library"`, []string{`cgo_import_dynamic`, `local`, `remote`, `library`}},
    65  		{`go:cgo_import_dynamic local' remote' "lib rary"`, []string{`cgo_import_dynamic`, `local'`, `remote'`, `lib rary`}},
    66  		{`go:cgo_import_static local`, []string{`cgo_import_static`, `local`}},
    67  		{`go:cgo_import_static local'`, []string{`cgo_import_static`, `local'`}},
    68  		{`go:cgo_dynamic_linker "/path/"`, []string{`cgo_dynamic_linker`, `/path/`}},
    69  		{`go:cgo_dynamic_linker "/p ath/"`, []string{`cgo_dynamic_linker`, `/p ath/`}},
    70  		{`go:cgo_ldflag "arg"`, []string{`cgo_ldflag`, `arg`}},
    71  		{`go:cgo_ldflag "a rg"`, []string{`cgo_ldflag`, `a rg`}},
    72  	}
    73  
    74  	var p noder
    75  	var nopos syntax.Pos
    76  	for _, tt := range tests {
    77  		p.pragcgobuf = nil
    78  		p.pragcgo(nopos, tt.in)
    79  
    80  		got := p.pragcgobuf
    81  		want := [][]string{tt.want}
    82  		if !reflect.DeepEqual(got, want) {
    83  			t.Errorf("pragcgo(%q) = %q; want %q", tt.in, got, want)
    84  			continue
    85  		}
    86  	}
    87  }