|
| 1 | +// |
| 2 | +// SemanticVersionCodingTests.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Chris Eplett on 11/3/23. |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | + |
| 10 | +import SemanticVersion |
| 11 | + |
| 12 | +final class SemanticVersionCodingTests: XCTestCase { |
| 13 | + func test_defaultCodable_is_default() throws { |
| 14 | + XCTAssertEqual(.defaultCodable, JSONEncoder().semanticVersionEncodingStrategy) |
| 15 | + XCTAssertEqual(.defaultCodable, JSONDecoder().semanticVersionDecodingStrategy) |
| 16 | + } |
| 17 | + |
| 18 | + func test_encodable_semverString() throws { |
| 19 | + let encoder = JSONEncoder() |
| 20 | + var actual: String |
| 21 | + |
| 22 | + encoder.semanticVersionEncodingStrategy = .semverString |
| 23 | + |
| 24 | + actual = String(data: try encoder.encode(SemanticVersion(1, 2, 3)), encoding: .utf8)! |
| 25 | + XCTAssertEqual(actual, #""1.2.3""#) |
| 26 | + |
| 27 | + actual = String(data: try encoder.encode(SemanticVersion(3, 2, 1, "alpha.4")), encoding: .utf8)! |
| 28 | + XCTAssertEqual(actual, #""3.2.1-alpha.4""#) |
| 29 | + |
| 30 | + actual = String(data: try encoder.encode(SemanticVersion(3, 2, 1, "", "build.42")), encoding: .utf8)! |
| 31 | + XCTAssertEqual(actual, #""3.2.1+build.42""#) |
| 32 | + |
| 33 | + actual = String(data: try encoder.encode(SemanticVersion(7, 7, 7, "beta.423", "build.17")), encoding: .utf8)! |
| 34 | + XCTAssertEqual(actual, #""7.7.7-beta.423+build.17""#) |
| 35 | + } |
| 36 | + |
| 37 | + func test_encodable_defaultCodable() throws { |
| 38 | + let encoder = JSONEncoder() |
| 39 | + var actual: String |
| 40 | + |
| 41 | + encoder.semanticVersionEncodingStrategy = .defaultCodable |
| 42 | + |
| 43 | + actual = String(data: try encoder.encode(SemanticVersion(1, 2, 3)), encoding: .utf8)! |
| 44 | + XCTAssertTrue(actual.contains(#""major":1"#)) |
| 45 | + XCTAssertTrue(actual.contains(#""minor":2"#)) |
| 46 | + XCTAssertTrue(actual.contains(#""patch":3"#)) |
| 47 | + XCTAssertTrue(actual.contains(#""preRelease":"""#)) |
| 48 | + XCTAssertTrue(actual.contains(#""build":"""#)) |
| 49 | + |
| 50 | + actual = String(data: try encoder.encode(SemanticVersion(3, 2, 1, "alpha.4")), encoding: .utf8)! |
| 51 | + XCTAssertTrue(actual.contains(#""major":3"#)) |
| 52 | + XCTAssertTrue(actual.contains(#""minor":2"#)) |
| 53 | + XCTAssertTrue(actual.contains(#""patch":1"#)) |
| 54 | + XCTAssertTrue(actual.contains(#""preRelease":"alpha.4""#)) |
| 55 | + XCTAssertTrue(actual.contains(#""build":"""#)) |
| 56 | + |
| 57 | + actual = String(data: try encoder.encode(SemanticVersion(3, 2, 1, "", "build.42")), encoding: .utf8)! |
| 58 | + XCTAssertTrue(actual.contains(#""major":3"#)) |
| 59 | + XCTAssertTrue(actual.contains(#""minor":2"#)) |
| 60 | + XCTAssertTrue(actual.contains(#""patch":1"#)) |
| 61 | + XCTAssertTrue(actual.contains(#""preRelease":"""#)) |
| 62 | + XCTAssertTrue(actual.contains(#""build":"build.42""#)) |
| 63 | + |
| 64 | + actual = String(data: try encoder.encode(SemanticVersion(7, 7, 7, "beta.423", "build.17")), encoding: .utf8)! |
| 65 | + XCTAssertTrue(actual.contains(#""major":7"#)) |
| 66 | + XCTAssertTrue(actual.contains(#""minor":7"#)) |
| 67 | + XCTAssertTrue(actual.contains(#""patch":7"#)) |
| 68 | + XCTAssertTrue(actual.contains(#""preRelease":"beta.423""#)) |
| 69 | + XCTAssertTrue(actual.contains(#""build":"build.17""#)) |
| 70 | + } |
| 71 | + |
| 72 | + func test_decodable_semverString() throws { |
| 73 | + let decoder = JSONDecoder() |
| 74 | + var json: Data |
| 75 | + |
| 76 | + decoder.semanticVersionDecodingStrategy = .semverString |
| 77 | + |
| 78 | + json = #""1.2.3-a.4+42.7""#.data(using: .utf8)! |
| 79 | + XCTAssertEqual( |
| 80 | + try decoder.decode(SemanticVersion.self, from: json), |
| 81 | + SemanticVersion(1, 2, 3, "a.4", "42.7") |
| 82 | + ) |
| 83 | + |
| 84 | + json = #"["1.2.3-a.4+42.7", "7.7.7"]"#.data(using: .utf8)! |
| 85 | + XCTAssertEqual( |
| 86 | + try decoder.decode([SemanticVersion].self, from: json), |
| 87 | + [SemanticVersion(1, 2, 3, "a.4", "42.7"), SemanticVersion(7, 7, 7)] |
| 88 | + ) |
| 89 | + |
| 90 | + struct Foo: Decodable, Equatable { |
| 91 | + let v: SemanticVersion |
| 92 | + } |
| 93 | + |
| 94 | + json = #"{"v": "1.2.3-a.4+42.7"}"#.data(using: .utf8)! |
| 95 | + XCTAssertEqual( |
| 96 | + try decoder.decode(Foo.self, from: json), |
| 97 | + Foo(v: SemanticVersion(1, 2, 3, "a.4", "42.7")) |
| 98 | + ) |
| 99 | + |
| 100 | + json = #"{"v": "I AM NOT A SEMVER"}"#.data(using: .utf8)! |
| 101 | + XCTAssertThrowsError(_ = try decoder.decode(Foo.self, from: json)) { error in |
| 102 | + switch error as? DecodingError { |
| 103 | + case .dataCorrupted(let context): |
| 104 | + XCTAssertEqual(context.codingPath.map(\.stringValue), ["v"]) |
| 105 | + XCTAssertEqual(context.debugDescription, "Expected valid semver 2.0 string") |
| 106 | + default: |
| 107 | + XCTFail("Expected DecodingError.dataCorrupted, got \(error)") |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + func test_decodable_defaultCodable() throws { |
| 113 | + let decoder = JSONDecoder() |
| 114 | + var json: Data |
| 115 | + |
| 116 | + decoder.semanticVersionDecodingStrategy = .defaultCodable |
| 117 | + |
| 118 | + json = """ |
| 119 | + { |
| 120 | + "major": 1, |
| 121 | + "minor": 2, |
| 122 | + "patch": 3, |
| 123 | + "preRelease": "a.4", |
| 124 | + "build": "42.7" |
| 125 | + } |
| 126 | + """.data(using: .utf8)! |
| 127 | + XCTAssertEqual( |
| 128 | + try decoder.decode(SemanticVersion.self, from: json), |
| 129 | + SemanticVersion(1, 2, 3, "a.4", "42.7") |
| 130 | + ) |
| 131 | + |
| 132 | + json = """ |
| 133 | + [ |
| 134 | + { |
| 135 | + "major": 1, |
| 136 | + "minor": 2, |
| 137 | + "patch": 3, |
| 138 | + "preRelease": "a.4", |
| 139 | + "build": "42.7" |
| 140 | + },{ |
| 141 | + "major": 7, |
| 142 | + "minor": 7, |
| 143 | + "patch": 7, |
| 144 | + "preRelease": "", |
| 145 | + "build": "" |
| 146 | + } |
| 147 | + ] |
| 148 | + """.data(using: .utf8)! |
| 149 | + XCTAssertEqual( |
| 150 | + try decoder.decode([SemanticVersion].self, from: json), |
| 151 | + [SemanticVersion(1, 2, 3, "a.4", "42.7"), SemanticVersion(7, 7, 7)] |
| 152 | + ) |
| 153 | + |
| 154 | + struct Foo: Decodable, Equatable { |
| 155 | + let v: SemanticVersion |
| 156 | + } |
| 157 | + |
| 158 | + json = """ |
| 159 | + { |
| 160 | + "v": { |
| 161 | + "major": 1, |
| 162 | + "minor": 2, |
| 163 | + "patch": 3, |
| 164 | + "preRelease": "a.4", |
| 165 | + "build": "42.7" |
| 166 | + } |
| 167 | + } |
| 168 | + """.data(using: .utf8)! |
| 169 | + XCTAssertEqual( |
| 170 | + try decoder.decode(Foo.self, from: json), |
| 171 | + Foo(v: SemanticVersion(1, 2, 3, "a.4", "42.7")) |
| 172 | + ) |
| 173 | + |
| 174 | + json = """ |
| 175 | + { |
| 176 | + "v": { |
| 177 | + "major": 1, |
| 178 | + "preRelease": "a.4", |
| 179 | + "build": "42.7" |
| 180 | + } |
| 181 | + } |
| 182 | + """.data(using: .utf8)! |
| 183 | + XCTAssertThrowsError(_ = try decoder.decode(Foo.self, from: json)) { error in |
| 184 | + switch error as? DecodingError { |
| 185 | + case .keyNotFound(let key, let context): |
| 186 | + XCTAssertEqual("minor", key.stringValue) |
| 187 | + XCTAssertEqual(["v"], context.codingPath.map(\.stringValue)) |
| 188 | + default: |
| 189 | + XCTFail("Expected DecodingError.keyNotFound, got \(error)") |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments