1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// Copyright (C) 2024 Rubén Beltrán del Río
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
import CoreGraphics
// You should have received a copy of the GNU General Public License
// along with this program. If not, see https://map.tranquil.systems.
import Testing
@testable import Map
struct MapParserStrategyTests {
@Test func testVertexParserStrategyCanHandle() async throws {
let strategy = VertexParserStrategy()
#expect(strategy.canHandle(line: "Node (10, 20)"))
#expect(strategy.canHandle(line: "NodeWithDecimals (30.5, 40.2)"))
#expect(strategy.canHandle(line: "Multi Word Node (0, 100)"))
#expect(strategy.canHandle(line: "Node C (50, 60) [square]"))
#expect(!strategy.canHandle(line: "Node A -> Node B"))
#expect(!strategy.canHandle(line: "[Note] (10, 20) Test"))
#expect(!strategy.canHandle(line: "Invalid line"))
}
@Test func testVertexParserStrategyHandle() async throws {
let strategy = VertexParserStrategy()
let vertices: [String: Vertex] = [:]
let (type, object) = strategy.handle(index: 0, line: "Test Node (25, 75)", vertices: vertices)
#expect(String(describing: type) == String(describing: Vertex.self))
let vertex = object as! Vertex
#expect(vertex.id == 0)
#expect(vertex.label == "Test Node")
#expect(vertex.position.x == 25.0)
#expect(vertex.position.y == 75.0)
#expect(vertex.shape == .circle)
}
@Test func testVertexParserStrategyHandleWithShape() async throws {
let strategy = VertexParserStrategy()
let vertices: [String: Vertex] = [:]
let (type, object) = strategy.handle(
index: 1, line: "Shaped Node (10, 20) [triangle]", vertices: vertices)
#expect(String(describing: type) == String(describing: Vertex.self))
let vertex = object as! Vertex
#expect(vertex.shape == .triangle)
}
@Test func testEdgeParserStrategyCanHandle() async throws {
let strategy = EdgeParserStrategy()
#expect(strategy.canHandle(line: "Node A -- Node B"))
#expect(strategy.canHandle(line: "Node A -> Node B"))
#expect(strategy.canHandle(line: "Multi Word A -> Multi Word B"))
#expect(!strategy.canHandle(line: "Node A (10, 20)"))
#expect(!strategy.canHandle(line: "[Note] (10, 20) Test"))
#expect(!strategy.canHandle(line: "Invalid line"))
}
@Test func testEdgeParserStrategyHandle() async throws {
let strategy = EdgeParserStrategy()
let vertices: [String: Vertex] = [
"Node A": Vertex(id: 0, label: "Node A", position: CGPoint(x: 10, y: 20)),
"Node B": Vertex(id: 1, label: "Node B", position: CGPoint(x: 30, y: 40)),
]
let (type, object) = strategy.handle(index: 2, line: "Node A -> Node B", vertices: vertices)
#expect(String(describing: type) == String(describing: MapEdge.self))
let edge = object as! MapEdge
#expect(edge.id == 2)
#expect(edge.origin.x == 10.0)
#expect(edge.origin.y == 20.0)
#expect(edge.destination.x == 30.0)
#expect(edge.destination.y == 40.0)
#expect(edge.arrowhead == true)
}
@Test func testEdgeParserStrategyHandleWithMissingVertex() async throws {
let strategy = EdgeParserStrategy()
let vertices: [String: Vertex] = [
"Node A": Vertex(id: 0, label: "Node A", position: CGPoint(x: 10, y: 20))
]
let (type, object) = strategy.handle(index: 2, line: "Node A -> Node B", vertices: vertices)
#expect(String(describing: type) == String(describing: NSObject.self))
#expect(object is NSObject)
}
@Test func testNoteParserStrategyCanHandle() async throws {
let strategy = NoteParserStrategy()
#expect(strategy.canHandle(line: "[Note] (10, 20) Test note"))
#expect(strategy.canHandle(line: "[Note] (50.5, 60.2) Multi word note"))
#expect(!strategy.canHandle(line: "Node A (10, 20)"))
#expect(!strategy.canHandle(line: "Node A -> Node B"))
#expect(!strategy.canHandle(line: "Invalid line"))
}
@Test func testInertiaParserStrategyCanHandle() async throws {
let strategy = InertiaParserStrategy()
#expect(strategy.canHandle(line: "[Indertia] Node A"))
#expect(strategy.canHandle(line: "[Inertia] Multi Word Node"))
#expect(!strategy.canHandle(line: "Node A (10, 20)"))
#expect(!strategy.canHandle(line: "[Note] (10, 20) Test"))
#expect(!strategy.canHandle(line: "Invalid line"))
}
@Test func testOpportunityParserStrategyCanHandle() async throws {
let strategy = OpportunityParserStrategy()
#expect(strategy.canHandle(line: "[Evolution] Node A +15"))
#expect(strategy.canHandle(line: "[Evolution] Node B -10"))
#expect(strategy.canHandle(line: "[Evolution] Multi Word Node +5.5"))
#expect(!strategy.canHandle(line: "Node A (10, 20)"))
#expect(!strategy.canHandle(line: "[Inertia] Node A"))
#expect(!strategy.canHandle(line: "Invalid line"))
}
@Test func testStageParserStrategyCanHandle() async throws {
let strategy = StageParserStrategy()
#expect(strategy.canHandle(line: "[I] 25"))
#expect(strategy.canHandle(line: "[II] 50.5"))
#expect(strategy.canHandle(line: "[III] 75"))
#expect(!strategy.canHandle(line: "[IV] 100"))
#expect(!strategy.canHandle(line: "Node A (10, 20)"))
#expect(!strategy.canHandle(line: "Invalid line"))
}
@Test func testGroupParserStrategyCanHandle() async throws {
let strategy = GroupParserStrategy()
#expect(strategy.canHandle(line: "[Group] Node A, Node B"))
#expect(strategy.canHandle(line: "[Group] Node A, Node B, Node C"))
#expect(strategy.canHandle(line: "[Group] Multi Word A, Multi Word B"))
#expect(!strategy.canHandle(line: "Node A (10, 20)"))
#expect(!strategy.canHandle(line: "[Note] (10, 20) Test"))
#expect(!strategy.canHandle(line: "Invalid line"))
}
}
|