-
Notifications
You must be signed in to change notification settings - Fork 30
/
siegfried_test.go
125 lines (103 loc) · 3.69 KB
/
siegfried_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
package siegfried
import (
"bytes"
"testing"
"github.com/richardlehane/siegfried/internal/persist"
"github.com/richardlehane/siegfried/internal/siegreader"
"github.com/richardlehane/siegfried/pkg/config"
"github.com/richardlehane/siegfried/pkg/core"
"github.com/richardlehane/siegfried/pkg/pronom"
)
func TestLoad(t *testing.T) {
s := New()
config.SetHome("./cmd/roy/data")
p, err := pronom.New()
if err != nil {
t.Fatal(err)
}
err = s.Add(p)
if err != nil {
t.Fatal(err)
}
}
func TestIdentify(t *testing.T) {
s := New()
s.nm = testEMatcher{}
s.bm = testBMatcher{}
s.cm = nil
s.ids = append(s.ids, testIdentifier{})
c, err := s.Identify(bytes.NewBufferString("test"), "test.doc", "")
if err != nil {
t.Error(err)
}
i := c[0]
if i.String() != "fmt/3" {
t.Error("expecting fmt/3")
}
}
func TestLabel(t *testing.T) {
s := &Siegfried{ids: []core.Identifier{testIdentifier{}}}
res := s.Label(testIdentification{})
if len(res) != 2 ||
res[0][0] != "namespace" ||
res[0][1] != "a" ||
res[1][0] != "id" ||
res[1][1] != "fmt/3" {
t.Errorf("bad label, got %v", res)
}
}
// extension matcher test stub
type testEMatcher struct{}
func (t testEMatcher) Identify(n string, sb *siegreader.Buffer, hints ...core.Hint) (chan core.Result, error) {
ret := make(chan core.Result)
go func() {
ret <- testResult(0)
close(ret)
}()
return ret, nil
}
func (t testEMatcher) String() string { return "" }
// byte matcher test stub
type testBMatcher struct{}
func (t testBMatcher) Identify(nm string, sb *siegreader.Buffer, hints ...core.Hint) (chan core.Result, error) {
ret := make(chan core.Result)
go func() {
ret <- testResult(1)
ret <- testResult(2)
close(ret)
}()
return ret, nil
}
func (t testBMatcher) String() string { return "" }
type testResult int
func (tr testResult) Index() int { return int(tr) }
func (tr testResult) Basis() string { return "" }
// identifier test stub
type testIdentifier struct{}
func (t testIdentifier) Add(m core.Matcher, mt core.MatcherType) (core.Matcher, error) {
return nil, nil
}
func (t testIdentifier) Recorder() core.Recorder { return testRecorder{} }
func (t testIdentifier) Name() string { return "a" }
func (t testIdentifier) Details() string { return "b" }
func (t testIdentifier) Fields() []string { return []string{"namespace", "id"} }
func (t testIdentifier) Save(l *persist.LoadSaver) {}
func (t testIdentifier) String() string { return "" }
func (t testIdentifier) Inspect(s ...string) (string, error) { return "", nil }
func (t testIdentifier) GraphP(i int) string { return "" }
func (t testIdentifier) Recognise(m core.MatcherType, i int) (bool, string) { return false, "" }
// recorder test stub
type testRecorder struct{}
func (t testRecorder) Active(m core.MatcherType) {}
func (t testRecorder) Record(m core.MatcherType, r core.Result) bool { return true }
func (t testRecorder) Satisfied(m core.MatcherType) (bool, core.Hint) { return false, core.Hint{} }
func (t testRecorder) Report() []core.Identification {
return []core.Identification{testIdentification{}}
}
// identification test stub
type testIdentification struct{}
func (t testIdentification) String() string { return "fmt/3" }
func (t testIdentification) Warn() string { return "" }
func (t testIdentification) Known() bool { return true }
func (t testIdentification) Values() []string { return []string{"a", "fmt/3"} }
func (t testIdentification) Archive() config.Archive { return 0 }