Debian Codegraph @

common/source_test.go

raw symbols SCIP
1 // Copyright 2018 Google LLC
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 common defines types common to parsing and other diagnostics.
16 package common
17
18 import (
19 "testing"
20 )
21
22 const (
23 unexpectedValue = "%s got snippet '%v', want '%v'"
24 unexpectedSnippet = "%s got snippet '%s', want '%v'"
25 snippetNotFound = "%s snippet not found, wanted '%v'"
26 snippetFound = "%s snippet found at line %d, wanted none"
27 )
28
29 // TestStringSource_Description the error description method.
30 func TestStringSource_Description(t *testing.T) {
31 contents := "example content\nsecond line"
32 source := NewStringSource(contents, "description-test")
33 // Verify the content
34 if source.Content() != contents {
35 t.Errorf(unexpectedValue, t.Name(), contents, source.Content())
36 }
37 // Verify the description
38 if source.Description() != "description-test" {
39 t.Errorf(unexpectedValue, t.Name(), source.Description(), "description-test")
40 }
41
42 // Assert that the snippets on lines 1 & 2 are what was expected.
43 if str2, found := source.Snippet(2); !found {
44 t.Errorf(snippetNotFound, t.Name(), 2)
45
46 } else if str2 != "second line" {
47 t.Errorf(unexpectedSnippet, t.Name(), str2, "second line")
48 }
49 if str1, found := source.Snippet(1); !found {
50 t.Errorf(snippetNotFound, t.Name(), 1)
51
52 } else if str1 != "example content" {
53 t.Errorf(unexpectedSnippet, t.Name(), str1, "example content")
54 }
55 }
56
57 // TestStringSource_LocationOffset make sure that the offsets accurately reflect
58 // the location of a character in source.
59 func TestStringSource_LocationOffset(t *testing.T) {
60 contents := "c.d &&\n\t b.c.arg(10) &&\n\t test(10)"
61 source := NewStringSource(contents, "offset-test")
62 expectedLineOffsets := []int32{7, 24, 35}
63 if len(expectedLineOffsets) != len(source.LineOffsets()) {
64 t.Errorf("Got list of size '%d', wanted size '%d'",
65 len(source.LineOffsets()), len(expectedLineOffsets))
66 } else {
67 for i, val := range expectedLineOffsets {
68 if val != source.LineOffsets()[i] {
69 t.Errorf("Got %d, wanted line %d offset of %d, ",
70 val, source.LineOffsets()[i], i)
71 }
72 }
73 }
74 // Ensure that selecting a set of characters across multiple lines works as
75 // expected.
76 charStart, _ := source.LocationOffset(NewLocation(1, 2))
77 charEnd, _ := source.LocationOffset(NewLocation(3, 2))
78 if string(contents[charStart:charEnd]) != "d &&\n\t b.c.arg(10) &&\n\t " {
79 t.Errorf(unexpectedValue, t.Name(),
80 string(contents[charStart:charEnd]),
81 "d &&\n\t b.c.arg(10) &&\n\t ")
82 }
83 if _, found := source.LocationOffset(NewLocation(4, 0)); found {
84 t.Error("Character offset was out of range of source, but still found.")
85 }
86 }
87
88 // TestStringSource_SnippetMultiline snippets of text from a multiline source.
89 func TestStringSource_SnippetMultiline(t *testing.T) {
90 source := NewStringSource("hello\nworld\nmy\nbub\n", "four-line-test")
91 if str, found := source.Snippet(1); !found {
92 t.Errorf(snippetNotFound, t.Name(), 1)
93 } else if str != "hello" {
94 t.Errorf(unexpectedSnippet, t.Name(), str, "hello")
95 }
96 if str2, found := source.Snippet(2); !found {
97 t.Errorf(snippetNotFound, t.Name(), 2)
98 } else if str2 != "world" {
99 t.Errorf(unexpectedSnippet, t.Name(), str2, "world")
100 }
101 if str3, found := source.Snippet(3); !found {
102 t.Errorf(snippetNotFound, t.Name(), 3)
103 } else if str3 != "my" {
104 t.Errorf(unexpectedSnippet, t.Name(), str3, "my")
105 }
106 if str4, found := source.Snippet(4); !found {
107 t.Errorf(snippetNotFound, t.Name(), 4)
108 } else if str4 != "bub" {
109 t.Errorf(unexpectedSnippet, t.Name(), str4, "bub")
110 }
111 if str5, found := source.Snippet(5); !found {
112 t.Errorf(snippetNotFound, t.Name(), 5)
113 } else if str5 != "" {
114 t.Errorf(unexpectedSnippet, t.Name(), str5, "")
115 }
116 }
117
118 // TestStringSource_SnippetSingleline snippets from a single line source.
119 func TestStringSource_SnippetSingleline(t *testing.T) {
120 source := NewStringSource("hello, world", "one-line-test")
121 if str, found := source.Snippet(1); !found {
122 t.Errorf(snippetNotFound, t.Name(), 1)
123
124 } else if str != "hello, world" {
125 t.Errorf(unexpectedSnippet, t.Name(), str, "hello, world")
126 }
127 if str2, found := source.Snippet(2); found {
128 t.Errorf(snippetFound, t.Name(), 2)
129 } else if str2 != "" {
130 t.Errorf(unexpectedSnippet, t.Name(), str2, "")
131 }
132 }
133
134 func TestNewInfoSource_NoPanicOnNil(t *testing.T) {
135 // Ensure there is no panic when passing nil, NewInfoSource should use proto v2 style accessors.
136 _ = NewInfoSource(nil)
137 }