File: source_test.go

package info (click to toggle)
golang-github-google-cel-go 0.18.2%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,888 kB
  • sloc: sh: 93; makefile: 12
file content (137 lines) | stat: -rw-r--r-- 4,909 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// package common defines types common to parsing and other diagnostics.
package common

import (
	"testing"
)

const (
	unexpectedValue   = "%s got snippet '%v', want '%v'"
	unexpectedSnippet = "%s got snippet '%s', want '%v'"
	snippetNotFound   = "%s snippet not found, wanted '%v'"
	snippetFound      = "%s snippet found at line %d, wanted none"
)

// TestStringSource_Description the error description method.
func TestStringSource_Description(t *testing.T) {
	contents := "example content\nsecond line"
	source := NewStringSource(contents, "description-test")
	// Verify the content
	if source.Content() != contents {
		t.Errorf(unexpectedValue, t.Name(), contents, source.Content())
	}
	// Verify the description
	if source.Description() != "description-test" {
		t.Errorf(unexpectedValue, t.Name(), source.Description(), "description-test")
	}

	// Assert that the snippets on lines 1 & 2 are what was expected.
	if str2, found := source.Snippet(2); !found {
		t.Errorf(snippetNotFound, t.Name(), 2)

	} else if str2 != "second line" {
		t.Errorf(unexpectedSnippet, t.Name(), str2, "second line")
	}
	if str1, found := source.Snippet(1); !found {
		t.Errorf(snippetNotFound, t.Name(), 1)

	} else if str1 != "example content" {
		t.Errorf(unexpectedSnippet, t.Name(), str1, "example content")
	}
}

// TestStringSource_LocationOffset make sure that the offsets accurately reflect
// the location of a character in source.
func TestStringSource_LocationOffset(t *testing.T) {
	contents := "c.d &&\n\t b.c.arg(10) &&\n\t test(10)"
	source := NewStringSource(contents, "offset-test")
	expectedLineOffsets := []int32{7, 24, 35}
	if len(expectedLineOffsets) != len(source.LineOffsets()) {
		t.Errorf("Got list of size '%d', wanted size '%d'",
			len(source.LineOffsets()), len(expectedLineOffsets))
	} else {
		for i, val := range expectedLineOffsets {
			if val != source.LineOffsets()[i] {
				t.Errorf("Got %d, wanted line %d offset of %d, ",
					val, source.LineOffsets()[i], i)
			}
		}
	}
	// Ensure that selecting a set of characters across multiple lines works as
	// expected.
	charStart, _ := source.LocationOffset(NewLocation(1, 2))
	charEnd, _ := source.LocationOffset(NewLocation(3, 2))
	if string(contents[charStart:charEnd]) != "d &&\n\t b.c.arg(10) &&\n\t " {
		t.Errorf(unexpectedValue, t.Name(),
			string(contents[charStart:charEnd]),
			"d &&\n\t b.c.arg(10) &&\n\t ")
	}
	if _, found := source.LocationOffset(NewLocation(4, 0)); found {
		t.Error("Character offset was out of range of source, but still found.")
	}
}

// TestStringSource_SnippetMultiline snippets of text from a multiline source.
func TestStringSource_SnippetMultiline(t *testing.T) {
	source := NewStringSource("hello\nworld\nmy\nbub\n", "four-line-test")
	if str, found := source.Snippet(1); !found {
		t.Errorf(snippetNotFound, t.Name(), 1)
	} else if str != "hello" {
		t.Errorf(unexpectedSnippet, t.Name(), str, "hello")
	}
	if str2, found := source.Snippet(2); !found {
		t.Errorf(snippetNotFound, t.Name(), 2)
	} else if str2 != "world" {
		t.Errorf(unexpectedSnippet, t.Name(), str2, "world")
	}
	if str3, found := source.Snippet(3); !found {
		t.Errorf(snippetNotFound, t.Name(), 3)
	} else if str3 != "my" {
		t.Errorf(unexpectedSnippet, t.Name(), str3, "my")
	}
	if str4, found := source.Snippet(4); !found {
		t.Errorf(snippetNotFound, t.Name(), 4)
	} else if str4 != "bub" {
		t.Errorf(unexpectedSnippet, t.Name(), str4, "bub")
	}
	if str5, found := source.Snippet(5); !found {
		t.Errorf(snippetNotFound, t.Name(), 5)
	} else if str5 != "" {
		t.Errorf(unexpectedSnippet, t.Name(), str5, "")
	}
}

// TestStringSource_SnippetSingleline snippets from a single line source.
func TestStringSource_SnippetSingleline(t *testing.T) {
	source := NewStringSource("hello, world", "one-line-test")
	if str, found := source.Snippet(1); !found {
		t.Errorf(snippetNotFound, t.Name(), 1)

	} else if str != "hello, world" {
		t.Errorf(unexpectedSnippet, t.Name(), str, "hello, world")
	}
	if str2, found := source.Snippet(2); found {
		t.Errorf(snippetFound, t.Name(), 2)
	} else if str2 != "" {
		t.Errorf(unexpectedSnippet, t.Name(), str2, "")
	}
}

func TestNewInfoSource_NoPanicOnNil(t *testing.T) {
	// Ensure there is no panic when passing nil, NewInfoSource should use proto v2 style accessors.
	_ = NewInfoSource(nil)
}