From 3cb445a31cb0db5661508c09e5b42fa13360894c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Tue, 6 Oct 2020 19:40:16 +0200 Subject: [PATCH] sexp: tests to submodule --- lib/spanned_sexp/src/lib.rs | 128 +++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 61 deletions(-) diff --git a/lib/spanned_sexp/src/lib.rs b/lib/spanned_sexp/src/lib.rs index 5662aa8..0fabba1 100644 --- a/lib/spanned_sexp/src/lib.rs +++ b/lib/spanned_sexp/src/lib.rs @@ -32,13 +32,6 @@ pub enum Sexp { List(Vec), } -#[test] -fn sexp_size() { - // I just want to see when this changes, in the diff. - use std::mem; - assert_eq!(mem::size_of::(), mem::size_of::()*5); -} - /// The representation of an s-expression parse error. pub struct Error { /// The error message. @@ -79,11 +72,6 @@ impl fmt::Debug for Error { } } -#[test] -fn show_an_error() { - assert_eq!(format!("{:?}", parse("(aaaa").unwrap_err()), "1:4: unexpected eof"); -} - fn get_line_and_column(s: &str, pos: usize) -> (usize, usize) { let mut line: usize = 1; let mut col: isize = -1; @@ -98,20 +86,6 @@ fn get_line_and_column(s: &str, pos: usize) -> (usize, usize) { (line, cmp::max(col, 0) as usize) } -#[test] -fn line_and_col_test() { - let s = "0123456789\n0123456789\n\n6"; - assert_eq!(get_line_and_column(s, 4), (1, 4)); - - assert_eq!(get_line_and_column(s, 10), (2, 0)); - assert_eq!(get_line_and_column(s, 11), (2, 0)); - assert_eq!(get_line_and_column(s, 15), (2, 4)); - - assert_eq!(get_line_and_column(s, 21), (3, 0)); - assert_eq!(get_line_and_column(s, 22), (4, 0)); - assert_eq!(get_line_and_column(s, 23), (4, 0)); - assert_eq!(get_line_and_column(s, 500), (4, 0)); -} #[cold] fn err_impl(message: &'static str, s: &str, pos: &usize) -> Err { @@ -375,43 +349,75 @@ impl fmt::Debug for Sexp { } } -#[test] -fn test_hello_world() { - assert_eq!( - parse("(hello -42\n\t -4.0 \"world\") ; comment").unwrap(), - list(&[ atom_s("hello"), atom_i(-42), atom_f(-4.0), atom_s("world") ])); -} +#[cfg(test)] +mod test { + use super::*; -#[test] -fn test_escaping() { - assert_eq!( - parse("(\"\\\"\\q\" \"1234\" 1234)").unwrap(), - list(&[ atom_s("\"\\q"), atom_s("1234"), atom_i(1234) ])); -} + #[test] + fn test_hello_world() { + assert_eq!( + parse("(hello -42\n\t -4.0 \"world\") ; comment").unwrap(), + list(&[ atom_s("hello"), atom_i(-42), atom_f(-4.0), atom_s("world") ])); + } -#[test] -fn test_pp() { - let s = "(hello world (what is (up) (4 6.4 you \"123\\\\ \\\"\")))"; - let sexp = parse(s).unwrap(); - assert_eq!(s, sexp.to_string()); - assert_eq!(s, format!("{:?}", sexp)); -} + #[test] + fn test_escaping() { + assert_eq!( + parse("(\"\\\"\\q\" \"1234\" 1234)").unwrap(), + list(&[ atom_s("\"\\q"), atom_s("1234"), atom_i(1234) ])); + } -#[test] -fn test_tight_parens() { - let s = "(hello(world))"; - let sexp = parse(s).unwrap(); - assert_eq!(sexp, Sexp::List(vec![Sexp::Atom(Atom::S("hello".into())), - Sexp::List(vec![Sexp::Atom(Atom::S("world".into()))])])); - let s = "(this (has)tight(parens))"; - let s2 = "( this ( has ) tight ( parens ) )"; - assert_eq!(parse(s).unwrap(), parse(s2).unwrap()); -} + #[test] + fn test_pp() { + let s = "(hello world (what is (up) (4 6.4 you \"123\\\\ \\\"\")))"; + let sexp = parse(s).unwrap(); + assert_eq!(s, sexp.to_string()); + assert_eq!(s, format!("{:?}", sexp)); + } + + #[test] + fn test_tight_parens() { + let s = "(hello(world))"; + let sexp = parse(s).unwrap(); + assert_eq!(sexp, Sexp::List(vec![Sexp::Atom(Atom::S("hello".into())), + Sexp::List(vec![Sexp::Atom(Atom::S("world".into()))])])); + let s = "(this (has)tight(parens))"; + let s2 = "( this ( has ) tight ( parens ) )"; + assert_eq!(parse(s).unwrap(), parse(s2).unwrap()); + } + + #[test] + fn test_space_in_atom() { + let sexp = list(&[ atom_s("hello world")]); + let sexp_as_string = sexp.to_string(); + assert_eq!("(\"hello world\")", sexp_as_string); + assert_eq!(sexp, parse(&sexp_as_string).unwrap()); + } + + #[test] + fn show_an_error() { + assert_eq!(format!("{:?}", parse("(aaaa").unwrap_err()), "1:4: unexpected eof"); + } + + #[test] + fn line_and_col_test() { + let s = "0123456789\n0123456789\n\n6"; + assert_eq!(get_line_and_column(s, 4), (1, 4)); + + assert_eq!(get_line_and_column(s, 10), (2, 0)); + assert_eq!(get_line_and_column(s, 11), (2, 0)); + assert_eq!(get_line_and_column(s, 15), (2, 4)); -#[test] -fn test_space_in_atom() { - let sexp = list(&[ atom_s("hello world")]); - let sexp_as_string = sexp.to_string(); - assert_eq!("(\"hello world\")", sexp_as_string); - assert_eq!(sexp, parse(&sexp_as_string).unwrap()); + assert_eq!(get_line_and_column(s, 21), (3, 0)); + assert_eq!(get_line_and_column(s, 22), (4, 0)); + assert_eq!(get_line_and_column(s, 23), (4, 0)); + assert_eq!(get_line_and_column(s, 500), (4, 0)); + } + + #[test] + fn sexp_size() { + // I just want to see when this changes, in the diff. + use std::mem; + assert_eq!(mem::size_of::(), mem::size_of::()*5); + } }