-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStateMachineParserTests.java
More file actions
102 lines (87 loc) · 4.98 KB
/
StateMachineParserTests.java
File metadata and controls
102 lines (87 loc) · 4.98 KB
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
package fsm;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.lang.Thread.State;
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class StateMachineParserTests {
private static final String BASE_URI = "src/test/resources/fsm/";
@Test
public void testSimpleStateMachine() {
StateMachine sm = StateMachineParser.parse(BASE_URI + "Simple.java");
StateMachine expectedSm = new StateMachine("Simple", List.of("open"), List.of("open", "closed"),
List.of(new StateMachineTransition("open", "closed", "close"),
new StateMachineTransition("open", "open", "read")));
assertStateMachineEquals(expectedSm, sm);
}
@Test
public void testOrTransition() {
// state1 || state2 => separate transitions from both state1 and state2
StateMachine sm = StateMachineParser.parse(BASE_URI + "OrTransition.java");
StateMachine expectedSm = new StateMachine("OrTransition", List.of("a"), List.of("a", "b", "c"), List
.of(new StateMachineTransition("a", "c", "action"), new StateMachineTransition("b", "c", "action")));
assertStateMachineEquals(expectedSm, sm);
}
@Test
public void testNegationTransition() {
// !state => all states except state
StateMachine sm = StateMachineParser.parse(BASE_URI + "NegationTransition.java");
StateMachine expectedSm = new StateMachine("NegationTransition", List.of("open"),
List.of("open", "closed", "locked"), List.of(new StateMachineTransition("open", "locked", "lock"),
new StateMachineTransition("closed", "locked", "lock")));
assertStateMachineEquals(expectedSm, sm);
}
@Test
public void testSelfLoop() {
// from=state, to=state or from=state => self-loop
StateMachine sm = StateMachineParser.parse(BASE_URI + "SelfLoop.java");
StateMachine expectedSm = new StateMachine("SelfLoop", List.of("idle"), List.of("idle", "running"),
List.of(new StateMachineTransition("idle", "idle", "noop"),
new StateMachineTransition("idle", "running", "start"),
new StateMachineTransition("running", "running", "tick")));
assertStateMachineEquals(expectedSm, sm);
}
@Test
public void testToOnlyTransition() {
// no from => all states contain a transition to state
StateMachine sm = StateMachineParser.parse(BASE_URI + "ToOnlyTransition.java");
StateMachine expectedSm = new StateMachine("ToOnlyTransition", List.of("a"), List.of("a", "b", "c"),
List.of(new StateMachineTransition("a", "c", "action"), new StateMachineTransition("b", "c", "action"),
new StateMachineTransition("c", "c", "action")));
assertStateMachineEquals(expectedSm, sm);
}
@Test
public void testMultipleInitialStates() {
// overloading constructors with different initial states
StateMachine sm = StateMachineParser.parse(BASE_URI + "MultipleInitialStates.java");
StateMachine expectedSm = new StateMachine("MultipleInitialStates", List.of("initialized", "uninitialized"),
List.of("initialized", "uninitialized", "error"),
List.of(new StateMachineTransition("uninitialized", "initialized", "init")));
assertStateMachineEquals(expectedSm, sm);
}
@Test
public void testExternalRefinementsInterface() {
// class name from @ExternalStateRefinements
StateMachine sm = StateMachineParser.parse(BASE_URI + "ExternalRefinements.java");
StateMachine expectedSm = new StateMachine("Connection", List.of("disconnected"),
List.of("connected", "disconnected"),
List.of(new StateMachineTransition("disconnected", "connected", "connect")));
assertStateMachineEquals(expectedSm, sm);
}
@Test
public void testConditionalTransition() {
// transitions for both branches of condition
StateMachine sm = StateMachineParser.parse(BASE_URI + "ConditionalTransition.java");
StateMachine expectedSm = new StateMachine("ConditionalTransition", List.of("off", "on"), List.of("on", "off"),
List.of(new StateMachineTransition("on", "off", "turnOff"),
new StateMachineTransition("off", "on", "turnOn")));
assertStateMachineEquals(expectedSm, sm);
}
private static void assertStateMachineEquals(StateMachine expected, StateMachine actual) {
assertNotNull(actual, "State machine should not be null");
assertEquals(expected.className(), actual.className(), "Class names should match");
assertEquals(expected.initialStates(), actual.initialStates(), "Initial states should match");
assertEquals(expected.states(), actual.states(), "States should match");
assertEquals(expected.transitions(), actual.transitions(), "State transitions should match");
}
}