Skip to content

Commit

Permalink
be_strings.Titled() upgrade from strings.Title() to cases. Support la…
Browse files Browse the repository at this point in the history
…nguages
  • Loading branch information
expectto committed Jan 21, 2024
1 parent 7a2b33c commit d70723b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions be_strings/matchers_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/expectto/be/types"
"github.com/onsi/gomega"
"github.com/onsi/gomega/gcustom"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"net/mail"
"regexp"
"strconv"
Expand Down Expand Up @@ -138,16 +140,20 @@ func Float() types.BeMatcher {

// Titled succeeds if actual is a string with the first letter of each word capitalized.
// Actual must be a string-like value (can be adjusted via SetStringFormat method).
func Titled() types.BeMatcher {
func Titled(languageArg ...language.Tag) types.BeMatcher {
lang := language.English
if len(languageArg) > 0 {
lang = languageArg[0]
}

return Psi(gcustom.MakeMatcher(func(actual interface{}) (bool, error) {
if err := expectAvailableStringFormat(actual); err != nil {
return false, err
}

str := cast.AsString(actual)

// todo: switch to cases
return strings.Title(str) == str, nil
return cases.Title(lang).String(str) == str, nil
}))
}

Expand Down

0 comments on commit d70723b

Please sign in to comment.