| 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 | "testingscip-go gomod github.com/golang/go/src go1.23.0 testing/" |
| 20 | ) |
| 21 | |
| 22 | const ( |
| 23 | unexpectedValuescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedValue. = "%s got snippet '%v', want '%v'" |
| 24 | unexpectedSnippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedSnippet. = "%s got snippet '%s', want '%v'" |
| 25 | snippetNotFoundscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/snippetNotFound. = "%s snippet not found, wanted '%v'" |
| 26 | snippetFoundscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/snippetFound. = "%s snippet found at line %d, wanted none" |
| 27 | ) |
| 28 | |
| 29 | // TestStringSource_Description the error description method. |
| 30 | func TestStringSource_Descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/TestStringSource_Description().(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#) { |
| 31 | contents := "example content\nsecond line" |
| 32 | source := NewStringSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewStringSource().(contents, "description-test") |
| 33 | // Verify the content |
| 34 | if source.Contentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Content.() != contents { |
| 35 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(unexpectedValuescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedValue., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), contents, source.Contentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Content.()) |
| 36 | } |
| 37 | // Verify the description |
| 38 | if source.Descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Description.() != "description-test" { |
| 39 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(unexpectedValuescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedValue., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), source.Descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Description.(), "description-test") |
| 40 | } |
| 41 | |
| 42 | // Assert that the snippets on lines 1 & 2 are what was expected. |
| 43 | if str2, found := source.Snippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Snippet.(2); !found { |
| 44 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(snippetNotFoundscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/snippetNotFound., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), 2) |
| 45 | |
| 46 | } else if str2 != "second line" { |
| 47 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(unexpectedSnippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedSnippet., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), str2, "second line") |
| 48 | } |
| 49 | if str1, found := source.Snippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Snippet.(1); !found { |
| 50 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(snippetNotFoundscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/snippetNotFound., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), 1) |
| 51 | |
| 52 | } else if str1 != "example content" { |
| 53 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(unexpectedSnippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedSnippet., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#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_LocationOffsetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/TestStringSource_LocationOffset().(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 | contents := "c.d &&\n\t b.c.arg(10) &&\n\t test(10)" |
| 61 | source := NewStringSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewStringSource().(contents, "offset-test") |
| 62 | expectedLineOffsets := []int32{7, 24, 35} |
| 63 | if len(expectedLineOffsets) != len(source.LineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#LineOffsets.()) { |
| 64 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("Got list of size '%d', wanted size '%d'", |
| 65 | len(source.LineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#LineOffsets.()), len(expectedLineOffsets)) |
| 66 | } else { |
| 67 | for i, val := range expectedLineOffsets { |
| 68 | if val != source.LineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#LineOffsets.()[i] { |
| 69 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("Got %d, wanted line %d offset of %d, ", |
| 70 | val, source.LineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/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.LocationOffsetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#LocationOffset.(NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewLocation().(1, 2)) |
| 77 | charEnd, _ := source.LocationOffsetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#LocationOffset.(NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewLocation().(3, 2)) |
| 78 | if string(contents[charStart:charEnd]) != "d &&\n\t b.c.arg(10) &&\n\t " { |
| 79 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(unexpectedValuescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedValue., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), |
| 80 | string(contents[charStart:charEnd]), |
| 81 | "d &&\n\t b.c.arg(10) &&\n\t ") |
| 82 | } |
| 83 | if _, found := source.LocationOffsetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#LocationOffset.(NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewLocation().(4, 0)); found { |
| 84 | t.Errorscip-go gomod github.com/golang/go/src go1.23.0 testing/common#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_SnippetMultilinescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/TestStringSource_SnippetMultiline().(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#) { |
| 90 | source := NewStringSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewStringSource().("hello\nworld\nmy\nbub\n", "four-line-test") |
| 91 | if str, found := source.Snippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Snippet.(1); !found { |
| 92 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(snippetNotFoundscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/snippetNotFound., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), 1) |
| 93 | } else if str != "hello" { |
| 94 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(unexpectedSnippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedSnippet., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), str, "hello") |
| 95 | } |
| 96 | if str2, found := source.Snippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Snippet.(2); !found { |
| 97 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(snippetNotFoundscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/snippetNotFound., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), 2) |
| 98 | } else if str2 != "world" { |
| 99 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(unexpectedSnippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedSnippet., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), str2, "world") |
| 100 | } |
| 101 | if str3, found := source.Snippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Snippet.(3); !found { |
| 102 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(snippetNotFoundscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/snippetNotFound., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), 3) |
| 103 | } else if str3 != "my" { |
| 104 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(unexpectedSnippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedSnippet., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), str3, "my") |
| 105 | } |
| 106 | if str4, found := source.Snippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Snippet.(4); !found { |
| 107 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(snippetNotFoundscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/snippetNotFound., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), 4) |
| 108 | } else if str4 != "bub" { |
| 109 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(unexpectedSnippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedSnippet., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), str4, "bub") |
| 110 | } |
| 111 | if str5, found := source.Snippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Snippet.(5); !found { |
| 112 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(snippetNotFoundscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/snippetNotFound., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), 5) |
| 113 | } else if str5 != "" { |
| 114 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(unexpectedSnippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedSnippet., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), str5, "") |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // TestStringSource_SnippetSingleline snippets from a single line source. |
| 119 | func TestStringSource_SnippetSinglelinescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/TestStringSource_SnippetSingleline().(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#) { |
| 120 | source := NewStringSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewStringSource().("hello, world", "one-line-test") |
| 121 | if str, found := source.Snippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Snippet.(1); !found { |
| 122 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(snippetNotFoundscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/snippetNotFound., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), 1) |
| 123 | |
| 124 | } else if str != "hello, world" { |
| 125 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(unexpectedSnippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedSnippet., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), str, "hello, world") |
| 126 | } |
| 127 | if str2, found := source.Snippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Snippet.(2); found { |
| 128 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(snippetFoundscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/snippetFound., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), 2) |
| 129 | } else if str2 != "" { |
| 130 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().(unexpectedSnippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/unexpectedSnippet., t.Namescip-go gomod github.com/golang/go/src go1.23.0 testing/common#Name().(), str2, "") |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func TestNewInfoSource_NoPanicOnNilscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/TestNewInfoSource_NoPanicOnNil().(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#) { |
| 135 | // Ensure there is no panic when passing nil, NewInfoSource should use proto v2 style accessors. |
| 136 | _ = NewInfoSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewInfoSource().(nil) |
| 137 | } |