-
Notifications
You must be signed in to change notification settings - Fork 3
/
json_test.go
38 lines (34 loc) · 1.01 KB
/
json_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
package ctoc
import (
"encoding/json"
"fmt"
"testing"
)
func TestOutputJSON(t *testing.T) {
total := &Language{}
files := []ClocFile{
{Name: "one.go", Lang: "Go"},
{Name: "two.go", Lang: "Go"},
}
jsonResult := NewJSONFilesResultFromCloc(total, files)
if jsonResult.Files[0].Name != "one.go" {
t.Errorf("invalid result. Name: one.go")
}
if jsonResult.Files[1].Name != "two.go" {
t.Errorf("invalid result. Name: two.go")
}
if jsonResult.Files[1].Lang != "Go" {
t.Errorf("invalid result. lang: Go")
}
// check output json text
buf, err := json.Marshal(jsonResult)
if err != nil {
fmt.Println(err)
t.Errorf("json marshal error")
}
actualJSONText := `{"files":[{"code":0,"comment":0,"blank":0,"name":"one.go","language":"Go","tokens":0},{"code":0,"comment":0,"blank":0,"name":"two.go","language":"Go","tokens":0}],"total":{"files":0,"code":0,"comment":0,"blank":0,"tokens":0}}`
resultJSONText := string(buf)
if actualJSONText != resultJSONText {
t.Errorf("invalid result. '%s'", resultJSONText)
}
}