Skip to content

Commit

Permalink
Change amount and subunitToUnit to BigInt (#61)
Browse files Browse the repository at this point in the history
* Add BigInt dependency

* Add tests / Handle encoding and decoding

* Update readme

* Change subunitToUnit to BigInt

* tmp pod update for travis

* Update the way to parse decimal (temporary bug workaround)

* Update codable

* Fix wrong target name
  • Loading branch information
mederic-p committed Jun 22, 2018
1 parent e56669e commit b38788b
Show file tree
Hide file tree
Showing 28 changed files with 676 additions and 233 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
@@ -1,5 +1,7 @@
language: swift
osx_image: xcode9.3
before_install:
- pod update
script:
set -o pipefail &&
xcodebuild -workspace OmiseGO.xcworkspace -scheme "OmiseGO" -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 8" test | xcpretty &&
Expand Down
1 change: 1 addition & 0 deletions OmiseGO.podspec
Expand Up @@ -12,6 +12,7 @@ Pod::Spec.new do |s|
s.swift_version = '4.0'

s.dependency 'Starscream', '~> 3.0'
s.dependency 'BigInt', '~> 3.1'

s.source_files = 'Source/**/*.swift'
end
20 changes: 20 additions & 0 deletions OmiseGO.xcodeproj/project.pbxproj
Expand Up @@ -17,6 +17,7 @@
031443021F8CAD56003DD5BE /* ResponseFixtureTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031443011F8CAD56003DD5BE /* ResponseFixtureTest.swift */; };
031863FD1F8F678300D13862 /* SettingFixtureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031863FC1F8F678300D13862 /* SettingFixtureTests.swift */; };
031BB1101F9861AD0095468F /* FixtureTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031BB10E1F9861AD0095468F /* FixtureTestCase.swift */; };
031DEFC920D3B2B5006CBB21 /* OMGNumberFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031DEFC720D3B2B0006CBB21 /* OMGNumberFormatter.swift */; };
031EA6842071DD8C00A34D16 /* DummyObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031EA6832071DD8B00A34D16 /* DummyObject.swift */; };
031EA6902071DE8000A34D16 /* APIEndpointTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031EA6862071DE7F00A34D16 /* APIEndpointTest.swift */; };
031EA6912071DE8000A34D16 /* RequestTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031EA6872071DE7F00A34D16 /* RequestTest.swift */; };
Expand All @@ -37,6 +38,8 @@
031EA6AE2071DEC800A34D16 /* QRScannerViewControllerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031EA6A32071DEC800A34D16 /* QRScannerViewControllerTest.swift */; };
031EA6AF2071DEC800A34D16 /* QRReaderTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031EA6A42071DEC800A34D16 /* QRReaderTest.swift */; };
031EA6B12071DF6500A34D16 /* LiveTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031EA6B02071DF6500A34D16 /* LiveTestCase.swift */; };
03317D7A20D77BB400BA60DB /* Codable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03317D7920D77BB400BA60DB /* Codable.swift */; };
03317D7C20D77D7400BA60DB /* BigUIntDummy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03317D7B20D77D7400BA60DB /* BigUIntDummy.swift */; };
0334869E2036C7EC0013B37A /* StubGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0334869D2036C7EC0013B37A /* StubGenerator.swift */; };
033522301FB5A5BF00BCBD11 /* WalletLiveTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0335222F1FB5A5BF00BCBD11 /* WalletLiveTests.swift */; };
033522321FB5A7D800BCBD11 /* SettingLiveTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 033522311FB5A7D800BCBD11 /* SettingLiveTests.swift */; };
Expand Down Expand Up @@ -96,6 +99,7 @@
039DA975203FA94100CCC56A /* PaginationParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 039DA974203FA94100CCC56A /* PaginationParams.swift */; };
039DA977203FAB0600CCC56A /* TransactionListParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 039DA976203FAB0600CCC56A /* TransactionListParams.swift */; };
039DA979203FB53F00CCC56A /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 039DA978203FB53F00CCC56A /* Pagination.swift */; };
03AA99AB20D7AFE100AFF1F8 /* OMGNumberFormatterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03AA99AA20D7AFE100AFF1F8 /* OMGNumberFormatterTests.swift */; };
03B1E7392040127600134CB4 /* DateDummy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03B1E7382040127600134CB4 /* DateDummy.swift */; };
03BCCCB31F8B311600F604DB /* OmiseGO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03BCCCA91F8B311600F604DB /* OmiseGO.framework */; };
03C2C07D1FB97F9C00B794AC /* RequestFixtureTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03C2C07C1FB97F9C00B794AC /* RequestFixtureTest.swift */; };
Expand Down Expand Up @@ -147,6 +151,7 @@
031443011F8CAD56003DD5BE /* ResponseFixtureTest.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = ResponseFixtureTest.swift; sourceTree = "<group>"; tabWidth = 4; };
031863FC1F8F678300D13862 /* SettingFixtureTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingFixtureTests.swift; sourceTree = "<group>"; };
031BB10E1F9861AD0095468F /* FixtureTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FixtureTestCase.swift; sourceTree = "<group>"; };
031DEFC720D3B2B0006CBB21 /* OMGNumberFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OMGNumberFormatter.swift; sourceTree = "<group>"; };
031EA6832071DD8B00A34D16 /* DummyObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DummyObject.swift; sourceTree = "<group>"; };
031EA6862071DE7F00A34D16 /* APIEndpointTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIEndpointTest.swift; sourceTree = "<group>"; };
031EA6872071DE7F00A34D16 /* RequestTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestTest.swift; sourceTree = "<group>"; };
Expand All @@ -167,6 +172,8 @@
031EA6A32071DEC800A34D16 /* QRScannerViewControllerTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QRScannerViewControllerTest.swift; sourceTree = "<group>"; };
031EA6A42071DEC800A34D16 /* QRReaderTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QRReaderTest.swift; sourceTree = "<group>"; };
031EA6B02071DF6500A34D16 /* LiveTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LiveTestCase.swift; sourceTree = "<group>"; };
03317D7920D77BB400BA60DB /* Codable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Codable.swift; sourceTree = "<group>"; };
03317D7B20D77D7400BA60DB /* BigUIntDummy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BigUIntDummy.swift; sourceTree = "<group>"; };
0334869D2036C7EC0013B37A /* StubGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StubGenerator.swift; sourceTree = "<group>"; };
0335222F1FB5A5BF00BCBD11 /* WalletLiveTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletLiveTests.swift; sourceTree = "<group>"; };
033522311FB5A7D800BCBD11 /* SettingLiveTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingLiveTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -227,6 +234,7 @@
039DA974203FA94100CCC56A /* PaginationParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationParams.swift; sourceTree = "<group>"; };
039DA976203FAB0600CCC56A /* TransactionListParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionListParams.swift; sourceTree = "<group>"; };
039DA978203FB53F00CCC56A /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = "<group>"; };
03AA99AA20D7AFE100AFF1F8 /* OMGNumberFormatterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OMGNumberFormatterTests.swift; sourceTree = "<group>"; };
03B1E7382040127600134CB4 /* DateDummy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateDummy.swift; sourceTree = "<group>"; };
03BCCCA91F8B311600F604DB /* OmiseGO.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OmiseGO.framework; sourceTree = BUILT_PRODUCTS_DIR; };
03BCCCB21F8B311600F604DB /* OmiseGOTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = OmiseGOTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -458,6 +466,8 @@
isa = PBXGroup;
children = (
037032BB1FBBE7A300DBCDDD /* Tool.swift */,
03317D7920D77BB400BA60DB /* Codable.swift */,
031DEFC720D3B2B0006CBB21 /* OMGNumberFormatter.swift */,
);
path = Helpers;
sourceTree = "<group>";
Expand All @@ -475,6 +485,7 @@
03D793B7204FE1D90055D7BC /* ListableDummy.swift */,
03F119CB2063A31E00C1772C /* DummySocketEventDelegate.swift */,
039656A82064D3B20073951A /* DummyWebSocketDelegate.swift */,
03317D7B20D77D7400BA60DB /* BigUIntDummy.swift */,
);
path = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -512,6 +523,7 @@
031EA6892071DE7F00A34D16 /* CodingTests */,
031EA6852071DE7F00A34D16 /* HTTPTests */,
03D793B3204FA2490055D7BC /* ToolsTests.swift */,
03AA99AA20D7AFE100AFF1F8 /* OMGNumberFormatterTests.swift */,
03BCCCB91F8B311600F604DB /* Info.plist */,
0370519D2040756F009FD899 /* secret.plist */,
);
Expand Down Expand Up @@ -722,10 +734,14 @@
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-OmiseGOTests/Pods-OmiseGOTests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/BigInt/BigInt.framework",
"${BUILT_PRODUCTS_DIR}/SipHash/SipHash.framework",
"${BUILT_PRODUCTS_DIR}/Starscream/Starscream.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/BigInt.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SipHash.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Starscream.framework",
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -762,6 +778,7 @@
0313D6DE203176AC003E99BA /* QRGenerator.swift in Sources */,
037032A71FBBE6B100DBCDDD /* JSONListResponse.swift in Sources */,
037032AC1FBBE6B100DBCDDD /* Reponse.swift in Sources */,
031DEFC920D3B2B5006CBB21 /* OMGNumberFormatter.swift in Sources */,
0377EBAF20564E7A0036A9D6 /* SocketMessage.swift in Sources */,
039DA975203FA94100CCC56A /* PaginationParams.swift in Sources */,
036128A22087315D00E13514 /* Avatar.swift in Sources */,
Expand All @@ -779,6 +796,7 @@
037032B01FBBE6B100DBCDDD /* Listable.swift in Sources */,
03E8E6732060C5320036CC9E /* SocketEvent.swift in Sources */,
037032B51FBBE6B100DBCDDD /* APIEndpoint.swift in Sources */,
03317D7A20D77BB400BA60DB /* Codable.swift in Sources */,
1AEE963020A564E600FCC886 /* HTTPTask.swift in Sources */,
039DA973203FA72800CCC56A /* Transaction.swift in Sources */,
0313D6DB20317696003E99BA /* QRScannerLoadingView.swift in Sources */,
Expand Down Expand Up @@ -828,8 +846,10 @@
0396627E203FFD6B004E5640 /* Extension.swift in Sources */,
0345BE291F96FE8C00B4CA16 /* ResponseLiveTest.swift in Sources */,
034FA0441FA31C930033B8D1 /* LogoutFixtureTests.swift in Sources */,
03AA99AB20D7AFE100AFF1F8 /* OMGNumberFormatterTests.swift in Sources */,
031EA6942071DE8000A34D16 /* DecodeTests.swift in Sources */,
217782191FB5579F002CBCA7 /* WalletFixtureTests.swift in Sources */,
03317D7C20D77D7400BA60DB /* BigUIntDummy.swift in Sources */,
03D793B4204FA2490055D7BC /* ToolsTests.swift in Sources */,
031EA6A62071DEC800A34D16 /* FixtureSocketClient.swift in Sources */,
03CEF4C11FB01CB5009B8CEB /* AuthenticationLiveTest.swift in Sources */,
Expand Down
46 changes: 46 additions & 0 deletions OmiseGOTests/CodingTests/DecodeTests.swift
Expand Up @@ -25,6 +25,52 @@ class DecodeTests: XCTestCase {
return try Data(contentsOf: fixtureFileURL)
}

