From 95b0115041ed15cadd61d599edbbe5e6bf66e7d6 Mon Sep 17 00:00:00 2001 From: ckaznocha Date: Fri, 9 Apr 2021 19:47:08 -0700 Subject: [PATCH] Fix edge case in isCamelCase --- linter/formatChecks.go | 15 ++++- linter/formatChecks_test.go | 110 ++++++++++++++++++++++-------------- 2 files changed, 80 insertions(+), 45 deletions(-) diff --git a/linter/formatChecks.go b/linter/formatChecks.go index 5c73635..41edf1f 100644 --- a/linter/formatChecks.go +++ b/linter/formatChecks.go @@ -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 } diff --git a/linter/formatChecks_test.go b/linter/formatChecks_test.go index 2dfe400..4135ca6 100644 --- a/linter/formatChecks_test.go +++ b/linter/formatChecks_test.go @@ -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) + } + }) } }