blob: f41b030a4846d254322bd50b36d6233524a2d485 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
Lorenz Brunf042e6f2020-06-24 16:46:09 +02002// SPDX-License-Identifier: Apache-2.0
Lorenz Brunf042e6f2020-06-24 16:46:09 +02003
4package jsonpatch
5
6import (
7 "testing"
8)
9
10func 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
30func 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}