Skip to content

Commit

Permalink
Cleaning up test, fixing **chives<->**chive
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Jun 9, 2024
1 parent d731e3f commit 5076336
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 45 deletions.
20 changes: 18 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,29 @@ import (
)

func ExampleSingularize() {
fmt.Println(inflector.Singularize("People"))
for _, s := range []string{
"People",
"Archives",
"octopuses",
} {
fmt.Println(inflector.Singularize(s))
}
// Output:
// Person
// Archive
// octopus
}

func ExamplePluralize() {
fmt.Println(inflector.Pluralize("octopus"))
for _, s := range []string{
"Person",
"Archive",
"octopus",
} {
fmt.Println(inflector.Pluralize(s))
}
// Output:
// People
// Archives
// octopuses
}
8 changes: 4 additions & 4 deletions inflector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
//
// Example:
//
// inflector.Singularize("People") // returns "Person"
//
// inflector.Pluralize("octopus) // returns "octopuses"
// inflector.Singularize("People") // returns "Person"
//
// inflector.Pluralize("octopus) // returns "octopuses"
package inflector

import (
Expand Down Expand Up @@ -105,7 +104,7 @@ func init() {
{`(?i)(matr|vert|ind)(ix|ex)$`, `${1}ices`},
{`(?i)(x|ch|ss|sh)$`, `${1}es`},
{`(?i)([^aeiouy]|qu)y$`, `${1}ies`},
{`(?i)(hive)$`, `$1s`},
{`(?i)(hive)s$`, `$1`},
{`(?i)(?:([^f])fe|([lre])f)$`, `${1}${2}ves`},
{`(?i)sis$`, `ses`},
{`(?i)([ti])um$`, `${1}a`},
Expand Down Expand Up @@ -185,6 +184,7 @@ func init() {
{`(?i)([^aeiouy]|qu)ies$`, `${1}y`},
{`(?i)(tive)s$`, `$1`},
{`(?i)([lre])ves$`, `${1}f`},
{`(?i)hives$`, `hive`},
{`(?i)([^fo])ves$`, `${1}fe`},
{`(?i)(hive)s$`, `$1`},
{`(?i)(drive)s$`, `$1`},
Expand Down
90 changes: 51 additions & 39 deletions inflector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,70 @@ import (
)

func TestPluralize(t *testing.T) {
for i, test := range pluralTests() {
if s := Pluralize(test.s); s != test.exp {
t.Errorf("test %d Pluralize(%s) = %s, expected: %s", i, test.s, s, test.exp)
}
// Second retrieval should returns the same result. This is also tests
// the cache
if s := Pluralize(test.s); s != test.exp {
t.Errorf("test %d (2) Pluralize(%s) = %s, expected: %s", i, test.s, s, test.exp)
}
for _, test := range pluralTests() {
t.Run(test.s, func(t *testing.T) {
if s := Pluralize(test.s); s != test.exp {
t.Errorf("Pluralize(%s) = %s, expected: %s", test.s, s, test.exp)
}
// Second retrieval should returns the same result. This is also tests
// the cache
if s := Pluralize(test.s); s != test.exp {
t.Errorf("Pluralize(%s) = %s, expected: %s (2nd)", test.s, s, test.exp)
}
})
}
}

func TestSingularize(t *testing.T) {
for i, test := range singularTests() {
if s := Singularize(test.s); s != test.exp {
t.Errorf("test %d Singularize(%s) = %s, expected: %s", i, test.s, s, test.exp)
}
// Second retrieval should returns the same result. This is also tests
// the cache
if s := Singularize(test.s); s != test.exp {
t.Errorf("test %d (2) Singularize(%s) = %s, expected: %s", i, test.s, s, test.exp)
}
for _, test := range singularTests() {
t.Run(test.s, func(t *testing.T) {
if s := Singularize(test.s); s != test.exp {
t.Errorf("Singularize(%s) = %s, expected: %s", test.s, s, test.exp)
}
// Second retrieval should returns the same result. This is also tests
// the cache
if s := Singularize(test.s); s != test.exp {
t.Errorf("Singularize(%s) = %s, expected: %s (2nd)", test.s, s, test.exp)
}
})
}
}

func TestReverse(t *testing.T) {
for i, test := range pluralTests() {
if !test.match {
continue
}
if s := Singularize(Pluralize(test.s)); s != test.s {
t.Errorf("test %d Singularize(Pluralize(%s)) != %s, got: %s", i, test.s, test.s, s)
}
func TestPluralizeInverse(t *testing.T) {
for _, test := range pluralTests() {
t.Run(test.s, func(t *testing.T) {
if !test.match {
return
}
if s := Singularize(Pluralize(test.s)); s != test.s {
t.Errorf("Singularize(Pluralize(%s)) != %s, got: %s", test.s, test.s, s)
}
})
}
for i, test := range singularTests() {
if !test.match {
continue
}
if s := Pluralize(Singularize(test.s)); s != test.s {
t.Errorf("test %d Pluralize(Singularize(%s)) != %s, got: %s", i, test.s, test.s, s)
}
}

func TestSingularizeInverse(t *testing.T) {
for _, test := range singularTests() {
t.Run(test.s, func(t *testing.T) {
if !test.match {
return
}
if s := Pluralize(Singularize(test.s)); s != test.s {
t.Errorf("Pluralize(Singularize(%s)) != %s, got: %s", test.s, test.s, s)
}
})
}
}

// test is an inflector test.
type test struct {
// inflectorTest is an inflector test.
type inflectorTest struct {
s string
exp string
match bool
}

func pluralTests() []test {
return []test{
func pluralTests() []inflectorTest {
return []inflectorTest{
{"categoria", "categorias", true},
{"house", "houses", true},
{"powerhouse", "powerhouses", true},
Expand Down Expand Up @@ -111,13 +122,14 @@ func pluralTests() []test {
{"objective", "objectives", true},
{"specie", "species", false},
{"species", "species", true},
{"chive", "chives", true},
{"", "", true},
}
}

// singularTests returns the singular tests.
func singularTests() []test {
return []test{
func singularTests() []inflectorTest {
return []inflectorTest{
{"categorias", "categoria", true},
{"menus", "menu", true},
{"news", "news", true},
Expand Down

0 comments on commit 5076336

Please sign in to comment.