Skip to content

Commit 444a8fe

Browse files
authored
Make JSONObject.loadFile throw (#18507)
2 parents 5f4e769 + 00cb24f commit 444a8fe

23 files changed

+282
-319
lines changed

WordPress/WordPressTest/ActivityContentFactoryTests.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ import XCTest
44
final class ActivityContentFactoryTests: XCTestCase {
55
private let contextManager = TestContextManager()
66

7-
func testActivityContentFactoryReturnsExpectedImplementationOfFormattableContent() {
8-
let subject = ActivityContentFactory.content(from: [mockBlock()], actionsParser: ActivityActionsParser()).first as? FormattableTextContent
7+
func testActivityContentFactoryReturnsExpectedImplementationOfFormattableContent() throws {
8+
let subject = ActivityContentFactory.content(from: [try mockBlock()], actionsParser: ActivityActionsParser()).first as? FormattableTextContent
99

1010
XCTAssertNotNil(subject)
1111
}
1212

13-
private func mockBlock() -> [String: AnyObject] {
14-
return 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) -> [String: AnyObject] {
18-
return JSONObject.loadFile(named: fileName)
19-
}
2017
}

WordPress/WordPressTest/ActivityLogFormattableContentTests.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ final class ActivityLogFormattableContentTests: XCTestCase {
66
let testData = ActivityLogTestData()
77
let actionsParser = ActivityActionsParser()
88

9-
func testPingbackContentIsParsedCorrectly() {
10-
let dictionary = testData.getPingbackDictionary()
9+
func testPingbackContentIsParsedCorrectly() throws {
10+
let dictionary = try testData.getPingbackDictionary()
1111

1212
let pingbackContent = ActivityContentFactory.content(from: [dictionary], actionsParser: actionsParser)
1313

@@ -21,8 +21,8 @@ final class ActivityLogFormattableContentTests: XCTestCase {
2121
XCTAssertEqual(pingback.ranges.last?.kind, .post)
2222
}
2323

24-
func testPostContentIsParsedCorrectly() {
25-
let dictionary = testData.getPostContentDictionary()
24+
func testPostContentIsParsedCorrectly() throws {
25+
let dictionary = try testData.getPostContentDictionary()
2626
let postContent = ActivityContentFactory.content(from: [dictionary], actionsParser: actionsParser)
2727

2828
XCTAssertEqual(postContent.count, 1)
@@ -34,8 +34,8 @@ final class ActivityLogFormattableContentTests: XCTestCase {
3434
XCTAssertEqual(post.ranges.first?.kind, .post)
3535
}
3636

37-
func testCommentContentIsParsedCorrectly() {
38-
let dictionary = testData.getCommentContentDictionary()
37+
func testCommentContentIsParsedCorrectly() throws {
38+
let dictionary = try testData.getCommentContentDictionary()
3939
let commentContent = ActivityContentFactory.content(from: [dictionary], actionsParser: actionsParser)
4040

4141
XCTAssertEqual(commentContent.count, 1)
@@ -48,8 +48,8 @@ final class ActivityLogFormattableContentTests: XCTestCase {
4848
XCTAssertEqual(comment.ranges.last?.kind, .post)
4949
}
5050

51-
func testThemeContentIsParsedCorrectly() {
52-
let dictionary = testData.getThemeContentDictionary()
51+
func testThemeContentIsParsedCorrectly() throws {
52+
let dictionary = try testData.getThemeContentDictionary()
5353
let themeContent = ActivityContentFactory.content(from: [dictionary], actionsParser: actionsParser)
5454

5555
XCTAssertEqual(themeContent.count, 1)
@@ -61,8 +61,8 @@ final class ActivityLogFormattableContentTests: XCTestCase {
6161
XCTAssertEqual(theme.ranges.first?.kind, .theme)
6262
}
6363

64-
func testSettingContentIsParsedCorrectly() {
65-
let dictionary = testData.getSettingsContentDictionary()
64+
func testSettingContentIsParsedCorrectly() throws {
65+
let dictionary = try testData.getSettingsContentDictionary()
6666
let settingsContent = ActivityContentFactory.content(from: [dictionary], actionsParser: actionsParser)
6767

6868
XCTAssertEqual(settingsContent.count, 1)
@@ -75,8 +75,8 @@ final class ActivityLogFormattableContentTests: XCTestCase {
7575
XCTAssertEqual(settings.ranges.last?.kind, .italic)
7676
}
7777

78-
func testSiteContentIsParsedCorreclty() {
79-
let dictionary = testData.getSiteContentDictionary()
78+
func testSiteContentIsParsedCorreclty() throws {
79+
let dictionary = try testData.getSiteContentDictionary()
8080
let siteContent = ActivityContentFactory.content(from: [dictionary], actionsParser: actionsParser)
8181

8282
XCTAssertEqual(siteContent.count, 1)
@@ -88,8 +88,8 @@ final class ActivityLogFormattableContentTests: XCTestCase {
8888
XCTAssertEqual(site.ranges.first?.kind, .site)
8989
}
9090

91-
func testPluginContentIsParsedCorreclty() {
92-
let dictionary = testData.getPluginContentDictionary()
91+
func testPluginContentIsParsedCorreclty() throws {
92+
let dictionary = try testData.getPluginContentDictionary()
9393
let pluginContent = ActivityContentFactory.content(from: [dictionary], actionsParser: actionsParser)
9494

9595
XCTAssertEqual(pluginContent.count, 1)

WordPress/WordPressTest/ActivityLogRangesTest.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ final class ActivityLogRangesTests: XCTestCase {
4646
XCTAssertEqual(defaultRange.range, range)
4747
}
4848

49-
func testRangeFactoryCreatesCommentRange() {
50-
let commentRangeRaw = testData.getCommentRangeDictionary()
49+
func testRangeFactoryCreatesCommentRange() throws {
50+
let commentRangeRaw = try testData.getCommentRangeDictionary()
5151
let range = ActivityRangesFactory.contentRange(from: commentRangeRaw)
5252

5353
XCTAssertNotNil(range)
@@ -59,8 +59,8 @@ final class ActivityLogRangesTests: XCTestCase {
5959
XCTAssertNotNil(commentRange?.url)
6060
}
6161

62-
func testRangeFactoryCreatesThemeRange() {
63-
let themeRangeRaw = testData.getThemeRangeDictionary()
62+
func testRangeFactoryCreatesThemeRange() throws {
63+
let themeRangeRaw = try testData.getThemeRangeDictionary()
6464
let range = ActivityRangesFactory.contentRange(from: themeRangeRaw)
6565

6666
XCTAssertNotNil(range)
@@ -72,8 +72,8 @@ final class ActivityLogRangesTests: XCTestCase {
7272
XCTAssertNotNil(themeRange?.url)
7373
}
7474

75-
func testRangeFactoryCreatesPostRange() {
76-
let postRangeRaw = testData.getPostRangeDictionary()
75+
func testRangeFactoryCreatesPostRange() throws {
76+
let postRangeRaw = try testData.getPostRangeDictionary()
7777

7878
let range = ActivityRangesFactory.contentRange(from: postRangeRaw)
7979
XCTAssertNotNil(range)
@@ -85,24 +85,24 @@ final class ActivityLogRangesTests: XCTestCase {
8585
XCTAssertEqual(postRange?.url?.absoluteString, testData.testPostUrl)
8686
}
8787

88-
func testRangeFactoryCreatesItalicRange() {
89-
let italicRangeRaw = testData.getItalicRangeDictionary()
88+
func testRangeFactoryCreatesItalicRange() throws {
89+
let italicRangeRaw = try testData.getItalicRangeDictionary()
9090

9191
let range = ActivityRangesFactory.contentRange(from: italicRangeRaw)
9292
XCTAssertNotNil(range)
9393
XCTAssertEqual(range?.kind, .italic)
9494
}
9595

96-
func testRangeFactoryCreatesSiteRange() {
97-
let siteRangeRaw = testData.getSiteRangeDictionary()
96+
func testRangeFactoryCreatesSiteRange() throws {
97+
let siteRangeRaw = try testData.getSiteRangeDictionary()
9898
let range = ActivityRangesFactory.contentRange(from: siteRangeRaw)
9999

100100
XCTAssertNotNil(range)
101101
XCTAssertEqual(range?.kind, .site)
102102
}
103103

104-
func testRangeFactoryCreatesPluginRange() {
105-
let pluginRangeRaw = testData.getPluginRangeDictionary()
104+
func testRangeFactoryCreatesPluginRange() throws {
105+
let pluginRangeRaw = try testData.getPluginRangeDictionary()
106106
let range = ActivityRangesFactory.contentRange(from: pluginRangeRaw)
107107

108108
XCTAssertNotNil(range)

WordPress/WordPressTest/ActivityLogTestData.swift

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

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

34-
func getCommentEventDictionary() -> [String: AnyObject] {
35-
return 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() -> [String: AnyObject] {
39-
return 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() -> [String: AnyObject] {
43-
return 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() -> [String: AnyObject] {
47-
return 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() -> [String: AnyObject] {
51-
return 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() -> [String: AnyObject] {
55-
return 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() -> [String: AnyObject] {
59-
return 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() -> [String: AnyObject] {
63-
return 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() -> [String: AnyObject] {
67-
return getDictionaryFromFile(named: "activity-log-plugin-content.json")
68-
}
69-
70-
func getCommentRangeDictionary() -> [String: AnyObject] {
71-
let dictionary = getCommentContentDictionary()
66+
func getCommentRangeDictionary() throws -> JSONObject {
67+
let dictionary = try getCommentContentDictionary()
7268
return getRange(at: 0, from: dictionary)
7369
}
7470

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

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

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

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

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

WordPress/WordPressTest/ApproveCommentActionTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ final class ApproveCommentActionTests: XCTestCase {
6565
XCTAssertEqual(action?.actionTitle, ApproveComment.TitleStrings.approve)
6666
}
6767

68-
func testExecuteCallsUnapproveWhenActionIsOn() {
68+
func testExecuteCallsUnapproveWhenActionIsOn() throws {
6969
action?.on = true
7070

71-
action?.execute(context: utility.mockCommentContext())
71+
action?.execute(context: try utility.mockCommentContext())
7272

7373
guard let mockService = action?.actionsService as? MockNotificationActionsService else {
7474
XCTFail()
@@ -78,17 +78,17 @@ final class ApproveCommentActionTests: XCTestCase {
7878
XCTAssertTrue(mockService.unapproveWasCalled)
7979
}
8080

81-
func testExecuteUpdatesActionTitleWhenActionIsOn() {
81+
func testExecuteUpdatesActionTitleWhenActionIsOn() throws {
8282
action?.on = true
8383

84-
action?.execute(context: utility.mockCommentContext())
84+
action?.execute(context: try utility.mockCommentContext())
8585
XCTAssertEqual(action?.actionTitle, ApproveComment.TitleStrings.approve)
8686
}
8787

88-
func testExecuteCallsApproveWhenActionIsOff() {
88+
func testExecuteCallsApproveWhenActionIsOff() throws {
8989
action?.on = false
9090

91-
action?.execute(context: utility.mockCommentContext())
91+
action?.execute(context: try utility.mockCommentContext())
9292

9393
guard let mockService = action?.actionsService as? MockNotificationActionsService else {
9494
XCTFail()
@@ -98,10 +98,10 @@ final class ApproveCommentActionTests: XCTestCase {
9898
XCTAssertTrue(mockService.approveWasCalled)
9999
}
100100

101-
func testExecuteUpdatesActionTitleWhenActionIsOff() {
101+
func testExecuteUpdatesActionTitleWhenActionIsOff() throws {
102102
action?.on = false
103103

104-
action?.execute(context: utility.mockCommentContext())
104+
action?.execute(context: try utility.mockCommentContext())
105105
XCTAssertEqual(action?.actionTitle, ApproveComment.TitleStrings.unapprove)
106106
}
107107
}

WordPress/WordPressTest/EditCommentActionTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ final class EditCommentActionTests: XCTestCase {
4141
XCTAssertEqual(action?.actionTitle, EditComment.title)
4242
}
4343

44-
func testExecuteCallsEdit() {
45-
action?.execute(context: utility.mockCommentContext())
44+
func testExecuteCallsEdit() throws {
45+
action?.execute(context: try utility.mockCommentContext())
4646

4747
guard let mockService = action?.actionsService as? MockNotificationActionsService else {
4848
XCTFail()

0 commit comments

Comments
 (0)