github.com/djarvur/go-swagger@v0.18.0/scan/validators_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 scan 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 ) 22 23 func TestParseResponseInfo(t *testing.T) { 24 //Parse just untagged ref 25 ref, arr, isDef, desc, err := parseTags("myModel") 26 assert.NoError(t, err) 27 assert.Equal(t, ref, "myModel") 28 assert.Equal(t, arr, 0) 29 assert.False(t, isDef) 30 assert.Equal(t, desc, "") 31 //Parse untagged ref & desc 32 ref, arr, isDef, desc, err = parseTags("myModel11 and my desc") 33 assert.NoError(t, err) 34 assert.Equal(t, ref, "myModel11") 35 assert.Equal(t, arr, 0) 36 assert.False(t, isDef) 37 assert.Equal(t, desc, "and my desc") 38 //Parse tagged body & desc 39 ref, arr, isDef, desc, err = parseTags("body:myModel55 description:and my desc") 40 assert.NoError(t, err) 41 assert.Equal(t, ref, "myModel55") 42 assert.Equal(t, arr, 0) 43 assert.True(t, isDef) 44 assert.Equal(t, desc, "and my desc") 45 //Parse tagged response & desc 46 ref, arr, isDef, desc, err = parseTags("response:myModel42 and my desc") 47 assert.NoError(t, err) 48 assert.Equal(t, ref, "myModel42") 49 assert.Equal(t, arr, 0) 50 assert.False(t, isDef) 51 assert.Equal(t, desc, "and my desc") 52 //Parse tagged array body & desc 53 ref, arr, isDef, desc, err = parseTags("body:[]myModel55 description:and my desc") 54 assert.NoError(t, err) 55 assert.Equal(t, ref, "myModel55") 56 assert.Equal(t, arr, 1) 57 assert.True(t, isDef) 58 assert.Equal(t, desc, "and my desc") 59 //Parse description with ignored tags after (which also test *just* description) 60 ref, arr, isDef, desc, err = parseTags("description:desc response:myModel28") 61 assert.NoError(t, err) 62 assert.Equal(t, ref, "") 63 assert.Equal(t, arr, 0) 64 assert.False(t, isDef) 65 assert.Equal(t, desc, "desc response:myModel28") 66 //Parse just description 67 ref, arr, isDef, desc, err = parseTags("description:desc") 68 assert.NoError(t, err) 69 assert.Equal(t, ref, "") 70 assert.Equal(t, arr, 0) 71 assert.False(t, isDef) 72 assert.Equal(t, desc, "desc") 73 //Parse unrecognized tags 74 _, _, _, _, err = parseTags("body:good woah:desc response:myModel28") 75 assert.NotNil(t, err) 76 //Parse repeated ref tag 77 _, _, _, _, err = parseTags("body:m1 response:m2") 78 assert.NotNil(t, err) 79 }