aboutsummaryrefslogtreecommitdiff
path: root/cpe/avstring_test.go
blob: 8cbb14b4989b42d7394acecd21ef944fe2390095 (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
package cpe

import (
  "testing"
)

func TestNewAvString(t *testing.T) {
  passTests := []struct {
    name string
    val token
    exp AvString
  } {
    { "any", token { Type: anyToken }, AvString { Type: AnyString } },
    { "na", token { Type: naToken }, AvString { Type: NaString } },
    { "empty", token { Type: valToken }, AvString { ValString, "" } },
    { "foo", token { valToken, "foo" }, AvString { ValString, "foo" } },
  }

  for _, test := range(passTests) {
    t.Run(test.name, func(t *testing.T) {
      got, err := newAvString(test.val)
      if err != nil {
        t.Error(err)
      } else if got.Type != test.exp.Type {
        t.Errorf("token: got %s, exp %s", got.Type, test.exp.Type)
      } else if got.Type == ValString && got.Val != test.exp.Val {
        t.Errorf("value: got \"%s\", exp \"%s\"", got.Val, test.exp.Val)
      }
    })
  }

  failTests := []struct {
    name string
    val token
    exp string
  } {{
    name: "invalid token",
    val:  token { Type: tokenType(127), Val: "foo" },
    exp:  "invalid token type: 0x7f",
  }}

  for _, test := range(failTests) {
    t.Run(test.name, func(t *testing.T) {
      got, err := newAvString(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)
      }
    })
  }
}

func TestAvStringString(t *testing.T) {
  tests := []struct {
    name string
    val AvString
    exp string
  } {
    { "any", AvString { AnyString, "" }, "*" },
    { "na", AvString { NaString, "" }, "-" },
    { "foo", AvString { ValString, "foo" }, "foo" },
    { "junk", AvString { AvStringType(255), "foo" }, "" },
  }

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

func TestAvStringTypeString(t *testing.T) {
  tests := []struct {
    val AvStringType
    exp string
  } {
    { AnyString, "any" },
    { NaString, "na" },
    { ValString, "val" },
    { AvStringType(255), "AvStringType(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)
      }
    })
  }
}