github.com/searKing/golang/go@v1.2.117/net/url/proto_test.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package url 6 7 import ( 8 "fmt" 9 "reflect" 10 "testing" 11 ) 12 13 type protoTests struct { 14 in string 15 out *Proto 16 } 17 18 var prototests = []protoTests{ 19 { 20 "RTSP/1.0", 21 &Proto{ 22 "RTSP", 23 1, 24 0, 25 }, 26 }, 27 } 28 29 func TestParseProto(t *testing.T) { 30 for _, tt := range prototests { 31 u, err := ParseProto(tt.in) 32 if err != nil { 33 t.Errorf("Parse(%q) returned error %v", tt.in, err) 34 continue 35 } 36 if !reflect.DeepEqual(u, tt.out) { 37 t.Errorf("Parse(%q):\n\tgot %v\n\twant %v\n", tt.in, pfmt(u), pfmt(tt.out)) 38 } 39 } 40 } 41 42 var stringProtoTests = []struct { 43 proto Proto 44 want string 45 }{ 46 // No leading slash on path should prepend slash on String() call 47 { 48 proto: Proto{ 49 Type: "RTSP", 50 Major: 1, 51 Minor: 0, 52 }, 53 want: "RTSP/1.0", 54 }, 55 } 56 57 func TestProtoString(t *testing.T) { 58 59 for _, tt := range prototests { 60 u, err := ParseProto(tt.in) 61 if err != nil { 62 t.Errorf("Parse(%q) returned error %s", tt.in, err) 63 continue 64 } 65 expected := tt.in 66 s := u.String() 67 if s != expected { 68 t.Errorf("Parse(%q).String() == %q (expected %q)", tt.in, s, expected) 69 } 70 } 71 72 for _, tt := range stringProtoTests { 73 if got := tt.proto.String(); got != tt.want { 74 t.Errorf("%+v.String() = %q; want %q", tt.proto, got, tt.want) 75 } 76 } 77 } 78 79 // more useful string for debugging than fmt's struct printer 80 func pfmt(p *Proto) string { 81 return fmt.Sprintf("type=%q, major=%q, minor=%#v", 82 p.Type, p.Major, p.Minor) 83 }