Debian Codegraph @

common/errors_test.go

raw
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 "strings"
20 "testing"
21 )
22
23 // TestErrors reporting and recording.
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 }