| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package common |
| 17 |
|
| 18 |
import ( |
| 19 |
"strings" |
| 20 |
"testing" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
func TestErrors(t *testing.T) { |
| 25 |
source := NewStringSource("a.b\n&&arg(missing, paren", "errors-test") |
| 26 |
errors := NewErrors(source) |
| 27 |
errors.ReportError(NewLocation(1, 1), "No such field") |
| 28 |
if len(errors.GetErrors()) != 1 { |
| 29 |
t.Errorf("%s first error not recorded", t.Name()) |
| 30 |
} |
| 31 |
errors.ReportError(NewLocation(2, 20), |
| 32 |
"Syntax error, missing paren") |
| 33 |
if len(errors.GetErrors()) != 2 { |
| 34 |
t.Errorf("%s second error not recorded", t.Name()) |
| 35 |
} |
| 36 |
got := errors.ToDisplayString() |
| 37 |
want := |
| 38 |
"ERROR: errors-test:1:2: No such field\n" + |
| 39 |
" | a.b\n" + |
| 40 |
" | .^\n" + |
| 41 |
"ERROR: errors-test:2:21: Syntax error, missing paren\n" + |
| 42 |
" | &&arg(missing, paren\n" + |
| 43 |
" | ....................^" |
| 44 |
if got != want { |
| 45 |
t.Errorf("%s got %s, wanted %s", t.Name(), got, want) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func TestErrorsReportingLimit(t *testing.T) { |
| 50 |
errors := NewErrors(NewTextSource("hello world")) |
| 51 |
for i := 0; i < 2*errors.maxErrorsToReport; i++ { |
| 52 |
errors.ReportError(NoLocation, "error %d", i) |
| 53 |
} |
| 54 |
if !strings.HasSuffix(errors.ToDisplayString(), "100 more errors were truncated") { |
| 55 |
t.Errorf("Error truncation did not succeed, got %s, wanted 100 errors truncated", errors.ToDisplayString()) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func TestErrorsAppendReportingLimit(t *testing.T) { |
| 60 |
errors := NewErrors(NewTextSource("hello world")) |
| 61 |
for i := 0; i < 75; i++ { |
| 62 |
errors.ReportError(NoLocation, "error %d", i) |
| 63 |
} |
| 64 |
errors2 := NewErrors(NewTextSource("hello world")) |
| 65 |
for i := 0; i < 75; i++ { |
| 66 |
errors2.ReportError(NoLocation, "error %d", i+75) |
| 67 |
} |
| 68 |
errors = errors.Append(errors2.GetErrors()) |
| 69 |
if !strings.HasSuffix(errors.ToDisplayString(), "50 more errors were truncated") { |
| 70 |
t.Errorf("Error truncation did not succeed, got %s, wanted 50 errors truncated", errors.ToDisplayString()) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestErrors_WideAndNarrowCharacters(t *testing.T) { |
| 75 |
source := NewStringSource("你好吗\n我a很好\n", "errors-test") |
| 76 |
errors := NewErrors(source) |
| 77 |
errors.ReportError(NewLocation(2, 3), "Unexpected character '好'") |
| 78 |
|
| 79 |
got := errors.ToDisplayString() |
| 80 |
want := "ERROR: errors-test:2:4: Unexpected character '好'\n" + |
| 81 |
" | 我a很好\n" + |
| 82 |
" | ...^" |
| 83 |
if got != want { |
| 84 |
t.Errorf("%s got %s, wanted %s", t.Name(), got, want) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func TestErrors_WideAndNarrowCharactersWithEmojis(t *testing.T) { |
| 89 |
source := NewStringSource(" '😁' in ['😁', '😑', '😦'] && in.😁", "errors-test") |
| 90 |
errors := NewErrors(source) |
| 91 |
errors.ReportError(NewLocation(1, 32), "Syntax error: extraneous input 'in' expecting {'[', '{', '(', '.', '-', '!', 'true', 'false', 'null', NUM_FLOAT, NUM_INT, NUM_UINT, STRING, BYTES, IDENTIFIER}") |
| 92 |
errors.ReportError(NewLocation(1, 35), "Syntax error: token recognition error at: '😁'") |
| 93 |
errors.ReportError(NewLocation(1, 36), "Syntax error: missing IDENTIFIER at '<EOF>'") |
| 94 |
got := errors.ToDisplayString() |
| 95 |
want := "ERROR: errors-test:1:33: Syntax error: extraneous input 'in' expecting {'[', '{', '(', '.', '-', '!', 'true', 'false', 'null', NUM_FLOAT, NUM_INT, NUM_UINT, STRING, BYTES, IDENTIFIER}\n" + |
| 96 |
" | '😁' in ['😁', '😑', '😦'] && in.😁\n" + |
| 97 |
" | ................................^\n" + |
| 98 |
"ERROR: errors-test:1:36: Syntax error: token recognition error at: '😁'\n" + |
| 99 |
" | '😁' in ['😁', '😑', '😦'] && in.😁\n" + |
| 100 |
" | ...................................^\n" + |
| 101 |
"ERROR: errors-test:1:37: Syntax error: missing IDENTIFIER at '<EOF>'\n" + |
| 102 |
" | '😁' in ['😁', '😑', '😦'] && in.😁\n" + |
| 103 |
" | ....................................^" |
| 104 |
if got != want { |
| 105 |
t.Errorf("%s got %s, wanted %s", t.Name(), got, want) |
| 106 |
} |
| 107 |
} |