Skip to content

Commit

Permalink
Merge pull request #38 from pacu/bugfixes
Browse files Browse the repository at this point in the history
Bugfixes
  • Loading branch information
pacu authored Mar 4, 2024
2 parents fa082f7 + a10af35 commit a80dead
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 37 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# Unreleased
## Added
- Dependency: `swift-custom-dump` to diff assertions in tests.

## Modified
- Bugfix: Fix bug where multiple recipient payments are parsed in different order every time [#37]

## [0.1.0-beta.1] - 2024-01-01
- Fixed [problem with literal Decimals](https://github.com/pacu/zcash-swift-payment-uri/issues/35)
- Always favor using `BigDecimal` to avoid misrepresentations of Decimal from
Expand Down
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
"version" : "1.0.0"
}
},
{
"identity" : "swift-custom-dump",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-custom-dump",
"state" : {
"revision" : "3ce83179e5f0c83ad54c305779c6b438e82aaf1d",
"version" : "1.2.1"
}
},
{
"identity" : "swift-parsing",
"kind" : "remoteSourceControl",
Expand Down
21 changes: 15 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ let dependencies: [Package.Dependency] = [
.package(url: "https://github.com/realm/SwiftLint.git", from: "0.54.0"),
.package(url: "https://github.com/pointfreeco/swift-parsing", from: "0.13.0"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", exact: Version(stringLiteral: "1.0.0")),
.package(url: "https://github.com/mgriebling/BigDecimal.git", from: "2.0.0")
.package(url: "https://github.com/mgriebling/BigDecimal.git", from: "2.0.0"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.0.0")
]

let targets: [Target] = [
Expand All @@ -18,18 +19,23 @@ let targets: [Target] = [
dependencies: [
.product(name: "Parsing", package: "swift-parsing"),
.product(name: "BigDecimal", package: "BigDecimal"),
.product(name: "CustomDump", package: "swift-custom-dump")
],
plugins: [.plugin(name: "SwiftLintPlugin", package: "SwiftLint")]),
plugins: [.plugin(name: "SwiftLintPlugin", package: "SwiftLint")]
),
.testTarget(
name: "zcash-swift-payment-uriTests",
dependencies: ["zcash-swift-payment-uri"]
dependencies: [
"zcash-swift-payment-uri"
]
),
]
#else // linux and others
let dependencies: [Package.Dependency] = [
.package(url: "https://github.com/pointfreeco/swift-parsing", from: "0.13.0"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", exact: Version(stringLiteral: "1.0.0")),
.package(url: "https://github.com/mgriebling/BigDecimal.git", from: "2.0.0")
.package(url: "https://github.com/mgriebling/BigDecimal.git", from: "2.0.0"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.0.0")
]

let targets: [Target] = [
Expand All @@ -44,7 +50,9 @@ let targets: [Target] = [
),
.testTarget(
name: "zcash-swift-payment-uriTests",
dependencies: ["zcash-swift-payment-uri"]
dependencies: [
"zcash-swift-payment-uri"
]
),
]
#endif
Expand All @@ -59,7 +67,8 @@ let package = Package(
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "zcash-swift-payment-uri",
targets: ["zcash-swift-payment-uri"]),
targets: ["zcash-swift-payment-uri"]
),
],
dependencies: dependencies,
targets: targets
Expand Down
2 changes: 1 addition & 1 deletion Sources/zcash-swift-payment-uri/Amount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public struct Amount: Equatable {

guard decimal <= Self.maxSupply.asDecimal() else { throw AmountError.greaterThanSupply }

self.value = BigDecimal(decimal).round(Self.zecRounding)
self.value = BigDecimal(decimal).round(Self.zecRounding).trim
}

public init(string: String) throws {
Expand Down
14 changes: 12 additions & 2 deletions Sources/zcash-swift-payment-uri/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,19 @@ enum Parser {
}
}

return try paramsByIndex.map {
try Payment.uniqueIndexedParameters(index: $0, parameters: $1)
var payments: [Payment] = []

try paramsByIndex.keys.sorted().forEach { index in
guard let params = paramsByIndex[index] else {
throw ZIP321.Errors.invalidParamIndex(index.description)
}

payments.append(
try Payment.uniqueIndexedParameters(index: index, parameters: params)
)
}

return payments
}
}

Expand Down
29 changes: 15 additions & 14 deletions Tests/zcash-swift-payment-uriTests/ParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import XCTest
import Parsing
import CustomDump
@testable import zcash_swift_payment_uri

final class ParsingTests: XCTestCase {
Expand Down Expand Up @@ -312,7 +313,7 @@ final class ParsingTests: XCTestCase {

XCTAssertEqual(partial.0, "")

XCTAssertEqual(
XCTAssertNoDifference(
partial.1, "?address.1=ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez&amount.1=1.0001&message.1=lunch")

}
Expand Down Expand Up @@ -379,7 +380,7 @@ final class ParsingTests: XCTestCase {

let result = try Parser.leadingAddress(validAddressURI, validating: Parser.onlyCharsetValidation)

XCTAssertEqual(result.1, expected)
XCTAssertNoDifference(result.1, expected)
XCTAssertEqual(result.0, nil)
}

Expand Down Expand Up @@ -459,7 +460,7 @@ final class ParsingTests: XCTestCase {
return
}

XCTAssertEqual(
XCTAssertNoDifference(
try Param.from(
queryKey: "address",
value: "tmEZhbWHTpdKMw5it8YDspUXSMGQyFwovpU",
Expand All @@ -474,7 +475,7 @@ final class ParsingTests: XCTestCase {
return
}

XCTAssertEqual(
XCTAssertNoDifference(
try Param.from(
queryKey: "address",
value: "u1fl5mprj0t9p4jg92hjjy8q5myvwc60c9wv0xachauqpn3c3k4xwzlaueafq27dcg7tzzzaz5jl8tyj93wgs983y0jq0qfhzu6n4r8rakpv5f4gg2lrw4z6pyqqcrcqx04d38yunc6je",
Expand All @@ -491,7 +492,7 @@ final class ParsingTests: XCTestCase {
return
}

XCTAssertEqual(
XCTAssertNoDifference(
try Param.from(
queryKey: "address",
value: "ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez",
Expand Down Expand Up @@ -520,23 +521,23 @@ final class ParsingTests: XCTestCase {
func testCharsetValidationPassesOnValidTransparentAddress() throws {
let address = try Parser.transparentEncodingCharsetParser
.parse("tmEZhbWHTpdKMw5it8YDspUXSMGQyFwovpU")
XCTAssertEqual("tmEZhbWHTpdKMw5it8YDspUXSMGQyFwovpU", address)
XCTAssertNoDifference("tmEZhbWHTpdKMw5it8YDspUXSMGQyFwovpU", address)
}

func testCharsetValidationPassesOnValidSaplingAddress() throws {
let expected = "ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez"
let address = try Parser.saplingEncodingCharsetParser
.parse(expected)

XCTAssertEqual("0yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez", address)
XCTAssertNoDifference("0yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez", address)
}

func testCharsetValidationPassesOnValidUnifiedAddress() throws {
let expected = "u1fl5mprj0t9p4jg92hjjy8q5myvwc60c9wv0xachauqpn3c3k4xwzlaueafq27dcg7tzzzaz5jl8tyj93wgs983y0jq0qfhzu6n4r8rakpv5f4gg2lrw4z6pyqqcrcqx04d38yunc6je"
let address = try Parser.unifiedEncodingCharsetParser
.parse(expected)

XCTAssertEqual("fl5mprj0t9p4jg92hjjy8q5myvwc60c9wv0xachauqpn3c3k4xwzlaueafq27dcg7tzzzaz5jl8tyj93wgs983y0jq0qfhzu6n4r8rakpv5f4gg2lrw4z6pyqqcrcqx04d38yunc6je", address)
XCTAssertNoDifference("fl5mprj0t9p4jg92hjjy8q5myvwc60c9wv0xachauqpn3c3k4xwzlaueafq27dcg7tzzzaz5jl8tyj93wgs983y0jq0qfhzu6n4r8rakpv5f4gg2lrw4z6pyqqcrcqx04d38yunc6je", address)
}

func testZcashParameterCreatesValidAddress() throws {
Expand All @@ -548,7 +549,7 @@ final class ParsingTests: XCTestCase {
return
}

XCTAssertEqual(
XCTAssertNoDifference(
IndexedParameter(index: 0, param: .address(recipient)),
try Parser.zcashParameter((query, nil, value), validating: Parser.onlyCharsetValidation)
)
Expand All @@ -559,7 +560,7 @@ final class ParsingTests: XCTestCase {
let query = "amount"[...]
let value = "1.00020112"[...]

XCTAssertEqual(
XCTAssertNoDifference(
IndexedParameter(index: 0, param: .amount(try Amount(string: String(value)))),
try Parser.zcashParameter((query, nil, value), validating: Parser.onlyCharsetValidation)
)
Expand All @@ -575,7 +576,7 @@ final class ParsingTests: XCTestCase {
return
}

XCTAssertEqual(
XCTAssertNoDifference(
IndexedParameter(index: UInt(index), param: .message(qcharDecodedValue)),
try Parser.zcashParameter((query, index, value), validating: Parser.onlyCharsetValidation)
)
Expand All @@ -591,7 +592,7 @@ final class ParsingTests: XCTestCase {
return
}

XCTAssertEqual(
XCTAssertNoDifference(
IndexedParameter(index: UInt(index), param: .label(qcharDecodedValue)),
try Parser.zcashParameter((query, index, value), validating: Parser.onlyCharsetValidation)
)
Expand All @@ -602,7 +603,7 @@ final class ParsingTests: XCTestCase {
let index = 99
let value = "VGhpcyBpcyBhIHNpbXBsZSBtZW1vLg"[...]

XCTAssertEqual(
XCTAssertNoDifference(
IndexedParameter(index: UInt(index), param: .memo(try MemoBytes(base64URL: "VGhpcyBpcyBhIHNpbXBsZSBtZW1vLg"))),
try Parser.zcashParameter((query, index, value), validating: Parser.onlyCharsetValidation)
)
Expand All @@ -613,7 +614,7 @@ final class ParsingTests: XCTestCase {
let index = 99
let value = "VGhpcyBpcyBhIHNpbXBsZSBtZW1vLg"[...]

XCTAssertEqual(
XCTAssertNoDifference(
IndexedParameter(index: UInt(index), param: .other(String(query), String(value))),
try Parser.zcashParameter((query, index, value), validating: Parser.onlyCharsetValidation)
)
Expand Down
9 changes: 5 additions & 4 deletions Tests/zcash-swift-payment-uriTests/RendererTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import XCTest
import CustomDump
@testable import zcash_swift_payment_uri
final class RendererTests: XCTestCase {
func testAmountRendersNoParamIndex() throws {
Expand Down Expand Up @@ -121,7 +122,7 @@ final class RendererTests: XCTestCase {
otherParams: nil
)

XCTAssertEqual(Render.payment(payment0, index: nil), expected)
XCTAssertNoDifference(Render.payment(payment0, index: nil), expected)
}

func testPaymentRendersWithParamIndex() throws {
Expand All @@ -144,7 +145,7 @@ final class RendererTests: XCTestCase {
otherParams: nil
)

XCTAssertEqual(Render.payment(payment1, index: 1), expected)
XCTAssertNoDifference(Render.payment(payment1, index: 1), expected)
}

func testPaymentRendersWithNoParamIndexAndNoAddressLabel() throws {
Expand All @@ -166,7 +167,7 @@ final class RendererTests: XCTestCase {
otherParams: nil
)

XCTAssertEqual(Render.payment(payment0, index: nil, omittingAddressLabel: true), expected)
XCTAssertNoDifference(Render.payment(payment0, index: nil, omittingAddressLabel: true), expected)
}

func testPaymentRendererIgnoresLabelOmissionWhenIndexIsProvided() throws {
Expand All @@ -189,6 +190,6 @@ final class RendererTests: XCTestCase {
otherParams: nil
)

XCTAssertEqual(Render.payment(payment1, index: 1, omittingAddressLabel: true), expected)
XCTAssertNoDifference(Render.payment(payment1, index: 1, omittingAddressLabel: true), expected)
}
}
17 changes: 7 additions & 10 deletions Tests/zcash-swift-payment-uriTests/ZIP321Tests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import XCTest
import CustomDump
@testable import zcash_swift_payment_uri
// swiftlint:disable line_length
final class ZcashSwiftPaymentUriTests: XCTestCase {
Expand All @@ -10,7 +11,7 @@ final class ZcashSwiftPaymentUriTests: XCTestCase {
return
}

XCTAssertEqual(
XCTAssertNoDifference(
ZIP321.request(recipient),
"zcash:ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez"
)
Expand All @@ -35,18 +36,18 @@ final class ZcashSwiftPaymentUriTests: XCTestCase {
otherParams: nil
)

XCTAssertEqual(
XCTAssertNoDifference(
ZIP321.uriString(
from: PaymentRequest(payments: [payment]),
formattingOptions: .useEmptyParamIndex(omitAddressLabel: true)
),
expected
)

XCTAssertEqual(ZIP321.request(payment, formattingOptions: .useEmptyParamIndex(omitAddressLabel: true)), expected)
XCTAssertNoDifference(ZIP321.request(payment, formattingOptions: .useEmptyParamIndex(omitAddressLabel: true)), expected)

// Roundtrip test
XCTAssertEqual(
XCTAssertNoDifference(
try ZIP321.request(from: expected, validatingRecipients: nil),
ParserResult.request(PaymentRequest(payments: [payment]))
)
Expand Down Expand Up @@ -89,7 +90,7 @@ final class ZcashSwiftPaymentUriTests: XCTestCase {

let paymentRequest = PaymentRequest(payments: [payment0, payment1])

XCTAssertEqual(ZIP321.uriString(from: paymentRequest, formattingOptions: .useEmptyParamIndex(omitAddressLabel: false)), expected)
XCTAssertNoDifference(ZIP321.uriString(from: paymentRequest, formattingOptions: .useEmptyParamIndex(omitAddressLabel: false)), expected)
}

func testParsingMultiplePaymentsRequestStartingWithNoParamIndex() throws {
Expand Down Expand Up @@ -131,10 +132,6 @@ final class ZcashSwiftPaymentUriTests: XCTestCase {

let result = try ZIP321.request(from: uriString)


XCTAssertEqual(
result,
ParserResult.request(paymentRequest)
)
XCTAssertNoDifference(result, ParserResult.request(paymentRequest))
}
}

0 comments on commit a80dead

Please sign in to comment.