From 8412c8f028e76a3c99277ed046ded312fd035348 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Tue, 2 Apr 2024 16:13:55 +0200 Subject: [PATCH] Add ConfidenceValueTests --- Package.swift | 8 +- Sources/Confidence/ConfidenceValue.swift | 4 +- .../ConfidenceValueTests.swift | 113 ++++++++++++++++++ 3 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 Tests/ConfidenceTests/ConfidenceValueTests.swift diff --git a/Package.swift b/Package.swift index 74afe395..545cf790 100644 --- a/Package.swift +++ b/Package.swift @@ -39,6 +39,12 @@ let package = Package( dependencies: [ "ConfidenceProvider", ] - ) + ), + .testTarget( + name: "ConfidenceTests", + dependencies: [ + "Confidence" + ] + ), ] ) diff --git a/Sources/Confidence/ConfidenceValue.swift b/Sources/Confidence/ConfidenceValue.swift index 3ea17ee4..71615bde 100644 --- a/Sources/Confidence/ConfidenceValue.swift +++ b/Sources/Confidence/ConfidenceValue.swift @@ -83,8 +83,8 @@ public enum ConfidenceValue: Equatable, Codable { } public func asDate() -> DateComponents? { - if case let .date(date) = self { - return date + if case let .date(dateComponents) = self { + return dateComponents } return nil diff --git a/Tests/ConfidenceTests/ConfidenceValueTests.swift b/Tests/ConfidenceTests/ConfidenceValueTests.swift new file mode 100644 index 00000000..220b2430 --- /dev/null +++ b/Tests/ConfidenceTests/ConfidenceValueTests.swift @@ -0,0 +1,113 @@ +import Confidence +import XCTest + +final class ConfidenceConfidenceValueTests: XCTestCase { + func testNull() { + let value = ConfidenceValue.null + XCTAssertTrue(value.isNull()) + } + + func testIntShouldConvertToInt() { + let value: ConfidenceValue = .integer(3) + XCTAssertEqual(value.asInteger(), 3) + } + + func testDoubleShouldConvertToDouble() { + let value: ConfidenceValue = .double(3.14) + XCTAssertEqual(value.asDouble(), 3.14) + } + + func testBoolShouldConvertToBool() { + let value: ConfidenceValue = .boolean(true) + XCTAssertEqual(value.asBoolean(), true) + } + + func testStringShouldConvertToString() { + let value: ConfidenceValue = .string("test") + XCTAssertEqual(value.asString(), "test") + } + + func testListShouldConvertToList() { + let value: ConfidenceValue = .list([.integer(3), .integer(4)]) + XCTAssertEqual(value.asList(), [.integer(3), .integer(4)]) + } + + func testStructShouldConvertToStruct() { + let value: ConfidenceValue = .structure(["field1": .integer(3), "field2": .string("test")]) + XCTAssertEqual(value.asStructure(), ["field1": .integer(3), "field2": .string("test")]) + } + + func testEmptyListAllowed() { + let value: ConfidenceValue = .list([]) + XCTAssertEqual(value.asList(), []) + } + + func testEncodeDecode() throws { + let formatter = DateFormatter() + formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" + let date = try XCTUnwrap(formatter.date(from: "2022-01-01 12:00:00")) + let dateComponents = DateComponents(year: 2024, month: 1, day: 1) + + let value: ConfidenceValue = .structure([ + "null": .null, + "bool": .boolean(true), + "int": .integer(3), + "double": .double(4.5), + "date": .date(dateComponents), + "timestamp": .timestamp(date), + "list": .list([.boolean(false), .integer(4)]), + "structure": .structure(["int": .integer(5)]), + ]) + + let result = try JSONEncoder().encode(value) + let decodedConfidenceValue = try JSONDecoder().decode(ConfidenceValue.self, from: result) + + XCTAssertEqual(value, decodedConfidenceValue) + } + + func testDecodeConfidenceValue() throws { + let formatter = DateFormatter() + formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" + let date = try XCTUnwrap(formatter.date(from: "2022-01-01 12:00:00")) + let dateComponents = DateComponents(year: 2024, month: 1, day: 1) + + let value: ConfidenceValue = .structure([ + "null": .null, + "bool": .boolean(true), + "int": .integer(3), + "double": .double(4.5), + "date": .date(dateComponents), + "timestamp": .timestamp(date), + "list": .list([.integer(3), .integer(5)]), + "structure": .structure(["field1": .string("test"), "field2": .integer(12)]), + ]) + let expected = TestConfidenceValue( + bool: true, + int: 3, + double: 4.5, + date: dateComponents, + timestamp: date, + list: [3, 5], + structure: .init(field1: "test", field2: 12)) + + let decodedConfidenceValue: TestConfidenceValue = try value.decode() + + XCTAssertEqual(decodedConfidenceValue, expected) + } + + struct TestConfidenceValue: Codable, Equatable { + var null: Bool? + var bool: Bool + var int: Int64 + var double: Double + var date: DateComponents + var timestamp: Date + var list: [Int64] + var structure: TestSubConfidenceValue + } + + struct TestSubConfidenceValue: Codable, Equatable { + var field1: String + var field2: Int64 + } +}