Skip to content

Commit

Permalink
add some unit tests (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
izik1 authored Jul 10, 2023
1 parent b38efae commit 3772ea9
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 2 deletions.
9 changes: 8 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,14 @@ let package = Package(
// .unsafeFlags(["-Xfrontend", "-warn-concurrency", "-Xfrontend", "-enable-actor-data-race-checks"])
// ]
),
.testTarget(name: "HederaTests", dependencies: ["Hedera"]),
.testTarget(
name: "HederaTests",
dependencies: [
"Hedera",
.product(name: "SnapshotTesting", package: "swift-snapshot-testing"),
],
exclude: ["__Snapshots__"]
),
.testTarget(
name: "HederaE2ETests",
dependencies: [
Expand Down
4 changes: 4 additions & 0 deletions Sources/Hedera/Account/AccountId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public struct AccountId: Sendable, EntityId, ValidateChecksums {
return "\(shard).\(realm).\(alias)"
}

if let evmAddress = evmAddress {
return String(describing: evmAddress)
}

return helper.description
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/Hedera/Contract/ContractId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public struct ContractId: EntityId {
guard let evmAddress = evmAddress else {
return helper.description
}
return "\(shard).\(realm).\(evmAddress)"

return "\(shard).\(realm).\(try! EvmAddress(evmAddress))"
}

public func toStringWithChecksum(_ client: Client) throws -> String {
Expand Down
157 changes: 157 additions & 0 deletions Tests/HederaTests/AccountIdTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import SnapshotTesting
import XCTest

@testable import Hedera

internal final class AccountIdTests: XCTestCase {
internal func testParse() {
XCTAssertEqual(try AccountId.fromString("0.0.1001"), AccountId(num: 1001))
}

internal func testToFromBytesRoundtrip() {
let accountId = AccountId(num: 1001)

XCTAssertEqual(accountId, try AccountId.fromBytes(accountId.toBytes()))
}

internal func testFromEvmAddressString() {
XCTAssertEqual(
AccountId(evmAddress: "0x302a300506032b6570032100114e6abc371b82da"),
try AccountId.fromString("0x302a300506032b6570032100114e6abc371b82da")
)
}

internal func testToEvmAddressString() {
XCTAssertEqual(
"0x302a300506032b6570032100114e6abc371b82da",
AccountId(evmAddress: "0x302a300506032b6570032100114e6abc371b82da").toString()
)
}

internal func testGoodChecksumOnMainnet() throws {
let accountId = try AccountId.fromString("0.0.123-vfmkw")
try accountId.validateChecksums(on: .mainnet)
}

internal func testGoodChecksumOnTestnet() throws {
let accountId = try AccountId.fromString("0.0.123-esxsf")
try accountId.validateChecksums(on: .testnet)
}

internal func testGoodChecksumOnPreviewnet() throws {
let accountId = try AccountId.fromString("0.0.123-ogizo")
try accountId.validateChecksums(on: .previewnet)
}

internal func testToStringWithChecksum() {
let client = Client.forTestnet()

XCTAssertEqual(
"0.0.123-esxsf",
try AccountId.fromString("0.0.123").toStringWithChecksum(client)
)
}

internal func testBadChecksumOnPreviewnet() {
let accountId: AccountId = "0.0.123-ntjli"

XCTAssertThrowsError(try accountId.validateChecksums(on: .previewnet))
}

internal func testMalformedIdFails() {
XCTAssertThrowsError(try AccountId.fromString("0.0."))
}

internal func testMalformedChecksum() {
XCTAssertThrowsError(try AccountId.fromString("0.0.123-ntjl"))
}

internal func testMalformedChecksum2() {
XCTAssertThrowsError(try AccountId.fromString("0.0.123-ntjl1"))
}

internal func testMalformedAlias() {
XCTAssertThrowsError(
try AccountId.fromString(
"0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf777"))
}
internal func testMalformedAlias2() {
XCTAssertThrowsError(
try AccountId.fromString(
"0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf777g"))
}
internal func testMalformedAliasKey3() {
XCTAssertThrowsError(
try AccountId.fromString(
"0.0.303a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777"))
}

internal func testFromStringAliasKey() {
assertSnapshot(
matching: try AccountId.fromString(
"0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777"),
as: .description
)
}

internal func testFromStringEvmAddress() {
assertSnapshot(
matching: try AccountId.fromString("0x302a300506032b6570032100114e6abc371b82da"),
as: .description
)
}

internal func testFromSolidityAddress() {
assertSnapshot(
matching: try AccountId.fromSolidityAddress("000000000000000000000000000000000000138D"),
as: .description
)
}

internal func testFromSolidityAddress0x() {
assertSnapshot(
matching: try AccountId.fromSolidityAddress("0x000000000000000000000000000000000000138D"),
as: .description
)
}

internal func testFromBytes() {
assertSnapshot(
matching: try AccountId.fromBytes(AccountId(num: 5005).toBytes()),
as: .description
)
}

internal func testFromBytesAlias() throws {
let bytes = try AccountId.fromString(
"0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777"
).toBytes()
assertSnapshot(
matching: try AccountId.fromBytes(bytes),
as: .description
)
}

internal func testFromBytesEvmAddress() throws {
let bytes = try AccountId.fromString("0x302a300506032b6570032100114e6abc371b82da").toBytes()

assertSnapshot(
matching: try AccountId.fromBytes(bytes),
as: .description
)
}

internal func testToSolidityAddress() {
assertSnapshot(
matching: try AccountId(num: 5005).toSolidityAddress(),
as: .lines
)
}

internal func testFromEvmAddress() {
assertSnapshot(
matching: try AccountId(evmAddress: .fromString("0x302a300506032b6570032100114e6abc371b82da")),
as: .description
)
}
}
64 changes: 64 additions & 0 deletions Tests/HederaTests/ContractIdTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import SnapshotTesting
import XCTest

@testable import Hedera

internal final class ContractIdTests: XCTestCase {
internal func testParse() {
assertSnapshot(matching: try ContractId.fromString("0.0.5005"), as: .description)
}

internal func testFromSolidityAddress() {
assertSnapshot(
matching: try ContractId.fromSolidityAddress("000000000000000000000000000000000000138D"),
as: .description
)
}

internal func testFromSolidityAddress0x() {
assertSnapshot(
matching: try ContractId.fromSolidityAddress("0x000000000000000000000000000000000000138D"),
as: .description
)
}

internal func testFromEvmAddress() {
assertSnapshot(
matching: try ContractId.fromEvmAddress(1, 2, "000000000000000000000000000000000000138D"),
as: .description
)
}

internal func testFromEvmAddress0x() {
assertSnapshot(
matching: try ContractId.fromEvmAddress(1, 2, "0x000000000000000000000000000000000000138D"),
as: .description
)
}

internal func testParseEvmAddress() {
assertSnapshot(
matching: try ContractId.fromString("1.2.98329e006610472e6b372c080833f6d79ed833cf"),
as: .description
)
}

internal func testToFromBytes() {
let a: ContractId = "1.2.3"
XCTAssertEqual(a, try .fromBytes(a.toBytes()))
let b: ContractId = "1.2.0x98329e006610472e6B372C080833f6D79ED833cf"
XCTAssertEqual(b, try .fromBytes(b.toBytes()))
}

internal func testToSolidityAddress() {
assertSnapshot(matching: try ContractId(num: 5005).toSolidityAddress(), as: .lines)
}

internal func testToSolidityAddress2() {
assertSnapshot(
matching: try ContractId.fromEvmAddress(1, 2, "0x98329e006610472e6B372C080833f6D79ED833cf")
.toSolidityAddress(),
as: .lines
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.5005
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x302a300506032b6570032100114e6abc371b82da
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x302a300506032b6570032100114e6abc371b82da
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.5005
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.5005
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x302a300506032b6570032100114e6abc371b82da
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
000000000000000000000000000000000000138d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.2.0x000000000000000000000000000000000000138d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.2.0x000000000000000000000000000000000000138d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.5005
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.5005
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.5005
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.2.0x98329e006610472e6b372c080833f6d79ed833cf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
000000000000000000000000000000000000138d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
98329e006610472e6b372c080833f6d79ed833cf

0 comments on commit 3772ea9

Please sign in to comment.