Skip to content

Commit

Permalink
feat: add test for top fie
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Sep 18, 2020
1 parent ed69884 commit 1f30752
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
20 changes: 20 additions & 0 deletions cmd/cloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
"log"
"os"
"path/filepath"
"strconv"
"strings"
)

type CocaClocConfig struct {
ByDirectory bool
TopFile bool
TopSizes int
}

var (
Expand Down Expand Up @@ -71,6 +73,23 @@ func processTopFile(dir string) {

cloc_app.SortLangeByCode(languageSummaries)

if len(languageSummaries) <= 3 {
for _, summary := range languageSummaries {
fmt.Fprintln(output, "Language: " + summary.Name)
table := cmd_util.NewOutput(output)
table.SetHeader([]string{"Length", "File", "Complexity", "WeightedComplexity"})
sizes := len(summary.Files)
if sizes >= clocConfig.TopSizes {
sizes = clocConfig.TopSizes
}

for _, file := range summary.Files[:sizes] {
table.Append([]string{strconv.Itoa(int(file.Code)), file.Language, strconv.Itoa(int(file.Complexity)),strconv.Itoa(int(file.WeightedComplexity)) })
}
table.Render()
}
}

sortContent, _ := json.MarshalIndent(languageSummaries, "", "\t")
cmd_util.WriteToCocaFile("sort_cloc.json", string(sortContent))
}
Expand Down Expand Up @@ -155,6 +174,7 @@ func addClocConfigs() {

flags.BoolVar(&clocConfig.ByDirectory, "by-directory", false, "list directory and out csv")
flags.BoolVar(&clocConfig.TopFile, "top-file", false, "list top change file")
flags.IntVar(&clocConfig.TopSizes, "top-size", 30, "top file sizes")

flags.Int64Var(&processor.AverageWage, "avg-wage", 56286, "average wage value used for basic COCOMO calculation")
flags.BoolVar(&processor.DisableCheckBinary, "binary", false, "disable binary file detection")
Expand Down
27 changes: 3 additions & 24 deletions cmd/cloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ func TestCloc(t *testing.T) {
func TestClocByDirectory(t *testing.T) {
abs := "../_fixtures/cloc/normal"

analysis := []testcase.CmdTestCase{{
Name: "analysis",
Cmd: "analysis -p " + abs,
Golden: "",
}}
RunTestCmd(t, analysis)

tests := []testcase.CmdTestCase{{
Name: "cloc",
Cmd: "cloc " + abs + " --by-directory --include-ext=java,kt",
Expand All @@ -42,13 +35,6 @@ func TestClocByDirectory(t *testing.T) {
func TestShouldReturnNullWhenIgnoreDir(t *testing.T) {
abs := "../_fixtures/cloc/someignore"

analysis := []testcase.CmdTestCase{{
Name: "analysis",
Cmd: "analysis -p " + abs,
Golden: "",
}}
RunTestCmd(t, analysis)

tests := []testcase.CmdTestCase{{
Name: "cloc",
Cmd: "cloc " + abs + " --by-directory",
Expand All @@ -58,19 +44,12 @@ func TestShouldReturnNullWhenIgnoreDir(t *testing.T) {
}

func TestShouldByFileSize(t *testing.T) {
abs := "../_fixtures"

analysis := []testcase.CmdTestCase{{
Name: "analysis",
Cmd: "analysis -p " + abs,
Golden: "",
}}
RunTestCmd(t, analysis)
abs := "../_fixtures/suggest"

tests := []testcase.CmdTestCase{{
Name: "cloc",
Cmd: "cloc " + abs + " --top-file",
Golden: "",
Cmd: "cloc " + abs + " --top-file --top-size=10",
Golden: "testdata/top_file.txt",
}}
RunTestCmd(t, tests)
}
11 changes: 11 additions & 0 deletions cmd/testdata/top_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Language: Java
| LENGTH | FILE | COMPLEXITY | WEIGHTEDCOMPLEXITY |
|--------|------|------------|--------------------|
| 40 | Java | 0 | 0 |
| 19 | Java | 0 | 0 |
| 13 | Java | 0 | 0 |
| 4 | Java | 0 | 0 |
Language: JSON
| LENGTH | FILE | COMPLEXITY | WEIGHTEDCOMPLEXITY |
|--------|------|------------|--------------------|
| 810 | JSON | 0 | 0 |

0 comments on commit 1f30752

Please sign in to comment.