| 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 commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ |
| 17 | |
| 18 | import ( |
| 19 | "stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/" |
| 20 | "testingscip-go gomod github.com/golang/go/src go1.23.0 testing/" |
| 21 | ) |
| 22 | |
| 23 | // TestErrors reporting and recording. |
| 24 | func TestErrorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/TestErrors().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 25 | source := NewStringSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewStringSource().("a.b\n&&arg(missing, paren", "errors-test") |
| 26 | errors := NewErrorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewErrors().(source) |
| 27 | errors.ReportErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ReportError().(NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewLocation().(1, 1), "No such field") |
| 28 | if len(errors.GetErrorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#GetErrors().()) != 1 { |
| 29 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("%s first error not recorded", t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().()) |
| 30 | } |
| 31 | errors.ReportErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ReportError().(NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewLocation().(2, 20), |
| 32 | "Syntax error, missing paren") |
| 33 | if len(errors.GetErrorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#GetErrors().()) != 2 { |
| 34 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("%s second error not recorded", t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().()) |
| 35 | } |
| 36 | got := errors.ToDisplayStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/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.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("%s got %s, wanted %s", t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), got, want) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestErrorsReportingLimitscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/TestErrorsReportingLimit().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 50 | errors := NewErrorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewErrors().(NewTextSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewTextSource().("hello world")) |
| 51 | for i := 0; i < 2*errors.maxErrorsToReportscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#maxErrorsToReport.; i++ { |
| 52 | errors.ReportErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ReportError().(NoLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NoLocation., "error %d", i) |
| 53 | } |
| 54 | if !stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.23.0 strings/HasSuffix().(errors.ToDisplayStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ToDisplayString().(), "100 more errors were truncated") { |
| 55 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("Error truncation did not succeed, got %s, wanted 100 errors truncated", errors.ToDisplayStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ToDisplayString().()) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestErrorsAppendReportingLimitscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/TestErrorsAppendReportingLimit().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 60 | errors := NewErrorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewErrors().(NewTextSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewTextSource().("hello world")) |
| 61 | for i := 0; i < 75; i++ { |
| 62 | errors.ReportErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ReportError().(NoLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NoLocation., "error %d", i) |
| 63 | } |
| 64 | errors2 := NewErrorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewErrors().(NewTextSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewTextSource().("hello world")) |
| 65 | for i := 0; i < 75; i++ { |
| 66 | errors2.ReportErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ReportError().(NoLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NoLocation., "error %d", i+75) |
| 67 | } |
| 68 | errors = errors.Appendscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#Append().(errors2.GetErrorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#GetErrors().()) |
| 69 | if !stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.23.0 strings/HasSuffix().(errors.ToDisplayStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ToDisplayString().(), "50 more errors were truncated") { |
| 70 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("Error truncation did not succeed, got %s, wanted 50 errors truncated", errors.ToDisplayStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ToDisplayString().()) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestErrors_WideAndNarrowCharactersscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/TestErrors_WideAndNarrowCharacters().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 75 | source := NewStringSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewStringSource().("你好吗\n我a很好\n", "errors-test") |
| 76 | errors := NewErrorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewErrors().(source) |
| 77 | errors.ReportErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ReportError().(NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewLocation().(2, 3), "Unexpected character '好'") |
| 78 | |
| 79 | got := errors.ToDisplayStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ToDisplayString().() |
| 80 | want := "ERROR: errors-test:2:4: Unexpected character '好'\n" + |
| 81 | " | 我a很好\n" + |
| 82 | " | ...^" |
| 83 | if got != want { |
| 84 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("%s got %s, wanted %s", t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), got, want) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestErrors_WideAndNarrowCharactersWithEmojisscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/TestErrors_WideAndNarrowCharactersWithEmojis().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 89 | source := NewStringSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewStringSource().(" '😁' in ['😁', '😑', '😦'] && in.😁", "errors-test") |
| 90 | errors := NewErrorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewErrors().(source) |
| 91 | errors.ReportErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ReportError().(NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewLocation().(1, 32), "Syntax error: extraneous input 'in' expecting {'[', '{', '(', '.', '-', '!', 'true', 'false', 'null', NUM_FLOAT, NUM_INT, NUM_UINT, STRING, BYTES, IDENTIFIER}") |
| 92 | errors.ReportErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ReportError().(NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewLocation().(1, 35), "Syntax error: token recognition error at: '😁'") |
| 93 | errors.ReportErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ReportError().(NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewLocation().(1, 36), "Syntax error: missing IDENTIFIER at '<EOF>'") |
| 94 | got := errors.ToDisplayStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/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.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("%s got %s, wanted %s", t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), got, want) |
| 106 | } |
| 107 | } |