aboutsummaryrefslogtreecommitdiff
path: root/tests/ref.rs
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2016-11-19 16:16:20 -0800
committerJoe Wilm <joe@jwilm.com>2016-11-19 21:34:11 -0800
commit66dbd29cd194a4c84f796f32827429895c2a3bba (patch)
tree5f5322cdae53d4fc295fe85f519192e9d38aab03 /tests/ref.rs
parentd97996e19de6856c23c51d05ec10f10db41e309d (diff)
downloadalacritty-66dbd29cd194a4c84f796f32827429895c2a3bba.tar.gz
alacritty-66dbd29cd194a4c84f796f32827429895c2a3bba.zip
Add support for recording/running ref tests
Ref tests use a recording of the terminal protocol and a serialization of the grid state to check that the parsing and action handling systems produce the correct result. Ref tests may be recorded by running alacritty with `--ref-test` and closing the terminal by using the window "X" button. At that point, the recording is fully written to disk, and a serialization of important state is recorded. Those files should be moved to an appropriate folder in the `tests/ref/` tree, and the `ref_test!` macro invocation should be updated accordingly. A couple of changes were necessary to make this work: * Ref tests shouldn't create a pty; the pty was refactored out of the `Term` type. * Repeatable lines/cols were needed; on startup, the terminal is resized * by default to 80x24 though that may be changed by passing `--dimensions w h`. * Calculating window size based on desired rows/columns and font metrics required making load_font callable multiple times. * Refactor types into library crate so they may be imported in an integration test. * A whole bunch of types needed symmetric serialization and deserialization. Mostly this was just adding derives, but the custom deserialization of Rgb had to change to a deserialize_with function. This initially adds one ref test as a sanity check, and more will be added in subsequent commits. This initial ref tests just starts the terminal and runs `ll`.
Diffstat (limited to 'tests/ref.rs')
-rw-r--r--tests/ref.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/ref.rs b/tests/ref.rs
new file mode 100644
index 00000000..029bf91e
--- /dev/null
+++ b/tests/ref.rs
@@ -0,0 +1,78 @@
+extern crate alacritty;
+extern crate serde_json;
+
+/// ref tests
+mod reference {
+ use std::fs::File;
+ use std::io::Read;
+ use std::path::Path;
+
+ use serde_json as json;
+
+ use alacritty::Grid;
+ use alacritty::Term;
+ use alacritty::term::Cell;
+ use alacritty::term::SizeInfo;
+ use alacritty::ansi;
+
+ macro_rules! ref_file {
+ ($ref_name:ident, $file:expr) => {
+ concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/tests/ref/", stringify!($ref_name), "/", $file
+ )
+ }
+ }
+
+ fn read_u8<P>(path: P) -> Vec<u8>
+ where P: AsRef<Path>
+ {
+ let mut res = Vec::new();
+ File::open(path.as_ref()).unwrap()
+ .read_to_end(&mut res).unwrap();
+
+ res
+ }
+
+ fn read_string<P>(path: P) -> String
+ where P: AsRef<Path>
+ {
+ let mut res = String::new();
+ File::open(path.as_ref()).unwrap()
+ .read_to_string(&mut res).unwrap();
+
+ res
+ }
+
+ macro_rules! ref_test {
+ ($name:ident) => {
+ #[test]
+ fn $name() {
+ let recording = read_u8(ref_file!($name, "alacritty.recording"));
+ let serialized_size = read_string(ref_file!($name, "size.json"));
+ let serialized_grid = read_string(ref_file!($name, "grid.json"));
+
+ let size: SizeInfo = json::from_str(&serialized_size[..]).unwrap();
+ let grid: Grid<Cell> = json::from_str(&serialized_grid[..]).unwrap();
+
+ let mut terminal = Term::new(size);
+ let mut parser = ansi::Processor::new();
+
+ for byte in recording {
+ parser.advance(&mut terminal, byte);
+ }
+
+ assert_eq!(grid, *terminal.grid());
+ }
+ };
+
+ ($( $name:ident ),*) => {
+ $(
+ ref_test! { $name }
+ ),*
+ }
+ }
+
+ // Ref tests!
+ ref_test! { ll }
+}