- error enum
- try
- do-try-catch
- 테스트코드 : 정상 확인 코드
- 테스트코드 : 오류 확인 코드
enum JsonError : Error {
case unSupportedArrayPattern
case unSupportedObjectPattern
}
public static func isValidate(to inputValue:String) throws {
if inputValue.isArray() {
guard inputValue.supportedArrayTypes() else {
throw JsonError.unSupportedArrayPattern
}
}
if inputValue.isObject() {
guard inputValue.supportedObjectTypes() else {
throw JsonError.unSupportedObjectPattern
}
}
}
func someFunc() -> Bool {
do {
try GrammarChecker.isValidate(to: input)
} catch JsonError.unSupportedArrayPattern {
return JsonError.unSupportedArrayPattern.isError()
} catch JsonError.unSupportedObjectPattern {
return JsonError.unSupportedObjectPattern.isError()
} catch {
// 해당 부분은 Default 형식으로 추가해주도록 체크되고 있습니다.
print("판단되지 않은 에러발생")
return true
}
}
func testPass(){
let input = "{\"name\":\"[oingbong{}}\"}"
XCTAssertNoThrow(try GrammarChecker.isValidate(to: input))
}
func testFail(){
let input = "[]"
XCTAssertThrowsError(try GrammarChecker.isValidate(to: input)) { error in
XCTAssertEqual(error as? JsonError, JsonError.unSupportedArrayPattern)
}
}