Skip to content

Commit

Permalink
Add ConfidenceValueTests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Apr 2, 2024
1 parent d3ac9a3 commit 8412c8f
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ let package = Package(
dependencies: [
"ConfidenceProvider",
]
)
),
.testTarget(
name: "ConfidenceTests",
dependencies: [
"Confidence"
]
),
]
)
4 changes: 2 additions & 2 deletions Sources/Confidence/ConfidenceValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
113 changes: 113 additions & 0 deletions Tests/ConfidenceTests/ConfidenceValueTests.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 8412c8f

Please sign in to comment.