diff --git a/src/protocol/lexer.rs b/src/protocol/lexer.rs index 4153a1a50a4698435b37f7215eae2581909d3d82..da622a3eabd0ea9c2b2d5f5e6f27aa02016ed6e9 100644 --- a/src/protocol/lexer.rs +++ b/src/protocol/lexer.rs @@ -2455,354 +2455,3 @@ impl Lexer<'_> { })) } } - -#[cfg(test)] -mod tests { - use crate::protocol::ast::*; - use crate::protocol::lexer::*; - use crate::protocol::inputsource::*; - - #[derive(Debug, Eq, PartialEq)] - enum ParserTypeClass { - Message, Bool, Byte, Short, Int, Long, String, Array, Nope - } - impl ParserTypeClass { - fn from(v: &ParserType) -> ParserTypeClass { - use ParserTypeVariant as PTV; - use ParserTypeClass as PTC; - match &v.variant { - PTV::Message => PTC::Message, - PTV::Bool => PTC::Bool, - PTV::Byte => PTC::Byte, - PTV::Short => PTC::Short, - PTV::Int => PTC::Int, - PTV::Long => PTC::Long, - PTV::String => PTC::String, - PTV::Array(_) => PTC::Array, - _ => PTC::Nope, - } - } - } - - #[test] - fn test_pragmas() { - let mut h = Heap::new(); - let mut input = InputSource::from_string(" - #version 0o7777 - #module something.dot.separated - ").expect("new InputSource"); - let mut lex = Lexer::new(&mut input); - let lexed = lex.consume_protocol_description(&mut h) - .expect("lex input source"); - let root = &h[lexed]; - assert_eq!(root.pragmas.len(), 2); - let pv = &h[root.pragmas[0]]; - let pm = &h[root.pragmas[1]]; - - if let Pragma::Version(v) = pv { - assert_eq!(v.version, 0o7777) - } else { - assert!(false, "first pragma not version"); - } - if let Pragma::Module(m) = pm { - assert_eq!(m.value, b"something.dot.separated"); - } else { - assert!(false, "second pragma not module"); - } - } - - #[test] - fn test_import() { - let mut h = Heap::new(); - let mut input = InputSource::from_string(" - // Module imports, with optional and explicit aliasing - import single_module; - import std.reo; - import something.other as alias; - // Symbol imports - import some_module::*; - import some_module::{Foo as Bar, Qux, Dix as Flu}; - import std.reo::{ - Foo as Bar, // because thing - Qux as Mox, // more explanations - Dix, /* yesh, import me */ - }; - ").unwrap(); - let mut lex = Lexer::new(&mut input); - let lexed = lex.consume_protocol_description(&mut h).unwrap(); - let root = &h[lexed]; - assert_eq!(root.imports.len(), 6); - let no_alias_single = h[root.imports[0]].as_module(); - let no_alias_multi = h[root.imports[1]].as_module(); - let with_alias = h[root.imports[2]].as_module(); - - assert_eq!(no_alias_single.module_name, b"single_module"); - assert_eq!(no_alias_single.alias, b"single_module"); - assert_eq!(no_alias_multi.module_name, b"std.reo"); - assert_eq!(no_alias_multi.alias, b"reo"); - assert_eq!(with_alias.module_name, b"something.other"); - assert_eq!(with_alias.alias, b"alias"); - - let all_symbols = h[root.imports[3]].as_symbols(); - let single_line_symbols = h[root.imports[4]].as_symbols(); - let multi_line_symbols = h[root.imports[5]].as_symbols(); - - assert_eq!(all_symbols.module_name, b"some_module"); - assert!(all_symbols.symbols.is_empty()); - assert_eq!(single_line_symbols.module_name, b"some_module"); - assert_eq!(single_line_symbols.symbols.len(), 3); - assert_eq!(single_line_symbols.symbols[0].name, b"Foo"); - assert_eq!(single_line_symbols.symbols[0].alias, b"Bar"); - assert_eq!(single_line_symbols.symbols[1].name, b"Qux"); - assert_eq!(single_line_symbols.symbols[1].alias, b"Qux"); - assert_eq!(single_line_symbols.symbols[2].name, b"Dix"); - assert_eq!(single_line_symbols.symbols[2].alias, b"Flu"); - assert_eq!(multi_line_symbols.module_name, b"std.reo"); - assert_eq!(multi_line_symbols.symbols.len(), 3); - assert_eq!(multi_line_symbols.symbols[0].name, b"Foo"); - assert_eq!(multi_line_symbols.symbols[0].alias, b"Bar"); - assert_eq!(multi_line_symbols.symbols[1].name, b"Qux"); - assert_eq!(multi_line_symbols.symbols[1].alias, b"Mox"); - assert_eq!(multi_line_symbols.symbols[2].name, b"Dix"); - assert_eq!(multi_line_symbols.symbols[2].alias, b"Dix"); - } - - #[test] - fn test_struct_definition() { - let mut h = Heap::new(); - let mut input = InputSource::from_string(" - struct Foo { - byte one, - short two, - Bar three, - } - struct Bar{int[] one, int[] two, Qux[] three} - ").unwrap(); - let mut lex = Lexer::new(&mut input); - let lexed = lex.consume_protocol_description(&mut h); - if let Err(err) = &lexed { - println!("{}", err); - } - let lexed = lexed.unwrap(); - let root = &h[lexed]; - - assert_eq!(root.definitions.len(), 2); - - // let symbolic_type = |v: &PrimitiveType| -> Vec { - // if let PrimitiveType::Symbolic(v) = v { - // v.identifier.value.clone() - // } else { - // assert!(false); - // unreachable!(); - // } - // }; - - let foo_def = h[root.definitions[0]].as_struct(); - assert_eq!(foo_def.identifier.value, b"Foo"); - assert_eq!(foo_def.fields.len(), 3); - assert_eq!(foo_def.fields[0].field.value, b"one"); - assert_eq!(ParserTypeClass::from(&h[foo_def.fields[0].parser_type]), ParserTypeClass::Byte); - assert_eq!(foo_def.fields[1].field.value, b"two"); - assert_eq!(ParserTypeClass::from(&h[foo_def.fields[1].parser_type]), ParserTypeClass::Short); - assert_eq!(foo_def.fields[2].field.value, b"three"); - // assert_eq!( - // symbolic_type(&h[foo_def.fields[2].the_type].the_type.primitive), - // Vec::from("Bar".as_bytes()) - // ); - - let bar_def = h[root.definitions[1]].as_struct(); - assert_eq!(bar_def.identifier.value, b"Bar"); - assert_eq!(bar_def.fields.len(), 3); - assert_eq!(bar_def.fields[0].field.value, b"one"); - assert_eq!(ParserTypeClass::from(&h[bar_def.fields[0].parser_type]), ParserTypeClass::Array); - assert_eq!(bar_def.fields[1].field.value, b"two"); - assert_eq!(ParserTypeClass::from(&h[bar_def.fields[1].parser_type]), ParserTypeClass::Array); - assert_eq!(bar_def.fields[2].field.value, b"three"); - assert_eq!(ParserTypeClass::from(&h[bar_def.fields[2].parser_type]), ParserTypeClass::Array); - // assert_eq!( - // symbolic_type(&h[bar_def.fields[2].parser_type].the_type.primitive), - // Vec::from("Qux".as_bytes()) - // ); - } - - #[test] - fn test_enum_definition() { - let mut h = Heap::new(); - let mut input = InputSource::from_string(" - enum Foo { - A = 0, - B = 5, - C, - D = 0xFF, - } - enum Bar { Ayoo, Byoo, Cyoo,} - enum Qux { A(byte[]), B(Bar[]), C(byte) - } - ").unwrap(); - let mut lex = Lexer::new(&mut input); - let lexed = lex.consume_protocol_description(&mut h).unwrap(); - let root = &h[lexed]; - - assert_eq!(root.definitions.len(), 3); - - let foo_def = h[root.definitions[0]].as_enum(); - assert_eq!(foo_def.identifier.value, b"Foo"); - assert_eq!(foo_def.variants.len(), 4); - assert_eq!(foo_def.variants[0].identifier.value, b"A"); - assert_eq!(foo_def.variants[0].value, EnumVariantValue::Integer(0)); - assert_eq!(foo_def.variants[1].identifier.value, b"B"); - assert_eq!(foo_def.variants[1].value, EnumVariantValue::Integer(5)); - assert_eq!(foo_def.variants[2].identifier.value, b"C"); - assert_eq!(foo_def.variants[2].value, EnumVariantValue::None); - assert_eq!(foo_def.variants[3].identifier.value, b"D"); - assert_eq!(foo_def.variants[3].value, EnumVariantValue::Integer(0xFF)); - - let bar_def = h[root.definitions[1]].as_enum(); - assert_eq!(bar_def.identifier.value, b"Bar"); - assert_eq!(bar_def.variants.len(), 3); - assert_eq!(bar_def.variants[0].identifier.value, b"Ayoo"); - assert_eq!(bar_def.variants[0].value, EnumVariantValue::None); - assert_eq!(bar_def.variants[1].identifier.value, b"Byoo"); - assert_eq!(bar_def.variants[1].value, EnumVariantValue::None); - assert_eq!(bar_def.variants[2].identifier.value, b"Cyoo"); - assert_eq!(bar_def.variants[2].value, EnumVariantValue::None); - - let qux_def = h[root.definitions[2]].as_enum(); - let enum_type = |value: &EnumVariantValue| -> &ParserType { - if let EnumVariantValue::Type(t) = value { - &h[*t] - } else { - assert!(false); - unreachable!(); - } - }; - assert_eq!(qux_def.identifier.value, b"Qux"); - assert_eq!(qux_def.variants.len(), 3); - assert_eq!(qux_def.variants[0].identifier.value, b"A"); - assert_eq!(ParserTypeClass::from(enum_type(&qux_def.variants[0].value)), ParserTypeClass::Array); - assert_eq!(qux_def.variants[1].identifier.value, b"B"); - assert_eq!(ParserTypeClass::from(enum_type(&qux_def.variants[1].value)), ParserTypeClass::Array); - // if let PrimitiveType::Symbolic(t) = &enum_type(&qux_def.variants[1].value).the_type.primitive { - // assert_eq!(t.identifier.value, Vec::from("Bar".as_bytes())); - // } else { assert!(false) } - - assert_eq!(qux_def.variants[2].identifier.value, b"C"); - assert_eq!(ParserTypeClass::from(enum_type(&qux_def.variants[2].value)), ParserTypeClass::Byte); - } - -// #[test] -// fn test_lowercase() { -// assert_eq!(lowercase(b'a'), b'a'); -// assert_eq!(lowercase(b'A'), b'a'); -// assert_eq!(lowercase(b'z'), b'z'); -// assert_eq!(lowercase(b'Z'), b'z'); -// } - -// #[test] -// fn test_basic_expression() { -// let mut h = Heap::new(); -// let mut is = InputSource::from_string("a+b;").unwrap(); -// let mut lex = Lexer::new(&mut is); -// match lex.consume_expression(&mut h) { -// Ok(expr) => { -// println!("{:?}", expr); -// if let Binary(bin) = &h[expr] { -// if let Variable(left) = &h[bin.left] { -// if let Variable(right) = &h[bin.right] { -// assert_eq!("a", format!("{}", h[left.identifier])); -// assert_eq!("b", format!("{}", h[right.identifier])); -// assert_eq!(Some(b';'), is.next()); -// return; -// } -// } -// } -// assert!(false); -// } -// Err(err) => { -// err.print(&is); -// assert!(false); -// } -// } -// } - -// #[test] -// fn test_paren_expression() { -// let mut h = Heap::new(); -// let mut is = InputSource::from_string("(true)").unwrap(); -// let mut lex = Lexer::new(&mut is); -// match lex.consume_paren_expression(&mut h) { -// Ok(expr) => { -// println!("{:#?}", expr); -// if let Constant(con) = &h[expr] { -// if let ast::Constant::True = con.value { -// return; -// } -// } -// assert!(false); -// } -// Err(err) => { -// err.print(&is); -// assert!(false); -// } -// } -// } - -// #[test] -// fn test_expression() { -// let mut h = Heap::new(); -// let mut is = InputSource::from_string("(x(1+5,get(y))-w[5])+z++\n").unwrap(); -// let mut lex = Lexer::new(&mut is); -// match lex.consume_expression(&mut h) { -// Ok(expr) => { -// println!("{:#?}", expr); -// } -// Err(err) => { -// err.print(&is); -// assert!(false); -// } -// } -// } - -// #[test] -// fn test_basic_statement() { -// let mut h = Heap::new(); -// let mut is = InputSource::from_string("while (true) { skip; }").unwrap(); -// let mut lex = Lexer::new(&mut is); -// match lex.consume_statement(&mut h) { -// Ok(stmt) => { -// println!("{:#?}", stmt); -// if let Statement::While(w) = &h[stmt] { -// if let Expression::Constant(_) = h[w.test] { -// if let Statement::Block(_) = h[w.body] { -// return; -// } -// } -// } -// assert!(false); -// } -// Err(err) => { -// err.print(&is); -// assert!(false); -// } -// } -// } - -// #[test] -// fn test_statement() { -// let mut h = Heap::new(); -// let mut is = InputSource::from_string( -// "label: while (true) { if (x++ > y[0]) break label; else continue; }\n", -// ) -// .unwrap(); -// let mut lex = Lexer::new(&mut is); -// match lex.consume_statement(&mut h) { -// Ok(stmt) => { -// println!("{:#?}", stmt); -// } -// Err(err) => { -// err.print(&is); -// assert!(false); -// } -// } -// } -}