Skip to content

Commit

Permalink
Fixed calculating average
Browse files Browse the repository at this point in the history
  • Loading branch information
liiri committed Sep 26, 2021
1 parent 8066731 commit 790e0bd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ Output example:

```json
{
"number_of_files": 9,
"counters_by_language": {
"go": {
"number_of_files": 9,
"total": {
"lines_of_code": 2375,
"keywords_complexity": 2.039535414531353,
"indentations_complexity": 11.931293051842783,
"indentations_diff_complexity": 1.9044440275914503
"lines_of_code": 2374,
"keywords_complexity": 2.039620976028679,
"indentations_complexity": 11.930908025104817,
"indentations_diff_complexity": 1.9046008903365483
},
"average": {
"lines_of_code": 263.8888888888889,
"keywords_complexity": 0.22661504605903923,
"indentations_complexity": 1.3256992279825315,
"indentations_diff_complexity": 0.21160489195460558
"lines_of_code": 263.77777777777777,
"keywords_complexity": 0.22662455289207545,
"indentations_complexity": 1.3256564472338686,
"indentations_diff_complexity": 0.21162232114850538
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions calculate/calculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Complexity(opts *options.Options) (*CodeSummary, error) {
}

for _, counters := range ctx.CountersByLanguage {
counters.Average = counters.Total.average(ctx.NumberOfFiles)
counters.Average = counters.Total.average(counters.NumberOfFiles)
}

return &ctx.CodeSummary, nil
Expand Down Expand Up @@ -110,7 +110,7 @@ func (ctx *context) visitPath(rootPath string, path string, info fs.FileInfo) er
ctx.CountersByLanguage[language] = summaryCounters
}
summaryCounters.Total.inc(fileCounters)
ctx.NumberOfFiles++
summaryCounters.NumberOfFiles++

return nil
}
Expand Down
11 changes: 8 additions & 3 deletions calculate/calculate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ func getFileCount(basePath string, includes []string, excludes []string) (float6
if err != nil {
return 0, err
}
return summary.NumberOfFiles, err
totalNumberOfFiles := float64(0)
for _, counters := range summary.CountersByLanguage {
totalNumberOfFiles += counters.NumberOfFiles
}
return totalNumberOfFiles, err
}

func mkdir(path string) {
Expand Down Expand Up @@ -154,7 +158,7 @@ func TestEncodings(t *testing.T) {
}
summary, err := Complexity(opts)
r.Nil(err)
r.Equal(float64(3), summary.NumberOfFiles)
r.Equal(float64(3), summary.CountersByLanguage["go"].NumberOfFiles)
r.Equal(float64(5*3), summary.CountersByLanguage["go"].Total.LinesOfCode)
}

Expand Down Expand Up @@ -185,10 +189,11 @@ func TestDogFood(t *testing.T) {
}
summary, err := Complexity(opts)
r.Nil(err)
r.Equal(float64(9), summary.NumberOfFiles)

r.Len(summary.CountersByLanguage, 1)

r.Equal(float64(9), summary.CountersByLanguage["go"].NumberOfFiles)

total := summary.CountersByLanguage["go"].Total
inRange(r, total.Lines, 2000, 4000)
inRange(r, total.LinesOfCode, 2000, 4000)
Expand Down
6 changes: 3 additions & 3 deletions calculate/counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package calculate
import "fmt"

type CodeSummary struct {
NumberOfFiles float64 `json:"number_of_files"`
CountersByLanguage map[Language]*SummaryCounters `json:"counters_by_language"`
}

type SummaryCounters struct {
Total *CodeCounters `json:"total"`
Average *CodeCounters `json:"average"`
NumberOfFiles float64 `json:"number_of_files"`
Total *CodeCounters `json:"total"`
Average *CodeCounters `json:"average"`
}

type CodeCounters struct {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"
)

const VERSION = "1.0.0"
const VERSION = "1.0.1"

func main() {
cli.AppHelpTemplate =
Expand Down

0 comments on commit 790e0bd

Please sign in to comment.