github.com/kaptinlin/jsonschema@v0.4.6/utils_test.go (about) 1 package jsonschema 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestReplace(t *testing.T) { 10 tests := []struct { 11 template string 12 params map[string]interface{} 13 expected string 14 }{ 15 { 16 "Additional property {property} does not match the schema", 17 map[string]interface{}{"property": "age"}, 18 "Additional property age does not match the schema", 19 }, 20 { 21 "Value should be at most {maximum}", 22 map[string]interface{}{"maximum": 100}, 23 "Value should be at most 100", 24 }, 25 { 26 "Found duplicates at the following index groups: {duplicates}", 27 map[string]interface{}{"duplicates": []int{1, 2, 3}}, 28 "Found duplicates at the following index groups: [1 2 3]", 29 }, 30 { 31 "Encoding '{encoding}' is not supported", 32 map[string]interface{}{"encoding": "utf-8"}, 33 "Encoding 'utf-8' is not supported", 34 }, 35 { 36 "Required properties {properties} are missing", 37 map[string]interface{}{"properties": []string{"name", "address"}}, 38 "Required properties [name address] are missing", 39 }, 40 { 41 "No placeholders here", 42 map[string]interface{}{"placeholder": "value"}, 43 "No placeholders here", 44 }, 45 { 46 "{value} should be greater than {exclusive_minimum}", 47 map[string]interface{}{"value": 5, "exclusive_minimum": 3}, 48 "5 should be greater than 3", 49 }, 50 { 51 "Unsupported format {format}", 52 map[string]interface{}{"format": "date-time"}, 53 "Unsupported format date-time", 54 }, 55 } 56 57 for _, test := range tests { 58 t.Run(test.template, func(t *testing.T) { 59 result := replace(test.template, test.params) 60 assert.Equal(t, test.expected, result) 61 }) 62 } 63 } 64 func TestResolveRelativeURI(t *testing.T) { 65 tests := []struct { 66 baseURI string 67 relativeURL string 68 expected string 69 }{ 70 {"http://example.com/base/", "relative/path", "http://example.com/base/relative/path"}, 71 {"http://example.com/base/", "/absolute/path", "http://example.com/absolute/path"}, 72 {"http://example.com/base/", "http://other.com/path", "http://other.com/path"}, 73 {"http://example.com/base/", "", "http://example.com/base/"}, 74 {"", "relative/path", "relative/path"}, 75 {"", "http://example.com/path", "http://example.com/path"}, 76 {"invalid-url", "relative/path", "relative/path"}, 77 {"http://example.com/base/", "invalid-url", "http://example.com/base/invalid-url"}, 78 {"http://example.com/base/", "relative", "http://example.com/base/relative"}, 79 {"http://example.com/base/", "anotherRelative", "http://example.com/base/anotherRelative"}, 80 } 81 82 for _, test := range tests { 83 t.Run(test.baseURI+"_"+test.relativeURL, func(t *testing.T) { 84 result := resolveRelativeURI(test.baseURI, test.relativeURL) 85 assert.Equal(t, test.expected, result) 86 }) 87 } 88 } 89 90 func TestGetBaseURI(t *testing.T) { 91 tests := []struct { 92 id string 93 expected string 94 }{ 95 {"", ""}, 96 {"invalid-url", ""}, 97 {"http://example.com", "http://example.com/"}, 98 {"http://example.com/schema.json", "http://example.com/"}, 99 {"http://example.com/dir/schema.json", "http://example.com/dir/"}, 100 {"http://example.com/dir/", "http://example.com/dir/"}, 101 {"https://example.com/dir/schema.json", "https://example.com/dir/"}, 102 {"https://example.com/dir/", "https://example.com/dir/"}, 103 {"https://example.com/dir/anotherdir/schema.json", "https://example.com/dir/anotherdir/"}, 104 } 105 106 for _, test := range tests { 107 t.Run(test.id, func(t *testing.T) { 108 result := getBaseURI(test.id) 109 assert.Equal(t, test.expected, result) 110 }) 111 } 112 } 113 114 func TestSplitRef(t *testing.T) { 115 tests := []struct { 116 ref string 117 expectedBaseURI string 118 expectedAnchor string 119 }{ 120 {"http://example.com/schema.json#definitions", "http://example.com/schema.json", "definitions"}, 121 {"http://example.com/schema.json#", "http://example.com/schema.json", ""}, 122 {"http://example.com/schema.json", "http://example.com/schema.json", ""}, 123 {"#definitions", "", "definitions"}, 124 {"", "", ""}, 125 } 126 127 for _, test := range tests { 128 t.Run(test.ref, func(t *testing.T) { 129 baseURI, anchor := splitRef(test.ref) 130 assert.Equal(t, test.expectedBaseURI, baseURI) 131 assert.Equal(t, test.expectedAnchor, anchor) 132 }) 133 } 134 } 135 136 func TestIsJSONPointer(t *testing.T) { 137 tests := []struct { 138 input string 139 expected bool 140 }{ 141 {"/", true}, 142 {"/property", true}, 143 {"/0/property", true}, 144 {"property", false}, 145 {"0/property", false}, 146 {"", false}, 147 {"#/", false}, 148 {"//property", true}, 149 } 150 151 for _, test := range tests { 152 t.Run(test.input, func(t *testing.T) { 153 result := isJSONPointer(test.input) 154 assert.Equal(t, test.expected, result) 155 }) 156 } 157 }