Croissant Runtime
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
crsn/crsn/crsn-sexp/src/test.rs

103 lines
3.4 KiB

use super::*;
use super::error::get_line_and_column;
#[test]
fn test_hello_world() {
let parsed = parse("(hello #ffcc00 'c' ' ' '\\\\' '\\n' 'not_a_char 0xf_f 0b01_01 18_446_744_073_709_551_615 9223372036854775807 9223372036854775808 -42\n\t -4.0 \"world\") ; comment").unwrap();
assert_eq!(
parsed,
list(&[
atom_s("hello"),
atom_u(0xffcc00),
atom_c('c'),
atom_c(' '),
atom_c('\\'),
atom_c('\n'),
atom_s("'not_a_char"),
atom_u(255),
atom_u(0b0101),
atom_u(18_446_744_073_709_551_615),
atom_u(9223372036854775807),
atom_u(9223372036854775808),
atom_i(-42),
atom_f(-4.0),
atom_qs("world")
]));
}
#[test]
fn test_escaping() {
assert_eq!(
parse(r#"("\"\\q\t\n\r " "1234" 1234)"#).unwrap(),
list(&[atom_qs("\"\\q\t\n\r "), atom_qs("1234"), atom_u(1234)]));
}
#[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()), Default::default()),
Sexp::List(vec![Sexp::Atom(Atom::S("world".into()), Default::default())], Default::default())], Default::default()));
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_qs("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), SourcePosition { line: 1, column: 4, index: 4 });
assert_eq!(get_line_and_column(s, 10), SourcePosition { line: 2, column: 0, index: 10 });
assert_eq!(get_line_and_column(s, 11), SourcePosition { line: 2, column: 0, index: 11 });
assert_eq!(get_line_and_column(s, 15), SourcePosition { line: 2, column: 4, index: 15 });
assert_eq!(get_line_and_column(s, 21), SourcePosition { line: 3, column: 0, index: 21 });
assert_eq!(get_line_and_column(s, 22), SourcePosition { line: 4, column: 0, index: 22 });
assert_eq!(get_line_and_column(s, 23), SourcePosition { line: 4, column: 0, index: 23 });
assert_eq!(get_line_and_column(s, 500), SourcePosition { line: 4, column: 0, index: 500 });
}
#[test]
fn sexp_size() {
// I just want to see when this changes, in the diff.
use std::mem;
assert_eq!(mem::size_of::<Sexp>(), mem::size_of::<isize>() * 6);
}
#[test]
fn test_peek() {
assert_eq!(('a', 1), peek("ahoj", 0).unwrap());
assert_eq!(('j', 4), peek("ahoj", 3).unwrap());
assert!(peek("ahoj", 4).is_err());
}
#[test]
fn test_peekn() {
assert_eq!(Some(('a', 1)), peekn("ahoj", 1, 0));
assert_eq!(Some(('h', 2)), peekn("ahoj", 2, 0));
assert_eq!(Some(('j', 4)), peekn("ahoj", 4, 0));
assert_eq!(Some(('o', 3)), peekn("ahoj", 1, 2));
assert_eq!(None, peekn("ahoj", 3, 2));
}