Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
refactor: format test files to 80 char per line
Browse files Browse the repository at this point in the history
  • Loading branch information
CassiusPacheco committed Nov 21, 2022
1 parent 146c5a0 commit 16b8316
Show file tree
Hide file tree
Showing 24 changed files with 288 additions and 63 deletions.
15 changes: 12 additions & 3 deletions Tests/ImmutableXCoreTests/Crypto/Keys/ECPrivateKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import XCTest
final class ECPrivateKeyTests: XCTestCase {
func testInitNumber() throws {
let biggerNumber = StarkCurve.N + 1
XCTAssertThrowsError(try ECPrivateKey(number: biggerNumber), "Should throw if number is not in the StarkCurve.N range") { error in
XCTAssertThrowsError(
try ECPrivateKey(number: biggerNumber),
"Should throw if number is not in the StarkCurve.N range"
) { error in
XCTAssertTrue(error is ImmutableXError)
}

XCTAssertThrowsError(try ECPrivateKey(number: BigInt.zero), "Should throw if number is smaller than 1") { error in
XCTAssertThrowsError(
try ECPrivateKey(number: BigInt.zero),
"Should throw if number is smaller than 1"
) { error in
XCTAssertTrue(error is ImmutableXError)
}

Expand All @@ -20,7 +26,10 @@ final class ECPrivateKeyTests: XCTestCase {
}

func testInitHex() throws {
XCTAssertThrowsError(try ECPrivateKey(hex: "invalid"), "Should throw if hex can't be represented as a BigInt") { error in
XCTAssertThrowsError(
try ECPrivateKey(hex: "invalid"),
"Should throw if hex can't be represented as a BigInt"
) { error in
XCTAssertTrue(error is ImmutableXError)
}

Expand Down
39 changes: 30 additions & 9 deletions Tests/ImmutableXCoreTests/Crypto/Stark/StarkKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ final class StarkKeyTests: XCTestCase {
// MARK: - Stark Key Generation

func testGenerateKeyPairFromRawSignature() throws {
let signature = "0x5a263fad6f17f23e7c7ea833d058f3656d3fe464baf13f6f5ccba9a2466ba2ce4c4a250231bcac7beb165aec4c9b049b4ba40ad8dd287dc79b92b1ffcf20cdcf1b"
let signature = "0x5a263fad6f17f23e7c7ea833d058f3656d3fe464baf13f6f5ccba9a246" +
"6ba2ce4c4a250231bcac7beb165aec4c9b049b4ba40ad8dd287dc79b92b1ffcf20cdcf1b"
let address = "0xa76e3eeb2f7143165618ab8feaabcd395b6fac7f"
let pair = try StarkKey.generateKeyPairFromRawSignature(signature, ethereumAddress: address)

XCTAssertEqual(pair.publicKey.asStarkPublicKey, "0x02a4c7332c55d6c1c510d24272d1db82878f2302f05b53bcc38695ed5f78fffd")
XCTAssertEqual(
pair.publicKey.asStarkPublicKey,
"0x02a4c7332c55d6c1c510d24272d1db82878f2302f05b53bcc38695ed5f78fffd"
)
}

func testGetKeyFromRawSignatureWithExtraGrinding() throws {
let signature = "0x6d1550458c7a9a1257d73adbcf0fabc12f4497e970d9fa62dd88bf7d9e12719148c96225c1402d8707fd061b1aae2222bdf13571dfc82b3aa9974039f247f2b81b"
let signature = "0x6d1550458c7a9a1257d73adbcf0fabc12f4497e970d9fa62dd88bf7d9e" +
"12719148c96225c1402d8707fd061b1aae2222bdf13571dfc82b3aa9974039f247f2b81b"
let address = "0xa4864d977b944315389d1765ffa7e66F74ee8cd7"
let pair = try StarkKey.generateKeyPairFromRawSignature(signature, ethereumAddress: address)
XCTAssertEqual(pair.publicKey.asStarkPublicKey, "0x035919acd61e97b3ecdc75ff8beed8d1803f7ea3cad2937926ae59cc3f8070d4")
XCTAssertEqual(
pair.publicKey.asStarkPublicKey,
"0x035919acd61e97b3ecdc75ff8beed8d1803f7ea3cad2937926ae59cc3f8070d4"
)
}

func testGetPrivateKeySignature() throws {
let signature = "0x21fbf0696d5e0aa2ef41a2b4ffb623bcaf070461d61cf7251c74161f82fec3a4370854bc0a34b3ab487c1bc021cd318c734c51ae29374f2beb0e6f2dd49b4bf41c"
let signature = "0x21fbf0696d5e0aa2ef41a2b4ffb623bcaf070461d61cf7251c74161f82fec3a4" +
"370854bc0a34b3ab487c1bc021cd318c734c51ae29374f2beb0e6f2dd49b4bf41c"

let result = StarkKey.grindKey(keySeed: signature.dropHexPrefix[0 ..< 64])
XCTAssertEqual(result, "766f11e90cd7c7b43085b56da35c781f8c067ac0d578eabdceebc4886435bda")
Expand All @@ -42,10 +51,14 @@ final class StarkKeyTests: XCTestCase {
func testGenerateFromSignerAsync() async throws {
let signer = SignerMock()
signer.getAddressReturnValue = "0xa76e3eeb2f7143165618ab8feaabcd395b6fac7f"
signer.signMessageReturnValue = "0x5a263fad6f17f23e7c7ea833d058f3656d3fe464baf13f6f5ccba9a2466ba2ce4c4a250231bcac7beb165aec4c9b049b4ba40ad8dd287dc79b92b1ffcf20cdcf1b"
signer.signMessageReturnValue = "0x5a263fad6f17f23e7c7ea833d058f3656d3fe464baf13f6f5ccba9a246" +
"6ba2ce4c4a250231bcac7beb165aec4c9b049b4ba40ad8dd287dc79b92b1ffcf20cdcf1b"

let pair = try await StarkKey.generateKeyPair(from: signer)
XCTAssertEqual(pair.publicKey.asStarkPublicKey, "0x02a4c7332c55d6c1c510d24272d1db82878f2302f05b53bcc38695ed5f78fffd")
XCTAssertEqual(
pair.publicKey.asStarkPublicKey,
"0x02a4c7332c55d6c1c510d24272d1db82878f2302f05b53bcc38695ed5f78fffd"
)
}

func testGenerateFromSignerAsyncFailure() async throws {
Expand All @@ -70,15 +83,23 @@ extension StarkKeyTests {
let privateKey = "7CEFD165C3A374AC3E05170D699BF6549E371522883B447B284A3C16FC04CCC"

let signature = try StarkKey.sign(message: encodedMessage, withPrivateKeyHex: privateKey)
XCTAssertEqual(signature, "0x0752063caed87ef11d6e91c4a226ebfe98f190d248b857d882ae331771e6e4620364a2c46e2190bbb243309a40da051b88f0657ea9d1c2ca11510fe18a8a22ae")
XCTAssertEqual(
signature,
"0x0752063caed87ef11d6e91c4a226ebfe98f190d248b857d882ae331771e6e" +
"4620364a2c46e2190bbb243309a40da051b88f0657ea9d1c2ca11510fe18a8a22ae"
)
}

func testSignWithPrivateKey() throws {
let encodedMessage = "e2919c6f19f93d3b9e40c1eef10660bd12240a1520793a28ef21a7457037dd"
let privateKey = try ECPrivateKey(hex: "7CEFD165C3A374AC3E05170D699BF6549E371522883B447B284A3C16FC04CCC")

let signature = try StarkKey.sign(message: encodedMessage, with: privateKey)
XCTAssertEqual(signature, "0x0752063caed87ef11d6e91c4a226ebfe98f190d248b857d882ae331771e6e4620364a2c46e2190bbb243309a40da051b88f0657ea9d1c2ca11510fe18a8a22ae")
XCTAssertEqual(
signature,
"0x0752063caed87ef11d6e91c4a226ebfe98f190d248b857d882ae331771e6" +
"e4620364a2c46e2190bbb243309a40da051b88f0657ea9d1c2ca11510fe18a8a22ae"
)
}

func testSignWithInvalidMessage() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ final class StarkSignatureTests: XCTestCase {

func testSerialized() throws {
let signature = try StarkSignature(r: BigInt(1), s: BigInt(2))
XCTAssertEqual(signature.serialized(), "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002")
XCTAssertEqual(
signature.serialized(),
"0x000000000000000000000000000000000000000000000000000000000000000" +
"10000000000000000000000000000000000000000000000000000000000000002"
)
}
}
21 changes: 17 additions & 4 deletions Tests/ImmutableXCoreTests/Custom APIs/ExchangesAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,25 @@ final class ExchangesAPITests: XCTestCase {
func testGetTransactionIdSuccessful() async throws {
builderMock.mock(.success(Response(response: HTTPURLResponse(), body: transactionIdResponseStub1)))

let response = try await exchangesAPI.getTransactionId(GetTransactionIdRequest(walletAddress: "0xa76e3eeb2f7143165618ab8feaabcd395b6fac7f", provider: .moonpay))
let response = try await exchangesAPI.getTransactionId(
GetTransactionIdRequest(walletAddress: "0xa76e3eeb2f7143165618ab8feaabcd395b6fac7f", provider: .moonpay)
)
XCTAssertEqual(response.id, 123)
}

func testGetTransactionIdFailure() async {
builderMock.mock(Result<Response<GetSignedMoonpayResponse>, ErrorResponse>.failure(.error(400, nil, nil, DummyError.something)))
builderMock.mock(
Result<Response<GetSignedMoonpayResponse>,
ErrorResponse>.failure(.error(400, nil, nil, DummyError.something))
)

await XCTAssertThrowsErrorAsync {
_ = try await self.exchangesAPI.getTransactionId(GetTransactionIdRequest(walletAddress: "0xa76e3eeb2f7143165618ab8feaabcd395b6fac7f", provider: .moonpay))
_ = try await self.exchangesAPI.getTransactionId(
GetTransactionIdRequest(
walletAddress: "0xa76e3eeb2f7143165618ab8feaabcd395b6fac7f",
provider: .moonpay
)
)
}
}

Expand All @@ -34,7 +44,10 @@ final class ExchangesAPITests: XCTestCase {
}

func testGetCurrenciesFailure() async {
builderMock.mock(Result<Response<[CurrencyCode]>, ErrorResponse>.failure(.error(400, nil, nil, DummyError.something)))
builderMock.mock(
Result<Response<[CurrencyCode]>,
ErrorResponse>.failure(.error(400, nil, nil, DummyError.something))
)

await XCTAssertThrowsErrorAsync {
_ = try await self.exchangesAPI.getCurrencies(address: "0xa76e3eeb2f7143165618ab8feaabcd395b6fac7f")
Expand Down
12 changes: 10 additions & 2 deletions Tests/ImmutableXCoreTests/Custom APIs/MoonpayAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ final class MoonpayAPITests: XCTestCase {
}

func testRequestEncodedString() async throws {
XCTAssertEqual(try request.asURLEncodedString(), "apiKey=pk_test_nGdsu1IBkjiFzmEvN8ddf4gM9GNy5Sgz&baseCurrencyCode=usd&colorCode=%23#008000&externalTransactionId=123&walletAddress=0xa76e3eeb2f7143165618ab8feaabcd395b6fac7f&walletAddresses=%7B%22gods_immutable%22%3A%220xa76e3eeb2f7143165618ab8feaabcd395b6fac7f%22%7D")
XCTAssertEqual(
try request.asURLEncodedString(),
"apiKey=pk_test_nGdsu1IBkjiFzmEvN8ddf4gM9GNy5Sgz&baseCurrencyCode=usd&colorCode=%23#008000&" + "externalTransactionId=123&walletAddress=0xa76e3eeb2f7143165618ab8feaabcd395b6fac7f&" + "walletAddresses=%7B%22gods_immutable%22%3A%220xa76e3eeb2f7143165618ab8feaabcd395b6fac7f%22%7D"
)
}

func testGetBuyCryptoURLSuccessful() async throws {
let url = try await moonpayAPI.getBuyCryptoURL(request)
XCTAssertEqual(url, "https://buy-staging.moonpay.io/?apiKey=pk_test_nGdsu1IBkjiFzmEvN8ddf4gM9GNy5Sgz&baseCurrencyCode=usd&colorCode=%23#008000&externalTransactionId=123&walletAddress=0xa76e3eeb2f7143165618ab8feaabcd395b6fac7f&walletAddresses=%7B%22gods_immutable%22%3A%220xa76e3eeb2f7143165618ab8feaabcd395b6fac7f%22%7D&signature=signature")
XCTAssertEqual(
url,
"https://buy-staging.moonpay.io/?apiKey=pk_test_nGdsu1IBkjiFzmEvN8ddf4gM9GNy5Sgz&" +
"baseCurrencyCode=usd&colorCode=%23#008000&externalTransactionId=123&" +
"walletAddress=0xa76e3eeb2f7143165618ab8feaabcd395b6fac7f&walletAddresses=" + "%7B%22gods_immutable%22%3A%220xa76e3eeb2f7143165618ab8feaabcd395b6fac7f%22%7D&signature=signature"
)
}

func testGetBuyCryptoURLFailure() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ final class ArrayExtensionsTests: XCTestCase {
func testAsHexString() throws {
let hex = "3035919acd61e97b3ecdc75ff8beed8d1803f7ea3cad2937926ae59cc3f8070d4"
XCTAssertEqual(
hex.sanitizeBytes().hexaToBytes.asHexString(byteLength: 32), "0x035919acd61e97b3ecdc75ff8beed8d1803f7ea3cad2937926ae59cc3f8070d4"
hex.sanitizeBytes().hexaToBytes.asHexString(byteLength: 32),
"0x035919acd61e97b3ecdc75ff8beed8d1803f7ea3cad2937926ae59cc3f8070d4"
)
XCTAssertEqual(
hex.sanitizeBytes(byteSize: 2).hexaToBytes.asHexString(byteLength: 64), "0x0" + hex
Expand Down
28 changes: 22 additions & 6 deletions Tests/ImmutableXCoreTests/Extensions/BigInt+ExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,46 @@ final class BigIntExtensionsTests: XCTestCase {
XCTAssertEqual(BigInt(2).isEven, true)
XCTAssertEqual(BigInt(14234).isEven, true)
XCTAssertEqual(BigInt(23423).isEven, false)
XCTAssertEqual(BigInt("402055865407347912633035864788341122847011912814621855552565784015096891163").isEven, false)
XCTAssertEqual(
BigInt("402055865407347912633035864788341122847011912814621855552565784015096891163").isEven,
false
)
}

func testHexStringInit() {
XCTAssertEqual(BigInt(hexString: "1376eb19ad1b0"), BigInt(342_423_542_354_352))
XCTAssertEqual(BigInt(hexString: "0x1376eb19ad1b0"), BigInt(342_423_542_354_352))
XCTAssertEqual(
BigInt(hexString: "7cefd165c3a374ac3e05170d699bf6549e371522883b447b284a3c16fc04ccc"), BigInt("3531907180084456080948977704976059596947036969275297986324692124860996930764")
BigInt(hexString: "7cefd165c3a374ac3e05170d699bf6549e371522883b447b284a3c16fc04ccc"),
BigInt("3531907180084456080948977704976059596947036969275297986324692124860996930764")
)
}

func testAsString() {
XCTAssertEqual(BigInt(342_423_542_354_352).asString(uppercased: false, radix: 16), "1376eb19ad1b0")
XCTAssertEqual(BigInt(342_423_542_354_352).asString(uppercased: true, radix: 16), "1376EB19AD1B0")
XCTAssertEqual(BigInt(342_423_542_354_352).asString(radix: 10), "342423542354352")
XCTAssertEqual(BigInt(hexString: "7cefd165c3a374ac3e05170d699bf6549e371522883b447b284a3c16fc04ccc")!.asString(radix: 10), "3531907180084456080948977704976059596947036969275297986324692124860996930764")
XCTAssertEqual(
BigInt(hexString: "7cefd165c3a374ac3e05170d699bf6549e371522883b447b284a3c16fc04ccc")!.asString(radix: 10),
"3531907180084456080948977704976059596947036969275297986324692124860996930764"
)
}

func testAsStringLength64() {
XCTAssertEqual(BigInt(342_423_542_354_352).asHexStringLength64(), "0000000000000000000000000000000000000000000000000001376eb19ad1b0")
XCTAssertEqual(BigInt(342_423_542_354_352).asHexStringLength64(uppercased: true), "0000000000000000000000000000000000000000000000000001376EB19AD1B0")
XCTAssertEqual(
BigInt(342_423_542_354_352).asHexStringLength64(),
"0000000000000000000000000000000000000000000000000001376eb19ad1b0"
)
XCTAssertEqual(
BigInt(342_423_542_354_352).asHexStringLength64(uppercased: true),
"0000000000000000000000000000000000000000000000000001376EB19AD1B0"
)
}

func testAs256bitLongData() {
XCTAssertEqual(BigInt(342_423_542_354_352).as256bitLongData(), "0000000000000000000000000000000000000000000000000001376eb19ad1b0".hexToData())
XCTAssertEqual(
BigInt(342_423_542_354_352).as256bitLongData(),
"0000000000000000000000000000000000000000000000000001376eb19ad1b0".hexToData()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import XCTest
final class IntExtensionsTests: XCTestCase {
func testByteCountFromBitCount() throws {
XCTAssertEqual(BigInt(123_123_123).bitWidth.byteCountFromBitCount, 4)
XCTAssertEqual(BigInt("402055865407347912633035864788341122847011912814621855552565784015096891163").bitWidth.byteCountFromBitCount, 32)
XCTAssertEqual(
BigInt("402055865407347912633035864788341122847011912814621855552565784015096891163")
.bitWidth.byteCountFromBitCount,
32
)
}
}
44 changes: 38 additions & 6 deletions Tests/ImmutableXCoreTests/ImmutableXTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ final class ImmutableXTests: XCTestCase {
// MARK: - Buy

func testBuyFlowSuccessAsync() async throws {
let response = try await core.buy(orderId: "1", fees: [feeEntryStub1], signer: SignerMock(), starkSigner: StarkSignerMock())
let response = try await core.buy(
orderId: "1",
fees: [feeEntryStub1],
signer: SignerMock(),
starkSigner: StarkSignerMock()
)
XCTAssertEqual(response, createTradeResponseStub1)
}

Expand All @@ -214,14 +219,25 @@ final class ImmutableXTests: XCTestCase {
buyWorkflow.mock(buyCompanion, id: "1")

await XCTAssertThrowsErrorAsync {
_ = try await self.core.buy(orderId: "1", fees: [feeEntryStub1], signer: SignerMock(), starkSigner: StarkSignerMock())
_ = try await self.core.buy(
orderId: "1",
fees: [feeEntryStub1],
signer: SignerMock(),
starkSigner: StarkSignerMock()
)
}
}

// MARK: - Sell

func testSellFlowSuccessAsync() async throws {
let response = try await core.sell(asset: erc721AssetStub1, sellToken: erc20AssetStub1, fees: [], signer: SignerMock(), starkSigner: StarkSignerMock())
let response = try await core.sell(
asset: erc721AssetStub1,
sellToken: erc20AssetStub1,
fees: [],
signer: SignerMock(),
starkSigner: StarkSignerMock()
)
XCTAssertEqual(response, createOrderResponseStub1)
}

Expand All @@ -231,7 +247,13 @@ final class ImmutableXTests: XCTestCase {
sellWorkflow.mock(sellCompanion)

await XCTAssertThrowsErrorAsync {
_ = try await self.core.sell(asset: erc721AssetStub1, sellToken: erc20AssetStub1, fees: [], signer: SignerMock(), starkSigner: StarkSignerMock())
_ = try await self.core.sell(
asset: erc721AssetStub1,
sellToken: erc20AssetStub1,
fees: [],
signer: SignerMock(),
starkSigner: StarkSignerMock()
)
}
}

Expand All @@ -255,7 +277,12 @@ final class ImmutableXTests: XCTestCase {
// MARK: - Transfer

func testTransferFlowSuccessAsync() async throws {
let response = try await core.transfer(token: ETHAsset(quantity: "10"), recipientAddress: "address", signer: SignerMock(), starkSigner: StarkSignerMock())
let response = try await core.transfer(
token: ETHAsset(quantity: "10"),
recipientAddress: "address",
signer: SignerMock(),
starkSigner: StarkSignerMock()
)
XCTAssertEqual(response, createTransferResponseStub1)
}

Expand All @@ -265,7 +292,12 @@ final class ImmutableXTests: XCTestCase {
transferWorkflowMock.mock(transferCompanion)

await XCTAssertThrowsErrorAsync {
_ = try await self.core.transfer(token: ETHAsset(quantity: "10"), recipientAddress: "address", signer: SignerMock(), starkSigner: StarkSignerMock())
_ = try await self.core.transfer(
token: ETHAsset(quantity: "10"),
recipientAddress: "address",
signer: SignerMock(),
starkSigner: StarkSignerMock()
)
}
}

Expand Down
16 changes: 14 additions & 2 deletions Tests/ImmutableXCoreTests/Mocks/API/CollectionsAPIMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ final class CollectionsAPIMock: CollectionsAPI {
return companion.returnValue
}

override class func listCollectionFilters(address: String, pageSize: Int? = nil, nextPageToken: String? = nil) async throws -> CollectionFilter {
override class func listCollectionFilters(
address: String,
pageSize: Int? = nil,
nextPageToken: String? = nil
) async throws -> CollectionFilter {
let companion = listCollectionFiltersCompanion!
companion.callsCount += 1

Expand All @@ -64,7 +68,15 @@ final class CollectionsAPIMock: CollectionsAPI {
return companion.returnValue
}

override class func listCollections(pageSize: Int? = nil, cursor: String? = nil, orderBy: CollectionsAPI.OrderBy_listCollections? = nil, direction: String? = nil, blacklist: String? = nil, whitelist: String? = nil, keyword: String? = nil) async throws -> ListCollectionsResponse {
override class func listCollections(
pageSize: Int? = nil,
cursor: String? = nil,
orderBy: CollectionsAPI.OrderBy_listCollections? = nil,
direction: String? = nil,
blacklist: String? = nil,
whitelist: String? = nil,
keyword: String? = nil
) async throws -> ListCollectionsResponse {
let companion = listCollectionsCompanion!
companion.callsCount += 1

Expand Down
Loading

0 comments on commit 16b8316

Please sign in to comment.