k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/strfmt/format_test.go (about) 1 // Copyright 2015 go-swagger maintainers 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package strfmt 16 17 import ( 18 "strings" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 type testFormat string 25 26 func (t testFormat) MarshalText() ([]byte, error) { 27 return []byte(string(t)), nil 28 } 29 30 func (t *testFormat) UnmarshalText(b []byte) error { 31 *t = testFormat(string(b)) 32 return nil 33 } 34 35 func (t testFormat) String() string { 36 return string(t) 37 } 38 39 func isTestFormat(s string) bool { 40 return strings.HasPrefix(s, "tf") 41 } 42 43 type tf2 string 44 45 func (t tf2) MarshalText() ([]byte, error) { 46 return []byte(string(t)), nil 47 } 48 49 func (t *tf2) UnmarshalText(b []byte) error { 50 *t = tf2(string(b)) 51 return nil 52 } 53 54 func istf2(s string) bool { 55 return strings.HasPrefix(s, "af") 56 } 57 58 func (t tf2) String() string { 59 return string(t) 60 } 61 62 type bf string 63 64 func (t bf) MarshalText() ([]byte, error) { 65 return []byte(string(t)), nil 66 } 67 68 func (t *bf) UnmarshalText(b []byte) error { 69 *t = bf(string(b)) 70 return nil 71 } 72 73 func (t bf) String() string { 74 return string(t) 75 } 76 77 func isbf(s string) bool { 78 return strings.HasPrefix(s, "bf") 79 } 80 81 func istf3(s string) bool { 82 return strings.HasPrefix(s, "ff") 83 } 84 85 func init() { 86 tf := testFormat("") 87 Default.Add("test-format", &tf, isTestFormat) 88 } 89 90 func TestFormatRegistry(t *testing.T) { 91 f2 := tf2("") 92 f3 := bf("") 93 registry := NewFormats() 94 95 assert.True(t, registry.ContainsName("test-format")) 96 assert.True(t, registry.ContainsName("testformat")) 97 assert.False(t, registry.ContainsName("ttt")) 98 99 assert.True(t, registry.Validates("testformat", "tfa")) 100 assert.False(t, registry.Validates("testformat", "ffa")) 101 102 assert.True(t, registry.Add("tf2", &f2, istf2)) 103 assert.True(t, registry.ContainsName("tf2")) 104 assert.False(t, registry.ContainsName("tfw")) 105 assert.True(t, registry.Validates("tf2", "afa")) 106 107 assert.False(t, registry.Add("tf2", &f3, isbf)) 108 assert.True(t, registry.ContainsName("tf2")) 109 assert.False(t, registry.ContainsName("tfw")) 110 assert.True(t, registry.Validates("tf2", "bfa")) 111 assert.False(t, registry.Validates("tf2", "afa")) 112 113 assert.False(t, registry.Add("tf2", &f2, istf2)) 114 assert.True(t, registry.Add("tf3", &f2, istf3)) 115 assert.True(t, registry.ContainsName("tf3")) 116 assert.True(t, registry.ContainsName("tf2")) 117 assert.False(t, registry.ContainsName("tfw")) 118 assert.True(t, registry.Validates("tf3", "ffa")) 119 120 assert.True(t, registry.DelByName("tf3")) 121 assert.True(t, registry.Add("tf3", &f2, istf3)) 122 123 assert.True(t, registry.DelByName("tf3")) 124 assert.False(t, registry.DelByName("unknown")) 125 assert.False(t, registry.Validates("unknown", "")) 126 } 127 128 type testStruct struct { 129 D Date `json:"d,omitempty"` 130 DT DateTime `json:"dt,omitempty"` 131 Dur Duration `json:"dur,omitempty"` 132 URI URI `json:"uri,omitempty"` 133 Eml Email `json:"eml,omitempty"` 134 UUID UUID `json:"uuid,omitempty"` 135 UUID3 UUID3 `json:"uuid3,omitempty"` 136 UUID4 UUID4 `json:"uuid4,omitempty"` 137 UUID5 UUID5 `json:"uuid5,omitempty"` 138 Hn Hostname `json:"hn,omitempty"` 139 Ipv4 IPv4 `json:"ipv4,omitempty"` 140 Ipv6 IPv6 `json:"ipv6,omitempty"` 141 Cidr CIDR `json:"cidr,omitempty"` 142 Mac MAC `json:"mac,omitempty"` 143 Isbn ISBN `json:"isbn,omitempty"` 144 Isbn10 ISBN10 `json:"isbn10,omitempty"` 145 Isbn13 ISBN13 `json:"isbn13,omitempty"` 146 Creditcard CreditCard `json:"creditcard,omitempty"` 147 Ssn SSN `json:"ssn,omitempty"` 148 Hexcolor HexColor `json:"hexcolor,omitempty"` 149 Rgbcolor RGBColor `json:"rgbcolor,omitempty"` 150 B64 Base64 `json:"b64,omitempty"` 151 Pw Password `json:"pw,omitempty"` 152 }