Skip to content

Commit

Permalink
wip: block comment stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Hideo Hattori committed May 29, 2017
1 parent 25e20c7 commit 6367021
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
12 changes: 11 additions & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,19 @@ func analyzeFile(filename string, language *Language) *ClocFile {
if language.multiLine != "" {
if strings.HasPrefix(line, language.multiLine) {
isInComments = true
} else if strings.HasSuffix(line, language.multiLineEnd) {
isInComments = true
} else if containComments(line, language.multiLine, language.multiLineEnd) {
isInComments = true
clocFile.Code++
if (language.multiLine != language.multiLineEnd) &&
(strings.HasSuffix(line, language.multiLine) || strings.HasPrefix(line, language.multiLineEnd)) {
clocFile.Code++
if opts.Debug {
fmt.Printf("[CODE,cd:%d,cm:%d,bk:%d,iscm:%v] %s\n",
clocFile.Code, clocFile.Comments, clocFile.Blanks, isInComments, lineOrg)
}
continue
}
}
}

Expand Down
66 changes: 66 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,69 @@ class A:
t.Errorf("invalid logic. code=%v", clocFile.Code)
}
}

func TestAnalayzeFile4Go(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "tmp.go")
if err != nil {
t.Logf("ioutil.TempFile() error. err=[%v]", err)
return
}
defer os.Remove(tmpfile.Name())

tmpfile.Write([]byte(`package main
func main() {
var n string /*
comment
comment
*/
}
`))

language := NewLanguage("Go", []string{"//"}, "/*", "*/")
clocFile := analyzeFile(tmpfile.Name(), language)
tmpfile.Close()

if clocFile.Blanks != 1 {
t.Errorf("invalid logic. blanks=%v", clocFile.Blanks)
}
if clocFile.Comments != 3 {
t.Errorf("invalid logic. comments=%v", clocFile.Comments)
}
if clocFile.Code != 4 {
t.Errorf("invalid logic. code=%v", clocFile.Code)
}
}

func TestAnalayzeFile4GoWithOnelineBlockComment(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "tmp.go")
if err != nil {
t.Logf("ioutil.TempFile() error. err=[%v]", err)
return
}
defer os.Remove(tmpfile.Name())

tmpfile.Write([]byte(`package main
func main() {
st := "/*"
a := 1
en := "*/"
/* comment */
}
`))

language := NewLanguage("Go", []string{"//"}, "/*", "*/")
clocFile := analyzeFile(tmpfile.Name(), language)
tmpfile.Close()

if clocFile.Blanks != 1 {
t.Errorf("invalid logic. blanks=%v", clocFile.Blanks)
}
if clocFile.Comments != 1 {
t.Errorf("invalid logic. comments=%v", clocFile.Comments)
}
if clocFile.Code != 6 {
t.Errorf("invalid logic. code=%v", clocFile.Code)
}
}

0 comments on commit 6367021

Please sign in to comment.