-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow
encode<T: Encodable>(_ value: T)
in JSONSingleValueEncodingCo…
…ntainer fixes #19
- Loading branch information
1 parent
f5a249d
commit ca81eae
Showing
2 changed files
with
45 additions
and
1 deletion.
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
35 changes: 35 additions & 0 deletions
35
Tests/JSONCodingTests/Encoding/JSONSingleValueEncodingContainerTests.swift
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,35 @@ | ||
import XCTest | ||
@testable import PureSwiftJSONCoding | ||
@testable import PureSwiftJSONParsing | ||
|
||
class JSONSingleValueEncodingContainerTests: XCTestCase { | ||
|
||
func testEncodeEncodable() { | ||
struct Name: Encodable { | ||
var firstName: String | ||
var surname: String | ||
} | ||
|
||
struct Object: Encodable { | ||
var name: Name | ||
|
||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
try container.encode(name) | ||
} | ||
} | ||
|
||
do { | ||
let object = Object(name: Name(firstName: "Adam", surname: "Fowler")) | ||
let json = try PureSwiftJSONCoding.JSONEncoder().encode(object) | ||
|
||
let parsed = try JSONParser().parse(bytes: json) | ||
XCTAssertEqual(parsed, .object(["firstName": .string("Adam"), "surname": .string("Fowler")])) | ||
} | ||
catch { | ||
XCTFail("Unexpected error: \(error)") | ||
} | ||
} | ||
|
||
} | ||
|