Issues (2)

checkstyle_test.go (1 issue)

Severity
1
package checkstyle
2
3
import (
4
	"encoding/xml"
5
	"reflect"
6
	"testing"
7
)
8
9
func TestCheckStyle(t *testing.T) {
0 ignored issues
show
cyclomatic complexity 12 of function TestCheckStyle() is high (> 10)
Loading history...
10
	xmlString := `<?xml version="1.0" encoding="UTF-8"?>
11
	<checkstyle version="1.0.0">
12
			<file name="/path/to/code/myfile.go">
13
					<error line="2"  message="msg1" source="Ruleset.RuleName"/>
14
					<error line="20"  message="msg2" source="Generic.Constant"/>
15
					<error line="47"  message="msg3" source="ScopeIndent"/>
16
					<error line="47" message="msg4" source="Format.MultipleAlignment"/>
17
					<error line="51" message="msg5" source="Comment.FunctionComment"/>
18
			</file>
19
	</checkstyle>`
20
21
	checkStyleElement := New()
22
	err := xml.Unmarshal([]byte(xmlString), &checkStyleElement)
23
	if err != nil {
24
		t.Error(err)
25
	}
26
27
	// <checkstyle>
28
	if checkStyleElement.Version != "1.0.0" {
29
		t.Error("Bad checkstyle version")
30
	}
31
	if len(checkStyleElement.File) != 1 {
32
		t.Error("Wrong number of child <file> elements")
33
	}
34
35
	// <file>
36
	fileElement := checkStyleElement.File[0]
37
	if fileElement.Name != "/path/to/code/myfile.go" {
38
		t.Error("Bad file name")
39
	}
40
	if len(fileElement.Error) != 5 {
41
		t.Error("Wrong number of child <error> elements")
42
	}
43
44
	// <error>
45
	errorElement := fileElement.Error[0]
46
	if errorElement.Line != 2 {
47
		t.Error("Bad line number")
48
	}
49
	if errorElement.Message != "msg1" {
50
		t.Error("Bad error message")
51
	}
52
	if errorElement.Source != "Ruleset.RuleName" {
53
		t.Error("Bad error source")
54
	}
55
	if errorElement.Severity != SeverityNone {
56
		t.Error("Bad error Severity")
57
	}
58
59
	// Test round trip
60
	roundtripXML := checkStyleElement.String()
61
	roundtrip := New()
62
	err = xml.Unmarshal([]byte(roundtripXML), &roundtrip)
63
	if err != nil {
64
		t.Error(err)
65
	}
66
	if !reflect.DeepEqual(roundtrip, checkStyleElement) {
67
		t.Error("Round Trip failed")
68
	}
69
70
}
71
72
func TestBuildCheckStyle(t *testing.T) {
73
	checkStyle := New()
74
75
	checkfile := checkStyle.EnsureFile("path/to/file")
76
77
	checkError := NewError(10, 5, SeverityError, "msg1", "test")
78
	checkfile.AddError(checkError)
79
80
	// Check to make sure they are the same
81
	checkfileDuplicate := checkStyle.EnsureFile("path/to/file")
82
	if checkfile != checkfileDuplicate {
83
		t.Error("checkfile != checkfileDuplicate")
84
	}
85
86
	// Check the output
87
	if checkStyle.String() != `<checkstyle version="1.0.0"><file name="path/to/file"><error line="10" column="5" severity="error" message="msg1" source="test"></error></file></checkstyle>` {
88
		t.Error("Wrong output for String()")
89
	}
90
}
91