Skip to content

Commit

Permalink
final fixes & polished test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Frédéric BIDON <fredbi@yahoo.com>
  • Loading branch information
fredbi committed Jan 11, 2024
1 parent 29a1726 commit c3d1f85
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 9 deletions.
13 changes: 13 additions & 0 deletions BENCHMARK.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,16 @@ BenchmarkToXXXName/ToHumanNameLower-16 18599661 1946 ns/op 92 B/op
BenchmarkToXXXName/ToHumanNameTitle-16 17581353 2054 ns/op 105 B/op 6 allocs/op
PASS

Othe other CPU
goos: linux
goarch: amd64
pkg: github.com/go-openapi/swag
cpu: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
BenchmarkToXXXName/ToGoName-4 270312 3882 ns/op 42 B/op 5 allocs/op
BenchmarkToXXXName/ToVarName-4 282775 4006 ns/op 62 B/op 7 allocs/op
BenchmarkToXXXName/ToFileName-4 541243 2044 ns/op 147 B/op 7 allocs/op
BenchmarkToXXXName/ToCommandName-4 540919 2067 ns/op 147 B/op 7 allocs/op
BenchmarkToXXXName/ToHumanNameLower-4 293074 3753 ns/op 92 B/op 6 allocs/op
BenchmarkToXXXName/ToHumanNameTitle-4 278977 3977 ns/op 104 B/op 6 allocs/op
PASS

17 changes: 14 additions & 3 deletions split.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ type (
complete bool
}
initialismMatches []initialismMatch
)

