| 1 | // Copyright 2025 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 |
| 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | func TestParseDescription(t *testing.T) { |
| 23 | tests := []struct { |
| 24 | name string |
| 25 | in string |
| 26 | out string |
| 27 | }{ |
| 28 | { |
| 29 | name: "empty", |
| 30 | }, |
| 31 | { |
| 32 | name: "single", |
| 33 | in: "hello", |
| 34 | out: "hello", |
| 35 | }, |
| 36 | { |
| 37 | name: "multi", |
| 38 | in: "hello\n\n\nworld", |
| 39 | out: "hello\nworld", |
| 40 | }, |
| 41 | } |
| 42 | |
| 43 | for _, tst := range tests { |
| 44 | tc := tst |
| 45 | t.Run(tc.name, func(t *testing.T) { |
| 46 | out := ParseDescription(tc.in) |
| 47 | if !reflect.DeepEqual(out, tc.out) { |
| 48 | t.Errorf("ParseDescription() got %v, wanted %v", out, tc.out) |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestParseDescriptions(t *testing.T) { |
| 55 | tests := []struct { |
| 56 | name string |
| 57 | in string |
| 58 | out []string |
| 59 | }{ |
| 60 | { |
| 61 | name: "empty", |
| 62 | }, |
| 63 | { |
| 64 | name: "single", |
| 65 | in: "hello", |
| 66 | out: []string{"hello"}, |
| 67 | }, |
| 68 | { |
| 69 | name: "multi", |
| 70 | in: "bar\nbaz\n\nfoo", |
| 71 | out: []string{"bar\nbaz", "foo"}, |
| 72 | }, |
| 73 | { |
| 74 | name: "multi", |
| 75 | in: "hello\n\n\nworld", |
| 76 | out: []string{"hello", "world"}, |
| 77 | }, |
| 78 | } |
| 79 | |
| 80 | for _, tst := range tests { |
| 81 | tc := tst |
| 82 | t.Run(tc.name, func(t *testing.T) { |
| 83 | out := ParseDescriptions(tc.in) |
| 84 | if !reflect.DeepEqual(out, tc.out) { |
| 85 | t.Errorf("ParseDescriptions() got %v, wanted %v", out, tc.out) |
| 86 | } |
| 87 | }) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestNewDoc(t *testing.T) { |
| 92 | tests := []struct { |
| 93 | newDoc func() *Doc |
| 94 | kind DocKind |
| 95 | name string |
| 96 | celType string |
| 97 | sig string |
| 98 | desc string |
| 99 | childCount int |
| 100 | }{ |
| 101 | { |
| 102 | newDoc: func() *Doc { |
| 103 | return NewMacroDoc("map", "map converts a list or map of values to a list", |
| 104 | NewExampleDoc("[1, 2].map(i, i * 2) // [2, 4]")) |
| 105 | }, |
| 106 | kind: DocMacro, |
| 107 | name: "map", |
| 108 | desc: "map converts a list or map of values to a list", |
| 109 | childCount: 1, |
| 110 | }, |
| 111 | { |
| 112 | newDoc: func() *Doc { |
| 113 | return NewVariableDoc( |
| 114 | "request", |
| 115 | "google.rpc.context.AttributeContext.Request", |
| 116 | "parameters related to an HTTP API request") |
| 117 | }, |
| 118 | kind: DocVariable, |
| 119 | name: "request", |
| 120 | celType: "google.rpc.context.AttributeContext.Request", |
| 121 | desc: "parameters related to an HTTP API request", |
| 122 | childCount: 0, |
| 123 | }, |
| 124 | { |
| 125 | newDoc: func() *Doc { |
| 126 | return NewFunctionDoc("getToken", |
| 127 | "get the JWT token from a request\nas deserialized JSON", |
| 128 | NewOverloadDoc("request_getToken", "request.getToken() -> map(string, dyn)", |
| 129 | NewExampleDoc("has(request.getToken().sub) // false"))) |
| 130 | }, |
| 131 | kind: DocFunction, |
| 132 | name: "getToken", |
| 133 | desc: "get the JWT token from a request\nas deserialized JSON", |
| 134 | childCount: 1, |
| 135 | }, |
| 136 | } |
| 137 | |
| 138 | for _, tst := range tests { |
| 139 | tc := tst |
| 140 | t.Run(tc.name, func(t *testing.T) { |
| 141 | d := tc.newDoc() |
| 142 | if d.Kind != tc.kind { |
| 143 | t.Errorf("got doc kind %v, wanted %v", d.Kind, tc.kind) |
| 144 | } |
| 145 | if d.Name != tc.name { |
| 146 | t.Errorf("got doc name %s, wanted %s", d.Name, tc.name) |
| 147 | } |
| 148 | if d.Signature != tc.sig { |
| 149 | t.Errorf("got signature %s, wanted %s", d.Signature, tc.sig) |
| 150 | } |
| 151 | if !reflect.DeepEqual(d.Description, tc.desc) { |
| 152 | t.Errorf("got description %v, wanted %v", d.Description, tc.desc) |
| 153 | } |
| 154 | if d.Type != tc.celType { |
| 155 | t.Errorf("got type %s, wanted %s", d.Type, tc.celType) |
| 156 | } |
| 157 | if len(d.Children) != tc.childCount { |
| 158 | t.Errorf("got children %v, wanted count %d", d.Children, tc.childCount) |
| 159 | } |
| 160 | }) |
| 161 | } |
| 162 | } |