From 5e937668e55dd3a8a267be3fafad049858eeb2ca Mon Sep 17 00:00:00 2001 From: Skgland Date: Fri, 26 Feb 2021 00:20:53 +0100 Subject: [PATCH] fix tests --- crates/prolog_parser/tests/parse_tokens.rs | 81 ++++++++++++++++++---- 1 file changed, 69 insertions(+), 12 deletions(-) diff --git a/crates/prolog_parser/tests/parse_tokens.rs b/crates/prolog_parser/tests/parse_tokens.rs index 69b632865..96dc26193 100644 --- a/crates/prolog_parser/tests/parse_tokens.rs +++ b/crates/prolog_parser/tests/parse_tokens.rs @@ -2,8 +2,47 @@ use prolog_parser::ast::*; use prolog_parser::lexer::{Lexer, Token}; use prolog_parser::tabled_rc::TabledData; +use std::fmt::{Debug, Formatter}; use std::rc::Rc; +fn token_eq(l: &Token, r: &Token) -> bool { + match (l, r) { + (Token::Constant(cl), Token::Constant(cr)) => cl == cr, + (Token::Var(vl), Token::Var(vr)) => vl == vr, + (Token::Open, Token::Open) + | (Token::OpenCT, Token::OpenCT) + | (Token::Close, Token::Close) + | (Token::OpenList, Token::OpenList) + | (Token::CloseList, Token::CloseList) + | (Token::OpenCurly, Token::OpenCurly) + | (Token::CloseCurly, Token::CloseCurly) + | (Token::HeadTailSeparator, Token::HeadTailSeparator) + | (Token::Comma, Token::Comma) + | (Token::End, Token::End) => true, + _ => false, + } +} + +fn token_slice_eq(l: &[Token], r: &[Token]) -> bool { + l.iter().zip(r.iter()).all(|(a, b)| token_eq(a, b)) +} + +struct TSW<'a>(&'a [Token]); + +impl<'a> Debug for TSW<'a> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + +impl<'a> PartialEq for TSW<'a> { + fn eq(&self, other: &Self) -> bool { + token_slice_eq(self.0, other.0) + } +} + +impl<'a> Eq for TSW<'a> {} + fn read_all_tokens(text: &str) -> Result, ParserError> { let atom_tbl = TabledData::new(Rc::new("my_module".to_string())); let flags = MachineFlags::default(); @@ -21,21 +60,30 @@ fn read_all_tokens(text: &str) -> Result, ParserError> { #[test] fn empty_multiline_comment() -> Result<(), ParserError> { let tokens = read_all_tokens("/**/ 4\n")?; - assert_eq!(tokens, [Token::Constant(Constant::Fixnum(4))]); + assert_eq!( + TSW(tokens.as_slice()), + TSW(&[Token::Constant(Constant::Fixnum(4))]) + ); Ok(()) } #[test] fn any_char_multiline_comment() -> Result<(), ParserError> { let tokens = read_all_tokens("/* █╗╚═══╝ © */ 4\n")?; - assert_eq!(tokens, [Token::Constant(Constant::Fixnum(4))]); + assert_eq!( + TSW(tokens.as_slice()), + TSW(&[Token::Constant(Constant::Fixnum(4))]) + ); Ok(()) } #[test] fn simple_char() -> Result<(), ParserError> { let tokens = read_all_tokens("'a'\n")?; - assert_eq!(tokens, [Token::Constant(Constant::Char('a'))]); + assert_eq!( + TSW(tokens.as_slice()), + TSW(&[Token::Constant(Constant::Char('a'))]) + ); Ok(()) } @@ -43,13 +91,13 @@ fn simple_char() -> Result<(), ParserError> { fn char_with_meta_seq() -> Result<(), ParserError> { let tokens = read_all_tokens(r#"'\\' '\'' '\"' '\`' "#)?; // use literal string so \ are escaped assert_eq!( - tokens, - [ + TSW(tokens.as_slice()), + TSW(&[ Token::Constant(Constant::Char('\\')), Token::Constant(Constant::Char('\'')), Token::Constant(Constant::Char('"')), Token::Constant(Constant::Char('`')) - ] + ]) ); Ok(()) } @@ -58,8 +106,8 @@ fn char_with_meta_seq() -> Result<(), ParserError> { fn char_with_control_seq() -> Result<(), ParserError> { let tokens = read_all_tokens(r"'\a' '\b' '\r' '\f' '\t' '\n' '\v' ")?; assert_eq!( - tokens, - [ + TSW(tokens.as_slice()), + TSW(&[ Token::Constant(Constant::Char('\u{07}')), Token::Constant(Constant::Char('\u{08}')), Token::Constant(Constant::Char('\r')), @@ -67,7 +115,7 @@ fn char_with_control_seq() -> Result<(), ParserError> { Token::Constant(Constant::Char('\t')), Token::Constant(Constant::Char('\n')), Token::Constant(Constant::Char('\u{0b}')), - ] + ]) ); Ok(()) } @@ -75,21 +123,30 @@ fn char_with_control_seq() -> Result<(), ParserError> { #[test] fn char_with_octseq() -> Result<(), ParserError> { let tokens = read_all_tokens(r"'\60433\' ")?; - assert_eq!(tokens, [Token::Constant(Constant::Char('愛'))]); // Japanese character + assert_eq!( + TSW(tokens.as_slice()), + TSW(&[Token::Constant(Constant::Char('愛'))]) + ); // Japanese character Ok(()) } #[test] fn char_with_octseq_0() -> Result<(), ParserError> { let tokens = read_all_tokens(r"'\0\' ")?; - assert_eq!(tokens, [Token::Constant(Constant::Char('\u{0000}'))]); + assert_eq!( + TSW(tokens.as_slice()), + TSW(&[Token::Constant(Constant::Char('\u{0000}'))]) + ); Ok(()) } #[test] fn char_with_hexseq() -> Result<(), ParserError> { let tokens = read_all_tokens(r"'\x2124\' ")?; - assert_eq!(tokens, [Token::Constant(Constant::Char('ℤ'))]); // Z math symbol + assert_eq!( + TSW(tokens.as_slice()), + TSW(&[Token::Constant(Constant::Char('ℤ'))]) + ); // Z math symbol Ok(()) }