type (
// memory pools of temporary objects.
//
// These are used to recycle temporarily allocated objects
// and relieve the GC from undue pressure.

matchesPool struct {
*sync.Pool
Expand Down Expand Up @@ -96,6 +103,7 @@ var (
}
)

// nameReplaceTable finds a word representation for special characters.
func nameReplaceTable(r rune) (string, bool) {
switch r {
case '@':
Expand All @@ -117,7 +125,9 @@ func nameReplaceTable(r rune) (string, bool) {
}
}

// split calls the splitter; splitter provides more control and post options
// split calls the splitter.
//
// Use newSplitter for more control and options
func split(str string) []string {
s := poolOfSplitters.BorrowSplitter()
lexems := s.split(str)
Expand Down Expand Up @@ -349,9 +359,9 @@ func (s splitter) appendBrokenDownCasualString(segments *[]nameLexem, str []rune
var addNameLexem func(string)
if s.postSplitInitialismCheck {
addNameLexem = func(original string) {
for i, initialism := range s.initialisms {
for i := range s.initialisms {
if isEqualFoldIgnoreSpace(s.initialismsUpperCased[i], original) {
addInitialismNameLexem(original, initialism)
addInitialismNameLexem(original, s.initialisms[i])

return
}
Expand Down Expand Up @@ -443,6 +453,7 @@ func isEqualFoldIgnoreSpace(base []rune, str string) bool {
}

if c := b[i]; c < utf8.RuneSelf {
// single byte rune case (ASCII)
if baseRune >= utf8.RuneSelf {
return false
}
Expand Down
72 changes: 72 additions & 0 deletions split_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package swag

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestSplitter(t *testing.T) {
s := newSplitter(withPostSplitInitialismCheck)

t.Run("should return an empty slice of lexems", func(t *testing.T) {
lexems := s.split("")
poolOfLexems.RedeemLexems(lexems)

require.NotNil(t, lexems)
require.Empty(t, lexems)
})
}

func TestIsEqualFoldIgnoreSpace(t *testing.T) {
t.Run("should find equal", func(t *testing.T) {
require.True(t, isEqualFoldIgnoreSpace([]rune(""), ""))
require.True(t, isEqualFoldIgnoreSpace([]rune(""), " "))

require.True(t, isEqualFoldIgnoreSpace([]rune("A"), " a"))
require.True(t, isEqualFoldIgnoreSpace([]rune("A"), "a "))
require.True(t, isEqualFoldIgnoreSpace([]rune("A"), " a "))

require.True(t, isEqualFoldIgnoreSpace([]rune("A"), "\ta\t"))
require.True(t, isEqualFoldIgnoreSpace([]rune("A"), "a"))
require.True(t, isEqualFoldIgnoreSpace([]rune("A"), "\u00A0a\u00A0"))

require.True(t, isEqualFoldIgnoreSpace([]rune("AB"), " ab"))
require.True(t, isEqualFoldIgnoreSpace([]rune("AB"), "ab "))
require.True(t, isEqualFoldIgnoreSpace([]rune("AB"), " ab "))
require.True(t, isEqualFoldIgnoreSpace([]rune("AB"), " ab "))

require.True(t, isEqualFoldIgnoreSpace([]rune("AB"), " AB"))
require.True(t, isEqualFoldIgnoreSpace([]rune("AB"), "AB "))
require.True(t, isEqualFoldIgnoreSpace([]rune("AB"), " AB "))
require.True(t, isEqualFoldIgnoreSpace([]rune("AB"), " AB "))

require.True(t, isEqualFoldIgnoreSpace([]rune("À"), " à"))
require.True(t, isEqualFoldIgnoreSpace([]rune("À"), "à "))
require.True(t, isEqualFoldIgnoreSpace([]rune("À"), " à "))
require.True(t, isEqualFoldIgnoreSpace([]rune("À"), " à "))
})

t.Run("should find different", func(t *testing.T) {
require.False(t, isEqualFoldIgnoreSpace([]rune("A"), ""))
require.False(t, isEqualFoldIgnoreSpace([]rune("A"), ""))
require.False(t, isEqualFoldIgnoreSpace([]rune("A"), " b "))
require.False(t, isEqualFoldIgnoreSpace([]rune("A"), " b "))

require.False(t, isEqualFoldIgnoreSpace([]rune("AB"), " A B "))
require.False(t, isEqualFoldIgnoreSpace([]rune("AB"), " a b "))
require.False(t, isEqualFoldIgnoreSpace([]rune("AB"), " AB \u00A0\u00A0x"))
require.False(t, isEqualFoldIgnoreSpace([]rune("AB"), " AB \u00A0\u00A0é"))

require.False(t, isEqualFoldIgnoreSpace([]rune("A"), ""))
require.False(t, isEqualFoldIgnoreSpace([]rune("A"), ""))
require.False(t, isEqualFoldIgnoreSpace([]rune("A"), " b "))
require.False(t, isEqualFoldIgnoreSpace([]rune("A"), " b "))

require.False(t, isEqualFoldIgnoreSpace([]rune("A"), " à"))
require.False(t, isEqualFoldIgnoreSpace([]rune("À"), " bà"))
require.False(t, isEqualFoldIgnoreSpace([]rune("À"), "àb "))
require.False(t, isEqualFoldIgnoreSpace([]rune("À"), " a "))
require.False(t, isEqualFoldIgnoreSpace([]rune("À"), "Á"))
})
}
2 changes: 1 addition & 1 deletion string_bytes_post_go120.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build go.1.20
//go:build go1.20

package swag

Expand Down
2 changes: 1 addition & 1 deletion string_bytes_pre_go120.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !go.1.20
//go:build !go1.20

package swag

Expand Down
15 changes: 11 additions & 4 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"
"unicode"

"github.com/pkg/profile"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -132,10 +133,14 @@ func TestToGoName(t *testing.T) {
)
}

for _, sample := range samples {
result := ToGoName(sample.str)
assert.Equal(t, sample.out, result,
"ToGoName(%q) == %q but got %q", sample.str, sample.out, result)
defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop()

for i := 0; i < 1000; i++ {
for _, sample := range samples {
result := ToGoName(sample.str)
assert.Equal(t, sample.out, result,
"ToGoName(%q) == %q but got %q", sample.str, sample.out, result)
}
}
}

Expand Down Expand Up @@ -499,6 +504,8 @@ func TestToGoNameUnicode(t *testing.T) {
{":éabc", "Éabc"},
{"get$ref", "GetDollarRef"},
{"get!ref", "GetBangRef"},
{"get&ref", "GetAndRef"},
{"get|ref", "GetPipeRef"},
// TODO: non unicode char
}

Expand Down

0 comments on commit c3d1f85

Please sign in to comment.