Skip to content

Commit

Permalink
Fix edge case in isCamelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
ckaznocha committed Apr 10, 2021
1 parent 0cf5db9 commit 95b0115
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 45 deletions.
15 changes: 13 additions & 2 deletions linter/formatChecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ import (
)

func isCamelCase(s string) bool {
first, _ := utf8.DecodeRuneInString(s)
first, size := utf8.DecodeRuneInString(s)

var numberCount int

for _, c := range s[size:] {
if unicode.IsNumber(c) {
numberCount++
}
}

allNumeric := len(s[size:]) == numberCount

if unicode.IsLower(first) ||
s == strings.ToUpper(s) ||
(!allNumeric && s == strings.ToUpper(s)) ||
strings.Contains(s, "_") {
return false
}
Expand Down
110 changes: 67 additions & 43 deletions linter/formatChecks_test.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,87 @@
package linter

import "testing"
import (
"testing"
)

func TestIsCamelCase(t *testing.T) {
stringsToTest := []struct {
test string
func Test_isCamelCase(t *testing.T) {
type args struct {
s string
}

tests := []struct {
name string
args args
want bool
}{
{"hello_world", false},
{"HELLO_WORLD", false},
{"helloWorld", false},
{"helloworld", false},
{"HELLOWORLD", false},
{"HelloWorld", true},
{"snake case", args{"hello_world"}, false},
{"screaming snake case", args{"HELLO_WORLD"}, false},
{"dromedary case", args{"helloWorld"}, false},
{"lower case", args{"helloworld"}, false},
{"screeming", args{"HELLOWORLD"}, false},
{"Pascal case", args{"HelloWorld"}, true},
{"dromedary case with first a signel leading alpha", args{"h264"}, false},
{"Pascal case with a single leading alpha", args{"H264"}, true},
{"Pascal case with some numbers after a leading alpha", args{"H264Encoded"}, true},
{"screeming with some numbers after a leading alpha", args{"H264ENCODED"}, false},
}

for _, v := range stringsToTest {
if got := isCamelCase(v.test); got != v.want {
t.Errorf("Expected %t, Received %t", v.want, got)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isCamelCase(tt.args.s); got != tt.want {
t.Errorf("isCamelCase() = %v, want %v", got, tt.want)
}
})
}
}

func TestIsLowerUnderscore(t *testing.T) {
stringsToTest := []struct {
test string
func Test_isLowerUnderscore(t *testing.T) {
type args struct {
s string
}

tests := []struct {
name string
args args
want bool
}{
{"hello_world", true},
{"HELLO_WORLD", false},
{"helloWorld", false},
{"helloworld", true},
{"HELLOWORLD", false},
{"HelloWorld", false},
{"snake case", args{"hello_world"}, true},
{"screaming snake case", args{"HELLO_WORLD"}, false},
{"dromedary case", args{"helloWorld"}, false},
{"lower case", args{"helloworld"}, true},
{"screaming", args{"HELLOWORLD"}, false},
{"Pascal case ", args{"HelloWorld"}, false},
}

for _, v := range stringsToTest {
if got := isLowerUnderscore(v.test); got != v.want {
t.Errorf("Expected %t, Received %t", v.want, got)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isLowerUnderscore(tt.args.s); got != tt.want {
t.Errorf("isLowerUnderscore(() = %v, want %v", got, tt.want)
}
})
}
}

func TestIsUpperUnderscore(t *testing.T) {
stringsToTest := []struct {
test string
func Test_isUpperUnderscore(t *testing.T) {
type args struct {
s string
}

tests := []struct {
name string
args args
want bool
}{
{"hello_world", false},
{"HELLO_WORLD", true},
{"helloWorld", false},
{"helloworld", false},
{"HELLOWORLD", true},
{"HelloWorld", false},
{"snake case", args{"hello_world"}, false},
{"screaming snake case", args{"HELLO_WORLD"}, true},
{"dromedary case", args{"helloWorld"}, false},
{"lower case", args{"helloworld"}, false},
{"screaming", args{"HELLOWORLD"}, true},
{"Pascal case ", args{"HelloWorld"}, false},
}

for _, v := range stringsToTest {
if got := isUpperUnderscore(v.test); got != v.want {
t.Errorf("Expected %t, Received %t", v.want, got)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isUpperUnderscore(tt.args.s); got != tt.want {
t.Errorf("sUpperUnderscore(() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 95b0115

Please sign in to comment.