Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/SGFKit/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Game: SGFKit.Game, Value: PropertyValue>(
_ definition: PropertyDefinition<Game, Value>,
value: Value
) {
self.init(
identifier: definition.name,
values: value.convertToComposes()
)
}
}

/// An enum indicating a composed value of a node.
Expand Down
50 changes: 50 additions & 0 deletions Tests/SGFKitTests/PropertyTests.swift
Original file line number Diff line number Diff line change
@@ -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<Go, GoPoint> = .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<Go, String> = .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<Go, Int> = .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<Go, GoPoint> = .black
let newNode = Node<Go>(properties: [Property(definition, value: point)])
node.children.append(newNode)

#expect(go.tree.convertToSGF() == "(;FF[4];B[ab](;B[ba])(;B[cc]))")
}
}