|
| 1 | +package helpers |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestGetRegexForPath(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + tpl string |
| 11 | + wantErr bool |
| 12 | + wantExpr string |
| 13 | + }{ |
| 14 | + { |
| 15 | + name: "well-formed template with default pattern", |
| 16 | + tpl: "/orders/{id}", |
| 17 | + wantErr: false, |
| 18 | + wantExpr: "^/orders/([^/]+)$", |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "well-formed template with custom pattern", |
| 22 | + tpl: "/orders/{id:[0-9]+}", |
| 23 | + wantErr: false, |
| 24 | + wantExpr: "^/orders/([0-9]+)$", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "missing name in template", |
| 28 | + tpl: "/orders/{:pattern}", |
| 29 | + wantErr: true, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "missing pattern in template", |
| 33 | + tpl: "/orders/{name:}", |
| 34 | + wantErr: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "unbalanced braces in template", |
| 38 | + tpl: "/orders/{id", |
| 39 | + wantErr: true, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "unbalanced braces in template", |
| 43 | + tpl: "/orders/id}", |
| 44 | + wantErr: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "template with multiple variables", |
| 48 | + tpl: "/orders/{id:[0-9]+}/items/{itemId}", |
| 49 | + wantErr: false, |
| 50 | + wantExpr: "^/orders/([0-9]+)/items/([^/]+)$", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "OData formatted URL with single quotes", |
| 54 | + tpl: "/entities('{id}')", |
| 55 | + wantErr: false, |
| 56 | + wantExpr: "^/entities\\('([^/]+)'\\)$", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "OData formatted URL with custom pattern", |
| 60 | + tpl: "/entities('{id:[0-9]+}')", |
| 61 | + wantErr: false, |
| 62 | + wantExpr: "^/entities\\('([0-9]+)'\\)$", |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + got, err := GetRegexForPath(tt.tpl) |
| 69 | + if (err != nil) != tt.wantErr { |
| 70 | + t.Errorf("GetRegexForPath() error = %v, wantErr %v", err, tt.wantErr) |
| 71 | + return |
| 72 | + } |
| 73 | + if !tt.wantErr && got.String() != tt.wantExpr { |
| 74 | + t.Errorf("GetRegexForPath() = %v, want %v", got.String(), tt.wantExpr) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestBraceIndices(t *testing.T) { |
| 81 | + tests := []struct { |
| 82 | + name string |
| 83 | + s string |
| 84 | + want []int |
| 85 | + wantErr bool |
| 86 | + }{ |
| 87 | + { |
| 88 | + name: "well-formed braces", |
| 89 | + s: "/orders/{id}/items/{itemId}", |
| 90 | + want: []int{8, 12, 19, 27}, |
| 91 | + wantErr: false, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "unbalanced braces", |
| 95 | + s: "/orders/{id/items/{itemId}", |
| 96 | + wantErr: true, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "unbalanced braces", |
| 100 | + s: "/orders/{id}/items/{itemId", |
| 101 | + wantErr: true, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "no braces", |
| 105 | + s: "/orders/id/items/itemId", |
| 106 | + want: []int{}, |
| 107 | + wantErr: false, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "OData formatted URL with single quotes", |
| 111 | + s: "/entities('{id}')", |
| 112 | + want: []int{11, 15}, |
| 113 | + wantErr: false, |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + for _, tt := range tests { |
| 118 | + t.Run(tt.name, func(t *testing.T) { |
| 119 | + got, err := BraceIndices(tt.s) |
| 120 | + if (err != nil) != tt.wantErr { |
| 121 | + t.Errorf("BraceIndices() error = %v, wantErr %v", err, tt.wantErr) |
| 122 | + return |
| 123 | + } |
| 124 | + if !tt.wantErr && !equal(got, tt.want) { |
| 125 | + t.Errorf("BraceIndices() = %v, want %v", got, tt.want) |
| 126 | + } |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func equal(a, b []int) bool { |
| 132 | + if len(a) != len(b) { |
| 133 | + return false |
| 134 | + } |
| 135 | + for i := range a { |
| 136 | + if a[i] != b[i] { |
| 137 | + return false |
| 138 | + } |
| 139 | + } |
| 140 | + return true |
| 141 | +} |
0 commit comments