Skip to content
4 changes: 4 additions & 0 deletions src/lex_4_25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ pub fn to_identifier(string: &str) -> Token {
return Token::Identifier(string.to_string());
}

pub fn to_version(string: &str) -> Token {
return Token::Version(string.to_string());
}

pub fn to_string_literal(string: &str) -> Token {
return Token::StringLiteral(string.to_string());
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod lex_4_25;
pub mod parse_4_25;
pub mod utils;
102 changes: 102 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use std::fmt::Debug;
use super::lex_4_25;
use super::parse_4_25;

pub mod test_utils {
use super::*;

/**
* Fail a test, panicing and printing expected and actual values
*/
pub fn fail_test<T: Debug + PartialEq>(expect: T, actual: T) {
panic!("\nExpected: {:#?} \nActual: {:#?}\n", expect, actual)
}

pub mod lexer {
use super::lex_4_25;

/**
* Advance cur in string using next_token, and check that the return matches
* the expected Token. If not, the test fails.
*/
pub fn expect_next_token(string: &Vec<char>, cur: &mut usize, token: lex_4_25::Token) {
match lex_4_25::next_token(&string, cur) {
ref next if *next == token => (),
actual => super::fail_test(token, actual)
};
}
}

pub mod parser {
use super::lex_4_25;
use super::parse_4_25;

/**
* Returns a Token as a ParseNode with no children
*/
pub fn as_leaf(token: lex_4_25::Token) -> Box<parse_4_25::ParseNode> {
Box::new(token.to_leaf())
}

/**
* Returns a ParseNode with the given Token serving as the node, and
* a passed-in array of children as the node's children.
*/
pub fn as_node(node: lex_4_25::Token, children: Vec<Box<parse_4_25::ParseNode>>) -> Box<parse_4_25::ParseNode> {
Box::new(parse_4_25::ParseNode {
node,
children
})
}

/**
* Returns a ParseNode with the given Token and children, but does
* not wrap the ParseNode in a Box
*/
pub fn as_node_raw(node: lex_4_25::Token, children: Vec<Box<parse_4_25::ParseNode>>) -> parse_4_25::ParseNode {
parse_4_25::ParseNode {
node,
children
}
}

/**
* Given an array of children, returns their ParseTree
*/
pub fn as_tree(children: Vec<parse_4_25::ParseNode>) -> parse_4_25::ParseTree {
parse_4_25::ParseTree {
children
}
}

/**
* Shorthand for parse(String::from("str-input")). Calls
* parse_4_25::parse and returns the resulting ParseTree
*/
pub fn parse_str(string: &str) -> parse_4_25::ParseTree {
parse_4_25::parse(String::from(string))
}

/**
* Given two ParseTrees, checks for equality. If unequal,
* panics and prints the prettified trees.
*/
pub fn expect_tree_eq(expect: parse_4_25::ParseTree, actual: parse_4_25::ParseTree) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the formatting here. I'd say that we should adopt it in the fail_test function, and get rid of this function and expect_node_eq in favor of using the generic fail_test

match expect == actual {
true => (),
false => super::fail_test(expect, actual)
}
}

/**
* Given two ParseNodes, checks for equality. If unequal,
* panics and prints the prettified nodes.
*/
pub fn expect_node_eq(expect: parse_4_25::ParseNode, actual: parse_4_25::ParseNode) {
match expect == actual {
true => (),
false => super::fail_test(expect, actual)
}
}
}
}
12 changes: 1 addition & 11 deletions tests/lex_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@ extern crate solfix;
#[cfg(test)]
mod lexer_tests {
use solfix::lex_4_25;

fn fail_test(expect: lex_4_25::Token, actual: lex_4_25::Token) {
panic!("Expected: {:?} | Actual: {:?}", expect, actual);
}

fn expect_next_token(s: &Vec<char>, cur: &mut usize, t: lex_4_25::Token) {
match lex_4_25::next_token(&s, cur) {
ref next if *next == t => (),
actual => fail_test(t, actual)
};
}
use solfix::utils::test_utils::lexer::*;

#[test]
fn recognition_test1() {
Expand Down
Loading