-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternTest.java
More file actions
35 lines (31 loc) · 1.12 KB
/
Copy pathPatternTest.java
File metadata and controls
35 lines (31 loc) · 1.12 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
package com.hanserwei.patterns.memento;
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 restoresIndependentSnapshots() {
Editor editor = new Editor();
History history = new History(editor);
history.checkpoint();
editor.write("A");
history.checkpoint();
editor.write("AB");
assertTrue(history.undo());
assertEquals("A", editor.text());
assertTrue(history.undo());
assertEquals("", editor.text());
assertFalse(history.undo());
}
/** 一个编辑器不能恢复另一个编辑器的快照. */
@Test
void rejectsForeignSnapshot() {
Editor first = new Editor();
Editor second = new Editor();
assertThrows(IllegalArgumentException.class, () -> second.restore(first.save()));
}
}