From 9dfbf8539dbd3ff8387cad63b975f6b5f2f83804 Mon Sep 17 00:00:00 2001 From: burstein Date: Tue, 8 Jul 2025 22:55:18 +0300 Subject: [PATCH] Fixed integration tests for correct triggers tests commit_hash:7a376d4e2eb42097b41fdef486c2d3d5586fb8ae --- client/ios/DivKitTests/IntegrationTests.swift | 125 +++++++++--------- 1 file changed, 62 insertions(+), 63 deletions(-) diff --git a/client/ios/DivKitTests/IntegrationTests.swift b/client/ios/DivKitTests/IntegrationTests.swift index 2e0a24078..cae890cd2 100644 --- a/client/ios/DivKitTests/IntegrationTests.swift +++ b/client/ios/DivKitTests/IntegrationTests.swift @@ -22,41 +22,63 @@ private func makeTestCases() -> [(String, IntegrationTestData)] { getFiles( "integration_test_data", forBundle: Bundle(for: IntegrationTests.self) - ).map { url in + ).flatMap { url in let fileName = url.lastPathComponent - let testData = try! JSONDecoder() - .decode(IntegrationTestData.self, from: Data(contentsOf: url)) - return ("\(fileName): \(testData.description)", testData) + let test = try! IntegrationTest(Data(contentsOf: url)) + + return test.cases + .enumerated() + .filter { $1.platforms.contains(.ios) } + .map { index, testCase in + ( + "\(fileName): test case index - [\(index)]", + IntegrationTestData(divData: test.divData, testCase: testCase) + ) + } } } @MainActor private func runTest(_ testData: IntegrationTestData) async { - for testCase in testData.cases.filter({ $0.platforms.contains(.ios) }) { - let reporter = MockReporter() - let divkitComponents = createDivKitComponents(testCase: testCase, reporter: reporter) - DivKitLogger.isEnabled = true - DivKitLogger.setLogger { reporter.insertErrorMessage($0) } - await setSource(testData.divData, components: divkitComponents) + let testCase = testData.testCase + let reporter = MockReporter() + DivKitLogger.isEnabled = true + DivKitLogger.setLogger { reporter.insertErrorMessage($0) } + let globalStorage = DivVariableStorage() + globalStorage.put( + testCase.expected.makeDefaultVariables(), + notifyObservers: false + ) + let divkitComponents = DivKitComponents( + flagsInfo: DivFlagsInfo(initializeTriggerOnSet: false), + reporter: reporter, + variablesStorage: DivVariablesStorage(outerStorage: globalStorage) + ) - testCase.divActions?.forEach { - divkitComponents.actionHandler.handle( - $0, - path: cardId.path, - source: .tap, - sender: nil - ) - } + let divView = DivView(divKitComponents: divkitComponents) + await divView.setSource(DivViewSource(kind: .divData(testData.divData), cardId: cardId)) + testCase.divActions?.forEach { + divkitComponents.actionHandler.handle( + $0, + path: cardId.path, + source: .tap, + sender: nil + ) + } + + Task { @MainActor in testCase.expected.forEach { switch $0 { case let .variable(name, value): + let variableValue = divkitComponents.variablesStorage.getVariableValue( + cardId: cardId, + name: DivVariableName(rawValue: name) + ) XCTAssertEqual( value.divVariableValue, - divkitComponents.variablesStorage.getVariableValue( - cardId: cardId, - name: DivVariableName(rawValue: name) - ) + variableValue, + "Variable name - '\(name)'" ) case let .error(message): XCTAssert( @@ -68,27 +90,6 @@ private func runTest(_ testData: IntegrationTestData) async { } } -private func createDivKitComponents( - testCase: IntegrationTestCase, - reporter: DivReporter -) -> DivKitComponents { - let globalStorage = DivVariableStorage() - globalStorage.put( - testCase.expected.makeDefaultVariables(), - notifyObservers: false - ) - return DivKitComponents( - reporter: reporter, - variablesStorage: DivVariablesStorage(outerStorage: globalStorage) - ) -} - -@MainActor -private func setSource(_ divData: DivData, components: DivKitComponents) async { - let divView = DivView(divKitComponents: components) - await divView.setSource(DivViewSource(kind: .divData(divData), cardId: cardId)) -} - private final class MockReporter: @unchecked Sendable, DivReporter { private(set) var errorMessages = Set() @@ -101,32 +102,30 @@ private final class MockReporter: @unchecked Sendable, DivReporter { } } -private struct IntegrationTestData: Decodable, @unchecked Sendable { - init(from decoder: any Decoder) throws { - let container = try decoder.container(keyedBy: CodingKeys.self) - let cardContainer = try container.nestedContainer( - keyedBy: DivDataCodingKeys.self, - forKey: .divData - ) +private struct IntegrationTestData { + let divData: DivData + let testCase: IntegrationTestCase +} - self.description = try container.decode(String.self, forKey: .description) - self.divData = try cardContainer.decode(DivData.self, forKey: .card) - self.cases = try container.decode([IntegrationTestCase].self, forKey: .cases) +private struct IntegrationTest: Decodable, @unchecked Sendable { + init(_ data: Data) throws { + let json = try JSONSerialization.jsonObject(with: data) as! [String: Any] + let casesJson = json["cases"]! + let divDataJson = json["div_data"] as! [String: Any] + let card = divDataJson["card"] as! [String: Any] + let templates = divDataJson["templates"] as? [String: Any] ?? [:] + + self.description = json["description"] as! String + self.divData = try DivData.resolve(card: card, templates: templates).unwrap() + self.cases = try! JSONDecoder().decode( + [IntegrationTestCase].self, + from: try JSONSerialization.data(withJSONObject: casesJson) + ) } let description: String let divData: DivData let cases: [IntegrationTestCase] - - private enum CodingKeys: String, CodingKey { - case description - case divData = "div_data" - case cases - } - - private enum DivDataCodingKeys: String, CodingKey { - case card - } } private struct IntegrationTestCase: Decodable {