Skip to content

Commit

Permalink
finish cleaning up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martyall committed Sep 14, 2023
1 parent 944be0d commit 7c5f4c6
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 611 deletions.
90 changes: 0 additions & 90 deletions src/Network/Ethereum/Web3/Solidity/Size.purs

This file was deleted.

22 changes: 12 additions & 10 deletions test/web3/Main.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Test.Main where

import Prelude

import Data.Identity (Identity(..))
import Data.Maybe (Maybe(..))
import Data.Newtype (un)
Expand All @@ -19,23 +20,24 @@ import Web3Spec.Live.RPCSpec as RPCSpec
import Web3Spec.Types.EtherUnitSpec as EtherUnitSpec
import Web3Spec.Types.VectorSpec as VectorSpec

-- import Web3Spec.Types.EtherUnitSpec as EtherUnitSpec

main :: Effect Unit
main =
launchAff_
do
let
cfg = defaultConfig { timeout = Just (Milliseconds $ 120.0 * 1000.0) }
p <- liftEffect $ httpProvider "http://localhost:8545"
void $ join
$ runSpecT cfg [ consoleReporter ] do
hoist do
EncodingDataSpec.spec
-- VectorSpec.spec
EncodingContainersSpec.spec
-- EncodingSimpleSpec.spec
-- EncodingGenericSpec.spec
-- EtherUnitSpec.spec
--RPCSpec.spec p
void $ join $ runSpecT cfg [ consoleReporter ] do
hoist do
EncodingDataSpec.spec
EncodingContainersSpec.spec
EncodingSimpleSpec.spec
EncodingGenericSpec.spec
EtherUnitSpec.spec
VectorSpec.spec
RPCSpec.spec p
where
hoist :: Spec ~> SpecT Aff Unit Aff
hoist = mapSpecTree (pure <<< un Identity) identity
91 changes: 89 additions & 2 deletions test/web3/Web3Spec/Encoding/ContainersSpec.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ module Web3Spec.Encoding.ContainersSpec (spec) where

import Prelude

import Control.Monad.Gen (chooseInt, frequency, suchThat)
import Control.Monad.Gen (chooseInt, frequency, oneOf, suchThat)
import Data.Array (filter, foldMap, (..))
import Data.Array.NonEmpty (NonEmptyArray, fromArray)
import Data.Array.NonEmpty as NEA
import Data.Either (Either(..))
import Data.Enum (toEnumWithDefaults)
import Data.Foldable (for_)
import Data.Generic.Rep (class Generic)
import Data.Int (toNumber)
import Data.Maybe (fromJust)
import Data.NonEmpty (NonEmpty(..))
import Data.Reflectable (reifyType)
import Data.String (CodePoint, fromCodePointArray)
import Data.Tuple (Tuple(..))
import Effect.Class (liftEffect)
import Network.Ethereum.Core.HexString (toByteString)
import Network.Ethereum.Core.HexString (genBytes, toByteString)
import Network.Ethereum.Web3.Solidity (class GenericABIDecode, class GenericABIEncode, Tuple4(..), Tuple5(..), genericABIEncode, genericFromData)
import Network.Ethereum.Web3.Solidity.AbiEncoding (class ABIEncode, class ABIDecode, toDataBuilder, fromData)
import Network.Ethereum.Web3.Solidity.Bytes as BytesN
import Network.Ethereum.Web3.Solidity.EncodingType (class EncodingType)
Expand All @@ -37,6 +39,7 @@ spec =
arrayTypePropertyTests
vecTypePropertyTests
nestedTypePropertyTests
tupleTests

typePropertyTests :: Spec Unit
typePropertyTests =
Expand Down Expand Up @@ -227,6 +230,78 @@ nestedTypePropertyTests = do
quickCheck \(x :: Array (Array BMPString)) ->
encodeDecode x === Right x

tupleTests :: Spec Unit
tupleTests = do
describe "Basic static sized Tuple Tests" $ do

it "Can encode/decode (intN, address, bool, uintN, bytesN)" $ liftEffect do
quickCheckGen $ do
n <- oneOf (pure <$> intSizes)
m <- oneOf (pure <$> intSizes)
k <- oneOf (pure <$> bytesSizes)
reifyType n \pn ->
reifyType m \pm ->
reifyType k \pk -> do
int <- IntN.generator pn
addr <- arbitrary :: Gen Address
bool <- arbitrary :: Gen Boolean
uint <- UIntN.generator pm
bytes <- BytesN.generator pk
let x = Tuple5 int addr bool uint bytes
pure $ genericEncodeDecode x === Right x

it "Can encode/decode (address[k], bool, intN[k], uint)" $ liftEffect do
quickCheckGen $ do
k1 <- chooseInt 1 10
k2 <- chooseInt 1 10
n <- oneOf (pure <$> intSizes)
m <- oneOf (pure <$> intSizes)
reifyType k1 \pk1 ->
reifyType k2 \pk2 ->
reifyType n \pn -> do
reifyType m \pm -> do
addrs <- arrayOf (Vector.generator pk1 (arbitrary @Address))
bool <- arbitrary @Boolean
ints <- Vector.generator pk2 (IntN.generator pn)
uint <- (UIntN.generator pm)
let x = Tuple4 addrs bool ints uint
pure $ genericEncodeDecode x === Right x

