|
| 1 | +// |
| 2 | +// JSONStringTests.swift |
| 3 | +// JSONSchema |
| 4 | +// |
| 5 | +// Created by Kevin Hermawan on 9/28/24. |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +@testable import JSONSchema |
| 10 | + |
| 11 | +final class JSONStringTests: XCTestCase { |
| 12 | + func testValidJSON() throws { |
| 13 | + let jsonString = """ |
| 14 | + { |
| 15 | + "type": "object", |
| 16 | + "properties": { |
| 17 | + "location": { |
| 18 | + "type": "string", |
| 19 | + "description": "The location to get the weather for, e.g. Bogor, Indonesia" |
| 20 | + }, |
| 21 | + "format": { |
| 22 | + "type": "string", |
| 23 | + "description": "The format to return the weather in, e.g. 'celsius' or 'fahrenheit'", |
| 24 | + "enum": ["celsius", "fahrenheit"] |
| 25 | + } |
| 26 | + }, |
| 27 | + "required": ["location", "format"] |
| 28 | + } |
| 29 | + """ |
| 30 | + |
| 31 | + let schema = try JSONSchema(jsonString: jsonString) |
| 32 | + XCTAssertEqual(schema.type, .object) |
| 33 | + XCTAssertNil(schema.description) |
| 34 | + |
| 35 | + let objectSchema = schema.objectSchema |
| 36 | + XCTAssertNotNil(objectSchema) |
| 37 | + |
| 38 | + let properties = objectSchema?.properties |
| 39 | + XCTAssertEqual(properties?.count, 2) |
| 40 | + |
| 41 | + let locationSchema = properties?["location"] |
| 42 | + XCTAssertEqual(locationSchema?.type, .string) |
| 43 | + XCTAssertEqual(locationSchema?.description, "The location to get the weather for, e.g. Bogor, Indonesia") |
| 44 | + |
| 45 | + let formatSchema = properties?["format"] |
| 46 | + XCTAssertEqual(formatSchema?.description, "The format to return the weather in, e.g. 'celsius' or 'fahrenheit'") |
| 47 | + XCTAssertEqual(formatSchema?.enumSchema?.values, [.string("celsius"), .string("fahrenheit")]) |
| 48 | + |
| 49 | + XCTAssertEqual(objectSchema?.required, ["location", "format"]) |
| 50 | + } |
| 51 | + |
| 52 | + func testInvalidJSON() { |
| 53 | + let invalidJsonString = "{ invalid json }" |
| 54 | + |
| 55 | + XCTAssertThrowsError(try JSONSchema(jsonString: invalidJsonString)) { error in |
| 56 | + XCTAssertTrue(error is DecodingError) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments