github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/utils/srl/srl_test.go (about) 1 package srl_test 2 3 import ( 4 "testing" 5 6 "github.com/clubpay/ronykit/kit/utils/srl" 7 . "github.com/onsi/ginkgo/v2" 8 . "github.com/onsi/gomega" 9 ) 10 11 func TestSRL(t *testing.T) { 12 RegisterFailHandler(Fail) 13 14 RunSpecs(t, "SRL TestSuite") 15 } 16 17 var _ = Describe("Address stringer & formatter", func() { 18 entries := []any{ 19 Entry("empty", srl.New("", "", ""), ""), 20 Entry("without group", srl.New("", "", "pos-vendor"), "@pos-vendor"), 21 Entry("without storage & id", srl.New("", "ir", ""), "ir"), 22 Entry("without storage", srl.New("", "ir", "pos-vendor"), "ir@pos-vendor"), 23 Entry("without path & id", srl.New("ws", "", ""), "ws:"), 24 Entry("without path", srl.New("ws", "", "pos-vendor"), "ws:@pos-vendor"), 25 Entry("without id", srl.New("ws", "ir", ""), "ws:ir"), 26 Entry("full", srl.New("ws", "ir", "pos-vendor"), "ws:ir@pos-vendor"), 27 Entry( 28 "internal worker specific", 29 srl.Storage("ws"). 30 Append(srl.Portable("doshii", "")). 31 Append(srl.Portable("str", "")). 32 Append(srl.Portable("tr", "")). 33 Append(srl.Portable("au", "")). 34 Append(srl.Portable("myRestaurant", "myOrder123")), 35 "ws:doshii:str:tr:au:myRestaurant@myOrder123", 36 ), 37 Entry( 38 "sdk worker specific", 39 srl.Storage("ws"). 40 Append(srl.Portable("doshii", "")). 41 Append(srl.Portable("sdk", "")). 42 Append(srl.Portable("ds", "")). 43 Append(srl.Portable("update-payment-status", "546905640")), 44 "ws:doshii:sdk:ds:update-payment-status@546905640", 45 ), 46 } 47 48 DescribeTable( 49 "address stringer", 50 append( 51 []any{ 52 func(addr srl.SRL, str string) { 53 Expect(addr.String()).To(BeIdenticalTo(str)) 54 }, 55 }, entries...)..., 56 ) 57 58 DescribeTable( 59 "address parser", 60 append( 61 []any{ 62 func(addr srl.SRL, str string) { 63 Expect(srl.Parse(str)).To(BeIdenticalTo(addr)) 64 }, 65 }, entries...)..., 66 ) 67 68 DescribeTable( 69 "parse formatted address", 70 append( 71 []any{ 72 func(addr srl.SRL, _ string) { 73 Expect(srl.Parse(addr.String())).To(BeIdenticalTo(addr)) 74 }, 75 }, entries...)..., 76 ) 77 78 DescribeTable( 79 "format parsed address", 80 append( 81 []any{ 82 func(_ srl.SRL, str string) { 83 Expect(srl.Parse(str).String()).To(BeIdenticalTo(str)) 84 }, 85 }, entries...)..., 86 ) 87 }) 88 89 // nolint 90 var _ = Describe("Address append", func() { 91 entries := []any{ 92 Entry("both empty", 93 srl.New("", "", ""), 94 srl.New("", "", ""), 95 srl.New("", "", ""), 96 ), 97 Entry("q with storage, empty src", 98 srl.New("ws", "", ""), 99 srl.New("", "", ""), 100 srl.New("ws", "", ""), 101 ), 102 Entry("empty q, src with storage", 103 srl.New("", "", ""), 104 srl.New("ws", "", ""), 105 srl.New("ws", "", ""), 106 ), 107 Entry("q & src with storage", 108 srl.New("ws", "", ""), 109 srl.New("rm", "", ""), 110 srl.New("rm", "", ""), 111 ), 112 113 Entry("full q, empty src", 114 srl.New("ws", "ir", "sapaad"), 115 srl.New("", "", ""), 116 srl.New("ws", "ir", "sapaad"), 117 ), 118 Entry("empty q, full src", 119 srl.New("", "", ""), 120 srl.New("ws", "ir", "sapaad"), 121 srl.New("ws", "ir", "sapaad"), 122 ), 123 124 Entry("q with path & id, src with storage", 125 srl.New("", "ir", "sapaad"), 126 srl.New("ws", "", ""), 127 srl.New("ws", "ir", "sapaad"), 128 ), 129 Entry("q with storage, src with path & id", 130 srl.New("ws", "", ""), 131 srl.New("", "ir", "sapaad"), 132 srl.New("ws", "ir", "sapaad"), 133 ), 134 135 Entry("full q & src", 136 srl.New("ws", "ir", "sapaad"), 137 srl.New("rm", "us", "foodics"), 138 srl.New("rm", "ir:us", "foodics"), 139 ), 140 } 141 142 DescribeTable( 143 "append two addresses", 144 append( 145 []any{ 146 func(addr1, addr2, expected srl.SRL) { 147 Expect(addr1.Append(addr2)).To(BeIdenticalTo(expected)) 148 }, 149 }, entries...)..., 150 ) 151 }) 152 153 // nolint 154 var _ = Describe("Address replace", func() { 155 entries := []any{ 156 Entry("both empty", 157 srl.New("", "", ""), 158 srl.New("", "", ""), 159 srl.New("", "", ""), 160 ), 161 Entry("q with storage, empty src", 162 srl.New("ws", "", ""), 163 srl.New("", "", ""), 164 srl.New("", "", ""), 165 ), 166 Entry("empty q, src with storage", 167 srl.New("", "", ""), 168 srl.New("ws", "", ""), 169 srl.New("ws", "", ""), 170 ), 171 Entry("q & src with storage", 172 srl.New("ws", "", ""), 173 srl.New("rm", "", ""), 174 srl.New("rm", "", ""), 175 ), 176 177 Entry("full q, empty src", 178 srl.New("ws", "ir", "sapaad"), 179 srl.New("", "", ""), 180 srl.New("", "", ""), 181 ), 182 Entry("empty q, full src", 183 srl.New("", "", ""), 184 srl.New("ws", "ir", "sapaad"), 185 srl.New("ws", "ir", "sapaad"), 186 ), 187 188 Entry("q with path & id, src with storage", 189 srl.New("", "ir", "sapaad"), 190 srl.New("ws", "", ""), 191 srl.New("ws", "", ""), 192 ), 193 Entry("q with storage, src with path & id", 194 srl.New("ws", "", ""), 195 srl.New("", "ir", "sapaad"), 196 srl.New("", "ir", "sapaad"), 197 ), 198 199 Entry("full q & src", 200 srl.New("ws", "ir", "sapaad"), 201 srl.New("rm", "us", "foodics"), 202 srl.New("rm", "us", "foodics"), 203 ), 204 } 205 206 DescribeTable( 207 "replace two addresses", 208 append( 209 []any{ 210 func(addr1, addr2, expected srl.SRL) { 211 Expect(addr1.Replace(addr2)).To(BeIdenticalTo(expected)) 212 }, 213 }, entries...)..., 214 ) 215 }) 216 217 // nolint 218 var _ = Describe("Address merge", func() { 219 entries := []any{ 220 Entry("both empty", 221 srl.New("", "", ""), 222 srl.New("", "", ""), 223 srl.New("", "", ""), 224 ), 225 Entry("q with storage, empty src", 226 srl.New("ws", "", ""), 227 srl.New("", "", ""), 228 srl.New("ws", "", ""), 229 ), 230 Entry("empty q, src with storage", 231 srl.New("", "", ""), 232 srl.New("ws", "", ""), 233 srl.New("ws", "", ""), 234 ), 235 Entry("q & src with storage", 236 srl.New("ws", "", ""), 237 srl.New("rm", "", ""), 238 srl.New("rm", "", ""), 239 ), 240 241 Entry("full q, empty src", 242 srl.New("ws", "ir", "sapaad"), 243 srl.New("", "", ""), 244 srl.New("ws", "ir", "sapaad"), 245 ), 246 Entry("empty q, full src", 247 srl.New("", "", ""), 248 srl.New("ws", "ir", "sapaad"), 249 srl.New("ws", "ir", "sapaad"), 250 ), 251 252 Entry("q with path & id, src with storage", 253 srl.New("", "ir", "sapaad"), 254 srl.New("ws", "", ""), 255 srl.New("ws", "ir", "sapaad"), 256 ), 257 Entry("q with storage, src with path & id", 258 srl.New("ws", "", ""), 259 srl.New("", "ir", "sapaad"), 260 srl.New("ws", "ir", "sapaad"), 261 ), 262 263 Entry("full q & src", 264 srl.New("ws", "ir", "sapaad"), 265 srl.New("rm", "us", "foodics"), 266 srl.New("rm", "us", "foodics"), 267 ), 268 } 269 270 DescribeTable( 271 "merge two addresses", 272 append( 273 []any{ 274 func(addr1, addr2, expected srl.SRL) { 275 Expect(addr1.Merge(addr2)).To(BeIdenticalTo(expected)) 276 }, 277 }, entries...)..., 278 ) 279 })