-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathattribute_test.go
160 lines (146 loc) · 3.65 KB
/
attribute_test.go
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
150
151
152
153
154
155
156
157
158
159
160
package cpe
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewStringAttr(t *testing.T) {
type testcase struct {
input string
valid bool
}
var cases = []testcase{
{"microsoft", true},
{"microsoft&google", true},
{"??crosoft", true},
{"microso??", true},
{"マイクロソフト", false},
{"microsoft&グーグル", false},
{"**crosoft", false},
{"microso**", false},
{"mic**roso", false},
// {"-microsoft", false}, // FIXME:this case must check to invalid
}
for i, c := range cases {
sa := NewStringAttr(c.input)
assert.Equal(t, c.valid, sa.IsValid(), "%d", i)
}
}
func TestWFNEncoded(t *testing.T) {
type testcase struct {
input string
expect string
}
var cases = []testcase{
{"foo-bar", "\"foo\\-bar\""},
{"Acrobat_Reader", "\"Acrobat_Reader\""},
{"\"oh_my!\"", "\"\\\"oh_my\\!\\\"\""},
{"g++", "\"g\\+\\+\""},
{"g.?", "\"g\\.?\""},
{"sr*", "\"sr*\""},
{"big$money", "\"big\\$money\""},
{"foo:bar", "\"foo\\:bar\""},
{"with_quoted~tilde", "\"with_quoted\\~tilde\""},
{"*SOFT*", "\"*SOFT*\""},
{"8.??", "\"8\\.??\""},
{"*8.??", "\"*8\\.??\""},
}
for i, c := range cases {
sa := NewStringAttr(c.input)
assert.Equal(t, c.expect, sa.wfnEncoded(), "%d", i)
}
}
func TestNewStringAttrFromWcnEncoded(t *testing.T) {
type testcase struct {
input string
expect string
}
var cases = []testcase{
{"\"foo\\-bar\"", "foo-bar"},
{"\"Acrobat_Reader\"", "Acrobat_Reader"},
{"\"\\\"oh_my\\!\\\"\"", "\"oh_my!\""},
{"\"g\\+\\+\"", "g++"},
{"\"g\\.?\"", "g.?"},
{"\"sr*\"", "sr*"},
{"\"big\\$money\"", "big$money"},
{"\"foo\\:bar\"", "foo:bar"},
{"\"with_quoted\\~tilde\"", "with_quoted~tilde"},
{"\"*SOFT*\"", "*SOFT*"},
{"\"8\\.??\"", "8.??"},
{"\"*8\\.??\"", "*8.??"},
}
for i, c := range cases {
sa := newStringAttrFromWfnEncoded(c.input)
assert.Equal(t, c.expect, sa.raw, "%d", i)
}
}
func TestNewPartAttrFromWcnEncoded(t *testing.T) {
type testcase struct {
input string
expect PartAttr
}
var cases = []testcase{
{`"a"`, Application},
{`"o"`, OperationgSystem},
{`"h"`, Hardware},
{`"z"`, PartNotSet},
}
for i, c := range cases {
pa := newPartAttrFromWfnEncoded(c.input)
assert.Equal(t, c.expect, pa, "%d", i)
}
}
func TestMatchWildcard(t *testing.T) {
type testcase struct {
input string
value string
expect bool
}
var cases = []testcase{
{"*123", "11123", true},
{"*123", "11123a", false},
{"123*", "12311", true},
{"123*", "112311", false},
{"??123", "11123", true},
{"??123", "1123", false},
{"??123", "11123a", false},
{"123??", "12311", true},
{"123??", "123111", false},
{"123??", "112311", false},
{"*123?", "111233", true},
{"*123*", "11123111", true},
{"?123?", "11231", true},
{"?123*", "112333", true},
{"??123*", "1112333", true},
{"*123??", "1112335", true},
{"*123??", "11123355", false},
{"??123*", "18112333", false},
}
for i, c := range cases {
assert.Equal(t, c.expect, match_wildcard(c.input, c.value), "%d", i)
}
}
func testComparition(t *testing.T) {
type testcase struct {
input Attribute
value Attribute
expect Relation
}
var cases = []testcase{
{Application, Application, Equal},
{NewStringAttr("Adobe"), Any, Subset},
{Any, NewStringAttr("Reader"), Superset},
{NewStringAttr("9.*"), NewStringAttr("9.3.2"), Superset},
{Any, Na, Superset},
{NewStringAttr("PalmOS"), Na, Disjoint},
}
for i, c := range cases {
assert.Equal(t, c.expect, c.input.Comparison(c.value), "%d", i)
}
}
func BenchmarkStringAttrComparison(b *testing.B) {
sa1 := NewStringAttr("hellohellohellohello")
sa2 := NewStringAttr("worldworldworldworld")
for i := 0; i < b.N; i++ {
sa1.Comparison(sa2)
}
}