forked from facebook/facebook-ios-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAEMEventTests.swift
185 lines (166 loc) · 4.44 KB
/
AEMEventTests.swift
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
@testable import FBAEMKit
import TestTools
import XCTest
final class AEMEventTests: XCTestCase {
enum Keys {
static let eventName = "event_name"
static let values = "values"
static let currency = "currency"
static let amount = "amount"
}
enum Values {
static let purchase = "fb_mobile_purchase"
static let subscribe = "Subscribe"
static let usd = "usd"
static let jpy = "jpy"
static let USD = "USD"
static let JPY = "JPY"
}
var sampleData: [String: Any] = [
Keys.eventName: Values.purchase,
Keys.values: [
[
Keys.currency: Values.usd,
Keys.amount: 100,
],
[
Keys.currency: Values.JPY,
Keys.amount: 1000,
],
],
]
var validEventWithValues: AEMEvent? = AEMEvent(dict: [
Keys.eventName: Values.purchase,
Keys.values: [
[
Keys.currency: Values.usd,
Keys.amount: 100.0,
],
[
Keys.currency: Values.JPY,
Keys.amount: 1000.0,
],
],
])
var validEventWithoutValues: AEMEvent? = AEMEvent(dict: [
Keys.eventName: Values.purchase,
])
func testValidCases() {
var event = validEventWithoutValues
XCTAssertEqual(
event?.eventName,
Values.purchase,
"AEM event name should match the expected event_name in the json"
)
XCTAssertNil(
event?.values,
"AEM event should not have unexpected values"
)
event = validEventWithValues
XCTAssertEqual(
event?.eventName,
Values.purchase,
"AEM event name should match the expected event_name in the json"
)
let expectedValues: [String: Double] = [
Values.USD: 100,
Values.JPY: 1000,
]
XCTAssertEqual(
event?.values,
expectedValues,
"AEM event should have the expected values in the json"
)
}
func testInvalidCases() {
var invalidData: [String: Any] = [:]
XCTAssertNil(AEMEvent(dict: invalidData))
invalidData = [
Keys.values: [
[
Keys.currency: Values.usd,
Keys.amount: 100,
],
[
Keys.currency: Values.JPY,
Keys.amount: 1000,
],
],
]
XCTAssertNil(AEMEvent(dict: invalidData))
invalidData = [
Keys.eventName: Values.purchase,
Keys.values: [
[
Keys.currency: 100,
Keys.amount: Values.usd,
],
[
Keys.currency: 1000,
Keys.amount: Values.jpy,
],
],
]
XCTAssertNil(AEMEvent(dict: invalidData))
invalidData = [
Keys.eventName: [Values.purchase, Values.subscribe],
Keys.values: [
[
Keys.currency: 100,
Keys.amount: Values.usd,
],
[
Keys.currency: 1000,
Keys.amount: Values.jpy,
],
],
]
XCTAssertNil(AEMEvent(dict: invalidData))
}
func testParsing() {
(1 ... 100).forEach { _ in
if let data = (Fuzzer.randomize(json: self.sampleData) as? [String: Any]) {
_ = AEMEvent(dict: data)
}
}
}
func testSecureCoding() {
XCTAssertTrue(
AEMEvent.supportsSecureCoding,
"AEM Events should support secure coding"
)
}
func testEncodingAndDecodingWithValues() throws {
let event = validEventWithValues
// swiftlint:disable:next force_unwrapping
let decodedObject = try CodabilityTesting.encodeAndDecode(event!)
// Test Objects
XCTAssertNotIdentical(decodedObject, event, .isCodable)
XCTAssertEqual(decodedObject, event, .isCodable)
// Test Properties
XCTAssertEqual(event?.eventName, decodedObject.eventName)
XCTAssertEqual(event?.values, decodedObject.values)
}
func testEncodingAndDecodingWithoutValues() throws {
let event = validEventWithoutValues
// swiftlint:disable:next force_unwrapping
let decodedObject = try CodabilityTesting.encodeAndDecode(event!)
// Test Objects
XCTAssertNotIdentical(decodedObject, event, .isCodable)
XCTAssertEqual(decodedObject, event, .isCodable)
// Test Properties
XCTAssertEqual(event?.eventName, decodedObject.eventName)
XCTAssertEqual(event?.values, decodedObject.values)
}
}
// MARK: - Assumptions
extension String {
fileprivate static let isCodable = "AEMEvents should be encodable and decodable"
}