Source file src/cmd/compile/internal/syntax/testing_test.go

     1  // Copyright 2020 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 syntax
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestErrorMap(t *testing.T) {
    14  	const src = `/* ERROR 0:0 */ /* ERROR "0:0" */ // ERROR 0:0
    15  // ERROR "0:0"
    16  x /* ERROR 3:1 */                // ignore automatically inserted semicolon here
    17  /* ERROR 3:1 */                  // position of x on previous line
    18     x /* ERROR 5:4 */ ;           // do not ignore this semicolon
    19  /* ERROR 5:22 */                 // position of ; on previous line
    20  	package /* ERROR 7:2 */  // indented with tab
    21          import  /* ERROR 8:9 */  // indented with blanks
    22  `
    23  	m := ErrorMap(strings.NewReader(src))
    24  	got := 0 // number of errors found
    25  	for line, errlist := range m {
    26  		for _, err := range errlist {
    27  			if err.Pos.Line() != line {
    28  				t.Errorf("%v: got map line %d; want %d", err, err.Pos.Line(), line)
    29  				continue
    30  			}
    31  			// err.Pos.Line() == line
    32  			msg := fmt.Sprintf("%d:%d", line, err.Pos.Col())
    33  			if err.Msg != msg {
    34  				t.Errorf("%v: got msg %q; want %q", err, err.Msg, msg)
    35  				continue
    36  			}
    37  		}
    38  		got += len(errlist)
    39  	}
    40  
    41  	want := strings.Count(src, "ERROR")
    42  	if got != want {
    43  		t.Errorf("ErrorMap got %d errors; want %d", got, want)
    44  	}
    45  }
    46  

View as plain text