| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 30 |
func TestStringSource_Description(t *testing.T) { |
| 31 |
contents := "example content\nsecond line" |
| 32 |
source := NewStringSource(contents, "description-test") |
| 33 |
|
| 34 |
if source.Content() != contents { |
| 35 |
t.Errorf(unexpectedValue, t.Name(), contents, source.Content()) |
| 36 |
} |
| 37 |
|
| 38 |
if source.Description() != "description-test" { |
| 39 |
t.Errorf(unexpectedValue, t.Name(), source.Description(), "description-test") |
| 40 |
} |
| 41 |
|
| 42 |
|
| 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 |
|
| 58 |
|
| 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 |
|
| 75 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 136 |
_ = NewInfoSource(nil) |
| 137 |
} |