diff --git a/Sources/SGFKit/Node.swift b/Sources/SGFKit/Node.swift index cdf7aa6..5f9bbf2 100644 --- a/Sources/SGFKit/Node.swift +++ b/Sources/SGFKit/Node.swift @@ -124,6 +124,20 @@ public struct Property: Hashable, Equatable, Sendable { self.identifier = identifier self.values = values } + + /// Creates a Property with type-safe property definition and value. + /// - Parameters: + /// - definition: A property definition that specifies the property type. + /// - value: The value conforming to the property definition's value type. + public init( + _ definition: PropertyDefinition, + value: Value + ) { + self.init( + identifier: definition.name, + values: value.convertToComposes() + ) + } } /// An enum indicating a composed value of a node. diff --git a/Tests/SGFKitTests/PropertyTests.swift b/Tests/SGFKitTests/PropertyTests.swift new file mode 100644 index 0000000..1896d69 --- /dev/null +++ b/Tests/SGFKitTests/PropertyTests.swift @@ -0,0 +1,50 @@ +@testable import SGFKit +import Testing + +@Suite +struct PropertyTests { + + @Test + func propertyInitializerWithMove() throws { + let point = GoPoint(column: 3, row: 3) + let definition: PropertyDefinition = .black + let property = Property(definition, value: point) + + #expect(property.identifier == "B") + #expect(property.values == [.single("cc")]) + } + + @Test + func propertyInitializerWithText() throws { + let comment = "This is a test comment" + let definition: PropertyDefinition = .comment + let property = Property(definition, value: comment) + + #expect(property.identifier == "C") + #expect(property.values == [.single(comment)]) + } + + @Test + func propertyInitializerWithNumber() throws { + let fileFormat = 4 + let definition: PropertyDefinition = .fileFormat + let property = Property(definition, value: fileFormat) + + #expect(property.identifier == "FF") + #expect(property.values == [.single("4")]) + } + + @Test + func propertyInitializerWithList() throws { + let go = try Go(input: "(;FF[4];B[ab];B[ba])") + let rootNode = go.tree.nodes[0] + let node = rootNode.children[0] + + let point = GoPoint(column: 3, row: 3) + let definition: PropertyDefinition = .black + let newNode = Node(properties: [Property(definition, value: point)]) + node.children.append(newNode) + + #expect(go.tree.convertToSGF() == "(;FF[4];B[ab](;B[ba])(;B[cc]))") + } +}