Finish adding anonymous labels, unit tests

This commit is contained in:
2025-06-17 23:49:35 -07:00
parent b5c817d64d
commit 0f59dd668a
8 changed files with 166 additions and 4 deletions
+2
View File
@@ -6,6 +6,8 @@ use grammar::Tag;
use crate::{peg::ParseState, world::Stat}; use crate::{peg::ParseState, world::Stat};
pub type ParsedStat = Vec<Chunk>; pub type ParsedStat = Vec<Chunk>;
#[derive(Debug)]
pub enum Chunk { pub enum Chunk {
Verbatim(String), Verbatim(String),
Label { Label {
+65 -3
View File
@@ -24,13 +24,13 @@ pub fn process_labels(board: &mut Board) {
is_ref: _, is_ref: _,
is_anon: _, is_anon: _,
} => { } => {
let sanitized = registry.sanitize(name); name.name = registry.sanitize(name).into();
*chunk = Chunk::Verbatim(sanitized.into());
} }
} }
} }
} }
// Assign names to anonymous labels
anonymous_forward_pass(&mut stats, &mut registry); anonymous_forward_pass(&mut stats, &mut registry);
anonymous_backward_pass(&mut stats); anonymous_backward_pass(&mut stats);
@@ -40,7 +40,11 @@ pub fn process_labels(board: &mut Board) {
.into_iter() .into_iter()
.map(|chunk| match chunk { .map(|chunk| match chunk {
Chunk::Verbatim(s) => s, Chunk::Verbatim(s) => s,
_ => unimplemented!(), Chunk::Label {
is_ref: _,
is_anon: _,
name,
} => name.name.into(),
}) })
.collect(); .collect();
old_stat.code = new_code; old_stat.code = new_code;
@@ -106,3 +110,61 @@ fn anonymous_backward_pass(stats: &mut [ParsedStat]) {
} }
} }
} }
#[cfg(test)]
mod test {
use std::fs;
use insta::assert_snapshot;
use crate::world::{Board, Stat};
use super::process_labels;
fn board_from_text(path: &str) -> Board {
let input = fs::read_to_string(path).unwrap();
let codes: Vec<String> = input.split("---\n").map(|s| s.into()).collect();
let blank = fs::read("tests/blank.brd").unwrap();
let mut board = Board::from_bytes(&blank).unwrap();
board.stats = codes
.into_iter()
.map(|code| Stat {
x: 1,
y: 1,
x_step: 0,
y_step: 0,
cycle: 3,
p1: 2,
p2: 0,
p3: 0,
follower: -1,
leader: -1,
under_element: 0,
under_color: 0,
instruction_pointer: 0,
bind_index: 0,
code,
})
.collect();
board
}
fn board_to_text(board: Board) -> String {
let codes: Vec<_> = board.stats.into_iter().map(|stat| stat.code).collect();
codes.join("---\n")
}
#[test]
fn test_label_sanitization() {
let mut board = board_from_text("tests/labels/sanitize.txt");
process_labels(&mut board);
assert_snapshot!(board_to_text(board));
}
#[test]
fn test_anonymous_labels() {
let mut board = board_from_text("tests/labels/anonymous.txt");
process_labels(&mut board);
assert_snapshot!(board_to_text(board));
}
}
+1 -1
View File
@@ -194,7 +194,7 @@ fn increment(s: &mut CompactString) {
mod test { mod test {
use std::collections::HashSet; use std::collections::HashSet;
use compact_str::{CompactString, CompactStringExt}; use compact_str::CompactString;
use insta::assert_snapshot; use insta::assert_snapshot;
use crate::labels::{parse::LabelName, sanitize::Registry}; use crate::labels::{parse::LabelName, sanitize::Registry};
@@ -0,0 +1,31 @@
---
source: src/labels/process.rs
expression: board_to_text(board)
---
#cycle 1
:_
#take gems 100 a
#give score 100
#_
:a
#take gems 10 b
#give score 10
#a
:b
#take gems 1 c
#give score 1
#b
:c
Your gems have been redeemed for score.
---
#end
:touch
#if not blocked rnd _
I'm another object with anonymous labels.
#end
:_
#if blocked rndne a
Anonymous label names can be reused.
#end
:a
Compare my code with the other object's.
@@ -0,0 +1,22 @@
---
source: src/labels/process.rs
expression: board_to_text(board)
---
#end
:touch
#take gems 100 lt_
#give gems 100
You have hundreds of gems.
#end
:lt_
#take gems 10 lt_0
#give gems 10
You have tens of gems.
#end
:lt_0
#take gems 1 lt1
#give gems 1
You have at least one gem.
#end
:lt1
You don't have any gems!
BIN
View File
Binary file not shown.
+27
View File
@@ -0,0 +1,27 @@
#cycle 1
:@
#take gems 100 @f
#give score 100
#@b
:@
#take gems 10 @f
#give score 10
#@b
:@
#take gems 1 @f
#give score 1
#@b
:@
Your gems have been redeemed for score.
---
#end
:touch
#if not blocked rnd @f
I'm another object with anonymous labels.
#end
:@
#if blocked rndne @f
Anonymous label names can be reused.
#end
:@
Compare my code with the other object's.
+18
View File
@@ -0,0 +1,18 @@
#end
:touch
#take gems 100 lt100
#give gems 100
You have hundreds of gems.
#end
:lt100
#take gems 10 lt10
#give gems 10
You have tens of gems.
#end
:lt10
#take gems 1 lt1
#give gems 1
You have at least one gem.
#end
:lt1
You don't have any gems!