...

Source file src/github.com/protocolbuffers/txtpbfmt/parser/parser_position_test.go

Documentation: github.com/protocolbuffers/txtpbfmt/parser

     1  package parser
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/protocolbuffers/txtpbfmt/ast"
     8  )
     9  
    10  func findNode(name string, nodes []*ast.Node) *ast.Node {
    11  	for _, n := range nodes {
    12  		if n.Name == name {
    13  			return n
    14  		}
    15  		if found := findNode(name, n.Children); found != nil {
    16  			return found
    17  		}
    18  	}
    19  	return nil
    20  }
    21  
    22  func mkString(lines ...string) string {
    23  	return strings.Join(lines, "\n")
    24  }
    25  
    26  func TestParsePositions(t *testing.T) {
    27  	for i, tc := range []struct {
    28  		in                 string
    29  		startByte, endByte int
    30  		start, end         ast.Position
    31  	}{
    32  		{
    33  			in:        `trackme: "foo"` + "\n",
    34  			startByte: 0,
    35  		},
    36  		{
    37  			in: mkString(
    38  				"# top",      // 5 bytes + newline
    39  				"trackme: 4", // 10 bytes + newline
    40  			),
    41  			startByte: 0,
    42  		},
    43  		{
    44  			in: mkString(
    45  				"# top",        // 5 bytes + newline
    46  				"unrelated: 1", // 12 bytes + newline
    47  				"trackme: 4",
    48  			),
    49  			startByte: 19,
    50  		},
    51  		{
    52  			in:        "trackme: {\n}",
    53  			startByte: 0, endByte: 11,
    54  			start: ast.Position{Line: 1, Column: 1},
    55  			end:   ast.Position{Line: 2},
    56  		},
    57  		{
    58  			in: mkString(
    59  				"# top",        // 5 bytes + newline
    60  				"unrelated: 1", // 12 bytes + newline
    61  				"",             // this already gets accounted for trackme
    62  				"trackme: 4",
    63  			),
    64  			startByte: 19,
    65  		},
    66  		{
    67  			in: mkString(
    68  				"# top",        // 5 bytes + newline
    69  				"unrelated: 1", // 12 bytes + newline
    70  				"# boo",        // this already gets accounted for trackme
    71  				"trackme: 4",
    72  			),
    73  			startByte: 19,
    74  		},
    75  		{
    76  			in: mkString(
    77  				"# top",        // 5 bytes + newline
    78  				"unrelated: 1", // 12 bytes + newline
    79  				"",             // this already gets accounted for trackme
    80  				"# boo",
    81  				"trackme: 4",
    82  			),
    83  			startByte: 19,
    84  		},
    85  		{
    86  			in: mkString(
    87  				"outer: {", // 8 bytes + newline
    88  				"  foo: 1", // 8 bytes + newline
    89  				"  trackme: 4",
    90  				"}",
    91  			),
    92  			startByte: 18,
    93  		},
    94  		{
    95  			in: mkString(
    96  				"outer: {", // 8 bytes + newline
    97  				"  foo: 1", // 8 bytes + newline
    98  				"",         // acounted already for trackme
    99  				"  # multiline desc",
   100  				"  # for trackme",
   101  				"  trackme: 4",
   102  				"}",
   103  			),
   104  			startByte: 18,
   105  		},
   106  		{
   107  			in: mkString(
   108  				"trackme: {",   // 10 bytes + newline
   109  				"  content: 1", // 12 bytes + newline
   110  				"}",
   111  			),
   112  			startByte: 0, endByte: 24,
   113  			start: ast.Position{Line: 1, Column: 1},
   114  			end:   ast.Position{Line: 3},
   115  		},
   116  		{
   117  			in: mkString(
   118  				"outer: {",          // 8 bytes + newline
   119  				"  a: 1",            // 6 bytes + newline
   120  				"  trackme: {",      // 12 bytes + newline
   121  				"    b: 2",          // 8 bytes + newline
   122  				"    # end comment", // 17 bytes + newline
   123  				"  }",
   124  				"}",
   125  			),
   126  			startByte: 9 + 7, endByte: 9 + 7 + 13 + 9 + 18,
   127  			start: ast.Position{Line: 3, Column: 1},
   128  			end:   ast.Position{Line: 6},
   129  		},
   130  	} {
   131  		nodes, err := Parse([]byte(tc.in))
   132  		if err != nil {
   133  			t.Fatal(err)
   134  		}
   135  		found := findNode("trackme", nodes)
   136  		if found == nil {
   137  			t.Fatalf("%d. TestParsePositions no node 'trackme' found", i)
   138  		}
   139  		if (uint32(tc.startByte) != found.Start.Byte) ||
   140  			(uint32(tc.endByte) != found.End.Byte) ||
   141  			(tc.start.Line > 0 && found.Start.Line != tc.start.Line) ||
   142  			(tc.start.Column > 0 && found.Start.Column != tc.start.Column) ||
   143  			(tc.end.Line > 0 && found.End.Line != tc.end.Line) {
   144  			t.Errorf("%d. TestParsePositions got = %+v, want = (%d, %d), (%d, %d, %d); input:\n%s",
   145  				i,
   146  				found,
   147  				tc.startByte, tc.endByte,
   148  				tc.start.Line, tc.start.Column, tc.end.Line,
   149  				tc.in)
   150  		}
   151  
   152  	}
   153  }
   154  

View as plain text