cuelang.org/go@v0.10.1/cue/token/position_test.go (about)

     1  // Copyright 2018 The CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package token
    16  
    17  import (
    18  	"fmt"
    19  	"slices"
    20  	"testing"
    21  )
    22  
    23  func checkPos(t *testing.T, msg string, got, want Position) {
    24  	if got.Filename != want.Filename {
    25  		t.Errorf("%s: got filename = %q; want %q", msg, got.Filename, want.Filename)
    26  	}
    27  	if got.Offset != want.Offset {
    28  		t.Errorf("%s: got offset = %d; want %d", msg, got.Offset, want.Offset)
    29  	}
    30  	if got.Line != want.Line {
    31  		t.Errorf("%s: got line = %d; want %d", msg, got.Line, want.Line)
    32  	}
    33  	if got.Column != want.Column {
    34  		t.Errorf("%s: got column = %d; want %d", msg, got.Column, want.Column)
    35  	}
    36  }
    37  
    38  func TestNoPos(t *testing.T) {
    39  	if NoPos.IsValid() {
    40  		t.Errorf("NoPos should not be valid")
    41  	}
    42  	checkPos(t, "nil NoPos", NoPos.Position(), Position{})
    43  }
    44  
    45  var tests = []struct {
    46  	filename string
    47  	source   []byte // may be nil
    48  	size     int
    49  	lines    []int
    50  }{
    51  	{"a", []byte{}, 0, []int{}},
    52  	{"b", []byte("01234"), 5, []int{0}},
    53  	{"c", []byte("\n\n\n\n\n\n\n\n\n"), 9, []int{0, 1, 2, 3, 4, 5, 6, 7, 8}},
    54  	{"d", nil, 100, []int{0, 5, 10, 20, 30, 70, 71, 72, 80, 85, 90, 99}},
    55  	{"e", nil, 777, []int{0, 80, 100, 120, 130, 180, 267, 455, 500, 567, 620}},
    56  	{"f", []byte("package p\n\nimport \"fmt\""), 23, []int{0, 10, 11}},
    57  	{"g", []byte("package p\n\nimport \"fmt\"\n"), 24, []int{0, 10, 11}},
    58  	{"h", []byte("package p\n\nimport \"fmt\"\n "), 25, []int{0, 10, 11, 24}},
    59  }
    60  
    61  func linecol(lines []int, offs int) (int, int) {
    62  	prevLineOffs := 0
    63  	for line, lineOffs := range lines {
    64  		if offs < lineOffs {
    65  			return line, offs - prevLineOffs + 1
    66  		}
    67  		prevLineOffs = lineOffs
    68  	}
    69  	return len(lines), offs - prevLineOffs + 1
    70  }
    71  
    72  func verifyPositions(t *testing.T, f *File, lines []int) {
    73  	if got := f.Lines(); !slices.Equal(got, lines) {
    74  		t.Fatalf("File.Lines got %v instead of %v", got, lines)
    75  	}
    76  	for offs := 0; offs < f.Size(); offs++ {
    77  		p := f.Pos(offs, 0)
    78  		offs2 := f.Offset(p)
    79  		if offs2 != offs {
    80  			t.Errorf("%s, Offset: got offset %d; want %d", f.Name(), offs2, offs)
    81  		}
    82  		line, col := linecol(lines, offs)
    83  		msg := fmt.Sprintf("%s (offs = %d, p = %d)", f.Name(), offs, p.offset)
    84  		checkPos(t, msg, f.Pos(offs, 0).Position(), Position{f.Name(), offs, line, col})
    85  		checkPos(t, msg, p.Position(), Position{f.Name(), offs, line, col})
    86  	}
    87  }
    88  
    89  func makeTestSource(size int, lines []int) []byte {
    90  	src := make([]byte, size)
    91  	for _, offs := range lines {
    92  		if offs > 0 {
    93  			src[offs-1] = '\n'
    94  		}
    95  	}
    96  	return src
    97  }
    98  
    99  func TestPositions(t *testing.T) {
   100  	const delta = 7 // a non-zero base offset increment
   101  	for _, test := range tests {
   102  		// verify consistency of test case
   103  		if test.source != nil && len(test.source) != test.size {
   104  			t.Errorf("%s: inconsistent test case: got file size %d; want %d", test.filename, len(test.source), test.size)
   105  		}
   106  
   107  		// add file and verify name and size
   108  		f := NewFile(test.filename, 1+delta, test.size)
   109  		if f.Name() != test.filename {
   110  			t.Errorf("got filename %q; want %q", f.Name(), test.filename)
   111  		}
   112  		if f.Size() != test.size {
   113  			t.Errorf("%s: got file size %d; want %d", f.Name(), f.Size(), test.size)
   114  		}
   115  		if f.Pos(0, 0).file != f {
   116  			t.Errorf("%s: f.Pos(0, 0) was not found in f", f.Name())
   117  		}
   118  
   119  		// add lines individually and verify all positions
   120  		for i, offset := range test.lines {
   121  			f.AddLine(offset)
   122  			if f.LineCount() != i+1 {
   123  				t.Errorf("%s, AddLine: got line count %d; want %d", f.Name(), f.LineCount(), i+1)
   124  			}
   125  			// adding the same offset again should be ignored
   126  			f.AddLine(offset)
   127  			if f.LineCount() != i+1 {
   128  				t.Errorf("%s, AddLine: got unchanged line count %d; want %d", f.Name(), f.LineCount(), i+1)
   129  			}
   130  			verifyPositions(t, f, test.lines[0:i+1])
   131  		}
   132  
   133  		// add lines with SetLines and verify all positions
   134  		if ok := f.SetLines(test.lines); !ok {
   135  			t.Errorf("%s: SetLines failed", f.Name())
   136  		}
   137  		if f.LineCount() != len(test.lines) {
   138  			t.Errorf("%s, SetLines: got line count %d; want %d", f.Name(), f.LineCount(), len(test.lines))
   139  		}
   140  		verifyPositions(t, f, test.lines)
   141  
   142  		// add lines with SetLinesForContent and verify all positions
   143  		src := test.source
   144  		if src == nil {
   145  			// no test source available - create one from scratch
   146  			src = makeTestSource(test.size, test.lines)
   147  		}
   148  		f.SetLinesForContent(src)
   149  		if f.LineCount() != len(test.lines) {
   150  			t.Errorf("%s, SetLinesForContent: got line count %d; want %d", f.Name(), f.LineCount(), len(test.lines))
   151  		}
   152  		verifyPositions(t, f, test.lines)
   153  	}
   154  }
   155  
   156  func TestLineInfo(t *testing.T) {
   157  	f := NewFile("foo", 1, 500)
   158  	lines := []int{0, 42, 77, 100, 210, 220, 277, 300, 333, 401}
   159  	// add lines individually and provide alternative line information
   160  	for _, offs := range lines {
   161  		f.AddLine(offs)
   162  		f.AddLineInfo(offs, "bar", 42)
   163  	}
   164  	// verify positions for all offsets
   165  	for offs := 0; offs <= f.Size(); offs++ {
   166  		p := f.Pos(offs, 0)
   167  		_, col := linecol(lines, offs)
   168  		msg := fmt.Sprintf("%s (offs = %d, p = %d)", f.Name(), offs, p.offset)
   169  		checkPos(t, msg, f.Position(f.Pos(offs, 0)), Position{"bar", offs, 42, col})
   170  		checkPos(t, msg, p.Position(), Position{"bar", offs, 42, col})
   171  	}
   172  }
   173  
   174  func TestPositionFor(t *testing.T) {
   175  	src := []byte(`
   176  foo
   177  b
   178  ar
   179  //line :100
   180  foobar
   181  //line bar:3
   182  done
   183  `)
   184  
   185  	const filename = "foo"
   186  	f := NewFile(filename, 1, len(src))
   187  	f.SetLinesForContent(src)
   188  
   189  	// verify position info
   190  	for i, offs := range f.lines {
   191  		got1 := f.PositionFor(f.Pos(int(offs), 0), false)
   192  		got2 := f.PositionFor(f.Pos(int(offs), 0), true)
   193  		got3 := f.Position(f.Pos(int(offs), 0))
   194  		want := Position{filename, int(offs), i + 1, 1}
   195  		checkPos(t, "1. PositionFor unadjusted", got1, want)
   196  		checkPos(t, "1. PositionFor adjusted", got2, want)
   197  		checkPos(t, "1. Position", got3, want)
   198  	}
   199  
   200  	// manually add //line info on lines l1, l2
   201  	const l1, l2 = 5, 7
   202  	f.AddLineInfo(int(f.lines[l1-1]), "", 100)
   203  	f.AddLineInfo(int(f.lines[l2-1]), "bar", 3)
   204  
   205  	// unadjusted position info must remain unchanged
   206  	for i, offs := range f.lines {
   207  		got1 := f.PositionFor(f.Pos(int(offs), 0), false)
   208  		want := Position{filename, int(offs), i + 1, 1}
   209  		checkPos(t, "2. PositionFor unadjusted", got1, want)
   210  	}
   211  
   212  	// adjusted position info should have changed
   213  	for i, offs := range f.lines {
   214  		got2 := f.PositionFor(f.Pos(int(offs), 0), true)
   215  		got3 := f.Position(f.Pos(int(offs), 0))
   216  		want := Position{filename, int(offs), i + 1, 1}
   217  		// manually compute wanted filename and line
   218  		line := want.Line
   219  		if i+1 >= l1 {
   220  			want.Filename = ""
   221  			want.Line = line - l1 + 100
   222  		}
   223  		if i+1 >= l2 {
   224  			want.Filename = "bar"
   225  			want.Line = line - l2 + 3
   226  		}
   227  		checkPos(t, "3. PositionFor adjusted", got2, want)
   228  		checkPos(t, "3. Position", got3, want)
   229  	}
   230  }