Switch unit tests to new label parser

This commit is contained in:
2025-06-08 18:02:31 -07:00
parent c5df4e1ce0
commit 6ea50ac3f0
3 changed files with 80 additions and 57 deletions
+44 -55
View File
@@ -9,7 +9,11 @@ use crate::{
pub fn print_labels(b: &Stat) { pub fn print_labels(b: &Stat) {
let code = &b.code; let code = &b.code;
let mut ps = ParseState::new(code); let mut ps = ParseState::new(code);
assert!(grammar::program(&mut ps), "New parser couldn't parse: {:?}", code); assert!(
grammar::program(&mut ps),
"New parser couldn't parse: {:?}",
code
);
let mut parser = Parser::new(&b.code); let mut parser = Parser::new(&b.code);
if !program.parse(&mut parser) { if !program.parse(&mut parser) {
@@ -29,8 +33,8 @@ mod grammar {
grammar! { grammar! {
program = (line ("\n" line)*)? EOI; program = (line ("\n" line)*)? EOI;
line = label_line / statement; line = label_line / statement / text;
statement = movement+ command? / command / text; statement = movement+ command? / command;
movement = ("/" / "?") s direction; movement = ("/" / "?") s direction;
text = !("#" / "/" / "?") (!"\n" ANY)*; text = !("#" / "/" / "?") (!"\n" ANY)*;
@@ -367,85 +371,70 @@ fn ww() -> impl Rule {
mod test { mod test {
use std::fs; use std::fs;
use insta::{assert_debug_snapshot, assert_snapshot}; use insta::assert_snapshot;
use crate::preprocess::peg::Ref; use crate::peg::ParseState;
use super::*; use super::{grammar::Tag, *};
fn parse<T: Rule>(rule: &T, input: &str) { fn parse<T: Clone, F: Fn(&mut ParseState<T>) -> bool>(rule: F, input: &str) {
let mut p = Parser::new(input); use crate::peg::backend::LowLevel;
let rule = (Ref(rule), EOF); let mut p = ParseState::new(input);
assert!(rule.parse(&mut p)); assert!(rule(&mut p));
assert!(p.eoi());
} }
fn parse_err<T: Rule>(rule: &T, input: &str) { fn parse_err<T: Clone, F: Fn(&mut ParseState<T>) -> bool>(rule: F, input: &str) {
let mut p = Parser::new(input); use crate::peg::backend::LowLevel;
let rule = (Ref(rule), EOF); let mut p = ParseState::new(input);
assert!(!rule.parse(&mut p)); assert!(!rule(&mut p) || !p.eoi());
} }
#[test] #[test]
fn test_label_name() { fn test_label() {
parse(&label_name, "foo"); parse(grammar::label, "foo");
parse(&label_name, ".loop"); parse(grammar::label, ".loop");
parse(&label_name, "foo.loop"); parse(grammar::label, "foo.loop");
parse(&label_name, "ns~foo"); parse(grammar::label, "ns~foo");
parse(&label_name, "ns~.loop"); parse(grammar::label, "ns~.loop");
parse_err(&label_name, "foo."); parse_err(grammar::label, "foo.");
parse_err(&label_name, "foo.bar.baz"); parse_err(grammar::label, "foo.bar.baz");
parse_err(&label_name, "foo~bar~baz"); parse_err(grammar::label, "foo~bar~baz");
parse_err(&label_name, "~foo"); parse_err(grammar::label, "~foo");
} }
#[test] #[test]
fn test_direction() { fn test_direction() {
parse(&direction, "n"); parse(grammar::direction, "n");
parse(&direction, "north"); parse(grammar::direction, "north");
parse(&direction, "rndp rndne"); parse(grammar::direction, "rndp rndne");
parse(&direction, "opp seek"); parse(grammar::direction, "opp seek");
parse(&direction, "cw cw cw flow"); parse(grammar::direction, "cw cw cw flow");
} }
#[test] #[test]
fn test_condition() { fn test_condition() {
parse(&condition, "alligned"); parse(grammar::condition, "alligned");
parse(&condition, "blocked seek"); parse(grammar::condition, "blocked seek");
parse(&condition, "not blocked rndp seek"); parse(grammar::condition, "not blocked rndp seek");
parse(&condition, "any red lion"); parse(grammar::condition, "any red lion");
parse(&condition, "any bear"); parse(grammar::condition, "any bear");
}
#[test]
fn test_references() {
let mut p = Parser::new("#send foo");
assert!(program.parse(&mut p));
let result: Vec<_> = p
.iter()
.filter(|x| x.kind() == "ref")
.map(|x| x.text())
.collect();
assert_debug_snapshot!(result, @r#"
[
"foo",
]
"#);
} }
#[test] #[test]
fn test_label_detection() { fn test_label_detection() {
let input = fs::read_to_string("tests/labels/find-all.txt").unwrap(); let input = fs::read_to_string("tests/labels/find-all.txt").unwrap();
let mut parser = Parser::new(&input); let mut parser = ParseState::new(&input);
assert!(program.parse(&mut parser)); assert!(grammar::program(&mut parser));
let mut result = String::new(); let mut result = String::new();
let mut last_index = 0; let mut last_index = 0;
for group in parser.iter() { for group in parser.captures() {
let before = &input[last_index..group.span().start]; let before = &input[last_index..group.span().start];
let inner = match group.kind() { let inner = match group.kind() {
"label" => format!("({})", group.text()), Tag::Label => format!("({})", group.text()),
"ref" => format!("[{}]", group.text()), Tag::Message => format!("[{}]", group.text()),
_ => group.text().into(), _ => group.text().into(),
}; };
result.push_str(before); result.push_str(before);
@@ -2,8 +2,25 @@
source: src/labels/labels.rs source: src/labels/labels.rs
expression: result expression: result
--- ---
'
'Find all the labels and their references 'Find all the labels and their references
'
'Examples of labels
:(foo) :(foo)
:(namespace~foo)
:(.local)
:(@)
'
'Test sends
#send [foo] #send [foo]
#send [self:foo]
#[self:foo]
#[@b]
#[.local]
#[foo.local]
'
'Test conditional sends
#if blocked n #send [foo] #if blocked n #send [foo]
#if bar [foo] #if blocked s #[foo]
#if blocked e send [foo]
#if blocked w [foo]
+18 -1
View File
@@ -1,5 +1,22 @@
'
'Find all the labels and their references 'Find all the labels and their references
'
'Examples of labels
:foo :foo
:namespace~foo
:.local
:@
'
'Test sends
#send foo #send foo
#send self:foo
#self:foo
#@b
#.local
#foo.local
'
'Test conditional sends
#if blocked n #send foo #if blocked n #send foo
#if bar foo #if blocked s #foo
#if blocked e send foo
#if blocked w foo