diff --git a/Tests/JSONAPITests/Empty Object Decoder/EmptyObjectDecoderTests.swift b/Tests/JSONAPITests/Empty Object Decoder/EmptyObjectDecoderTests.swift index bda37d1..5e13165 100644 --- a/Tests/JSONAPITests/Empty Object Decoder/EmptyObjectDecoderTests.swift +++ b/Tests/JSONAPITests/Empty Object Decoder/EmptyObjectDecoderTests.swift @@ -32,6 +32,24 @@ class EmptyObjectDecoderTests: XCTestCase { XCTAssertNoThrow(try [UInt64].init(from: EmptyObjectDecoder())) } + func testNonEmptyArray() { + XCTAssertThrowsError(try NonEmptyArray.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayString.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayInt.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayDouble.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayBool.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayFloat.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayInt8.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayInt16.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayInt32.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayInt64.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayUInt.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayUInt8.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayUInt16.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayUInt32.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try NonEmptyArrayUInt64.init(from: EmptyObjectDecoder())) + } + func testNonEmptyStruct() { XCTAssertThrowsError(try NonEmptyStruct.init(from: EmptyObjectDecoder())) XCTAssertThrowsError(try NonEmptyStructString.init(from: EmptyObjectDecoder())) @@ -52,6 +70,7 @@ class EmptyObjectDecoderTests: XCTestCase { func testWantingNil() { XCTAssertThrowsError(try StructWithNil.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try ArrayWithNil.init(from: EmptyObjectDecoder())) } func testWantingSingleValue() { @@ -60,24 +79,33 @@ class EmptyObjectDecoderTests: XCTestCase { func testWantingNestedKeyed() { XCTAssertThrowsError(try StructWithNestedKeyedCall.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try ArrayWithNestedKeyedCall.init(from: EmptyObjectDecoder())) } func testWantingNestedUnkeyed() { XCTAssertThrowsError(try StructWithNestedUnkeyedCall.init(from: EmptyObjectDecoder())) + XCTAssertThrowsError(try ArrayWithNestedUnkeyedCall.init(from: EmptyObjectDecoder())) } func testWantsSuper() { XCTAssertThrowsError(try StructWithUnkeyedSuper.init(from: EmptyObjectDecoder())) - XCTAssertThrowsError(try StructWithKeyedSuper.init(from: EmptyObjectDecoder())) + + XCTAssertThrowsError(try ArrayWithUnkeyedSuper.init(from: EmptyObjectDecoder())) } func testKeysAndCodingPath() { XCTAssertEqual(try? EmptyObjectDecoder().container(keyedBy: EmptyStructWithCodingKeys.CodingKeys.self).allKeys.count, 0) XCTAssertEqual(try? EmptyObjectDecoder().container(keyedBy: EmptyStructWithCodingKeys.CodingKeys.self).codingPath.count, 0) + + XCTAssertEqual(try? EmptyObjectDecoder().unkeyedContainer().codingPath.count, 0) + XCTAssertEqual(try? EmptyObjectDecoder().unkeyedContainer().currentIndex, 0) + XCTAssertEqual(try? EmptyObjectDecoder().unkeyedContainer().count, 0) } } +// MARK: - struct + struct EmptyStruct: Decodable { } @@ -218,3 +246,148 @@ struct StructWithKeyedSuper: Decodable { let _ = try decoder.container(keyedBy: CodingKeys.self).superDecoder(forKey: .hello) } } + +// MARK: - array + +struct NonEmptyArray: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(T.self) + } +} + +struct NonEmptyArrayString: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(String.self) + } +} + +struct NonEmptyArrayInt: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(Int.self) + } +} + +struct NonEmptyArrayDouble: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(Double.self) + } +} + +struct NonEmptyArrayBool: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(Bool.self) + } +} + +struct NonEmptyArrayFloat: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(Float.self) + } +} + +struct NonEmptyArrayInt8: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(Int8.self) + } +} + +struct NonEmptyArrayInt16: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(Int16.self) + } +} + +struct NonEmptyArrayInt32: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(Int32.self) + } +} + +struct NonEmptyArrayInt64: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(Int64.self) + } +} + +struct NonEmptyArrayUInt: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(UInt.self) + } +} + +struct NonEmptyArrayUInt8: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(UInt8.self) + } +} + +struct NonEmptyArrayUInt16: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(UInt16.self) + } +} + +struct NonEmptyArrayUInt32: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(UInt32.self) + } +} + +struct NonEmptyArrayUInt64: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.decode(UInt64.self) + } +} + +struct ArrayWithNil: Decodable { + + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + + let _ = try container.decodeNil() + } +} + +struct ArrayWithNestedKeyedCall: Decodable { + + enum NestedKeys: String, CodingKey { + case world + } + + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + + let _ = try container.nestedContainer(keyedBy: NestedKeys.self) + } +} + +struct ArrayWithNestedUnkeyedCall: Decodable { + + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + + let _ = try container.nestedUnkeyedContainer() + } +} + +struct ArrayWithUnkeyedSuper: Decodable { + init(from decoder: Decoder) throws { + var container = try decoder.unkeyedContainer() + let _ = try container.superDecoder() + } +}