Skip to content

Commit

Permalink
fix json result
Browse files Browse the repository at this point in the history
  • Loading branch information
Hideo Hattori committed Feb 13, 2020
1 parent 6acb1b3 commit 344b532
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion file.go
Expand Up @@ -14,7 +14,7 @@ type ClocFile struct {
Comments int32 `xml:"comment,attr" json:"comment"`
Blanks int32 `xml:"blank,attr" json:"blank"`
Name string `xml:"name,attr" json:"name"`
Lang string `xml:"language,attr" json"language"`
Lang string `xml:"language,attr" json:"language"`
}

type ClocFiles []ClocFile
Expand Down
38 changes: 38 additions & 0 deletions json_test.go
@@ -0,0 +1,38 @@
package gocloc

import (
"encoding/json"
"fmt"
"testing"
)

func TestOutputJSON(t *testing.T) {
total := &Language{}
files := []ClocFile{
ClocFile{Name: "one.go", Lang: "Go"},
ClocFile{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"},{"code":0,"comment":0,"blank":0,"name":"two.go","language":"Go"}],"total":{"files":0,"code":0,"comment":0,"blank":0}}`
resultJSONText := string(buf)
if actualJSONText != resultJSONText {
t.Errorf("invalid result. '%s'", resultJSONText)
}
}

0 comments on commit 344b532

Please sign in to comment.