@@ -132,6 +132,21 @@ final class ProtoDateTests: XCTestCase {
132132 XCTFail ( " Expected an error but none thrown. " )
133133 }
134134
135+ func testProtoDate_invalidDate_asDate_throws( ) {
136+ // Reminder: Only 30 days in November
137+ let protoDate = ProtoDate ( year: 2024 , month: 11 , day: 31 )
138+
139+ do {
140+ _ = try protoDate. asDate ( )
141+ } catch let error as ProtoDate . DateConversionError {
142+ XCTAssertTrue ( error. localizedDescription. contains ( " Invalid date " ) )
143+ return
144+ } catch {
145+ XCTFail ( " Expected \( ProtoDate . DateConversionError. self) , got \( error) . " )
146+ }
147+ XCTFail ( " Expected an error but none thrown. " )
148+ }
149+
135150 func testProtoDate_empty_asDate_throws( ) {
136151 let protoDate = ProtoDate ( year: nil , month: nil , day: nil )
137152
@@ -275,7 +290,12 @@ final class ProtoDateTests: XCTestCase {
275290
276291 do {
277292 _ = try decoder. decode ( ProtoDate . self, from: jsonData)
278- } catch DecodingError . dataCorrupted {
293+ } catch let DecodingError . dataCorrupted( context) {
294+ XCTAssertEqual (
295+ context. codingPath as? [ ProtoDate . CodingKeys ] ,
296+ [ ProtoDate . CodingKeys. year, ProtoDate . CodingKeys. month, ProtoDate . CodingKeys. day]
297+ )
298+ XCTAssertTrue ( context. debugDescription. contains ( " Invalid date " ) )
279299 return
280300 }
281301 XCTFail ( " Expected a DecodingError. " )
@@ -287,7 +307,72 @@ final class ProtoDateTests: XCTestCase {
287307
288308 do {
289309 _ = try decoder. decode ( ProtoDate . self, from: jsonData)
290- } catch DecodingError . dataCorrupted {
310+ } catch let DecodingError . dataCorrupted( context) {
311+ XCTAssertEqual (
312+ context. codingPath as? [ ProtoDate . CodingKeys ] ,
313+ [ ProtoDate . CodingKeys. year, ProtoDate . CodingKeys. month, ProtoDate . CodingKeys. day]
314+ )
315+ XCTAssertTrue ( context. debugDescription. contains ( " Invalid date " ) )
316+ return
317+ }
318+ XCTFail ( " Expected a DecodingError. " )
319+ }
320+
321+ func testDecodeProtoDate_invalidYear_throws( ) throws {
322+ let json = """
323+ {
324+ " year " : -2024,
325+ " month " : 12,
326+ " day " : 31
327+ }
328+ """
329+ let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
330+
331+ do {
332+ _ = try decoder. decode ( ProtoDate . self, from: jsonData)
333+ } catch let DecodingError . dataCorrupted( context) {
334+ XCTAssertEqual ( context. codingPath as? [ ProtoDate . CodingKeys ] , [ ProtoDate . CodingKeys. year] )
335+ XCTAssertTrue ( context. debugDescription. contains ( " Invalid year " ) )
336+ return
337+ }
338+ XCTFail ( " Expected a DecodingError. " )
339+ }
340+
341+ func testDecodeProtoDate_invalidMonth_throws( ) throws {
342+ let json = """
343+ {
344+ " year " : 2024,
345+ " month " : 13,
346+ " day " : 31
347+ }
348+ """
349+ let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
350+
351+ do {
352+ _ = try decoder. decode ( ProtoDate . self, from: jsonData)
353+ } catch let DecodingError . dataCorrupted( context) {
354+ XCTAssertEqual ( context. codingPath as? [ ProtoDate . CodingKeys ] , [ ProtoDate . CodingKeys. month] )
355+ XCTAssertTrue ( context. debugDescription. contains ( " Invalid month " ) )
356+ return
357+ }
358+ XCTFail ( " Expected a DecodingError. " )
359+ }
360+
361+ func testDecodeProtoDate_invalidDay_throws( ) throws {
362+ let json = """
363+ {
364+ " year " : 2024,
365+ " month " : 12,
366+ " day " : 32
367+ }
368+ """
369+ let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
370+
371+ do {
372+ _ = try decoder. decode ( ProtoDate . self, from: jsonData)
373+ } catch let DecodingError . dataCorrupted( context) {
374+ XCTAssertEqual ( context. codingPath as? [ ProtoDate . CodingKeys ] , [ ProtoDate . CodingKeys. day] )
375+ XCTAssertTrue ( context. debugDescription. contains ( " Invalid day " ) )
291376 return
292377 }
293378 XCTFail ( " Expected a DecodingError. " )
0 commit comments