Browse Source

Tests for the whole file

master
Julio Biason 1 year ago
parent
commit
5008ce668d
  1. 7
      pestfoamtest/src/foam.pest
  2. 75
      pestfoamtest/src/lib.rs

7
pestfoamtest/src/foam.pest

@ -3,10 +3,11 @@ multi_comment = { "/*" ~ (!"*/" ~ ANY)* ~ "*/" }
single_comment = { "//" ~ (!NEWLINE ~ ANY)* } single_comment = { "//" ~ (!NEWLINE ~ ANY)* }
identifier = { ASCII_ALPHA+ ~ (ASCII_ALPHA | ASCII_DIGIT | "_")* } identifier = { ASCII_ALPHA+ ~ (ASCII_ALPHA | ASCII_DIGIT | "_")* }
rvalue = { ('a'..'z' | 'A'..'Z' | '0'..'9' | "_" | ".")+ } rvalue = { ('a'..'z' | 'A'..'Z' | '0'..'9' | "_" | ".")+ }
attribution = { identifier ~ whitespace+ ~ rvalue ~ ";" } middle = { (whitespace | multi_comment)+ }
dictionary = { identifier ~ whitespace+ ~ "{" ~ (dictionary | attribution | whitespace)+ ~ "}" } attribution = { identifier ~ middle ~ rvalue ~ ";" }
dictionary = { identifier ~ middle ~ "{" ~ (dictionary | attribution | whitespace | multi_comment | single_comment)+ ~ "}" }
list_size = _{ ASCII_DIGIT+ ~ whitespace+ } list_size = _{ ASCII_DIGIT+ ~ whitespace+ }
list = { identifier ~ whitespace+ ~ list_size? ~ "(" ~ (rvalue | whitespace)+ ~ ");" } list = { identifier ~ middle ~ list_size? ~ "(" ~ (rvalue | whitespace | multi_comment | single_comment)+ ~ ");" }
file = { SOI ~ ( file = { SOI ~ (
whitespace whitespace

75
pestfoamtest/src/lib.rs

@ -4,93 +4,124 @@ use pest_derive::Parser;
#[derive(Parser)] #[derive(Parser)]
#[grammar = "foam.pest"] #[grammar = "foam.pest"]
pub struct Foam; struct FoamParser;
pub enum Foam {
Attribution { name: String, value: String },
}
#[cfg(test)] #[cfg(test)]
mod test { mod parser {
use super::*; use super::*;
use pest::Parser; use pest::Parser;
#[test] #[test]
fn multi_comment() { fn multi_comment() {
let parse = Foam::parse(Rule::multi_comment, "/* this is comment */"); let parse = FoamParser::parse(Rule::multi_comment, "/* this is comment */");
assert!(parse.is_ok()); assert!(parse.is_ok());
} }
#[test] #[test]
fn broken_multi() { fn broken_multi() {
let parse = Foam::parse(Rule::multi_comment, "/* bad comment"); let parse = FoamParser::parse(Rule::multi_comment, "/* bad comment");
assert!(parse.is_err()); assert!(parse.is_err());
} }
#[test] #[test]
fn single_comment() { fn single_comment() {
let parse = Foam::parse(Rule::single_comment, "// this is comment"); let parse = FoamParser::parse(Rule::single_comment, "// this is comment");
assert!(parse.is_ok()); assert!(parse.is_ok());
} }
#[test]
fn chained_comments() {
let text = "/* this is one comment */\n// And this is another";
let parse = Foam::parse(Rule::file, text);
assert!(parse.is_ok(), "{:?}", parse);
}
#[test] #[test]
fn identifier() { fn identifier() {
let parse = Foam::parse(Rule::identifier, "FoamFile"); let parse = FoamParser::parse(Rule::identifier, "FoamFile");
assert!(parse.is_ok()); assert!(parse.is_ok());
let parse = Foam::parse(Rule::identifier, "foam_file"); let parse = FoamParser::parse(Rule::identifier, "foam_file");
assert!(parse.is_ok()); assert!(parse.is_ok());
} }
#[test] #[test]
fn broken_identifer() { fn broken_identifer() {
let parse = Foam::parse(Rule::identifier, "123"); let parse = FoamParser::parse(Rule::identifier, "123");
assert!(parse.is_err()); assert!(parse.is_err());
let parse = Foam::parse(Rule::identifier, "asd "); let parse = FoamParser::parse(Rule::identifier, "asd ");
assert!(parse.is_ok(), "{:?}", parse); assert!(parse.is_ok(), "{:?}", parse);
// XXX check if the identifier lost its space. // XXX check if the identifier lost its space.
} }
#[test] #[test]
fn attribution() { fn attribution() {
let parse = Foam::parse(Rule::attribution, "version 2.0;"); let parse = FoamParser::parse(Rule::attribution, "version 2.0;");
assert!(parse.is_ok(), "{:?}", parse); assert!(parse.is_ok(), "{:?}", parse);
} }
#[test] #[test]
fn broken_attribution() { fn broken_attribution() {
let parse = Foam::parse(Rule::attribution, "version 2.0"); let parse = FoamParser::parse(Rule::attribution, "version 2.0");
assert!(parse.is_err(), "{:?}", parse); assert!(parse.is_err(), "{:?}", parse);
} }
#[test] #[test]
fn dictionary() { fn dictionary() {
let text = "FoamFile\n{\nversion 2.0;\nformat ascii;\nclass dictionary;\nlocation system;\nobject caseSetupDict;\n}"; let text = "FoamFile\n{\nversion 2.0;\nformat ascii;\nclass dictionary;\nlocation system;\nobject caseSetupDict;\n}";
let parse = Foam::parse(Rule::dictionary, text); let parse = FoamParser::parse(Rule::dictionary, text);
assert!(parse.is_ok(), "{:#?}", parse); assert!(parse.is_ok(), "{:#?}", parse);
} }
#[test] #[test]
fn dict_in_dict() { fn dict_in_dict() {
let text = "dict1 { dict2 { class bad; } }"; let text = "dict1 { dict2 { class bad; } }";
let parse = Foam::parse(Rule::dictionary, text); let parse = FoamParser::parse(Rule::dictionary, text);
assert!(parse.is_ok(), "{:#?}", parse); assert!(parse.is_ok(), "{:#?}", parse);
} }
#[test] #[test]
fn list() { fn list() {
let text = "list_name ( 1 2 3 );"; let text = "list_name ( 1 2 3 );";
let parse = Foam::parse(Rule::list, text); let parse = FoamParser::parse(Rule::list, text);
assert!(parse.is_ok(), "{:#?}", parse); assert!(parse.is_ok(), "{:#?}", parse);
} }
#[test] #[test]
fn sized_list() { fn sized_list() {
let text = "list_name 3 ( 1 2 3 );"; let text = "list_name 3 ( 1 2 3 );";
let parse = Foam::parse(Rule::list, text); let parse = FoamParser::parse(Rule::list, text);
assert!(parse.is_ok(), "{:#?}", parse); assert!(parse.is_ok(), "{:#?}", parse);
} }
} }
#[cfg(test)]
mod file {
use super::*;
use pest::Parser;
#[test]
fn chained_comments() {
let text = "/* this is one comment */\n// And this is another";
let parse = FoamParser::parse(Rule::file, text);
assert!(parse.is_ok(), "{:?}", parse);
}
#[test]
fn attribution_with_comment() {
let text = "version 2.0; // this is good";
let parse = FoamParser::parse(Rule::file, text);
assert!(parse.is_ok(), "{:?}", parse);
}
#[test]
fn mid_comment() {
let text = "version /* this is comment */ 2.0;";
let parse = FoamParser::parse(Rule::file, text);
assert!(parse.is_ok(), "{:?}", parse);
}
#[test]
fn complex() {
let text = "/* this is file */\nList ( 1\n2 // this is ok\n 3);";
let parse = FoamParser::parse(Rule::file, text);
assert!(parse.is_ok(), "{:?}", parse);
}
}

Loading…
Cancel
Save