github.com/fenixara/go@v0.0.0-20170127160404-96ea0918e670/src/cmd/go/generate_test.go (about)

     1  // Copyright 2011 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 main
     6  
     7  import (
     8  	"reflect"
     9  	"runtime"
    10  	"testing"
    11  )
    12  
    13  type splitTest struct {
    14  	in  string
    15  	out []string
    16  }
    17  
    18  var splitTests = []splitTest{
    19  	{"", nil},
    20  	{"x", []string{"x"}},
    21  	{" a b\tc ", []string{"a", "b", "c"}},
    22  	{` " a " `, []string{" a "}},
    23  	{"$GOARCH", []string{runtime.GOARCH}},
    24  	{"$GOOS", []string{runtime.GOOS}},
    25  	{"$GOFILE", []string{"proc.go"}},
    26  	{"$GOPACKAGE", []string{"sys"}},
    27  	{"a $XXNOTDEFINEDXX b", []string{"a", "", "b"}},
    28  	{"/$XXNOTDEFINED/", []string{"//"}},
    29  	{"/$DOLLAR/", []string{"/$/"}},
    30  	{"yacc -o $GOARCH/yacc_$GOFILE", []string{"go", "tool", "yacc", "-o", runtime.GOARCH + "/yacc_proc.go"}},
    31  }
    32  
    33  func TestGenerateCommandParse(t *testing.T) {
    34  	g := &Generator{
    35  		r:        nil, // Unused here.
    36  		path:     "/usr/ken/sys/proc.go",
    37  		dir:      "/usr/ken/sys",
    38  		file:     "proc.go",
    39  		pkg:      "sys",
    40  		commands: make(map[string][]string),
    41  	}
    42  	g.setEnv()
    43  	g.setShorthand([]string{"-command", "yacc", "go", "tool", "yacc"})
    44  	for _, test := range splitTests {
    45  		// First with newlines.
    46  		got := g.split("//go:generate " + test.in + "\n")
    47  		if !reflect.DeepEqual(got, test.out) {
    48  			t.Errorf("split(%q): got %q expected %q", test.in, got, test.out)
    49  		}
    50  		// Then with CRLFs, thank you Windows.
    51  		got = g.split("//go:generate " + test.in + "\r\n")
    52  		if !reflect.DeepEqual(got, test.out) {
    53  			t.Errorf("split(%q): got %q expected %q", test.in, got, test.out)
    54  		}
    55  	}
    56  }