-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner_test.go
82 lines (78 loc) · 7.1 KB
/
scanner_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
package main
import (
"os"
"testing"
)
// (text []byte, path string, showlines bool, outputFile string)
var scantests = []struct {
text []byte
path string
showlines bool
outputFile string
match string
exp string
}{
//Golang tests
{[]byte("//@todo some comment"), "test.go", true, "output.txt", "@todo", "test.go\n 1)//@todo some comment\n"}, //can get single line comment
{[]byte("some words //@todo some comment"), "test.go", true, "output.txt", "@todo", "test.go\n 1)//@todo some comment\n"}, //can get comment at end of line
{[]byte("//@todo some comment"), "test.go", true, "output.txt", "todo", ""}, //ignores comments when a different match is used
{[]byte("fmt.Println(\"//@todo some comment\")"), "test.go", true, "output.txt", "@todo", ""}, //ignores comments inside a print statement or string
{[]byte("/* Some multiline \n@todo comment \ncomment2 */"), "test.go", true, "output.txt", "@todo", "test.go\n 1)/* Some multiline \n@todo comment \ncomment2 */\n"}, //can get multi-line comments
//Python tests
{[]byte("#@todo some comment"), "test.py", true, "output.txt", "@todo", "test.py\n 1)#@todo some comment\n"},
{[]byte("some words first #@todo some comment"), "test1.py", true, "output.txt", "@todo", "test1.py\n 1)#@todo some comment\n"},
{[]byte("#@todo some comment"), "test2.py", true, "output.txt", "todo", ""},
{[]byte("print(\"#@todo some comment\")"), "test3.py", true, "output.txt", "@todo", ""},
{[]byte("#some multiline \n#@todo comment\n#with 3 lines"), "test4.py", true, "output.txt", "@todo", "test4.py\n 1)#some multiline \n#@todo comment\n#with 3 lines\n"},
//HTML tests
{[]byte("<!-- @todo some comment -->"), "test.html", true, "output.txt", "@todo", "test.html\n 1)<!-- @todo some comment -->\n"}, //can get single line comment
{[]byte("<!-- stuff @todo some comment -->"), "test2.html", true, "output.txt", "@todo", "test2.html\n 1)<!-- stuff @todo some comment -->\n"}, //can get comment at end of line
{[]byte("<!-- @todo some comment -->"), "test3.html", true, "output.txt", "note", ""}, //ignores comments when a different match is used
{[]byte("fmt.Println(\"<!-- @todo some comment\")"), "test4.html", true, "output.txt", "@todo", ""}, //ignores comments inside a print statement or string
{[]byte("<!-- Some multiline \n@todo comment \ncomment2 \n-->"), "test5.html", true, "output.txt", "@todo", "test5.html\n 1)<!-- Some multiline \n@todo comment \ncomment2 \n-->\n"}, //can get multi-line comments
//LUA tests
{[]byte("--[[@todo some comment--]]"), "test.lua", true, "output.txt", "@todo", "test.lua\n 1)--[[@todo some comment--]]\n"}, //can get single line comment
{[]byte("some words --[[@todo some comment--]]"), "test2.lua", true, "output.txt", "@todo", "test2.lua\n 1)--[[@todo some comment--]]\n"}, //can get comment at end of line
{[]byte("--[[@todo some comment--]]"), "test3.lua", true, "output.txt", "note", ""}, //ignores comments when a different match is used
{[]byte("fmt.Println(\"//@todo some comment\")"), "test4.lua", true, "output.txt", "@todo", ""}, //ignores comments inside a print statement or string
{[]byte("--[[ Some multiline \n@todo comment \ncomment2 --]]"), "test5.lua", true, "output.txt", "@todo", "test5.lua\n 1)--[[ Some multiline \n@todo comment \ncomment2 --]]\n"}, //can get multi-line comments
//Ruby tests
{[]byte("#@todo some comment"), "test.rb", true, "output.txt", "@todo", "test.rb\n 1)#@todo some comment\n"}, //can get single line comment
{[]byte("some words #@todo some comment"), "test2.rb", true, "output.txt", "@todo", "test2.rb\n 1)#@todo some comment\n"}, //can get comment at end of line
{[]byte("#@todo some comment"), "test3.rb", true, "output.txt", "note", ""}, //ignores comments when a different match is used
{[]byte("fmt.Println(\"#@todo some comment\")"), "test4.rb", true, "output.txt", "@todo", ""}, //ignores comments inside a print statement or string
{[]byte("=begin\n Some multiline \n@todo comment \ncomment2 \n=end"), "test5.rb", true, "output.txt", "@todo", "test5.rb\n 1)=begin\n Some multiline \n@todo comment \ncomment2 \n=end\n"}, //can get multi-line comments
//tmpl file tests
{[]byte("{{/* @todo some comment */}}"), "test.tmpl", true, "output.txt", "@todo", "test.tmpl\n 1){{/* @todo some comment */}}\n"}, //can get single line comment
{[]byte("some words {{/* @todo some comment */}}"), "test2.tmpl", true, "output.txt", "@todo", "test2.tmpl\n 1){{/* @todo some comment */}}\n"}, //can get comment at end of line
{[]byte("{{/* @todo some comment */}}"), "test3.tmpl", true, "output.txt", "note", ""}, //ignores comments when a different match is used
{[]byte("fmt.Println(\"{{/*@todo some comment*/}}\")"), "test4.tmpl", true, "output.txt", "@todo", ""}, //ignores comments inside a print statement or string
{[]byte("{{/* Some multiline \n@todo comment \ncomment2 \n*/}}"), "test5.tmpl", true, "output.txt", "@todo", "test5.tmpl\n 1){{/* Some multiline \n@todo comment \ncomment2 \n*/}}\n"}, //can get multi-line comments
}
func TestScan(t *testing.T) {
var Cfg Config
Cfg.Linenum = "false"
for _, e := range scantests {
//fmt.Println("TESTING", e)
lexer = newLexer(e.match) //sets the matching string
err := Scan(e.text, e.path, e.showlines, e.outputFile) //e.text, e.path, e.showlines, e.outputFile
if err != nil {
t.Errorf("Scan failed %s", err.Error())
}
b, err := os.ReadFile(e.outputFile) // just pass the output file name
if err != nil {
t.Errorf("Could not read output file %s", e.outputFile)
}
if e.exp != string(b) {
t.Errorf("Got: %s Expected: %s", string(b), e.exp)
}
err = os.Truncate(e.outputFile, 0)
if err != nil {
t.Errorf("%s", err.Error())
}
}
err := os.Remove("output.txt")
if err != nil {
t.Errorf("Could not remove old test file output.txt %s", err.Error())
}
}