Skip to content

Commit f4f19a2

Browse files
author
blinky3713
committed
add more tests
1 parent f1a2149 commit f4f19a2

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

test/Spec/BigNumber.purs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@ module CoreSpec.BigNumber (bigNumberSpec) where
22

33

44
import Prelude
5-
import Data.Maybe (Maybe(Just))
5+
6+
import Control.Monad.Except (runExcept)
7+
import Data.Argonaut as A
8+
import Data.Either (Either(..), fromRight)
9+
import Data.Foreign (toForeign)
10+
import Data.Foreign.Class (decode, encode)
11+
import Data.Maybe (Maybe(Just), fromJust)
12+
import Network.Ethereum.Core.BigNumber (BigNumber, decimal, embed, hexadecimal, parseBigNumber)
13+
import Partial.Unsafe (unsafePartial)
14+
import Simple.JSON (readImpl)
615
import Test.Spec (Spec, describe, it)
716
import Test.Spec.Assertions (shouldEqual)
8-
import Network.Ethereum.Core.BigNumber (BigNumber, decimal, embed, hexadecimal, parseBigNumber)
17+
918

1019
bigNumberSpec :: forall r . Spec r Unit
1120
bigNumberSpec = describe "BigNumber-spec" do
@@ -47,3 +56,15 @@ bigNumberSpec = describe "BigNumber-spec" do
4756
((parseBigNumber decimal "21") >>= \x -> pure $ x - zero) `shouldEqual` parseBigNumber hexadecimal "0x15"
4857
(Just $ one `mul` one) `shouldEqual` parseBigNumber decimal "1"
4958
(Just $ one * embed (-7)) `shouldEqual` parseBigNumber hexadecimal "-0x7"
59+
60+
it "can handle deserialization" do
61+
let bnString = "f43"
62+
d1 = unsafePartial $ fromRight $ runExcept $ readImpl (toForeign bnString)
63+
d2 = unsafePartial $ fromRight $ runExcept $ decode (toForeign bnString)
64+
d3 = unsafePartial $ fromRight $ A.decodeJson (A.fromString bnString)
65+
d4 = unsafePartial $ fromJust $ parseBigNumber hexadecimal bnString
66+
d4 `shouldEqual` d1
67+
d4 `shouldEqual` d2
68+
d4 `shouldEqual` d3
69+
runExcept (decode (encode d1)) `shouldEqual` Right d4
70+
(A.decodeJson (A.encodeJson d1)) `shouldEqual` Right d4

test/Spec/Hex.purs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ module CoreSpec.Hex (hexSpec) where
22

33
import Prelude
44

5+
import Control.Monad.Except (runExcept)
56
import Data.Argonaut as A
6-
import Data.Either (Either(..))
77
import Data.ByteString as BS
8+
import Data.Either (Either(..), fromRight)
9+
import Data.Foreign (toForeign)
10+
import Data.Foreign.Class (encode, decode)
811
import Data.Maybe (Maybe(Just), fromJust)
912
import Network.Ethereum.Core.HexString (HexString, mkHexString, toByteString, toUtf8, toAscii, fromUtf8, fromAscii)
1013
import Node.Encoding (Encoding(Hex))
1114
import Partial.Unsafe (unsafePartial)
15+
import Simple.JSON (readImpl)
1216
import Test.Spec (Spec, describe, it)
1317
import Test.Spec.Assertions (shouldEqual)
1418

@@ -57,3 +61,15 @@ hexSpec = describe "hex-spec" do
5761
hxJson = A.fromString "0x6d79537472696e67"
5862
(A.encodeJson <$> (A.decodeJson hxJson :: Either String HexString)) `shouldEqual` Right hxJson
5963
A.decodeJson (A.encodeJson hx) `shouldEqual` Right hx
64+
65+
it "can handle deserialization" do
66+
let hxString = "0f43"
67+
d1 = unsafePartial $ fromRight $ runExcept $ readImpl (toForeign hxString)
68+
d2 = unsafePartial $ fromRight $ runExcept $ decode (toForeign hxString)
69+
d3 = unsafePartial $ fromRight $ A.decodeJson (A.fromString hxString)
70+
d4 = unsafePartial $ fromJust $ mkHexString hxString
71+
d4 `shouldEqual` d1
72+
d4 `shouldEqual` d2
73+
d4 `shouldEqual` d3
74+
runExcept (decode (encode d1)) `shouldEqual` Right d4
75+
(A.decodeJson (A.encodeJson d1)) `shouldEqual` Right d4

0 commit comments

Comments
 (0)