aboutsummaryrefslogtreecommitdiff
path: root/cpe/token_test.go
blob: 595df2c539d43acd2bd00e7be2bf40b292449cba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package cpe

import (
  "reflect"
  "testing"
)

func TestTokenTypeString(t *testing.T) {
  tests := []struct {
    val tokenType
    exp string
  } {
    { anyToken, "any" },
    { naToken, "na" },
    { valToken, "val" },
    { tokenType(255), "tokenType(255)" },
  }

  for _, test := range(tests) {
    t.Run(test.exp, func(t *testing.T) {
      got := test.val.String()
      if got != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
      }
    })
  }
}

func TestNewToken(t *testing.T) {
  passTests := []struct {
    name string
    val string
    exp token
  } {
    { "any", "*", token { Type: anyToken } },
    { "na", "-", token { Type: naToken } },
    { "empty", "", token { Type: valToken } },
    { "foo", "foo", token { Type: valToken, Val: "foo" } },
  }

  for _, test := range(passTests) {
    t.Run(test.name, func(t *testing.T) {
      got := newToken([]byte(test.val))
      if got.Type != test.exp.Type {
        t.Errorf("token: got %s, exp %s", got.Type, test.exp.Type)
      } else if got.Type == valToken && got.Val != test.exp.Val {
        t.Errorf("value: got \"%s\", exp \"%s\"", got.Val, test.exp.Val)
      }
    })
  }
}

func TestTokenize(t *testing.T) {
  passTests := []struct {
    val string
    exp []token
  } {{
    val: "foo",
    exp: []token { token { Type: valToken, Val: "foo" } },
  }, {
    val: "foo:bar",
    exp: []token {
      token { Type: valToken, Val: "foo" },
      token { Type: valToken, Val: "bar" },
    },
  }, {
    val: "*",
    exp: []token { token { Type: anyToken } },
  }, {
    val: "-",
    exp: []token { token { Type: naToken } },
  }, {
    val: "*:bar",
    exp: []token {
      token { Type: anyToken },
      token { Type: valToken, Val: "bar" },
    },
  }, {
    val: "foo:*",
    exp: []token {
      token { Type: valToken, Val: "foo" },
      token { Type: anyToken },
    },
  }, {
    val: "-:bar",
    exp: []token {
      token { Type: naToken },
      token { Type: valToken, Val: "bar" },
    },
  }, {
    val: "foo:-",
    exp: []token {
      token { Type: valToken, Val: "foo" },
      token { Type: naToken },
    },
  }, {
    val: "foo\\*:-",
    exp: []token {
      token { Type: valToken, Val: "foo*" },
      token { Type: naToken },
    },
  }}

  for _, test := range(passTests) {
    t.Run(test.val, func(t *testing.T) {
      // tokenize, check for error
      got, err := tokenize([]byte(test.val))
      if err != nil {
        t.Error(err)
        return
      }

      if !reflect.DeepEqual(got, test.exp) {
        t.Errorf("token: got %v, exp %v", got, test.exp)
        return
      }
    })
  }

  failTests := []struct {
    id  string
    val string
    exp string
  } {{
    id:  "invalid escape",
    val: "foo\\.",
    exp: "invalid escape byte: 0x2e",
  }, {
    id:  "invalid byte",
    val: "\n",
    exp: "invalid byte: 0x0a",
  }, {
    id:  "unterminated escape",
    val: "\\",
    exp: "unterminated escape at end of buffer",
  }}

  for _, test := range(failTests) {
    t.Run(test.id, func(t *testing.T) {
      // tokenize, check for error
      got, err := tokenize([]byte(test.val))
      if err == nil {
        t.Errorf("got %v, exp error", got)
      } else if err.Error() != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", err.Error(), test.exp)
      }
    })
  }
}