-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEncodableValueTests.swift
More file actions
92 lines (85 loc) · 3.19 KB
/
Copy pathEncodableValueTests.swift
File metadata and controls
92 lines (85 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2020-Present Datadog, Inc.
*/
@testable import EventsExporter
import XCTest
class EncodableValueTests: XCTestCase {
func testItEncodesDifferentEncodableValues() throws {
let encoder = JSONEncoder()
XCTAssertEqual(
try encoder.encode(EncodingContainer(EncodableValue("string"))).utf8String,
#"{"value":"string"}"#
)
XCTAssertEqual(
try encoder.encode(EncodingContainer(EncodableValue(123))).utf8String,
#"{"value":123}"#
)
XCTAssertEqual(
try encoder.encode(EncodableValue(["a", "b", "c"])).utf8String,
#"["a","b","c"]"#
)
XCTAssertEqual(
try encoder.encode(
EncodingContainer(EncodableValue(URL(string: "https://example.com/image.png")!))
).utf8String,
#"{"value":"https:\/\/example.com\/image.png"}"#
)
struct Foo: Encodable {
let bar = "bar_"
}
XCTAssertEqual(
try encoder.encode(EncodableValue(Foo())).utf8String,
#"{"bar":"bar_"}"#
)
}
}
class JSONStringEncodableValueTests: XCTestCase {
func testItEncodesDifferentEncodableValuesAsString() throws {
let encoder = JSONEncoder()
encoder.outputFormatting = [.sortedKeys]
XCTAssertEqual(
try encoder.encode(
EncodingContainer(JSONStringEncodableValue("string", encodedUsing: JSONEncoder()))
).utf8String,
#"{"value":"string"}"#
)
XCTAssertEqual(
try encoder.encode(
EncodingContainer(JSONStringEncodableValue(123, encodedUsing: JSONEncoder()))
).utf8String,
#"{"value":"123"}"#
)
XCTAssertEqual(
try encoder.encode(
EncodingContainer(JSONStringEncodableValue(["a", "b", "c"], encodedUsing: JSONEncoder()))
).utf8String,
#"{"value":"[\"a\",\"b\",\"c\"]"}"#
)
XCTAssertEqual(
try encoder.encode(
EncodingContainer(
JSONStringEncodableValue(URL(string: "https://example.com/image.png")!, encodedUsing: JSONEncoder())
)
).utf8String,
#"{"value":"https:\/\/example.com\/image.png"}"#
)
struct Foo: Encodable {
let bar = "bar_"
}
XCTAssertEqual(
try encoder.encode(
EncodingContainer(JSONStringEncodableValue(Foo(), encodedUsing: JSONEncoder()))
).utf8String,
#"{"value":"{\"bar\":\"bar_\"}"}"#
)
}
func testWhenValueCannotBeEncoded_itThrowsErrorDuringEncoderInvocation() {
let encoder = JSONEncoder()
let value = JSONStringEncodableValue(FailingEncodableMock(errorMessage: "ops..."), encodedUsing: JSONEncoder())
XCTAssertThrowsError(try encoder.encode(value)) { error in
XCTAssertEqual((error as? ErrorMock)?.description, "ops...")
}
}
}