func testSuccessfullyDecodesANumberWithInt32Size() {
do {
let jsonData = try self.jsonData(withFileName: "bigint_int32")
let decodedData = try self.jsonDecoder.decode(BigIntDummy.self, from: jsonData)
XCTAssertEqual(decodedData.value.description, "2147483647")
} catch let thrownError {
XCTFail(thrownError.localizedDescription)
}
}

func testSuccessfullyDecodesANumberWithInt64Size() {
do {
let jsonData = try self.jsonData(withFileName: "bigint_int64")
let decodedData = try self.jsonDecoder.decode(BigIntDummy.self, from: jsonData)
XCTAssertEqual(decodedData.value.description, "922337203685400")
} catch let thrownError {
XCTFail(thrownError.localizedDescription)
}
}

func testSuccessfullyDecodesANumberWith38Digits() {
do {
let jsonData = try self.jsonData(withFileName: "bigint_over_int64")
let decodedData = try self.jsonDecoder.decode(BigIntDummy.self, from: jsonData)
XCTAssertEqual(decodedData.value.description, "99999999999999999999999999999999999998")
} catch let thrownError {
XCTFail(thrownError.localizedDescription)
}
}

func testFailsToDecodeANumberWith39Digits() {
do {
let jsonData = try self.jsonData(withFileName: "bigint_invalid")
XCTAssertThrowsError(try self.jsonDecoder.decode(BigIntDummy.self, from: jsonData), "Failed to decode value", { error -> Void in
switch error {
case DecodingError.dataCorrupted(let context):
XCTAssertEqual(context.debugDescription, "Invalid number")
default:
XCTFail("Should raise a data corrupted error")
}
})
} catch _ {
XCTFail("Should raise a decoding error")
}
}

