Skip to content

Commit

Permalink
Makes most strings tests pass (#16)
Browse files Browse the repository at this point in the history
* Makes most strings tests pass

* Fixes comment for softBreak, deletes bad test
  • Loading branch information
gampleman authored Sep 6, 2023
1 parent 8989224 commit 9776a8e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 89 deletions.
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

19 changes: 6 additions & 13 deletions src/String/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,14 @@ decapitalize word =

{-| Capitalize the first character of each word in a string.
toTitleCase "this is a phrase" == "This Is A Phrase"
toTitleCase "this is a phrase" --> "This Is A Phrase"
toTitleCase "hello, world" == "Hello, World"
toTitleCase "hello, world" --> "Hello, World"
-}
toTitleCase : String -> String
toTitleCase ws =
let
uppercaseMatch =
Regex.replace (regexFromString "\\w+") (.match >> toSentenceCase)
in
ws
|> Regex.replace
(regexFromString "^([a-z])|\\s+([a-z])")
(.match >> uppercaseMatch)
Regex.replace (regexFromString "^([^\\s])|\\s[^\\s]") (.match >> String.toUpper) ws


{-| Replace text within a portion of a string given a substitution
Expand Down Expand Up @@ -186,7 +179,7 @@ breaker width string acc =
{-| Break a string into a list of strings of a specified maximum length,
without truncating words.
softBreak 6 "The quick brown fox" == [ "The quick", " brown", " fox" ]
softBreak 6 "The quick brown fox" --> [ "The ", "quick ", "brown ", "fox" ]
-}
softBreak : Int -> String -> List String
Expand All @@ -202,7 +195,7 @@ softBreak width string =

softBreakRegexp : Int -> Regex.Regex
softBreakRegexp width =
regexFromString <| ".{1," ++ String.fromInt width ++ "}(\\s+|$)|\\S+?(\\s+|$)"
regexFromString <| ".{0," ++ String.fromInt (width - 1) ++ "}(\\s|$)|\\S+?(\\s|$)"


{-| Trim the whitespace of both sides of the string and compress
Expand All @@ -214,7 +207,7 @@ repeated whitespace internally to a single whitespace char.
clean : String -> String
clean string =
string
|> Regex.replace (regexFromString "\\s\\s+") (always " ")
|> Regex.replace (regexFromString "\\s+") (always " ")
|> String.trim


Expand Down
6 changes: 3 additions & 3 deletions tests/String/CamelizeTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ camelizeTest =
|> String.contains "-"
|> Expect.equal False
|> Expect.onFail "Camelize should remove underscores"
, fuzz Fuzz.string "It is the same lowercased string after removing the dashes and spaces" <|
, fuzz Fuzz.string "It is the same uppercased string after removing the dashes and spaces" <|
\s ->
let
expected =
replace "-" ""
>> replace "_" ""
>> Regex.replace (Regex.fromString "\\s+" |> Maybe.withDefault Regex.never) (\_ -> "")
>> String.toLower
>> String.toUpper
in
camelize s
|> String.toLower
|> String.toUpper
|> Expect.equal (expected s)
, fuzz (validWords '-') "The first letter after each dash is capitalized" <|
\s ->
Expand Down
18 changes: 2 additions & 16 deletions tests/String/HumanizeTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,18 @@ humanizeTest =
|> String.all Char.isLower
|> Expect.equal True
|> Expect.onFail "Not all characters in the string are lowercased"
, fuzz (validWords [ '_', '-' ]) "It removes a trailing `_id` & replaces underscores and dashes with a single whitespace" <|
\s ->
let
expected =
String.toLower
>> Regex.replaceAtMost 1 (regex "_id$") (\_ -> "")
>> String.replace "-" " "
>> String.replace "_" " "
>> Regex.replace (regex "\\s+") (\_ -> " ")
>> String.trim
in
humanize (String.toLower s)
|> String.toLower
|> Expect.equal (expected s)
, fuzz Fuzz.string "It yields the same string after removing underscores, dashes and spaces" <|
\s ->
let
expected =
String.replace "-" ""
>> String.replace "_" ""
>> Regex.replace (regex "\\s+") (\_ -> "")
>> String.toLower
>> String.toUpper
in
humanize s
|> String.replace " " ""
|> String.toLower
|> String.toUpper
|> Expect.equal (expected s)
, fuzz (validWords []) "It adds a space before each group of uppercase letter" <|
\s ->
Expand Down
74 changes: 18 additions & 56 deletions tests/String/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Char.Extra
import Expect
import Fuzz exposing (Fuzzer, string)
import Regex exposing (Regex)
import String exposing (fromChar, replace, toLower, toUpper, uncons)
import String exposing (fromChar, replace, toLower, uncons)
import String.Extra
import Test exposing (Test, describe, fuzz, fuzz2, test)
import Tuple exposing (first, second)
Expand All @@ -20,31 +20,17 @@ toSentenceCaseTest =
describe "toSentenceCase"
[ fuzz Fuzz.string "It converts the first char of the string to uppercase" <|
\string ->
let
result =
string
|> String.Extra.toSentenceCase
|> uncons
|> Maybe.map (first >> fromChar)
|> Maybe.withDefault ""

expected =
string
|> uncons
|> Maybe.map (first >> fromChar >> toUpper)
|> Maybe.withDefault ""
in
Expect.equal expected result
string
|> String.Extra.toSentenceCase
|> String.left 1
|> (\pref -> String.startsWith pref (String.toUpper string))
|> Expect.equal True
, fuzz Fuzz.string "The tail of the string remains untouched" <|
\string ->
let
result =
(String.Extra.toSentenceCase >> tail) string

expected =
tail string
in
Expect.equal expected result
string
|> String.Extra.toSentenceCase
|> String.endsWith (tail string)
|> Expect.equal True
]


Expand Down Expand Up @@ -100,21 +86,6 @@ toTitleCaseTest =
|> List.map String.Extra.toSentenceCase
in
Expect.equal expected result
, fuzz (Fuzz.list Fuzz.string) "It does not change the length of the string" <|
\strings ->
let
result =
strings
|> String.join " "
|> String.Extra.toTitleCase
|> String.length

expected =
strings
|> String.join " "
|> String.length
in
Expect.equal expected result
]


Expand Down Expand Up @@ -162,7 +133,7 @@ softBreakTest =
String.Extra.softBreak width (String.trim string)
|> String.concat
|> Expect.equal (String.trim string)
, fuzz2 Fuzz.string (Fuzz.intRange 1 10) "The list should not have more elements than words" <|
, fuzz2 (Fuzz.map String.Extra.clean Fuzz.string) (Fuzz.intRange 1 10) "The list should not have more elements than words on a clean string" <|
\string width ->
String.Extra.softBreak width string
|> List.length
Expand All @@ -173,22 +144,13 @@ softBreakTest =
cleanTest : Test
cleanTest =
describe "clean"
[ {- Test.skip <|
fuzz Fuzz.string "The String.split result is the same as String.words" <|
\string ->
let
result =
string
|> clean
|> String.split " "
expected =
String.words string
in
Expect.equal expected result
,
-}
fuzz Fuzz.string "It trims the string on the left side" <|
[ fuzz Fuzz.string "The String.split result is the same as String.words" <|
\string ->
string
|> String.Extra.clean
|> String.split " "
|> Expect.equal (String.words string)
, fuzz Fuzz.string "It trims the string on the left side" <|
\string ->
string
|> String.Extra.clean
Expand Down

0 comments on commit 9776a8e

Please sign in to comment.