Skip to content

Commit

Permalink
Fix pretty printing of BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
jhbertra committed Nov 2, 2021
1 parent 46b7e86 commit 7fdeeb0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ let additions =
, "web-uievents"
]
, repo = "https://github.com/input-output-hk/purescript-web-common"
, version = "v1.1.0"
, version = "v1.1.1"
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Text/Pretty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Text.Pretty
import Prelude

import Data.BigInt.Argonaut (BigInt)
import Data.BigInt.Argonaut as BigInt
import Data.Foldable (any, intercalate)
import Data.Generic.Rep (class Generic, Argument(..), Constructor(..), NoArguments, Product(..), Sum(..), from)
import Data.String.Extra (repeat)
Expand Down Expand Up @@ -62,7 +63,7 @@ instance prettyString :: Pretty String where
pretty = text <<< show

instance prettyBigInt :: Pretty BigInt where
pretty = text <<< show
pretty = text <<< BigInt.toString

instance prettyNoArguments :: Pretty NoArguments where
pretty _ = mempty
Expand Down
17 changes: 16 additions & 1 deletion test/Data/BigInt/Argonaut/Spec.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@ import Data.Argonaut.Decode (JsonDecodeError)
import Data.Argonaut.Extra (encodeStringifyJson, parseDecodeJson)
import Data.BigInt as BI
import Data.BigInt.Argonaut (BigInt, fromInt, withJsonPatch)
import Data.BigInt.Argonaut as BigInt
import Data.Generic.Rep (class Generic)
import Data.Newtype (unwrap)
import Data.String (codePointFromChar, dropWhile, null)
import Data.String.Gen (genDigitString)
import Test.QuickCheck (class Arbitrary, (===))
import Test.QuickCheck.Gen (suchThat)
import Test.Spec (Spec, around_, describe, it)
import Test.Spec.Assertions (shouldNotEqual)
import Test.Spec.Assertions (shouldEqual, shouldNotEqual)
import Test.Spec.QuickCheck (quickCheck)
import Text.Pretty (genericPretty, pretty)

newtype DigitString = DigitString String

data TestData = TestData String BigInt

derive instance genericTestData :: Generic TestData _

instance arbitraryDigitString :: Arbitrary DigitString where
arbitrary =
map DigitString
Expand Down Expand Up @@ -52,3 +59,11 @@ bigIntSpec =
(unwrap <<< add (fromInt 10))
<$> decoded === (add (BI.fromInt 10) <<< unwrap)
<$> decoded
it "Renders prettily as just the integer value" do
show (pretty $ BigInt.fromInt 10) `shouldEqual` "10"
it "Renders from withing genericPretty" do
show (genericPretty $ TestData "hello" $ BigInt.fromInt 10)
`shouldEqual`
"TestData \"hello\" 10"
it "Renders with \"fromString\" with show" do
show (BigInt.fromInt 10) `shouldEqual` "fromString \"10\""

0 comments on commit 7fdeeb0

Please sign in to comment.