Skip to content

Commit ba6a435

Browse files
author
Ravindra Kumar Bundela
committed
Add unit tests for DecodingErrorReporter and UnstructuredError
1 parent fd41dba commit ba6a435

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//
2+
// DecodingErrorReporterTests.swift
3+
// OBAKitCoreTests
4+
//
5+
// Copyright © Open Transit Software Foundation
6+
// This source code is licensed under the Apache 2.0 license found in the
7+
// LICENSE file in the root directory of this source tree.
8+
//
9+
10+
import Foundation
11+
import Testing
12+
@testable import OBAKitCore
13+
14+
struct DummyCodingKey: CodingKey {
15+
var stringValue: String
16+
var intValue: Int?
17+
init(stringValue: String) {
18+
self.stringValue = stringValue
19+
}
20+
init?(intValue: Int) {
21+
self.stringValue = "\(intValue)"
22+
self.intValue = intValue
23+
}
24+
}
25+
26+
@Suite(.serialized)
27+
final class DecodingErrorReporterTests {
28+
29+
@Test func Message formatting for keyNotFound() {
30+
let key = DummyCodingKey(stringValue: "missing_key")
31+
let context = DecodingError.Context(codingPath: [], debugDescription: "Key was not found.")
32+
let error = DecodingError.keyNotFound(key, context)
33+
34+
let message = DecodingErrorReporter.message(from: error)
35+
#expect(message.contains("Missing key: 'missing_key'"))
36+
#expect(message.contains("Path: root"))
37+
#expect(message.contains("Context: Key was not found."))
38+
}
39+
40+
@Test func Message formatting for typeMismatch() {
41+
let context = DecodingError.Context(codingPath: [DummyCodingKey(stringValue: "parent"), DummyCodingKey(stringValue: "child")], debugDescription: "Expected String but found Int.")
42+
let error = DecodingError.typeMismatch(String.self, context)
43+
44+
let message = DecodingErrorReporter.message(from: error)
45+
#expect(message.contains("Type mismatch (expected String)"))
46+
#expect(message.contains("Path: parent → child"))
47+
#expect(message.contains("Context: Expected String but found Int."))
48+
}
49+
50+
@Test func Message formatting for valueNotFound() {
51+
let context = DecodingError.Context(codingPath: [DummyCodingKey(stringValue: "value")], debugDescription: "Null encountered.")
52+
let error = DecodingError.valueNotFound(Int.self, context)
53+
54+
let message = DecodingErrorReporter.message(from: error)
55+
#expect(message.contains("Missing value (expected Int)"))
56+
#expect(message.contains("Path: value"))
57+
#expect(message.contains("Context: Null encountered."))
58+
}
59+
60+
@Test func Message formatting for dataCorrupted() {
61+
let context = DecodingError.Context(codingPath: [], debugDescription: "Invalid JSON.")
62+
let error = DecodingError.dataCorrupted(context)
63+
64+
let message = DecodingErrorReporter.message(from: error)
65+
#expect(message.contains("Data corrupted"))
66+
#expect(message.contains("Path: root"))
67+
#expect(message.contains("Context: Invalid JSON."))
68+
}
69+
70+
@Test func Report handler is called() {
71+
let url = URL(string: "https://api.onebusaway.org/test")!
72+
let httpMethod = "GET"
73+
let context = DecodingError.Context(codingPath: [], debugDescription: "Test")
74+
let error = DecodingError.dataCorrupted(context)
75+
76+
var handlerCalled = false
77+
var reportedURL: URL?
78+
var reportedMethod: String?
79+
var reportedMessage: String?
80+
81+
DecodingErrorReporter.reportHandler = { err, u, m, msg in
82+
handlerCalled = true
83+
reportedURL = u
84+
reportedMethod = m
85+
reportedMessage = msg
86+
}
87+
88+
DecodingErrorReporter.report(error: error, url: url, httpMethod: httpMethod)
89+
90+
#expect(handlerCalled == true)
91+
#expect(reportedURL == url)
92+
#expect(reportedMethod == "GET")
93+
#expect(reportedMessage?.contains("Data corrupted") == true)
94+
95+
// Cleanup
96+
DecodingErrorReporter.reportHandler = nil
97+
}
98+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// UnstructuredErrorTests.swift
3+
// OBAKitCoreTests
4+
//
5+
// Copyright © Open Transit Software Foundation
6+
// This source code is licensed under the Apache 2.0 license found in the
7+
// LICENSE file in the root directory of this source tree.
8+
//
9+
10+
import Foundation
11+
import Testing
12+
@testable import OBAKitCore
13+
14+
@Suite(.serialized)
15+
final class UnstructuredErrorTests {
16+
17+
@Test func Initialization sets properties correctly() {
18+
let error = UnstructuredError("Something went wrong", recoverySuggestion: "Try again later")
19+
20+
#expect(error.errorDescription == "Something went wrong")
21+
#expect(error.recoverySuggestion == "Try again later")
22+
}
23+
24+
@Test func Initialization without recovery suggestion() {
25+
let error = UnstructuredError("Only description")
26+
27+
#expect(error.errorDescription == "Only description")
28+
#expect(error.recoverySuggestion == nil)
29+
}
30+
}

0 commit comments

Comments
 (0)