-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry_test.go
More file actions
74 lines (65 loc) · 1.88 KB
/
Copy pathregistry_test.go
File metadata and controls
74 lines (65 loc) · 1.88 KB
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
package detector
import (
"testing"
"github.com/randomcodespace/codeiq/internal/model"
)
type fakeDetector struct {
name string
lang string
}
func (f fakeDetector) Name() string { return f.name }
func (f fakeDetector) SupportedLanguages() []string { return []string{f.lang} }
func (f fakeDetector) DefaultConfidence() model.Confidence { return model.ConfidenceLexical }
func (f fakeDetector) Detect(*Context) *Result { return EmptyResult() }
func TestRegistryRegisterAndFor(t *testing.T) {
r := NewRegistry()
a := fakeDetector{"a", "java"}
b := fakeDetector{"b", "python"}
c := fakeDetector{"c", "java"}
r.Register(a)
r.Register(b)
r.Register(c)
java := r.For("java")
if len(java) != 2 {
t.Fatalf("For(\"java\") len = %d, want 2", len(java))
}
py := r.For("python")
if len(py) != 1 || py[0].Name() != "b" {
t.Fatalf("For(\"python\") = %+v", py)
}
if r.For("rust") != nil {
t.Fatal("For(\"rust\") should be nil")
}
}
func TestRegistryDeterministicOrder(t *testing.T) {
r := NewRegistry()
r.Register(fakeDetector{"zeta", "java"})
r.Register(fakeDetector{"alpha", "java"})
r.Register(fakeDetector{"middle", "java"})
got := r.For("java")
want := []string{"alpha", "middle", "zeta"}
for i, d := range got {
if d.Name() != want[i] {
t.Errorf("order[%d] = %q, want %q", i, d.Name(), want[i])
}
}
}
func TestRegistryDuplicateNameRejected(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("expected panic on duplicate registration")
}
}()
r := NewRegistry()
r.Register(fakeDetector{"dup", "java"})
r.Register(fakeDetector{"dup", "python"})
}
func TestRegistryAll(t *testing.T) {
r := NewRegistry()
r.Register(fakeDetector{"d2", "java"})
r.Register(fakeDetector{"d1", "java"})
all := r.All()
if len(all) != 2 || all[0].Name() != "d1" || all[1].Name() != "d2" {
t.Fatalf("All() order = %+v", all)
}
}