aboutsummaryrefslogtreecommitdiff
path: root/cpe/part_test.go
blob: 269d16e5b737274f6cdf13acd5412fb7566b01eb (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
package cpe

import (
  "testing"
)

func TestNewPart(t *testing.T) {
  passTests := []struct {
    name string
    val token
    exp Part
  } {{
    name: "any",
    val:  token { Type: anyToken },
    exp:  AnyPart,
  }, {
    name: "na",
    val:  token { Type: naToken },
    exp:  NAPart,
  }, {
    name: "a",
    val:  token { Type: valToken, Val: "a" },
    exp:  ApplicationPart,
  }, {
    name: "h",
    val:  token { Type: valToken, Val: "h" },
    exp:  HardwarePart,
  }, {
    name: "o",
    val:  token { Type: valToken, Val: "o" },
    exp:  OperatingSystemPart,
  }}

  for _, test := range(passTests) {
    t.Run(test.name, func(t *testing.T) {
      if got, err := newPart(test.val); err != nil {
        t.Error(err)
      } else if got != test.exp {
        t.Errorf("got %s, exp %s", got, test.exp)
      }
    })
  }

  failTests := []struct {
    name string
    val token
    exp string
  } {{
    name: "invalid token",
    val:  token { Type: valToken, Val: "foo" },
    exp:  "unknown part: \"foo\"",
  }, {
    name: "empty token",
    val:  token { Type: valToken },
    exp:  "unknown part: \"\"",
  }, {
    name: "unknown token type",
    val:  token { Type: tokenType(255) },
    exp:  "unknown token type: 0xff",
  }}

  for _, test := range(failTests) {
    t.Run(test.name, func(t *testing.T) {
      // tokenize, check for error
      got, err := newPart(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 TestPartString(t *testing.T) {
  tests := []struct {
    val Part
    exp string
  } {{
    val:  AnyPart,
    exp:  "*",
  }, {
    val:  NAPart,
    exp:  "-",
  }, {
    val:  ApplicationPart,
    exp:  "a",
  }, {
    val:  HardwarePart,
    exp:  "h",
  }, {
    val:  OperatingSystemPart,
    exp:  "o",
  }, {
    val:  Part(255),
    exp:  "Part(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)
      }
    })
  }
}