describe "Basic dynamic sized Tuple Tests" $ do

it "Can encode/decode (intN[], bytes, address[][k], string[k][], bool)" $ liftEffect do
quickCheckGen $ do
n <- oneOf (pure <$> intSizes)
m <- chooseInt 1 10
k <- chooseInt 1 10
reifyType n \pn ->
reifyType m \pm ->
reifyType k \pk -> do
ints <- arrayOf (IntN.generator pn)
bytes <- toByteString <$> (chooseInt 1 100 >>= genBytes)
addrs <- Vector.generator pm (arrayOf $ arbitrary @Address)
strings <- arrayOf (Vector.generator pk (arbitrary @BMPString))
bool <- arbitrary :: Gen Boolean
let x = Tuple5 ints bytes addrs strings bool
pure $ genericEncodeDecode x === Right x

it "Can encode/decode (address[k], bool, intN[k], uint)" $ liftEffect do
quickCheckGen $ do
k1 <- chooseInt 1 10
k2 <- chooseInt 1 10
n <- oneOf (pure <$> intSizes)
m <- oneOf (pure <$> intSizes)
reifyType k1 \pk1 ->
reifyType k2 \pk2 ->
reifyType n \pn -> do
reifyType m \pm -> do
addrs <- arrayOf (Vector.generator pk1 (arbitrary @Address))
bool <- arbitrary @Boolean
ints <- Vector.generator pk2 (IntN.generator pn)
uint <- (UIntN.generator pm)
let x = Tuple4 addrs bool ints uint
pure $ genericEncodeDecode x === Right x

--------------------------------------------------------------------------------
newtype BMPString = BMPString String

Expand Down Expand Up @@ -279,6 +354,18 @@ encodeDecode x =
in
(fromData a)

genericEncodeDecode
:: forall a rep
. Show a
=> Eq a
=> Generic a rep
=> GenericABIEncode rep
=> GenericABIDecode rep
=> a
-> Either ParseError a
genericEncodeDecode a =
genericFromData $ genericABIEncode a

intSizes :: NonEmptyArray Int
intSizes = unsafePartial fromJust
$ fromArray
Expand Down
35 changes: 16 additions & 19 deletions test/web3/Web3Spec/Encoding/DataSpec.purs
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
module Web3Spec.Encoding.DataSpec (spec, approve) where

import Prelude
import Data.Maybe (fromJust)
import Network.Ethereum.Web3.Solidity (UIntN, Tuple2, uIntNFromBigNumber)
import Network.Ethereum.Web3.Solidity.Sizes (s256)
import Network.Ethereum.Web3.Types (Address, HexString, TransactionOptions, NoPay, Web3, mkHexString, mkAddress)

import Data.Functor.Tagged (Tagged, tagged)
import Network.Ethereum.Web3.Contract (sendTx, mkDataField)
import Effect.Class (liftEffect)
import Network.Ethereum.Core.Keccak256 (toSelector)
import Network.Ethereum.Web3.Solidity.Generic (genericFromRecordFields)
import Type.Proxy (Proxy(..))
import Partial.Unsafe (unsafePartial)
import Network.Ethereum.Web3.Contract (sendTx, mkDataField)
import Network.Ethereum.Web3.Solidity (Tuple2, UIntN)
import Network.Ethereum.Web3.Solidity.AbiEncoding (toDataBuilder)
import Network.Ethereum.Web3.Solidity.Generic (genericFromRecordFields)
import Network.Ethereum.Web3.Types (Address, HexString, NoPay, TransactionOptions, Web3)
import Test.QuickCheck (quickCheck, (===))
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)
import Type.Proxy (Proxy(..))

spec :: Spec Unit
spec =
describe "data maker" do
it "can make the approval data" do
let
addr = unsafePartial fromJust $ (mkAddress =<< mkHexString "78534a937a855e15be172de35f2211626f92f8ec")

val = unsafePartial fromJust $ uIntNFromBigNumber s256 one

approvalD = mkDataField (Proxy :: Proxy ApproveFn) { _spender: addr, _value: val }
it "can make the approval data" $ liftEffect do
quickCheck $ \(args :: { _spender :: Address, _value :: UIntN 256 }) ->
let
approvalD = mkDataField (Proxy :: Proxy ApproveFn) args

sel = toSelector "approve(address,uint256)"
sel = toSelector "approve(address,uint256)"

fullDat = sel <> toDataBuilder addr <> toDataBuilder val
approvalD `shouldEqual` fullDat
fullDat = sel <> toDataBuilder args._spender <> toDataBuilder args._value
in
approvalD === fullDat

type ApproveFn = Tagged "approve(address,uint256)" (Tuple2 (Tagged "_spender" Address) (Tagged "_value" (UIntN 256)))

Expand Down
Loading

0 comments on commit 7c5f4c6

Please sign in to comment.