github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/go/build/read_test.go (about)

     1  // Copyright 2012 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 build
     6  
     7  import (
     8  	"io"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  const quote = "`"
    14  
    15  type readTest struct {
    16  	// Test input contains ℙ where readImports should stop.
    17  	in  string
    18  	err string
    19  }
    20  
    21  var readImportsTests = []readTest{
    22  	{
    23  		`package p`,
    24  		"",
    25  	},
    26  	{
    27  		`package p; import "x"`,
    28  		"",
    29  	},
    30  	{
    31  		`package p; import . "x"`,
    32  		"",
    33  	},
    34  	{
    35  		`package p; import "x";ℙvar x = 1`,
    36  		"",
    37  	},
    38  	{
    39  		`package p
    40  		
    41  		// comment
    42  		
    43  		import "x"
    44  		import _ "x"
    45  		import a "x"
    46  		
    47  		/* comment */
    48  		
    49  		import (
    50  			"x" /* comment */
    51  			_ "x"
    52  			a "x" // comment
    53  			` + quote + `x` + quote + `
    54  			_ /*comment*/ ` + quote + `x` + quote + `
    55  			a ` + quote + `x` + quote + `
    56  		)
    57  		import (
    58  		)
    59  		import ()
    60  		import()import()import()
    61  		import();import();import()
    62  		
    63  		ℙvar x = 1
    64  		`,
    65  		"",
    66  	},
    67  }
    68  
    69  var readCommentsTests = []readTest{
    70  	{
    71  		`ℙpackage p`,
    72  		"",
    73  	},
    74  	{
    75  		`ℙpackage p; import "x"`,
    76  		"",
    77  	},
    78  	{
    79  		`ℙpackage p; import . "x"`,
    80  		"",
    81  	},
    82  	{
    83  		`// foo
    84  
    85  		/* bar */
    86  
    87  		/* quux */ // baz
    88  		
    89  		/*/ zot */
    90  
    91  		// asdf
    92  		ℙHello, world`,
    93  		"",
    94  	},
    95  }
    96  
    97  func testRead(t *testing.T, tests []readTest, read func(io.Reader) ([]byte, error)) {
    98  	for i, tt := range tests {
    99  		var in, testOut string
   100  		j := strings.Index(tt.in, "ℙ")
   101  		if j < 0 {
   102  			in = tt.in
   103  			testOut = tt.in
   104  		} else {
   105  			in = tt.in[:j] + tt.in[j+len("ℙ"):]
   106  			testOut = tt.in[:j]
   107  		}
   108  		r := strings.NewReader(in)
   109  		buf, err := read(r)
   110  		if err != nil {
   111  			if tt.err == "" {
   112  				t.Errorf("#%d: err=%q, expected success (%q)", i, err, string(buf))
   113  			} else if !strings.Contains(err.Error(), tt.err) {
   114  				t.Errorf("#%d: err=%q, expected %q", i, err, tt.err)
   115  			}
   116  			continue
   117  		}
   118  		if tt.err != "" {
   119  			t.Errorf("#%d: success, expected %q", i, tt.err)
   120  			continue
   121  		}
   122  
   123  		out := string(buf)
   124  		if out != testOut {
   125  			t.Errorf("#%d: wrong output:\nhave %q\nwant %q\n", i, out, testOut)
   126  		}
   127  	}
   128  }
   129  
   130  func TestReadImports(t *testing.T) {
   131  	testRead(t, readImportsTests, func(r io.Reader) ([]byte, error) { return readImports(r, true, nil) })
   132  }
   133  
   134  func TestReadComments(t *testing.T) {
   135  	testRead(t, readCommentsTests, readComments)
   136  }
   137  
   138  var readFailuresTests = []readTest{
   139  	{
   140  		`package`,
   141  		"syntax error",
   142  	},
   143  	{
   144  		"package p\n\x00\nimport `math`\n",
   145  		"unexpected NUL in input",
   146  	},
   147  	{
   148  		`package p; import`,
   149  		"syntax error",
   150  	},
   151  	{
   152  		`package p; import "`,
   153  		"syntax error",
   154  	},
   155  	{
   156  		"package p; import ` \n\n",
   157  		"syntax error",
   158  	},
   159  	{
   160  		`package p; import "x`,
   161  		"syntax error",
   162  	},
   163  	{
   164  		`package p; import _`,
   165  		"syntax error",
   166  	},
   167  	{
   168  		`package p; import _ "`,
   169  		"syntax error",
   170  	},
   171  	{
   172  		`package p; import _ "x`,
   173  		"syntax error",
   174  	},
   175  	{
   176  		`package p; import .`,
   177  		"syntax error",
   178  	},
   179  	{
   180  		`package p; import . "`,
   181  		"syntax error",
   182  	},
   183  	{
   184  		`package p; import . "x`,
   185  		"syntax error",
   186  	},
   187  	{
   188  		`package p; import (`,
   189  		"syntax error",
   190  	},
   191  	{
   192  		`package p; import ("`,
   193  		"syntax error",
   194  	},
   195  	{
   196  		`package p; import ("x`,
   197  		"syntax error",
   198  	},
   199  	{
   200  		`package p; import ("x"`,
   201  		"syntax error",
   202  	},
   203  }
   204  
   205  func TestReadFailures(t *testing.T) {
   206  	// Errors should be reported (true arg to readImports).
   207  	testRead(t, readFailuresTests, func(r io.Reader) ([]byte, error) { return readImports(r, true, nil) })
   208  }
   209  
   210  func TestReadFailuresIgnored(t *testing.T) {
   211  	// Syntax errors should not be reported (false arg to readImports).
   212  	// Instead, entire file should be the output and no error.
   213  	// Convert tests not to return syntax errors.
   214  	tests := make([]readTest, len(readFailuresTests))
   215  	copy(tests, readFailuresTests)
   216  	for i := range tests {
   217  		tt := &tests[i]
   218  		if !strings.Contains(tt.err, "NUL") {
   219  			tt.err = ""
   220  		}
   221  	}
   222  	testRead(t, tests, func(r io.Reader) ([]byte, error) { return readImports(r, false, nil) })
   223  }