func testCustomDateDecodingStrategy() {
let jsonDecoder = JSONDecoder()
jsonDecoder.dateDecodingStrategy = .custom({return try dateDecodingStrategy(decoder: $0)})
Expand Down
65 changes: 57 additions & 8 deletions OmiseGOTests/CodingTests/EncodeTests.swift
Expand Up @@ -8,6 +8,7 @@

import XCTest
@testable import OmiseGO
import BigInt

extension String {

Expand Down Expand Up @@ -102,6 +103,57 @@ class EncodeTests: XCTestCase {
""".uglifiedEncodedString())
}

func testBigIntSuccessfullyEncodeWithInt32Size() {
let encodable = BigIntDummy(value: "2147483647")
do {
let encodedData = try self.encoder.encode(encodable)
XCTAssertEqual(String(data: encodedData, encoding: .utf8)!,
"""
{"value": 2147483647}
""".uglifiedEncodedString())
} catch _ {
XCTFail("Should not raise an error")
}
}

func testBigIntSuccessfullyEncodeWithInt64Size() {
let encodable = BigIntDummy(value: "922337203685400")
do {
let encodedData = try self.encoder.encode(encodable)
XCTAssertEqual(String(data: encodedData, encoding: .utf8)!,
"""
{"value": 922337203685400}
""".uglifiedEncodedString())
} catch _ {
XCTFail("Should not raise an error")
}
}

func testBigIntSuccessfullyEncodeWith38Digits() {
let encodable = BigIntDummy(value: "99999999999999999999999999999999999998")
do {
let encodedData = try self.encoder.encode(encodable)
XCTAssertEqual(String(data: encodedData, encoding: .utf8)!,
"""
{"value": 99999999999999999999999999999999999998}
""".uglifiedEncodedString())
} catch _ {
XCTFail("Should not raise an error")
}
}

func testBigIntFailsToEncodeWith39Digits() {
let encodable = BigIntDummy(value: BigInt("999999999999999999999999999999999999991"))
XCTAssertThrowsError(try serialize(encodable), "Failed to encode value", { error -> Void in
switch error {
case EncodingError.invalidValue(_, let context):
XCTAssertEqual(context.debugDescription, "Value is exceeding the maximum encodable number")
default:
XCTFail("Should raise a data corrupted error")
}
})
}

func testInvalidJSONDictionaryEncoding() {
let data = "an encoded string".data(using: .utf8)!
let metadata: [String: Any] = ["invalid_data": data]
Expand All @@ -111,18 +163,15 @@ class EncodeTests: XCTestCase {
optionalMetadataArray: nil,
unavailableMetadata: nil,
unavailableMetadataArray: nil)
do {
_ = try self.encoder.encode(metadataDummy)
} catch let error as EncodingError {
XCTAssertThrowsError(try self.encoder.encode(metadataDummy), "Failed to encode dictionary", { error -> Void in
switch error {
case .invalidValue(let value, let context):
case EncodingError.invalidValue(let value, let context):
XCTAssertEqual(value as? Data, data)
XCTAssertEqual(context.debugDescription, "Invalid JSON value")
default:
XCTFail("Unexpected error")
}

} catch _ {
XCTFail("Unexpected error")
}
})
}

func testInvalidJSONArrayEncoding() {
Expand Down
3 changes: 3 additions & 0 deletions OmiseGOTests/FixtureTests/Fixtures/objects/bigint_int32.json
@@ -0,0 +1,3 @@
{
"value": 2147483647,
}
3 changes: 3 additions & 0 deletions OmiseGOTests/FixtureTests/Fixtures/objects/bigint_int64.json
@@ -0,0 +1,3 @@
{
"value": 922337203685400,
}
@@ -0,0 +1,3 @@
{
"value": 999999999999999999999999999999999999991
}
@@ -0,0 +1,3 @@
{
"value": 99999999999999999999999999999999999998,
}
30 changes: 30 additions & 0 deletions OmiseGOTests/Helpers/BigUIntDummy.swift
@@ -0,0 +1,30 @@
//
// BigIntDummy.swift
// OmiseGOTests
//
// Created by Mederic Petit on 18/6/18.
// Copyright © 2017-2018 Omise Go Pte. Ltd. All rights reserved.
//

@testable import OmiseGO
import BigInt

struct BigIntDummy {
let value: BigInt
}

extension BigIntDummy: Codable {
private enum CodingKeys: String, CodingKey {
case value
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(value, forKey: .value)
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
value = try container.decode(BigInt.self, forKey: .value)
}
}

0 comments on commit b38788b

Please sign in to comment.