From 0f59dd668a2ba1f4f87dd796e712aaec067bfc02 Mon Sep 17 00:00:00 2001 From: Chris Mounce Date: Tue, 17 Jun 2025 23:49:35 -0700 Subject: [PATCH] Finish adding anonymous labels, unit tests --- src/labels/parse.rs | 2 + src/labels/process.rs | 68 +++++++++++++++++- src/labels/sanitize.rs | 2 +- ...bels__process__test__anonymous_labels.snap | 31 ++++++++ ...ls__process__test__label_sanitization.snap | 22 ++++++ tests/blank.brd | Bin 0 -> 195 bytes tests/labels/anonymous.txt | 27 +++++++ tests/labels/sanitize.txt | 18 +++++ 8 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 src/labels/snapshots/zasm__labels__process__test__anonymous_labels.snap create mode 100644 src/labels/snapshots/zasm__labels__process__test__label_sanitization.snap create mode 100644 tests/blank.brd create mode 100644 tests/labels/anonymous.txt create mode 100644 tests/labels/sanitize.txt diff --git a/src/labels/parse.rs b/src/labels/parse.rs index cf76ca6..7c24e3a 100644 --- a/src/labels/parse.rs +++ b/src/labels/parse.rs @@ -6,6 +6,8 @@ use grammar::Tag; use crate::{peg::ParseState, world::Stat}; pub type ParsedStat = Vec; + +#[derive(Debug)] pub enum Chunk { Verbatim(String), Label { diff --git a/src/labels/process.rs b/src/labels/process.rs index c3786b8..a61d5ce 100644 --- a/src/labels/process.rs +++ b/src/labels/process.rs @@ -24,13 +24,13 @@ pub fn process_labels(board: &mut Board) { is_ref: _, is_anon: _, } => { - let sanitized = registry.sanitize(name); - *chunk = Chunk::Verbatim(sanitized.into()); + name.name = registry.sanitize(name).into(); } } } } + // Assign names to anonymous labels anonymous_forward_pass(&mut stats, &mut registry); anonymous_backward_pass(&mut stats); @@ -40,7 +40,11 @@ pub fn process_labels(board: &mut Board) { .into_iter() .map(|chunk| match chunk { Chunk::Verbatim(s) => s, - _ => unimplemented!(), + Chunk::Label { + is_ref: _, + is_anon: _, + name, + } => name.name.into(), }) .collect(); 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 = 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)); + } +} diff --git a/src/labels/sanitize.rs b/src/labels/sanitize.rs index ba8bbcb..3998466 100644 --- a/src/labels/sanitize.rs +++ b/src/labels/sanitize.rs @@ -194,7 +194,7 @@ fn increment(s: &mut CompactString) { mod test { use std::collections::HashSet; - use compact_str::{CompactString, CompactStringExt}; + use compact_str::CompactString; use insta::assert_snapshot; use crate::labels::{parse::LabelName, sanitize::Registry}; diff --git a/src/labels/snapshots/zasm__labels__process__test__anonymous_labels.snap b/src/labels/snapshots/zasm__labels__process__test__anonymous_labels.snap new file mode 100644 index 0000000..070056d --- /dev/null +++ b/src/labels/snapshots/zasm__labels__process__test__anonymous_labels.snap @@ -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. diff --git a/src/labels/snapshots/zasm__labels__process__test__label_sanitization.snap b/src/labels/snapshots/zasm__labels__process__test__label_sanitization.snap new file mode 100644 index 0000000..1f9adc5 --- /dev/null +++ b/src/labels/snapshots/zasm__labels__process__test__label_sanitization.snap @@ -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! diff --git a/tests/blank.brd b/tests/blank.brd new file mode 100644 index 0000000000000000000000000000000000000000..8ea504342094e6a6d9fa3bb6a2009c1592738999 GIT binary patch literal 195 wcmX@ez!Q>Ll9Q@XoLrQen#VvCU}Ta1&%lq29)P%%0!Bt`CO|~7NV!Z literal 0 HcmV?d00001 diff --git a/tests/labels/anonymous.txt b/tests/labels/anonymous.txt new file mode 100644 index 0000000..498f8cd --- /dev/null +++ b/tests/labels/anonymous.txt @@ -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. diff --git a/tests/labels/sanitize.txt b/tests/labels/sanitize.txt new file mode 100644 index 0000000..0aa41f7 --- /dev/null +++ b/tests/labels/sanitize.txt @@ -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!