Skip to content

Commit 8cfbefe

Browse files
committed
Move building validity tests to separate file
1 parent 73b9127 commit 8cfbefe

File tree

2 files changed

+156
-143
lines changed

2 files changed

+156
-143
lines changed

src/Building.test.ts

Lines changed: 0 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import {
99
Turn,
1010
reverseConnection,
1111
onFloor,
12-
isValidBuilding,
13-
assertValidBuilding,
1412
} from ".";
1513

1614
const { RIGHT, LEFT, BACK, FRONT } = Direction;
@@ -298,147 +296,6 @@ it("works with multiple forks and stairs", () => {
298296
]);
299297
});
300298

301-
describe("Building.validity", () => {
302-
it("marks valid buildings correctly", () => {
303-
const valid = expect.objectContaining({ valid: true });
304-
305-
const b = new Building([new Hallway([new Room("a"), new Room("b")])]);
306-
expect(isValidBuilding(b)).toEqual(valid);
307-
assertValidBuilding(b);
308-
309-
expect(
310-
isValidBuilding(
311-
new Building([
312-
new Hallway([
313-
new Room("a"),
314-
new Room("b"),
315-
new Turn(RIGHT),
316-
new Fork(LEFT, "a", ""),
317-
]),
318-
])
319-
)
320-
).toEqual(valid);
321-
322-
expect(
323-
isValidBuilding(
324-
new Building([
325-
new Hallway([new Room("a"), new Room("b"), new Fork(LEFT, "a", "")]),
326-
new Hallway([
327-
new Room("z"),
328-
new Fork(RIGHT, reverseConnection("a"), ""),
329-
]),
330-
])
331-
)
332-
).toEqual(valid);
333-
334-
expect(
335-
isValidBuilding(
336-
new Building([
337-
new Hallway([
338-
new Room("a"),
339-
new Room("b"),
340-
new Stairs(LEFT, onFloor("a", 2)),
341-
]),
342-
new Hallway([new Room("z"), new Stairs(RIGHT, onFloor("a", 1))]),
343-
])
344-
)
345-
).toEqual(valid);
346-
});
347-
348-
it("marks buildings with duplicated names as invalid", () => {
349-
const b = new Building([new Hallway([new Room("a"), new Room("a")])]);
350-
351-
expect(isValidBuilding(b)).toEqual({
352-
valid: false,
353-
reason: "There's more than one room with the name 'a'",
354-
connectedSections: [],
355-
});
356-
expect(() => {
357-
assertValidBuilding(b);
358-
}).toThrow("There's more than one room with the name 'a'");
359-
360-
expect(
361-
isValidBuilding(
362-
new Building([
363-
new Hallway([
364-
new Room("a"),
365-
new Room("b"),
366-
new Stairs(LEFT, onFloor("c", 4)),
367-
]),
368-
new Hallway([new Room("a"), new Stairs(RIGHT, onFloor("b", 1))]),
369-
])
370-
)
371-
).toEqual(
372-
expect.objectContaining({
373-
valid: false,
374-
reason: "There's more than one room with the name 'a'",
375-
})
376-
);
377-
});
378-
379-
it("marks buildings with negative weights as invalid", () => {
380-
expect(
381-
isValidBuilding(
382-
new Building([
383-
new Hallway([new Room("a"), new Room("b"), new Fork(LEFT, "a", "")]),
384-
new Hallway([
385-
new Fork(RIGHT, "f", ""),
386-
new Room("z"),
387-
new Fork(RIGHT, reverseConnection("a"), "", -2),
388-
]),
389-
])
390-
)
391-
).toEqual(
392-
expect.objectContaining({
393-
valid: false,
394-
reason: `The edge from node 'f' to node 'ReversedConnection-----a' has a negative weight`,
395-
})
396-
);
397-
});
398-
399-
it("marks buildings with no nodes in a Hallway as invalid", () => {
400-
expect(
401-
isValidBuilding(
402-
new Building([
403-
new Hallway([
404-
new Room("a"),
405-
new Room("b"),
406-
new Fork(LEFT, reverseConnection("b"), ""),
407-
]),
408-
new Hallway([new Room("z"), new Fork(RIGHT, "b", "")]),
409-
new Hallway([new Room("c"), new Room("d")]),
410-
])
411-
)
412-
).toEqual(
413-
expect.objectContaining({
414-
valid: false,
415-
reason: "The hallway at index 2 has no nodes (Forks or Stairs)",
416-
})
417-
);
418-
});
419-
420-
it("marks buildings with disconnected graphs as invalid", () => {
421-
expect(
422-
isValidBuilding(
423-
new Building([
424-
new Hallway([new Room("a"), new Room("b"), new Fork(LEFT, "a", "")]),
425-
new Hallway([
426-
new Room("z"),
427-
new Fork(RIGHT, reverseConnection("b"), ""),
428-
]),
429-
new Hallway([new Room("c"), new Fork(RIGHT, "8", "")]),
430-
])
431-
)
432-
).toEqual(
433-
expect.objectContaining({
434-
valid: false,
435-
reason:
436-
"Not all nodes are connected; see isValidBuilding(building).connectedSections to find which node groups are separated.",
437-
})
438-
);
439-
});
440-
});
441-
442299
describe("correct transition phrasing", () => {
443300
test("for parallel hallways", () => {
444301
const building = new Building([

src/buildingValidity.test.ts

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import {
2+
Building,
3+
Hallway,
4+
Room,
5+
Turn,
6+
Fork,
7+
Stairs,
8+
Direction,
9+
isValidBuilding,
10+
assertValidBuilding,
11+
reverseConnection,
12+
onFloor,
13+
} from ".";
14+
15+
const { LEFT, RIGHT } = Direction;
16+
17+
describe("Building.validity", () => {
18+
it("marks valid buildings correctly", () => {
19+
const valid = expect.objectContaining({ valid: true });
20+
21+
const b = new Building([new Hallway([new Room("a"), new Room("b")])]);
22+
expect(isValidBuilding(b)).toEqual(valid);
23+
assertValidBuilding(b);
24+
25+
expect(
26+
isValidBuilding(
27+
new Building([
28+
new Hallway([
29+
new Room("a"),
30+
new Room("b"),
31+
new Turn(RIGHT),
32+
new Fork(LEFT, "a", ""),
33+
]),
34+
])
35+
)
36+
).toEqual(valid);
37+
38+
expect(
39+
isValidBuilding(
40+
new Building([
41+
new Hallway([new Room("a"), new Room("b"), new Fork(LEFT, "a", "")]),
42+
new Hallway([
43+
new Room("z"),
44+
new Fork(RIGHT, reverseConnection("a"), ""),
45+
]),
46+
])
47+
)
48+
).toEqual(valid);
49+
50+
expect(
51+
isValidBuilding(
52+
new Building([
53+
new Hallway([
54+
new Room("a"),
55+
new Room("b"),
56+
new Stairs(LEFT, onFloor("a", 2)),
57+
]),
58+
new Hallway([new Room("z"), new Stairs(RIGHT, onFloor("a", 1))]),
59+
])
60+
)
61+
).toEqual(valid);
62+
});
63+
64+
it("marks buildings with duplicated names as invalid", () => {
65+
const b = new Building([new Hallway([new Room("a"), new Room("a")])]);
66+
67+
expect(isValidBuilding(b)).toEqual({
68+
valid: false,
69+
reason: "There's more than one room with the name 'a'",
70+
connectedSections: [],
71+
});
72+
expect(() => {
73+
assertValidBuilding(b);
74+
}).toThrow("There's more than one room with the name 'a'");
75+
76+
expect(
77+
isValidBuilding(
78+
new Building([
79+
new Hallway([
80+
new Room("a"),
81+
new Room("b"),
82+
new Stairs(LEFT, onFloor("c", 4)),
83+
]),
84+
new Hallway([new Room("a"), new Stairs(RIGHT, onFloor("b", 1))]),
85+
])
86+
)
87+
).toEqual(
88+
expect.objectContaining({
89+
valid: false,
90+
reason: "There's more than one room with the name 'a'",
91+
})
92+
);
93+
});
94+
95+
it("marks buildings with negative weights as invalid", () => {
96+
expect(
97+
isValidBuilding(
98+
new Building([
99+
new Hallway([new Room("a"), new Room("b"), new Fork(LEFT, "a", "")]),
100+
new Hallway([
101+
new Fork(RIGHT, "f", ""),
102+
new Room("z"),
103+
new Fork(RIGHT, reverseConnection("a"), "", -2),
104+
]),
105+
])
106+
)
107+
).toEqual(
108+
expect.objectContaining({
109+
valid: false,
110+
reason: `The edge from node 'f' to node 'ReversedConnection-----a' has a negative weight`,
111+
})
112+
);
113+
});
114+
115+
it("marks buildings with no nodes in a Hallway as invalid", () => {
116+
expect(
117+
isValidBuilding(
118+
new Building([
119+
new Hallway([
120+
new Room("a"),
121+
new Room("b"),
122+
new Fork(LEFT, reverseConnection("b"), ""),
123+
]),
124+
new Hallway([new Room("z"), new Fork(RIGHT, "b", "")]),
125+
new Hallway([new Room("c"), new Room("d")]),
126+
])
127+
)
128+
).toEqual(
129+
expect.objectContaining({
130+
valid: false,
131+
reason: "The hallway at index 2 has no nodes (Forks or Stairs)",
132+
})
133+
);
134+
});
135+
136+
it("marks buildings with disconnected graphs as invalid", () => {
137+
expect(
138+
isValidBuilding(
139+
new Building([
140+
new Hallway([new Room("a"), new Room("b"), new Fork(LEFT, "a", "")]),
141+
new Hallway([
142+
new Room("z"),
143+
new Fork(RIGHT, reverseConnection("b"), ""),
144+
]),
145+
new Hallway([new Room("c"), new Fork(RIGHT, "8", "")]),
146+
])
147+
)
148+
).toEqual(
149+
expect.objectContaining({
150+
valid: false,
151+
reason:
152+
"Not all nodes are connected; see isValidBuilding(building).connectedSections to find which node groups are separated.",
153+
})
154+
);
155+
});
156+
});

0 commit comments

Comments
 (0)