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
4 changes: 4 additions & 0 deletions Sources/SGFKit/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ extension Collection: NodeProtocol {
func node(treeStructureDidUpdated number: Int?) {
refreshStructure()
}

public static func == (lhs: Collection<Game>, rhs: Collection<Game>) -> Bool {
lhs.nodes == rhs.nodes
}
}

private final class NumberPublisher {
Expand Down
9 changes: 8 additions & 1 deletion Sources/SGFKit/Node.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
protocol NodeProtocol<Game>: AnyObject {
protocol NodeProtocol<Game>: AnyObject, Equatable {
associatedtype Game: SGFKit.Game

var number: Int? { get }
Expand All @@ -9,6 +9,7 @@ protocol NodeProtocol<Game>: AnyObject {

/// A node object for SGF.
public final class Node<Game: SGFKit.Game> {

/// A unique number for the node in the collection.
///
/// The numbering rule follows [the official documents rule](https://www.red-bean.com/sgf/sgf4.html#1)
Expand Down Expand Up @@ -105,6 +106,12 @@ extension Node: NodeProtocol {
func node(treeStructureDidUpdated index: Int?) {
parent?.node(treeStructureDidUpdated: index)
}

public static func == (lhs: Node<Game>, rhs: Node<Game>) -> Bool {
lhs.number == rhs.number
&& lhs.children == rhs.children
&& lhs.properties == rhs.properties
}
}

/// A struct indicating a raw property of a node.
Expand Down
94 changes: 94 additions & 0 deletions Tests/SGFKitTests/NodeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
@testable import SGFKit
import Testing

@Suite
struct NodeTests {

// MARK: - Equatable Tests

@Test
func equalityWithSameProperties() throws {
let property1 = Property(identifier: "B", values: [.single("cc")])
let node1 = Node<Go>(properties: [property1])

let property2 = Property(identifier: "B", values: [.single("cc")])
let node2 = Node<Go>(properties: [property2])

#expect(node1 == node2)
}

@Test
func equalityWithDifferentProperties() throws {
let property1 = Property(identifier: "B", values: [.single("cc")])
let node1 = Node<Go>(properties: [property1])

let property2 = Property(identifier: "B", values: [.single("dd")])
let node2 = Node<Go>(properties: [property2])

#expect(node1 != node2)
}

@Test
func equalityWithDifferentNumbers() throws {
let property1 = Property(identifier: "B", values: [.single("cc")])
let node1 = Node<Go>(number: 1, properties: [property1])

let property2 = Property(identifier: "B", values: [.single("cc")])
let node2 = Node<Go>(number: 2, properties: [property2])

#expect(node1 != node2)
}

@Test
func equalityWithSameChildren() throws {
let childProperty1 = Property(identifier: "W", values: [.single("dd")])
let child1 = Node<Go>(properties: [childProperty1])

let property1 = Property(identifier: "B", values: [.single("cc")])
let node1 = Node<Go>(properties: [property1])
node1.children = [child1]

let childProperty2 = Property(identifier: "W", values: [.single("dd")])
let child2 = Node<Go>(properties: [childProperty2])

let property2 = Property(identifier: "B", values: [.single("cc")])
let node2 = Node<Go>(properties: [property2])
node2.children = [child2]

#expect(node1 == node2)
}

@Test
func equalityWithDifferentChildren() throws {
let childProperty1 = Property(identifier: "W", values: [.single("dd")])
let child1 = Node<Go>(properties: [childProperty1])

let property1 = Property(identifier: "B", values: [.single("cc")])
let node1 = Node<Go>(properties: [property1])
node1.children = [child1]

let childProperty2 = Property(identifier: "W", values: [.single("ee")])
let child2 = Node<Go>(properties: [childProperty2])

let property2 = Property(identifier: "B", values: [.single("cc")])
let node2 = Node<Go>(properties: [property2])
node2.children = [child2]

#expect(node1 != node2)
}

@Test
func equalityWithDifferentChildrenCount() throws {
let childProperty1 = Property(identifier: "W", values: [.single("dd")])
let child1 = Node<Go>(properties: [childProperty1])

let property1 = Property(identifier: "B", values: [.single("cc")])
let node1 = Node<Go>(properties: [property1])
node1.children = [child1]

let property2 = Property(identifier: "B", values: [.single("cc")])
let node2 = Node<Go>(properties: [property2])

#expect(node1 != node2)
}
}