Skip to content

Commit 00cb24f

Browse files
committed
Simplify JSON parsing code in unit test
* Remove various definition of `getDictionaryFromFile(named:)` by calling `JSONObject.loadFile(named:)` directly. * Use `JSONObject` alias instead of Dictionary type as return type.
1 parent bd4604c commit 00cb24f

9 files changed

+59
-88
lines changed

WordPress/WordPressTest/ActivityContentFactoryTests.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ final class ActivityContentFactoryTests: XCTestCase {
1010
XCTAssertNotNil(subject)
1111
}
1212

13-
private func mockBlock() throws -> [String: AnyObject] {
14-
return try getDictionaryFromFile(named: "activity-log-activity-content.json")
13+
private func mockBlock() throws -> JSONObject {
14+
return try .loadFile(named: "activity-log-activity-content.json")
1515
}
1616

17-
private func getDictionaryFromFile(named fileName: String) throws -> [String: AnyObject] {
18-
return try JSONObject.loadFile(named: fileName)
19-
}
2017
}

WordPress/WordPressTest/ActivityLogTestData.swift

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,72 +27,68 @@ class ActivityLogTestData {
2727
return "https://wordpress.com/comment/137726971/7"
2828
}
2929

30-
private func getDictionaryFromFile(named fileName: String) throws -> [String: AnyObject] {
31-
return try JSONObject.loadFile(named: fileName)
30+
func getCommentEventDictionary() throws -> JSONObject {
31+
return try .loadFile(named: "activity-log-comment.json")
3232
}
3333

34-
func getCommentEventDictionary() throws -> [String: AnyObject] {
35-
return try getDictionaryFromFile(named: "activity-log-comment.json")
34+
func getPostEventDictionary() throws -> JSONObject {
35+
return try .loadFile(named: "activity-log-post.json")
3636
}
3737

38-
func getPostEventDictionary() throws -> [String: AnyObject] {
39-
return try getDictionaryFromFile(named: "activity-log-post.json")
38+
func getPingbackDictionary() throws -> JSONObject {
39+
return try .loadFile(named: "activity-log-pingback-content.json")
4040
}
4141

42-
func getPingbackDictionary() throws -> [String: AnyObject] {
43-
return try getDictionaryFromFile(named: "activity-log-pingback-content.json")
42+
func getPostContentDictionary() throws -> JSONObject {
43+
return try .loadFile(named: "activity-log-post-content.json")
4444
}
4545

46-
func getPostContentDictionary() throws -> [String: AnyObject] {
47-
return try getDictionaryFromFile(named: "activity-log-post-content.json")
46+
func getCommentContentDictionary() throws -> JSONObject {
47+
return try .loadFile(named: "activity-log-comment-content.json")
4848
}
4949

50-
func getCommentContentDictionary() throws -> [String: AnyObject] {
51-
return try getDictionaryFromFile(named: "activity-log-comment-content.json")
50+
func getThemeContentDictionary() throws -> JSONObject {
51+
return try .loadFile(named: "activity-log-theme-content.json")
5252
}
5353

54-
func getThemeContentDictionary() throws -> [String: AnyObject] {
55-
return try getDictionaryFromFile(named: "activity-log-theme-content.json")
54+
func getSettingsContentDictionary() throws -> JSONObject {
55+
return try .loadFile(named: "activity-log-settings-content.json")
5656
}
5757

58-
func getSettingsContentDictionary() throws -> [String: AnyObject] {
59-
return try getDictionaryFromFile(named: "activity-log-settings-content.json")
58+
func getSiteContentDictionary() throws -> JSONObject {
59+
return try .loadFile(named: "activity-log-site-content.json")
6060
}
6161

62-
func getSiteContentDictionary() throws -> [String: AnyObject] {
63-
return try getDictionaryFromFile(named: "activity-log-site-content.json")
62+
func getPluginContentDictionary() throws -> JSONObject {
63+
return try .loadFile(named: "activity-log-plugin-content.json")
6464
}
6565

66-
func getPluginContentDictionary() throws -> [String: AnyObject] {
67-
return try getDictionaryFromFile(named: "activity-log-plugin-content.json")
68-
}
69-
70-
func getCommentRangeDictionary() throws -> [String: AnyObject] {
66+
func getCommentRangeDictionary() throws -> JSONObject {
7167
let dictionary = try getCommentContentDictionary()
7268
return getRange(at: 0, from: dictionary)
7369
}
7470

75-
func getPostRangeDictionary() throws -> [String: AnyObject] {
71+
func getPostRangeDictionary() throws -> JSONObject {
7672
let dictionary = try getPostContentDictionary()
7773
return getRange(at: 0, from: dictionary)
7874
}
7975

80-
func getThemeRangeDictionary() throws -> [String: AnyObject] {
76+
func getThemeRangeDictionary() throws -> JSONObject {
8177
let dictionary = try getThemeContentDictionary()
8278
return getRange(at: 0, from: dictionary)
8379
}
8480

85-
func getItalicRangeDictionary() throws -> [String: AnyObject] {
81+
func getItalicRangeDictionary() throws -> JSONObject {
8682
let dictionary = try getSettingsContentDictionary()
8783
return getRange(at: 0, from: dictionary)
8884
}
8985

90-
func getSiteRangeDictionary() throws -> [String: AnyObject] {
86+
func getSiteRangeDictionary() throws -> JSONObject {
9187
let dictionary = try getSiteContentDictionary()
9288
return getRange(at: 0, from: dictionary)
9389
}
9490

95-
func getPluginRangeDictionary() throws -> [String: AnyObject] {
91+
func getPluginRangeDictionary() throws -> JSONObject {
9692
let dictionary = try getPluginContentDictionary()
9793
return getRange(at: 0, from: dictionary)
9894
}

WordPress/WordPressTest/FormattableCommentContentTests.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,16 @@ final class FormattableCommentContentTests: XCTestCase {
133133
XCTAssertNotNil(markAsSpam)
134134
}
135135

136-
private func mockDictionary() throws -> [String: AnyObject] {
137-
return try getDictionaryFromFile(named: "notifications-comment-content.json")
138-
}
139-
140-
private func getDictionaryFromFile(named fileName: String) throws -> [String: AnyObject] {
141-
return try JSONObject.loadFile(named: fileName)
136+
private func mockDictionary() throws -> JSONObject {
137+
return try .loadFile(named: "notifications-comment-content.json")
142138
}
143139

144140
private func loadLikeNotification() throws -> WordPress.Notification {
145141
return try utility.loadLikeNotification()
146142
}
147143

148-
private func loadMeta() throws -> [String: AnyObject] {
149-
return try getDictionaryFromFile(named: "notifications-comment-meta.json")
144+
private func loadMeta() throws -> JSONObject {
145+
return try .loadFile(named: "notifications-comment-meta.json")
150146
}
151147

152148
private func mockedActions() -> [FormattableContentAction] {

WordPress/WordPressTest/FormattableContentGroupTests.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,8 @@ final class FormattableContentGroupTests: XCTestCase {
5959
return FormattableTextContent(text: text, ranges: [], actions: [])
6060
}
6161

62-
private func mockActivity() throws -> [String: AnyObject] {
63-
return try getDictionaryFromFile(named: "activity-log-activity-content.json")
62+
private func mockActivity() throws -> JSONObject {
63+
return try .loadFile(named: "activity-log-activity-content.json")
6464
}
6565

66-
private func getDictionaryFromFile(named fileName: String) throws -> [String: AnyObject] {
67-
return try JSONObject.loadFile(named: fileName)
68-
}
6966
}

WordPress/WordPressTest/FormattableUserContentTests.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,16 @@ final class FormattableUserContentTests: XCTestCase {
129129
XCTAssertEqual(id, Expectations.metaSiteId)
130130
}
131131

132-
private func mockDictionary() throws -> [String: AnyObject] {
133-
return try getDictionaryFromFile(named: "notifications-user-content.json")
134-
}
135-
136-
private func getDictionaryFromFile(named fileName: String) throws -> [String: AnyObject] {
137-
return try JSONObject.loadFile(named: fileName)
132+
private func mockDictionary() throws -> JSONObject {
133+
return try .loadFile(named: "notifications-user-content.json")
138134
}
139135

140136
private func loadLikeNotification() throws -> WordPress.Notification {
141137
return try .fixture(fromFile: "notifications-like.json", insertInto: contextManager.mainContext)
142138
}
143139

144-
private func loadMeta() throws -> [String: AnyObject] {
145-
return try getDictionaryFromFile(named: "notifications-user-content-meta.json")
140+
private func loadMeta() throws -> JSONObject {
141+
return try .loadFile(named: "notifications-user-content-meta.json")
146142
}
147143

148144
private func mockedActions() -> [FormattableContentAction] {

WordPress/WordPressTest/NotificationContentRangeFactoryTests.swift

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,28 @@ final class NotificationContentRangeFactoryTests: XCTestCase {
4040
XCTAssertNotNil(subject)
4141
}
4242

43-
private func mockCommentRange() throws -> [String: AnyObject] {
44-
return try getDictionaryFromFile(named: "notifications-comment-range.json")
43+
private func mockCommentRange() throws -> JSONObject {
44+
return try .loadFile(named: "notifications-comment-range.json")
4545
}
4646

47-
private func mockIconRange() throws -> [String: AnyObject] {
48-
return try getDictionaryFromFile(named: "notifications-icon-range.json")
47+
private func mockIconRange() throws -> JSONObject {
48+
return try .loadFile(named: "notifications-icon-range.json")
4949
}
5050

51-
private func mockPostRange() throws -> [String: AnyObject] {
52-
return try getDictionaryFromFile(named: "notifications-post-range.json")
51+
private func mockPostRange() throws -> JSONObject {
52+
return try .loadFile(named: "notifications-post-range.json")
5353
}
5454

55-
private func mockSiteRange() throws -> [String: AnyObject] {
56-
return try getDictionaryFromFile(named: "notifications-site-range.json")
55+
private func mockSiteRange() throws -> JSONObject {
56+
return try .loadFile(named: "notifications-site-range.json")
5757
}
5858

59-
private func mockUserRange() throws -> [String: AnyObject] {
60-
return try getDictionaryFromFile(named: "notifications-user-range.json")
59+
private func mockUserRange() throws -> JSONObject {
60+
return try .loadFile(named: "notifications-user-range.json")
6161
}
6262

63-
private func mockBlockQuoteRange() throws -> [String: AnyObject] {
64-
return try getDictionaryFromFile(named: "notifications-blockquote-range.json")
63+
private func mockBlockQuoteRange() throws -> JSONObject {
64+
return try .loadFile(named: "notifications-blockquote-range.json")
6565
}
6666

67-
private func getDictionaryFromFile(named fileName: String) throws -> [String: AnyObject] {
68-
return try JSONObject.loadFile(named: fileName)
69-
}
7067
}

WordPress/WordPressTest/NotificationTextContentTests.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,12 @@ final class NotificationTextContentTests: XCTestCase {
8787
XCTAssertEqual(action?.identifier, approveCommentIdentifier)
8888
}
8989

90-
private func mockDictionary() throws -> [String: AnyObject] {
91-
return try getDictionaryFromFile(named: "notifications-text-content.json")
90+
private func mockDictionary() throws -> JSONObject {
91+
return try .loadFile(named: "notifications-text-content.json")
9292
}
9393

94-
private func mockButtonContentDictionary() throws -> [String: AnyObject] {
95-
return try getDictionaryFromFile(named: "notifications-button-text-content.json")
96-
}
97-
98-
private func getDictionaryFromFile(named fileName: String) throws -> [String: AnyObject] {
99-
return try JSONObject.loadFile(named: fileName)
94+
private func mockButtonContentDictionary() throws -> JSONObject {
95+
return try .loadFile(named: "notifications-button-text-content.json")
10096
}
10197

10298
private func loadLikeNotification() throws -> WordPress.Notification {

WordPress/WordPressTest/NotificationsContentFactoryTests.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,16 @@ final class NotificationsContentFactoryTests: XCTestCase {
2323
XCTAssertNotNil(subject)
2424
}
2525

26-
private func mockTextContentDictionary() throws -> [String: AnyObject] {
27-
return try getDictionaryFromFile(named: "notifications-text-content.json")
26+
private func mockTextContentDictionary() throws -> JSONObject {
27+
return try .loadFile(named: "notifications-text-content.json")
2828
}
2929

30-
private func mockCommentContentDictionary() throws -> [String: AnyObject] {
31-
return try getDictionaryFromFile(named: "notifications-comment-content.json")
30+
private func mockCommentContentDictionary() throws -> JSONObject {
31+
return try .loadFile(named: "notifications-comment-content.json")
3232
}
3333

34-
private func mockUserContentDictionary() throws -> [String: AnyObject] {
35-
return try getDictionaryFromFile(named: "notifications-user-content.json")
36-
}
37-
38-
private func getDictionaryFromFile(named fileName: String) throws -> [String: AnyObject] {
39-
return try JSONObject.loadFile(named: fileName)
34+
private func mockUserContentDictionary() throws -> JSONObject {
35+
return try .loadFile(named: "notifications-user-content.json")
4036
}
4137

4238
func loadLikeNotification() throws -> WordPress.Notification {

WordPress/WordPressTest/PinghubTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class PingHubTests: XCTestCase {
106106

107107
private extension PingHubTests {
108108
func loadJSONMessage(name: String) throws -> [String: AnyObject]? {
109-
return try JSONObject.loadFile(name, type: "json")
109+
return try .loadFile(name, type: "json")
110110
}
111111
}
112112

0 commit comments

Comments
 (0)