git.iko.soy/rowtype-yoga/purescript-fixed-precision
on main help sign in
Empty string parses nothing (#16)

* Empty string parses nothing * Only check empty strings once

commit0e966d28f13cec27454ebdcc9fa87a5f253685c1 authorJoe Vargas <joevargas92@gmail.com> dateFri 18 Oct 2019, 09:25 (7 years ago) parentcd68cb9
2 files changed+9−2
src/Data/Fixed.purs+2 test/Main.purs+7−2
src/Data/Fixed.purs+2
@@ -41,6 +41,7 @@ import Data.Int as Int
4141import Data.Maybe (Maybe(..))
4242import Data.Monoid as Monoid
4343import Data.String.CodeUnits as StringCU
44+import Data.String as String
4445import Math as Math
4546
4647-- | A kind for type-level precision information
@@ -340,6 +341,7 @@ fromString str =
340341 { before, after } = StringCU.splitAt wholeDigits str
341342
342343 in do
344+ guard (not (String.null str))
343345 wholePart <-
344346 BigInt.fromString before
345347 fractionPart <-
test/Main.purs+7−2
@@ -2,14 +2,14 @@ module Test.Main where
22
33import Prelude
44
5+import Data.Fixed (class KnownPrecision, Fixed, PProxy, fromNumber, toNumber, fromString, reifyPrecision, reflectPrecision, toString, P100)
56import Data.BigInt as BigInt
6−import Data.Fixed (class KnownPrecision, Fixed, PProxy, fromNumber, toNumber, fromString, reifyPrecision, reflectPrecision, toString)
77import Data.Maybe (Maybe(..), fromJust, isJust)
88import Effect (Effect)
99import Effect.Console (log)
1010import Math as Math
1111import Partial.Unsafe (unsafePartial)
12−import Test.QuickCheck (arbitrary, quickCheck', (<?>))
12+import Test.QuickCheck (arbitrary, quickCheck', (<?>), (===))
1313import Test.QuickCheck.Gen (Gen, chooseInt, suchThat)
1414
1515-- Just here to package up the safe use of `unsafePartial`.
@@ -91,6 +91,11 @@ main = do
9191 log "Parsing/printing"
9292 log ""
9393
94+ log "An empty string parses Nothing"
95+ quickCheck' 1
96+ let x = fromString "" :: Maybe (Fixed P100)
97+ in x === Nothing
98+
9499 log "Roundtrip: fromString <<< toString == Just"
95100 quickCheck' 1000 do
96101 precision <- chooseInt 0 100