Skip to content

Commit 6d8a011

Browse files
committed
Add basic test cases
1 parent 11cfa75 commit 6d8a011

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

main_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"io/ioutil"
7+
"os"
8+
"strings"
9+
"testing"
10+
)
11+
12+
var (
13+
inDir = "testdata/input/"
14+
outDir = "testdata/output/"
15+
)
16+
17+
func TestProcess(t *testing.T) {
18+
files, err := ioutil.ReadDir(inDir)
19+
if err != nil {
20+
t.Errorf("failed to read the testdata: %s", err)
21+
}
22+
23+
for _, file := range files {
24+
t.Run(file.Name(), func(t *testing.T) {
25+
inpath := inDir + file.Name()
26+
outpath := outDir + file.Name()
27+
28+
f, err := os.Open(inpath)
29+
if err != nil {
30+
t.Errorf("failed to load input: %s", err)
31+
}
32+
33+
in := bufio.NewReader(f)
34+
out := &bytes.Buffer{}
35+
36+
process(in, out)
37+
38+
expectedBytes, err := ioutil.ReadFile(outpath)
39+
if err != nil {
40+
t.Errorf("failed to load output: %s", err)
41+
}
42+
43+
actual := out.String()
44+
expected := string(expectedBytes)
45+
if strings.Compare(expected, actual) != 0 {
46+
t.Errorf("expected:\n\n%s\nbut got:\n\n%s\n", expected, actual)
47+
}
48+
})
49+
}
50+
}

testdata/input/with-rule.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
./file1.yaml:4:5: [warning] comment not indented like content (comments-indentation)
2+
./file1.yaml:5:6: [error] wrong indentation: expected 4 but found 5 (indentation)
3+
./file2.yaml:7:5: [error] syntax error: expected <block end>, but found '<block mapping start>' (syntax)

testdata/input/without-rule.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
./file1.yaml:4:5: [warning] comment not indented like content
2+
./file1.yaml:5:6: [error] wrong indentation: expected 4 but found 5
3+
./file2.yaml:7:5: [error] syntax error: expected <block end>, but found '<block mapping start>'

testdata/output/with-rule.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<checkstyle version="5.0">
3+
<file name="./file1.yaml">
4+
<error line="4" column="5" severity="warning" source="comments-indentation" message="comment not indented like content"></error>
5+
<error line="5" column="6" severity="error" source="indentation" message="wrong indentation: expected 4 but found 5"></error>
6+
</file>
7+
<file name="./file2.yaml">
8+
<error line="7" column="5" severity="error" source="syntax" message="syntax error: expected &lt;block end&gt;, but found &#39;&lt;block mapping start&gt;&#39;"></error>
9+
</file>
10+
</checkstyle>

testdata/output/without-rule.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<checkstyle version="5.0">
3+
<file name="./file1.yaml">
4+
<error line="4" column="5" severity="warning" message="comment not indented like content"></error>
5+
<error line="5" column="6" severity="error" message="wrong indentation: expected 4 but found 5"></error>
6+
</file>
7+
<file name="./file2.yaml">
8+
<error line="7" column="5" severity="error" message="syntax error: expected &lt;block end&gt;, but found &#39;&lt;block mapping start&gt;&#39;"></error>
9+
</file>
10+
</checkstyle>

0 commit comments

Comments
 (0)