| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Lorenz Brun | f042e6f | 2020-06-24 16:46:09 +0200 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Lorenz Brun | f042e6f | 2020-06-24 16:46:09 +0200 | [diff] [blame] | 3 | |
| 4 | package jsonpatch |
| 5 | |
| 6 | import ( |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestEncodeJSONRefToken(t *testing.T) { |
| 11 | tests := []struct { |
| 12 | name string |
| 13 | token string |
| 14 | want string |
| 15 | }{ |
| 16 | {"Passes through normal characters", "asdf123", "asdf123"}, |
| 17 | {"Encodes simple slashes", "a/b", "a~1b"}, |
| 18 | {"Encodes tildes", "m~n", "m~0n"}, |
| 19 | {"Encodes bot tildes and slashes", "a/m~n", "a~1m~0n"}, |
| 20 | } |
| 21 | for _, tt := range tests { |
| 22 | t.Run(tt.name, func(t *testing.T) { |
| 23 | if got := EncodeJSONRefToken(tt.token); got != tt.want { |
| 24 | t.Errorf("EncodeJSONRefToken() = %v, want %v", got, tt.want) |
| 25 | } |
| 26 | }) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestPointerFromParts(t *testing.T) { |
| 31 | type args struct { |
| 32 | pathParts []string |
| 33 | } |
| 34 | tests := []struct { |
| 35 | name string |
| 36 | args args |
| 37 | want string |
| 38 | }{ |
| 39 | {"Empty path", args{[]string{}}, ""}, |
| 40 | {"Single level path", args{[]string{"foo"}}, "/foo"}, |
| 41 | {"Multi-level path", args{[]string{"foo", "0"}}, "/foo/0"}, |
| 42 | {"Path starting with empty key", args{[]string{""}}, "/"}, |
| 43 | {"Path with part containing /", args{[]string{"a/b"}}, "/a~1b"}, |
| 44 | {"Path with part containing spaces", args{[]string{" "}}, "/ "}, |
| 45 | } |
| 46 | for _, tt := range tests { |
| 47 | t.Run(tt.name, func(t *testing.T) { |
| 48 | if got := PointerFromParts(tt.args.pathParts); got != tt.want { |
| 49 | t.Errorf("PointerFromParts() = %v, want %v", got, tt.want) |
| 50 | } |
| 51 | }) |
| 52 | } |
| 53 | } |