github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/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.setShorthand([]string{"-command", "yacc", "go", "tool", "yacc"}) 43 for _, test := range splitTests { 44 // First with newlines. 45 got := g.split("//go:generate " + test.in + "\n") 46 if !reflect.DeepEqual(got, test.out) { 47 t.Errorf("split(%q): got %q expected %q", test.in, got, test.out) 48 } 49 // Then with CRLFs, thank you Windows. 50 got = g.split("//go:generate " + test.in + "\r\n") 51 if !reflect.DeepEqual(got, test.out) { 52 t.Errorf("split(%q): got %q expected %q", test.in, got, test.out) 53 } 54 } 55 }