Skip to content

Commit c567573

Browse files
committed
Changed all the local tests to subclass from LocalTestCase instead of XCTestCase so they automatically inherit the default initialization code for ganache
Some light lint cleanup
1 parent 343b450 commit c567573

23 files changed

+365
-488
lines changed

Tests/web3swiftTests/localTests/ABIEncoderSoliditySha3Test.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
import XCTest
1111
@testable import web3swift
1212

13-
class ABIEncoderSoliditySha3Test: XCTestCase {
13+
class ABIEncoderSoliditySha3Test: LocalTestCase {
1414

1515
func test_soliditySha3() throws {
1616
var hex = try ABIEncoder.soliditySha3(true).toHexString().addHexPrefix()

Tests/web3swiftTests/localTests/DataConversionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import XCTest
1616

1717
// Some base58 test vectors pulled from: https://tools.ietf.org/id/draft-msporny-base58-01.html
1818
// note that one of the return values is incorrect in the reference above, it is corrected here
19-
class DataConversionTests: XCTestCase {
19+
class DataConversionTests: LocalTestCase {
2020
// test an empty input for the base58 decoder & decoder
2121
func testBase58() throws {
2222
let vector = ""

Tests/web3swiftTests/localTests/EIP1559BlockTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import BigInt
44
@testable
55
import web3swift
66

7-
class EIP1559BlockTests: XCTestCase {
7+
class EIP1559BlockTests: LocalTestCase {
88
let uselessBlockPart = (
99
number: BigUInt(12_965_000),
1010
hash: Data(fromHex: "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46")!, // "hash":

Tests/web3swiftTests/localTests/EIP712Tests.swift

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import XCTest
22
@testable import web3swift
33

4-
class EIP712Tests: XCTestCase {
4+
class EIP712Tests: LocalTestCase {
55
func testWithoutChainId() throws {
6-
6+
77
let to = EthereumAddress("0x3F06bAAdA68bB997daB03d91DBD0B73e196c5A4d")!
8-
8+
99
let value = EIP712.UInt256(0)
10-
10+
1111
let amountLinen = EIP712.UInt256("0001000000000000000")//
12-
12+
1313
let function = ABI.Element.Function(
1414
name: "approveAndMint",
1515
inputs: [
@@ -18,22 +18,22 @@ class EIP712Tests: XCTestCase {
1818
outputs: [.init(name: "", type: .bool)],
1919
constant: false,
2020
payable: false)
21-
21+
2222
let object = ABI.Element.function(function)
23-
23+
2424
let safeTxData = object.encodeParameters([
2525
EthereumAddress("0x41B5844f4680a8C38fBb695b7F9CFd1F64474a72")! as AnyObject,
2626
amountLinen as AnyObject
2727
])!
28-
28+
2929
let operation: EIP712.UInt8 = 1
30-
30+
3131
let safeTxGas = EIP712.UInt256(250000)
32-
32+
3333
let baseGas = EIP712.UInt256(60000)
34-
34+
3535
let gasPrice = EIP712.UInt256("20000000000")
36-
36+
3737
let gasToken = EthereumAddress("0x0000000000000000000000000000000000000000")!
3838
print(1)
3939
let refundReceiver = EthereumAddress("0x7c07D32e18D6495eFDC487A32F8D20daFBa53A5e")!
@@ -76,13 +76,13 @@ class EIP712Tests: XCTestCase {
7676
}
7777

7878
func testWithChainId() throws {
79-
79+
8080
let to = EthereumAddress("0x3F06bAAdA68bB997daB03d91DBD0B73e196c5A4d")!
81-
81+
8282
let value = EIP712.UInt256(0)
83-
83+
8484
let amount = EIP712.UInt256("0001000000000000000")
85-
85+
8686
let function = ABI.Element.Function(
8787
name: "approveAndMint",
8888
inputs: [
@@ -91,28 +91,28 @@ class EIP712Tests: XCTestCase {
9191
outputs: [.init(name: "", type: .bool)],
9292
constant: false,
9393
payable: false)
94-
94+
9595
let object = ABI.Element.function(function)
96-
96+
9797
let safeTxData = object.encodeParameters([
9898
EthereumAddress("0x41B5844f4680a8C38fBb695b7F9CFd1F64474a72")! as AnyObject,
9999
amount as AnyObject
100100
])!
101-
101+
102102
let operation: EIP712.UInt8 = 1
103-
103+
104104
let safeTxGas = EIP712.UInt256(250000)
105-
105+
106106
let baseGas = EIP712.UInt256(60000)
107-
107+
108108
let gasPrice = EIP712.UInt256("20000000000")
109-
109+
110110
let gasToken = EthereumAddress("0x0000000000000000000000000000000000000000")!
111-
111+
112112
let refundReceiver = EthereumAddress("0x7c07D32e18D6495eFDC487A32F8D20daFBa53A5e")!
113-
113+
114114
let nonce: EIP712.UInt256 = .init(0)
115-
115+
116116
let safeTX = SafeTx(
117117
to: to,
118118
value: value,
@@ -124,25 +124,24 @@ class EIP712Tests: XCTestCase {
124124
gasToken: gasToken,
125125
refundReceiver: refundReceiver,
126126
nonce: nonce)
127-
127+
128128
let mnemonic = "normal dune pole key case cradle unfold require tornado mercy hospital buyer"
129129
let keystore = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "")!
130-
130+
131131
let verifyingContract = EthereumAddress("0x76106814dc6150b0fe510fbda4d2d877ac221270")!
132-
132+
133133
let account = keystore.addresses?[0]
134134
let password = ""
135135
let chainId: EIP712.UInt256? = EIP712.UInt256(42)
136-
136+
137137
let signature = try Web3Signer.signEIP712(
138138
safeTx: safeTX,
139139
keystore: keystore,
140140
verifyingContract: verifyingContract,
141141
account: account!,
142142
password: password,
143143
chainId: chainId)
144-
144+
145145
XCTAssertEqual(signature.toHexString(), "f1f423cb23efad5035d4fb95c19cfcd46d4091f2bd924680b88c4f9edfa1fb3a4ce5fc5d169f354e3b464f45a425ed3f6203af06afbacdc5c8224a300ce9e6b21b")
146146
}
147147
}
148-

Tests/web3swiftTests/localTests/LocalTestCase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class LocalTestCase: XCTestCase {
1616

1717
// check to see if we need to run the one-time setup
1818
if isSetUp { return }
19-
isSetUp = true;
19+
isSetUp = true
2020

2121
let web3 = try! Web3.new(LocalTestCase.url)
2222

Tests/web3swiftTests/localTests/web3swiftAdvancedABIv2Tests.swift

Lines changed: 60 additions & 73 deletions
Large diffs are not rendered by default.

Tests/web3swiftTests/localTests/web3swiftBasicLocalNodeTests.swift

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,91 +12,83 @@ import BigInt
1212

1313
@testable import web3swift
1414

15-
class web3swiftBasicLocalNodeTests: XCTestCase {
16-
17-
// this is needed for any test suites that depend on the block-chain state from Ganache
18-
override class func setUp() {
19-
super.setUp()
20-
preloadGanache()
21-
}
15+
class web3swiftBasicLocalNodeTests: LocalTestCase {
2216

2317
func testDeployWithRemoteSigning() throws {
24-
let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
25-
let allAddresses = try web3.eth.getAccounts()
26-
18+
let allAddresses = try ganache.eth.getAccounts()
19+
2720
let abiString = "[{\"constant\":true,\"inputs\":[],\"name\":\"getFlagData\",\"outputs\":[{\"name\":\"data\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"data\",\"type\":\"string\"}],\"name\":\"setFlagData\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"
2821
let bytecode = Data.fromHex("6060604052341561000f57600080fd5b6103358061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063a16e94bf14610051578063a46b5b6b146100df575b600080fd5b341561005c57600080fd5b61006461013c565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a4578082015181840152602081019050610089565b50505050905090810190601f1680156100d15780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156100ea57600080fd5b61013a600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190505061020d565b005b610144610250565b6000808073ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102035780601f106101d857610100808354040283529160200191610203565b820191906000526020600020905b8154815290600101906020018083116101e657829003601f168201915b5050505050905090565b806000808073ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600001908051906020019061024c929190610264565b5050565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102a557805160ff19168380011785556102d3565b828001600101855582156102d3579182015b828111156102d25782518255916020019190600101906102b7565b5b5090506102e091906102e4565b5090565b61030691905b808211156103025760008160009055506001016102ea565b5090565b905600a165627a7a7230582017359d063cd7fdf56f19ca186a54863ce855c8f070acece905d8538fbbc4d1bf0029")!
29-
30-
let contract = web3.contract(abiString, at: nil, abiVersion: 2)!
31-
22+
23+
let contract = ganache.contract(abiString, at: nil, abiVersion: 2)!
24+
3225
let parameters = [] as [AnyObject]
3326
let deployTx = contract.deploy(bytecode: bytecode, parameters: parameters)!
3427
deployTx.transactionOptions.from = allAddresses[0]
3528
deployTx.transactionOptions.gasLimit = .manual(3000000)
36-
29+
3730
let result = try deployTx.sendPromise().wait()
3831
let txHash = result.hash
3932
print("Transaction with hash " + txHash)
40-
33+
4134
Thread.sleep(forTimeInterval: 1.0)
42-
43-
let receipt = try web3.eth.getTransactionReceipt(txHash)
35+
36+
let receipt = try ganache.eth.getTransactionReceipt(txHash)
4437
print(receipt)
45-
38+
4639
switch receipt.status {
4740
case .notYetProcessed:
4841
return
4942
default:
5043
break
5144
}
52-
53-
let details = try web3.eth.getTransactionDetails(txHash)
45+
46+
let details = try ganache.eth.getTransactionDetails(txHash)
5447
print(details)
5548
}
5649

5750
func testEthSendExampleWithRemoteSigning() throws {
58-
let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
59-
let allAddresses = try web3.eth.getAccounts()
51+
let allAddresses = try ganache.eth.getAccounts()
6052
let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
61-
let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
62-
53+
let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
54+
6355
let parameters = [] as [AnyObject]
6456
let sendTx = contract.method("fallback", parameters: parameters)!
65-
57+
6658
let valueToSend = Web3.Utils.parseToBigUInt("1.0", units: .eth)
6759
sendTx.transactionOptions.value = valueToSend
6860
sendTx.transactionOptions.from = allAddresses[0]
69-
70-
let balanceBeforeTo = try web3.eth.getBalancePromise(address: sendToAddress).wait()
71-
let balanceBeforeFrom = try web3.eth.getBalancePromise(address: allAddresses[0]).wait()
61+
62+
let balanceBeforeTo = try ganache.eth.getBalancePromise(address: sendToAddress).wait()
63+
let balanceBeforeFrom = try ganache.eth.getBalancePromise(address: allAddresses[0]).wait()
7264
print("Balance before to: " + balanceBeforeTo.description)
7365
print("Balance before from: " + balanceBeforeFrom.description)
74-
66+
7567
let result = try sendTx.sendPromise().wait()
7668
let txHash = result.hash
7769
print("Transaction with hash " + txHash)
78-
70+
7971
Thread.sleep(forTimeInterval: 1.0)
80-
81-
let receipt = try web3.eth.getTransactionReceipt(txHash)
72+
73+
let receipt = try ganache.eth.getTransactionReceipt(txHash)
8274
print(receipt)
83-
75+
8476
switch receipt.status {
8577
case .notYetProcessed:
8678
return
8779
default:
8880
break
8981
}
90-
91-
let details = try web3.eth.getTransactionDetails(txHash)
82+
83+
let details = try ganache.eth.getTransactionDetails(txHash)
9284
print(details)
93-
94-
95-
let balanceAfterTo = try web3.eth.getBalancePromise(address: sendToAddress).wait()
96-
let balanceAfterFrom = try web3.eth.getBalancePromise(address: allAddresses[0]).wait()
85+
86+
87+
let balanceAfterTo = try ganache.eth.getBalancePromise(address: sendToAddress).wait()
88+
let balanceAfterFrom = try ganache.eth.getBalancePromise(address: allAddresses[0]).wait()
9789
print("Balance after to: " + balanceAfterTo.description)
9890
print("Balance after from: " + balanceAfterFrom.description)
99-
91+
10092
XCTAssert(balanceAfterTo - balanceBeforeTo == valueToSend)
10193
let txnGasPrice = details.transaction.parameters.gasPrice ?? 0
10294
XCTAssert(balanceBeforeFrom - (balanceAfterFrom + receipt.gasUsed * txnGasPrice) == valueToSend)
@@ -105,21 +97,19 @@ class web3swiftBasicLocalNodeTests: XCTestCase {
10597
// FIXME: Crashes on CI/CD
10698
// FIXME: Fails on ganache
10799
// func testSignPersonal() throws {
108-
// let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
109-
// let allAddresses = try web3.eth.getAccounts()
110-
111-
// let response = try web3.personal.signPersonalMessage(message: "hello world".data(using: .utf8)!, from: allAddresses[0])
112-
100+
// let allAddresses = try ganache.eth.getAccounts()
101+
102+
// let response = try ganache.personal.signPersonalMessage(message: "hello world".data(using: .utf8)!, from: allAddresses[0])
103+
113104
// XCTAssert(response.toHexString() == "b686c8ddc854bd49de9eb62eb4e52af4c69a89802b40fe9a295e346b111406393c6e3f05114561ab845a47196ad22c33cec67592af9a9e42bfc067a20c7d4b6101")
114105
// }
115-
106+
116107
// MARK: Ganache doesn't support a mempool for now
117108
// func testTxPoolStatus() throws {
118-
// let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
119-
// let allAddresses = try web3.eth.getAccounts()
109+
// let allAddresses = try ganache.eth.getAccounts()
120110
//
121111
// let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
122-
// let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
112+
// let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
123113
//
124114
// let parameters = [] as [AnyObject]
125115
// let sendTx = contract.method("fallback", parameters: parameters)!
@@ -130,17 +120,16 @@ class web3swiftBasicLocalNodeTests: XCTestCase {
130120
//
131121
// let _ = try sendTx.sendPromise().wait()
132122
//
133-
// let result = try web3.txPool.getStatus()
123+
// let result = try ganache.txPool.getStatus()
134124
//
135125
// print(result)
136126
// }
137127
//
138128
// func testTxPoolInspect() throws {
139-
// let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
140-
// let allAddresses = try web3.eth.getAccounts()
129+
// let allAddresses = try ganache.eth.getAccounts()
141130
//
142131
// let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
143-
// let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
132+
// let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
144133
//
145134
// let parameters = [] as [AnyObject]
146135
// let sendTx = contract.method("fallback", parameters: parameters)!
@@ -151,17 +140,16 @@ class web3swiftBasicLocalNodeTests: XCTestCase {
151140
//
152141
// let _ = try sendTx.sendPromise().wait()
153142
//
154-
// let result = try web3.txPool.getInspect()
143+
// let result = try ganache.txPool.getInspect()
155144
//
156145
// print(result)
157146
// }
158147
//
159148
// func testTxPoolContent() throws {
160-
// let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
161-
// let allAddresses = try web3.eth.getAccounts()
149+
// let allAddresses = try ganache.eth.getAccounts()
162150
//
163151
// let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
164-
// let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
152+
// let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
165153
//
166154
// let parameters = [] as [AnyObject]
167155
// let sendTx = contract.method("fallback", parameters: parameters)!
@@ -172,7 +160,7 @@ class web3swiftBasicLocalNodeTests: XCTestCase {
172160
//
173161
// let _ = try sendTx.sendPromise().wait()
174162
//
175-
// let result = try web3.txPool.getContent()
163+
// let result = try ganache.txPool.getContent()
176164
//
177165
// print(result)
178166
// }

Tests/web3swiftTests/localTests/web3swiftDecodeSolidityErrorType.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,11 @@ import web3swift
1111

1212
/// Since solidity 0.8.4 a new type was introduced called `error`.
1313
/// Contracts' ABI with this type were not decodable.
14-
class web3swiftDecodeSolidityErrorType: XCTestCase {
15-
16-
// this is needed for any test suites that depend on the block-chain state from Ganache
17-
override class func setUp() {
18-
super.setUp()
19-
preloadGanache()
20-
}
14+
class web3swiftDecodeSolidityErrorType: LocalTestCase {
2115

2216
func testStructuredErrorTypeDecoding() throws {
2317
let contractAbiWithErrorTypes = "[{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"disallowedAddress\",\"type\":\"address\"}],\"name\":\"NotAllowedAddress\",\"type\":\"error\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"bytes4\",\"name\":\"disallowedFunction\",\"type\":\"bytes4\"}],\"name\":\"NotAllowedFunction\",\"type\":\"error\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"permission\",\"type\":\"string\"}],\"name\":\"NotAuthorised\",\"type\":\"error\"}]"
24-
let web3Instance = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
25-
let contract = web3.web3contract(web3: web3Instance, abiString: contractAbiWithErrorTypes)
18+
let contract = web3.web3contract(web3: ganache, abiString: contractAbiWithErrorTypes)
2619
assert(contract != nil)
2720
}
2821
}

0 commit comments

Comments
 (0)