iOS/Sources/PushServer/Tests/SharedPushTests/NotificationParserLegacy.test.swift
mat1th 97834bfd5e
Update swift lint and format + appy fixes (#2585)
## Summary
Swift lint and swiftformat are outdated. This PR does update those +
applies the new formatting form swiftformat.
There is 1 swift file with a manual change:
`Sources/Vehicle/Templates/Areas/CarPlayAreasViewModel.swift`. This is
done because `swiftlint` did create the following swiftlint error:
`error: Cyclomatic Complexity Violation: Function should have complexity
10 or less; currently complexity is 11 (cyclomatic_complexity)`.

Because it does change a lot of files the question is if we want to
finetune the `swiftformat` rules.

## Screenshots
No user facing changes.

## Link to pull request in Documentation repository
NA

## Any other notes
NA
2024-02-22 13:06:39 +01:00

69 lines
2.6 KiB
Swift

import Foundation
import SharedPush
import XCTest
class NotificationParserLegacyTests: XCTestCase {
private struct NotificationCase {
var name: String = "(unknown)"
var input: [String: Any]
var headers: [String: Any]
var payload: [String: Any]
var rateLimit: Bool // unused in tests
var expected: [String: Any] { [
"headers": headers,
"payload": payload,
] }
init(jsonObject: Any) throws {
let dict = try XCTUnwrap(jsonObject as? [String: Any])
self.input = try XCTUnwrap(dict["input"] as? [String: Any])
self.headers = try XCTUnwrap(dict["headers"] as? [String: Any])
self.payload = try XCTUnwrap(dict["payload"] as? [String: Any])
self.rateLimit = dict["headers"] as? Bool ?? true
}
}
private var notificationCases: [NotificationCase]!
override func setUpWithError() throws {
super.setUp()
let container = try XCTUnwrap(
Bundle.module.url(forResource: "notification_test_cases", withExtension: "bundle")
)
let enumerator = try XCTUnwrap(FileManager.default.enumerator(at: container, includingPropertiesForKeys: nil))
notificationCases = try enumerator.allObjects
.map { try XCTUnwrap($0 as? URL) }
.filter { $0.pathExtension == "json" }
.map { try ($0.lastPathComponent, Data(contentsOf: $0)) }
.map { try ($0.0, JSONSerialization.jsonObject(with: $0.1, options: [])) }
.map {
var notificationCase = try NotificationCase(jsonObject: $0.1)
notificationCase.name = $0.0
return notificationCase
}
XCTAssertFalse(notificationCases.isEmpty)
}
func testAllCases() throws {
let parser = LegacyNotificationParserImpl(pushSource: "<test-push-value>")
for data in notificationCases {
func prettyString(from object: [String: Any]) throws -> String {
let data = try JSONSerialization.data(withJSONObject: object, options: [.sortedKeys])
return try XCTUnwrap(String(data: data, encoding: .utf8))
}
let resultStruct = parser.result(from: data.input, defaultRegistrationInfo: [:])
let result = ["headers": resultStruct.headers, "payload": resultStruct.payload]
let resultString = try prettyString(from: result)
let expected = data.expected
let expectedString = try prettyString(from: expected)
XCTAssertEqual(resultString, expectedString, data.name)
}
}
}