|
| 1 | +@testable import TestConfigurator |
| 2 | +import XCTest |
| 3 | + |
| 4 | +class SkippedTestsTests: XCTestCase { |
| 5 | + // Sample JSON for skippedTests as an array of strings |
| 6 | + let jsonWithArray = """ |
| 7 | + { |
| 8 | + "skippedTests": [ |
| 9 | + "DigitalRewardsServiceTests", |
| 10 | + "LoyaltyCreditCardRewardsPointViewModelTests" |
| 11 | + ] |
| 12 | + } |
| 13 | + """.data(using: .utf8)! |
| 14 | + |
| 15 | + // Sample JSON for skippedTests as a dictionary |
| 16 | + let jsonWithDictionary = """ |
| 17 | + { |
| 18 | + "skippedTests": { |
| 19 | + "suites": [ |
| 20 | + { |
| 21 | + "name": "SparksMissionOfferVisibleTrackingEventTests" |
| 22 | + } |
| 23 | + ] |
| 24 | + } |
| 25 | + } |
| 26 | + """.data(using: .utf8)! |
| 27 | + |
| 28 | + func testDecodeSkippedTestsAsArray() throws { |
| 29 | + let decoder = JSONDecoder() |
| 30 | + let container = try decoder.decode(SkippedTestsContainer.self, from: jsonWithArray) |
| 31 | + |
| 32 | + if case let .array(skippedTests) = container.skippedTests { |
| 33 | + XCTAssertEqual(skippedTests, [ |
| 34 | + "DigitalRewardsServiceTests", |
| 35 | + "LoyaltyCreditCardRewardsPointViewModelTests" |
| 36 | + ]) |
| 37 | + } else { |
| 38 | + XCTFail("Expected skippedTests to be an array") |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + func testDecodeSkippedTestsAsDictionary() throws { |
| 43 | + let decoder = JSONDecoder() |
| 44 | + let container = try decoder.decode(SkippedTestsContainer.self, from: jsonWithDictionary) |
| 45 | + |
| 46 | + if case let .dictionary(suites) = container.skippedTests { |
| 47 | + XCTAssertEqual(suites.suites.count, 1) |
| 48 | + XCTAssertEqual(suites.suites[0].name, "SparksMissionOfferVisibleTrackingEventTests") |
| 49 | + } else { |
| 50 | + XCTFail("Expected skippedTests to be a dictionary") |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + func testEncodeSkippedTestsAsArray() throws { |
| 55 | + let container = SkippedTestsContainer( |
| 56 | + skippedTests: .array([ |
| 57 | + "DigitalRewardsServiceTests", |
| 58 | + "LoyaltyCreditCardRewardsPointViewModelTests" |
| 59 | + ]) |
| 60 | + ) |
| 61 | + |
| 62 | + let encoder = JSONEncoder() |
| 63 | + encoder.outputFormatting = .prettyPrinted |
| 64 | + let encodedData = try encoder.encode(container) |
| 65 | + let encodedString = String(data: encodedData, encoding: .utf8) |
| 66 | + |
| 67 | + XCTAssertNotNil(encodedString) |
| 68 | + XCTAssertTrue(encodedString!.contains("\"skippedTests\" : [")) |
| 69 | + XCTAssertTrue(encodedString!.contains("\"DigitalRewardsServiceTests\"")) |
| 70 | + } |
| 71 | + |
| 72 | + func testEncodeSkippedTestsAsDictionary() throws { |
| 73 | + let container = SkippedTestsContainer( |
| 74 | + skippedTests: .dictionary( |
| 75 | + Tests.Suites(suites: [ |
| 76 | + Tests.Suites.Suite(name: "SparksMissionOfferVisibleTrackingEventTests") |
| 77 | + ]) |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + let encoder = JSONEncoder() |
| 82 | + encoder.outputFormatting = .prettyPrinted |
| 83 | + let encodedData = try encoder.encode(container) |
| 84 | + let encodedString = String(data: encodedData, encoding: .utf8) |
| 85 | + |
| 86 | + XCTAssertNotNil(encodedString) |
| 87 | + XCTAssertTrue(encodedString!.contains("\"skippedTests\" : {")) |
| 88 | + XCTAssertTrue(encodedString!.contains("\"suites\" : [")) |
| 89 | + XCTAssertTrue(encodedString!.contains("\"name\" : \"SparksMissionOfferVisibleTrackingEventTests\"")) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// Container to isolate the "skippedTests" field for testing |
| 94 | +struct SkippedTestsContainer: Codable { |
| 95 | + let skippedTests: Tests |
| 96 | +} |
0 commit comments