1 module music_player;
2 
3 import cushion;
4 import std.datetime.stopwatch;
5 
6 @safe unittest
7 {
8 	// Programs to be driven by STM
9 	string status = "stopped";
10 	StopWatch playTime;
11 	void startMusic() { playTime.start(); status = "playing"; }
12 	void stopMusic()  { playTime.stop();  status = "stopped"; }
13 	void resetMusic() { playTime.reset(); }
14 	
15 	// Create StateTransitor instance
16 	mixin(loadStmFromCsv!"MusicPlayer"("#>"));
17 	auto stm = makeStm();
18 	
19 	// Initial state is "stop" that most left state.
20 	assert(stm.currentState == stm.State.stop);
21 	assert(stm.getStateName(stm.currentState) == "#>stop");
22 	assert(stm.getEventName(stm.Event.onStart) == "onStart");
23 	assert(status == "stopped");
24 	
25 	// onStart event / transit to the "#>play" state
26 	stm.put(stm.Event.onStart);
27 	assert(stm.currentState == stm.State.play);
28 	assert(playTime.running);
29 	() @trusted { import core.thread: Thread, msecs; Thread.sleep(10.msecs); }(); // progress in playing...
30 	assert(playTime.peek != 0.msecs);
31 	assert(status == "playing");
32 	
33 	// onStart event / transit to the "#>pause" state
34 	stm.put(stm.Event.onStart);
35 	assert(stm.currentState == stm.State.pause);
36 	assert(!playTime.running);
37 	assert(status == "stopped");
38 	
39 	// onStop event / transit to the "#>stop" state
40 	stm.put(stm.Event.onStop);
41 	assert(stm.currentState == stm.State.stop);
42 	assert(!playTime.running);
43 	() @trusted { import core.thread: Thread, msecs; Thread.sleep(10.msecs); }(); // progress in stopped...
44 	assert(playTime.peek == 0.msecs);
45 }
46 
47 
48 
49 @safe unittest
50 {
51 	// Programs to be driven by STM
52 	string status = "stopped";
53 	StopWatch playTime;
54 	void startMusic() { playTime.start(); status = "playing"; }
55 	void stopMusic()  { playTime.stop();  status = "stopped"; }
56 	void resetMusic() { playTime.reset(); }
57 	void delay(uint tim) @trusted
58 	{
59 		import core.thread: Thread, msecs;
60 		Thread.sleep(tim.msecs);
61 	}
62 
63 	// Create StateTransitor instance
64 	mixin(decodeStmFromCsv(import("MusicPlayer.stm.csv"),
65 	                       import("MusicPlayer.map.csv"),
66 	                       "MusicPlayer.stm.csv",
67 	                       "MusicPlayer.map.csv",
68 	                       "#>",
69 	                       "makeStm"));
70 	auto stm = makeStm();
71 
72 	// Initial state is "stop" that most left state.
73 	assert(stm.currentState == stm.State.stop);
74 	assert(stm.getStateName(stm.currentState) == "#>stop");
75 	assert(stm.getEventName(stm.Event.onStart) == "onStart");
76 	assert(status == "stopped");
77 
78 	// onStart event / transit to the "#>play" state
79 	stm.put(stm.Event.onStart);
80 	assert(stm.currentState == stm.State.play);
81 	assert(playTime.running);
82 	delay(10); // progress in playing...
83 	assert(playTime.peek != 0.msecs);
84 	assert(status == "playing");
85 
86 	// onStart event / transit to the "#>pause" state
87 	stm.put(stm.Event.onStart);
88 	assert(stm.currentState == stm.State.pause);
89 	assert(!playTime.running);
90 	assert(status == "stopped");
91 
92 	// onStop event / transit to the "#>stop" state
93 	stm.put(stm.Event.onStop);
94 	assert(stm.currentState == stm.State.stop);
95 	assert(!playTime.running);
96 	delay(10); // progress in stopped...
97 	assert(playTime.peek == 0.msecs);
98 }