-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test cases for LosslessValue Bool Int value (#28)
* Test cases for LosslessValue Bool Int value * DefaultFalse solution * Add DefaultTrue case * Add small comment * Intercept boolean numbers in LosslessValue * Clean up LosslessValue test cases * Add documentation to BoolCodableStrategy decoding * Remove comment * Fix incorrect documentation * Add String misalignment test case * Add test cases for DefaultTrue * Change DefaultFalse and DefaultTrue test names * Add invalid value test case for DefaultFalse * Add LosslessValue test for Bool edge case Co-authored-by: Mark Sands <[email protected]>
- Loading branch information
Showing
7 changed files
with
161 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
public struct DefaultTrueStrategy: BoolCodableStrategy { | ||
public static var defaultValue: Bool { return true } | ||
} | ||
|
||
/// Decodes Bools defaulting to `true` if applicable | ||
/// | ||
/// `@DefaultTrue` decodes Bools and defaults the value to true if the Decoder is unable to decode the value. | ||
public typealias DefaultTrue = DefaultCodable<DefaultTrueStrategy> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import XCTest | ||
@testable import BetterCodable | ||
|
||
class DefaultTrueTests: XCTestCase { | ||
struct Fixture: Equatable, Codable { | ||
@DefaultTrue var truthy: Bool | ||
} | ||
|
||
func testDecodingFailableArrayDefaultsToFalse() throws { | ||
let jsonData = #"{ "truthy": null }"#.data(using: .utf8)! | ||
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData) | ||
XCTAssertEqual(fixture.truthy, true) | ||
} | ||
|
||
func testDecodingKeyNotPresentDefaultsToFalse() throws { | ||
let jsonData = #"{}"#.data(using: .utf8)! | ||
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData) | ||
XCTAssertEqual(fixture.truthy, true) | ||
} | ||
|
||
func testEncodingDecodedFailableArrayDefaultsToFalse() throws { | ||
let jsonData = #"{ "truthy": null }"#.data(using: .utf8)! | ||
var _fixture = try JSONDecoder().decode(Fixture.self, from: jsonData) | ||
|
||
_fixture.truthy = false | ||
|
||
let fixtureData = try JSONEncoder().encode(_fixture) | ||
let fixture = try JSONDecoder().decode(Fixture.self, from: fixtureData) | ||
XCTAssertEqual(fixture.truthy, false) | ||
} | ||
|
||
func testEncodingDecodedFulfillableBoolRetainsValue() throws { | ||
let jsonData = #"{ "truthy": true }"#.data(using: .utf8)! | ||
let _fixture = try JSONDecoder().decode(Fixture.self, from: jsonData) | ||
let fixtureData = try JSONEncoder().encode(_fixture) | ||
let fixture = try JSONDecoder().decode(Fixture.self, from: fixtureData) | ||
|
||
XCTAssertEqual(fixture.truthy, true) | ||
} | ||
|
||
func testDecodingMisalignedBoolIntValueDecodesCorrectBoolValue() throws { | ||
let jsonData = #"{ "truthy": 1 }"#.data(using: .utf8)! | ||
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData) | ||
XCTAssertEqual(fixture.truthy, true) | ||
|
||
let jsonData2 = #"{ "truthy": 0 }"#.data(using: .utf8)! | ||
let fixture2 = try JSONDecoder().decode(Fixture.self, from: jsonData2) | ||
XCTAssertEqual(fixture2.truthy, false) | ||
} | ||
|
||
func testDecodingInvalidValueDecodesToDefaultValue() throws { | ||
let jsonData = #"{ "truthy": "invalidValue" }"#.data(using: .utf8)! | ||
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData) | ||
XCTAssertEqual( | ||
fixture.truthy, | ||
true, | ||
"Should fall in to the else block and return default value" | ||
) | ||
} | ||
|
||
func testDecodingMisalignedBoolStringValueDecodesCorrectBoolValue() throws { | ||
let jsonData = #"{ "truthy": "true" }"#.data(using: .utf8)! | ||
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData) | ||
XCTAssertEqual(fixture.truthy, true) | ||
|
||
let jsonData2 = #"{ "truthy": "false" }"#.data(using: .utf8)! | ||
let fixture2 = try JSONDecoder().decode(Fixture.self, from: jsonData2) | ||
XCTAssertEqual(fixture2.truthy, false) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters