-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathlanguage_test.go
More file actions
136 lines (122 loc) · 3.37 KB
/
language_test.go
File metadata and controls
136 lines (122 loc) · 3.37 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
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
// SPDX-License-Identifier: MIT
package main
import (
"sort"
"strings"
"testing"
)
func TestDetectLanguage(t *testing.T) {
initLanguageDatabase()
tests := []struct {
filename string
want string
}{
{"main.go", "Go"},
{"App.java", "Java"},
{"script.py", "Python"},
{"index.js", "JavaScript"},
{"style.css", "CSS"},
{"page.html", "HTML"},
{"Makefile", "Makefile"},
{"unknown.zzzzz", ""},
}
for _, tt := range tests {
t.Run(tt.filename, func(t *testing.T) {
got := detectLanguage(tt.filename, nil)
if got != tt.want {
t.Errorf("detectLanguage(%q) = %q, want %q", tt.filename, got, tt.want)
}
})
}
}
func TestFileCodeStats(t *testing.T) {
initLanguageDatabase()
content := []byte("package main\n\n// main entry point\nfunc main() {\n\tprintln(\"hello\")\n}\n")
lang, lines, code, comment, blank, complexity, contentByteType := fileCodeStats("main.go", content)
if lang != "Go" {
t.Errorf("expected language Go, got %q", lang)
}
if lines == 0 {
t.Error("expected non-zero lines")
}
if code == 0 {
t.Error("expected non-zero code")
}
if comment == 0 {
t.Error("expected non-zero comment")
}
if blank == 0 {
t.Error("expected non-zero blank")
}
_ = complexity // complexity may be zero for simple code
if len(contentByteType) == 0 {
t.Error("expected non-empty contentByteType")
}
if len(contentByteType) != len(content) {
t.Errorf("expected contentByteType length %d, got %d", len(content), len(contentByteType))
}
}
func TestFileCodeStatsUnknown(t *testing.T) {
initLanguageDatabase()
content := []byte("some random text\n")
lang, lines, code, comment, blank, complexity, contentByteType := fileCodeStats("unknown.zzzzz", content)
if lang != "" {
t.Errorf("expected empty language, got %q", lang)
}
if lines != 0 || code != 0 || comment != 0 || blank != 0 || complexity != 0 {
t.Errorf("expected all zero stats, got lines=%d code=%d comment=%d blank=%d complexity=%d",
lines, code, comment, blank, complexity)
}
if contentByteType != nil {
t.Errorf("expected nil contentByteType for unknown file, got length %d", len(contentByteType))
}
}
func TestLanguageExtensions(t *testing.T) {
initLanguageDatabase()
t.Run("single language", func(t *testing.T) {
exts := languageExtensions([]string{"Go"})
if len(exts) == 0 {
t.Fatal("expected at least one extension for Go")
}
found := false
for _, e := range exts {
if e == "go" {
found = true
break
}
}
if !found {
t.Errorf("expected 'go' in extensions, got %v", exts)
}
})
t.Run("multiple languages", func(t *testing.T) {
exts := languageExtensions([]string{"Go", "Java"})
hasGo, hasJava := false, false
for _, e := range exts {
if e == "go" {
hasGo = true
}
if e == "java" {
hasJava = true
}
}
if !hasGo || !hasJava {
t.Errorf("expected both 'go' and 'java' in extensions, got %v", exts)
}
})
t.Run("case insensitive", func(t *testing.T) {
lower := languageExtensions([]string{"go"})
upper := languageExtensions([]string{"Go"})
sort.Strings(lower)
sort.Strings(upper)
if strings.Join(lower, ",") != strings.Join(upper, ",") {
t.Errorf("case sensitivity mismatch: %v vs %v", lower, upper)
}
})
t.Run("unknown language", func(t *testing.T) {
exts := languageExtensions([]string{"NotARealLanguage12345"})
if len(exts) != 0 {
t.Errorf("expected no extensions for unknown language, got %v", exts)
}
})
}