github.com/AngusLu/go-swagger@v0.28.0/scan/validators_test.go (about) 1 // +build !go1.11 2 3 // Copyright 2015 go-swagger maintainers 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package scan 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestParseResponseInfo(t *testing.T) { 26 //Parse just untagged ref 27 ref, arr, isDef, desc, err := parseTags("myModel") 28 assert.NoError(t, err) 29 assert.Equal(t, ref, "myModel") 30 assert.Equal(t, arr, 0) 31 assert.False(t, isDef) 32 assert.Equal(t, desc, "") 33 //Parse untagged ref & desc 34 ref, arr, isDef, desc, err = parseTags("myModel11 and my desc") 35 assert.NoError(t, err) 36 assert.Equal(t, ref, "myModel11") 37 assert.Equal(t, arr, 0) 38 assert.False(t, isDef) 39 assert.Equal(t, desc, "and my desc") 40 //Parse tagged body & desc 41 ref, arr, isDef, desc, err = parseTags("body:myModel55 description:and my desc") 42 assert.NoError(t, err) 43 assert.Equal(t, ref, "myModel55") 44 assert.Equal(t, arr, 0) 45 assert.True(t, isDef) 46 assert.Equal(t, desc, "and my desc") 47 //Parse tagged response & desc 48 ref, arr, isDef, desc, err = parseTags("response:myModel42 and my desc") 49 assert.NoError(t, err) 50 assert.Equal(t, ref, "myModel42") 51 assert.Equal(t, arr, 0) 52 assert.False(t, isDef) 53 assert.Equal(t, desc, "and my desc") 54 //Parse tagged array body & desc 55 ref, arr, isDef, desc, err = parseTags("body:[]myModel55 description:and my desc") 56 assert.NoError(t, err) 57 assert.Equal(t, ref, "myModel55") 58 assert.Equal(t, arr, 1) 59 assert.True(t, isDef) 60 assert.Equal(t, desc, "and my desc") 61 //Parse description with ignored tags after (which also test *just* description) 62 ref, arr, isDef, desc, err = parseTags("description:desc response:myModel28") 63 assert.NoError(t, err) 64 assert.Equal(t, ref, "") 65 assert.Equal(t, arr, 0) 66 assert.False(t, isDef) 67 assert.Equal(t, desc, "desc response:myModel28") 68 //Parse just description 69 ref, arr, isDef, desc, err = parseTags("description:desc") 70 assert.NoError(t, err) 71 assert.Equal(t, ref, "") 72 assert.Equal(t, arr, 0) 73 assert.False(t, isDef) 74 assert.Equal(t, desc, "desc") 75 //Parse unrecognized tags 76 _, _, _, _, err = parseTags("body:good woah:desc response:myModel28") 77 assert.NotNil(t, err) 78 //Parse repeated ref tag 79 _, _, _, _, err = parseTags("body:m1 response:m2") 80 assert.NotNil(t, err) 81 }