Skip to content

Commit

Permalink
增加 DecimalTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Darktt committed Nov 27, 2023
1 parent 25b73a6 commit 378a32b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
4 changes: 4 additions & 0 deletions JsonDecodeProtection.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
552C7BE5293A1E3E00B4B5EE /* MissingKeyProtectionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 552C7BE4293A1E3E00B4B5EE /* MissingKeyProtectionTest.swift */; };
559545A7293E12140060F43D /* ObjectProtectionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 559545A6293E12140060F43D /* ObjectProtectionTest.swift */; };
55A1DAA22940AF3A00244FE2 /* NumberProtectionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55A1DAA12940AF3A00244FE2 /* NumberProtectionTest.swift */; };
55F3EC822AD97D1800BAEABB /* DecimalTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55F3EC812AD97D1800BAEABB /* DecimalTest.swift */; };
F22A05A729E40BFD003D30C1 /* DTAES.swift in Sources */ = {isa = PBXBuildFile; fileRef = F22A05A629E40BFD003D30C1 /* DTAES.swift */; };
F2432DE32939D8F400B1E010 /* BoolProtectionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5515DD71293781F300F895E6 /* BoolProtectionTest.swift */; };
F257F85B294C5CAA00FED754 /* NumbersProtectionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = F257F85A294C5CAA00FED754 /* NumbersProtectionTest.swift */; };
Expand Down Expand Up @@ -66,6 +67,7 @@
552C7BE4293A1E3E00B4B5EE /* MissingKeyProtectionTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MissingKeyProtectionTest.swift; sourceTree = "<group>"; };
559545A6293E12140060F43D /* ObjectProtectionTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectProtectionTest.swift; sourceTree = "<group>"; };
55A1DAA12940AF3A00244FE2 /* NumberProtectionTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NumberProtectionTest.swift; sourceTree = "<group>"; };
55F3EC812AD97D1800BAEABB /* DecimalTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecimalTest.swift; sourceTree = "<group>"; };
F20F219B29F0C81000A1B6F1 /* Package.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; };
F22A05A629E40BFD003D30C1 /* DTAES.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DTAES.swift; sourceTree = "<group>"; };
F2432DDA2939D5D100B1E010 /* JsonDecodeProtectionTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = JsonDecodeProtectionTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -137,6 +139,7 @@
F297055F29E3EF11003CF4B2 /* AESDecoderTest.swift */,
5515DD71293781F300F895E6 /* BoolProtectionTest.swift */,
F2E188552AAAAF8C00E7F0B5 /* DateProtectionTest.swift */,
55F3EC812AD97D1800BAEABB /* DecimalTest.swift */,
552C7BE4293A1E3E00B4B5EE /* MissingKeyProtectionTest.swift */,
55A1DAA12940AF3A00244FE2 /* NumberProtectionTest.swift */,
F257F85A294C5CAA00FED754 /* NumbersProtectionTest.swift */,
Expand Down Expand Up @@ -301,6 +304,7 @@
559545A7293E12140060F43D /* ObjectProtectionTest.swift in Sources */,
F257F85B294C5CAA00FED754 /* NumbersProtectionTest.swift in Sources */,
55A1DAA22940AF3A00244FE2 /* NumberProtectionTest.swift in Sources */,
55F3EC822AD97D1800BAEABB /* DecimalTest.swift in Sources */,
F2432DE32939D8F400B1E010 /* BoolProtectionTest.swift in Sources */,
552C7BE5293A1E3E00B4B5EE /* MissingKeyProtectionTest.swift in Sources */,
);
Expand Down
58 changes: 58 additions & 0 deletions JsonDecodeProtectionTests/DecimalTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// DecimalTest.swift
// JsonDecodeProtectionTests
//
// Created by Darktt on 2023/10/13.
//

import XCTest
@testable
import JsonDecodeProtection

struct TestObject: Decodable
{
@NumberProtection
var value: Decimal?
}

final class DecimalTest: XCTestCase
{
var jsonString: String!

var formatter: NumberFormatter = NumberFormatter()

override func setUpWithError() throws
{
// Put setup code here. This method is called before the invocation of each test method in the class.

// Arrange

// Act

// Assert
}

func testFloat0_0001AndRoundDown() throws
{
self.jsonString = """
{
"value": 0.001
}
"""

let jsonData: Data = self.jsonString.data(using: .utf8)!
let jsonDecoder = JSONDecoder()

// Act
let object = try jsonDecoder.decode(TestObject.self, from: jsonData)
let valueNumber = NSDecimalNumber(decimal: object.value ?? .zero)
self.formatter.minimumFractionDigits = 3
self.formatter.maximumFractionDigits = 9
self.formatter.roundingMode = .down

// Assert
let result: String = self.formatter.string(from: valueNumber)!

XCTAssertTrue(result == "0.001")
}
}
7 changes: 5 additions & 2 deletions SourceCode/JsonProtection/NumberProtection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ extension Decimal: NumberType
public
init?<T>(_ source: T) where T : BinaryInteger
{
let value = String(source)
guard let value = source as? Int else {

return nil
}

self.init(string: value)
self.init(string: "\(value)")
}

public
Expand Down

0 comments on commit 378a32b

Please sign in to comment.