Debian Codegraph @

syntax/tags_test.go

raw symbols SCIP
1 package syntax
2
3 import (
4 "reflect"
5 "runtime"
6 "testing"
7 )
8
9 func TestTagParsing(t *testing.T) {
10 cases := []struct {
11 encoded string
12 opts fieldOptions
13 }{
14 {
15 encoded: "head=2,min=3,max=60000",
16 opts: fieldOptions{
17 headerSize: 2,
18 minSize: 3,
19 maxSize: 60000,
20 },
21 },
22 {
23 encoded: "head=varint,min=3,max=60000",
24 opts: fieldOptions{
25 varintHeader: true,
26 minSize: 3,
27 maxSize: 60000,
28 },
29 },
30 {
31 encoded: "head=none,min=3,max=60000",
32 opts: fieldOptions{
33 omitHeader: true,
34 minSize: 3,
35 maxSize: 60000,
36 },
37 },
38 {
39 encoded: "varint",
40 opts: fieldOptions{varint: true},
41 },
42 {
43 encoded: "optional",
44 opts: fieldOptions{optional: true},
45 },
46 {
47 encoded: "omit",
48 opts: fieldOptions{omit: true},
49 },
50 }
51
52 for _, c := range cases {
53 parsed := parseTag(c.encoded)
54 if !reflect.DeepEqual(parsed, c.opts) {
55 t.Fatalf("Incorrect options parsing: [%+v] != [%+v]", parsed, c.opts)
56 }
57 }
58 }
59
60 func TestTagConsistency(t *testing.T) {
61
62 cases := []string{
63 "head=3,head=none",
64 "head=none,head=varint",
65 "head=varint,head=3",
66 "min=4,max=2",
67 "head=3,varint",
68 "varint,optional",
69 "optional,head=3",
70 "omit,varint",
71 }
72
73 tryToParse := func(opts string) (err error) {
74 defer func() {
75 if r := recover(); r != nil {
76 if _, ok := r.(runtime.Error); ok {
77 panic(r)
78 }
79 if s, ok := r.(string); ok {
80 panic(s)
81 }
82 err = r.(error)
83 }
84 }()
85 parseTag(opts)
86 return nil
87 }
88
89 for _, opts := range cases {
90 err := tryToParse(opts)
91 if err == nil {
92 t.Fatalf("Incorrectly allowed inconsistent options: [%s]", opts)
93 }
94 }
95 }
96
97 func TestTagValidity(t *testing.T) {
98 sliceTags := parseTag("head=2")
99 uintTags := parseTag("varint")
100 ptrTags := parseTag("optional")
101
102 sliceType := reflect.TypeOf([]byte{})
103 uintType := reflect.TypeOf(uint8(0))
104 ptrType := reflect.TypeOf(new(uint8))
105
106 if !sliceTags.ValidForType(sliceType) {
107 t.Fatalf("Rejected valid tags for slice")
108 }
109
110 if !uintTags.ValidForType(uintType) {
111 t.Fatalf("Rejected valid tags for uint")
112 }
113
114 if !ptrTags.ValidForType(ptrType) {
115 t.Fatalf("Rejected valid tags for ptr")
116 }
117
118 if uintTags.ValidForType(sliceType) {
119 t.Fatalf("Accepted invalid tags for slice: %v", uintTags)
120 }
121
122 if ptrTags.ValidForType(uintType) {
123 t.Fatalf("Accepted invalid tags for uint: %v", ptrTags)
124 }
125
126 if sliceTags.ValidForType(ptrType) {
127 t.Fatalf("Accepted invalid tags for ptr: %v", sliceTags)
128 }
129 }