|
| 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 | +// https://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 discovery |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/google/go-cmp/cmp" |
| 21 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 22 | + "github.com/googleapis/librarian/internal/sidekick/internal/api" |
| 23 | +) |
| 24 | + |
| 25 | +func TestParseUriTemplateSuccess(t *testing.T) { |
| 26 | + for _, test := range []struct { |
| 27 | + input string |
| 28 | + want *api.PathTemplate |
| 29 | + }{ |
| 30 | + {"locations/global/firewallPolicies", api.NewPathTemplate().WithLiteral("locations").WithLiteral("global").WithLiteral("firewallPolicies")}, |
| 31 | + {"locations/global/operations/{operation}", api.NewPathTemplate().WithLiteral("locations").WithLiteral("global").WithLiteral("operations").WithVariable(api.NewPathVariable("operation"))}, |
| 32 | + {"projects/{project}/zones/{zone}/{parentName}/reservationSubBlocks", api.NewPathTemplate().WithLiteral("projects").WithVariable(api.NewPathVariable("project")).WithLiteral("zones").WithVariable(api.NewPathVariable("zone")).WithVariable(api.NewPathVariable("parentName")).WithLiteral("reservationSubBlocks")}, |
| 33 | + } { |
| 34 | + got, err := ParseUriTemplate(test.input) |
| 35 | + if err != nil { |
| 36 | + t.Errorf("expected a successful parse with input=%s, err=%v", test.input, err) |
| 37 | + continue |
| 38 | + } |
| 39 | + if diff := cmp.Diff(test.want, got, cmpopts.EquateEmpty()); diff != "" { |
| 40 | + t.Errorf("mismatch [%s] (-want, +got):\n%s", test.input, diff) |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestParseUriTemplateError(t *testing.T) { |
| 46 | + for _, test := range []struct { |
| 47 | + input string |
| 48 | + }{ |
| 49 | + {"v1/{+parent}/externalAccountKeys"}, |
| 50 | + {"a/b/c/"}, |
| 51 | + {"a/b/c|"}, |
| 52 | + {"a/b/{c}|"}, |
| 53 | + {"a/b/{c}}/d"}, |
| 54 | + {"a/b/{c}}"}, |
| 55 | + {"a/b/{c}/"}, |
| 56 | + {"{foo}}bar"}, |
| 57 | + } { |
| 58 | + if got, err := ParseUriTemplate(test.input); err == nil { |
| 59 | + t.Errorf("expected a parsing error with input=%s, got=%v", test.input, got) |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestParseExpression(t *testing.T) { |
| 65 | + for _, test := range []struct { |
| 66 | + input string |
| 67 | + want string |
| 68 | + }{ |
| 69 | + {"{abc}", "abc"}, |
| 70 | + {"{Abc}", "Abc"}, |
| 71 | + {"{abc012}", "abc012"}, |
| 72 | + {"{abc_012}", "abc_012"}, |
| 73 | + {"{abc_012}/foo/{bar}", "abc_012"}, |
| 74 | + } { |
| 75 | + gotSegment, gotWidth, err := parseExpression(test.input) |
| 76 | + if err != nil { |
| 77 | + t.Errorf("expected a successful parse with input=%s, err=%v", test.input, err) |
| 78 | + continue |
| 79 | + } |
| 80 | + if diff := cmp.Diff(&api.PathSegment{Variable: api.NewPathVariable(test.want)}, gotSegment); diff != "" { |
| 81 | + t.Errorf("mismatch [%s] (-want, +got):\n%s", test.input, diff) |
| 82 | + } |
| 83 | + if len(test.want)+2 != gotWidth { |
| 84 | + t.Errorf("mismatch want=%d, got=%d", len(test.want), gotWidth) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestParseExpressionError(t *testing.T) { |
| 90 | + for _, input := range []string{ |
| 91 | + "", "(a)", |
| 92 | + "{+a}", "{#a}", |
| 93 | + "{.a}", "{/a}", "{?a}", "{&a}", |
| 94 | + "{=a}", "{,a}", "{!a}", "{@a}", "{|a}", |
| 95 | + "{a,b}", "{_abc}", "{0abc}", "{ab"} { |
| 96 | + if gotSegment, gotWidth, err := parseExpression(input); err == nil { |
| 97 | + t.Errorf("expected a parsing error with input=%s, gotSegment=%v, gotWidth=%v", input, gotSegment, gotWidth) |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestParseLiteral(t *testing.T) { |
| 103 | + for _, test := range []struct { |
| 104 | + input string |
| 105 | + want string |
| 106 | + }{ |
| 107 | + {"abc/def", "abc"}, |
| 108 | + {"abcde/f", "abcde"}, |
| 109 | + {"abcdef", "abcdef"}, |
| 110 | + } { |
| 111 | + gotSegment, gotWidth, err := parseLiteral(test.input) |
| 112 | + if err != nil { |
| 113 | + t.Errorf("expected a successful parse with input=%s, err=%v", test.input, err) |
| 114 | + continue |
| 115 | + } |
| 116 | + if diff := cmp.Diff(&api.PathSegment{Literal: &test.want}, gotSegment); diff != "" { |
| 117 | + t.Errorf("mismatch [%s] (-want, +got):\n%s", test.input, diff) |
| 118 | + } |
| 119 | + if len(test.want) != gotWidth { |
| 120 | + t.Errorf("mismatch want=%d, got=%d", len(test.want), gotWidth) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestParseLiteralError(t *testing.T) { |
| 126 | + for _, input := range []string{"", "^", "'", "/", "abc^"} { |
| 127 | + if gotSegment, gotWidth, err := parseLiteral(input); err == nil { |
| 128 | + t.Errorf("expected a parsing error with input=%s, gotSegment=%v, gotWidth=%v", input, gotSegment, gotWidth) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments