-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternTest.java
More file actions
39 lines (35 loc) · 1.36 KB
/
Copy pathPatternTest.java
File metadata and controls
39 lines (35 loc) · 1.36 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
package com.hanserwei.patterns.command;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/** 验证本模式的协作契约与边界行为. */
public final class PatternTest {
/** 两次追加依次撤销到各自执行前状态. */
@Test
void undoesInReverseOrder() {
TextEditor editor = new TextEditor();
CommandHistory history = new CommandHistory();
history.execute(new AppendCommand(editor, "Java"));
history.execute(new AppendCommand(editor, " 25"));
assertEquals("Java 25", editor.text());
assertTrue(history.undo());
assertEquals("Java", editor.text());
assertTrue(history.undo());
assertEquals("", editor.text());
assertFalse(history.undo());
}
/** 执行失败不会额外增加历史条目. */
@Test
void excludesFailedExecution() {
TextEditor editor = new TextEditor();
Command command = new AppendCommand(editor, "Java");
CommandHistory history = new CommandHistory();
history.execute(command);
assertThrows(IllegalStateException.class, () -> history.execute(command));
assertTrue(history.undo());
assertFalse(history.undo());
assertEquals("", editor.text());
}
}