Skip to content

Commit

Permalink
Added tests for line parser
Browse files Browse the repository at this point in the history
  • Loading branch information
frantufro committed Jan 21, 2025
1 parent d1886f0 commit f2c98f0
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod test_case;

pub type StringId = usize;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum Block {
String(StringId)
}
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions compat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ default-run = 'cuentitos-compat'
clap = { version = "4.5.23", features = ["derive"] }
colored = "3.0.0"
glob = "0.3.1"
cuentitos-common = { path = "../common" }
3 changes: 1 addition & 2 deletions compat/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::test_runner::TestResult;
use crate::test_runner::TestRunner;
use crate::test_case::TestCase;
use glob::{glob, GlobError};
use std::path::PathBuf;
use clap::Parser;
use colored::Colorize;
use cuentitos_common::test_case::TestCase;

mod test_runner;
mod test_case;

#[derive(Parser, Debug)]
struct Args {
Expand Down
30 changes: 30 additions & 0 deletions docs/architecture/000005-lines-of-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Lines of Text

### Submitters

- Fran Tufro

## Change Log

- 2025-01-21 - [First Draft of ADR created](https://github.com/hiddenpeopleclub/cuentitos/pull/52).

## Context

Cuentitos language wants to be as writer friendly as possible, because of that
is going to be text-first.

We want to support a scripting format that will let a writer do as much as they
can with text, minimizing the use of mouse or additional tools.

The first step is to support writing lines of text that get parsed and
transformed into objects that can be processed by the runtime.

## Proposed Design

I will split functionality from data when it comes to string management.

The parser will iterate through all the script and provide an id to each line
of text, this id will be used to reference the line of text in the string table.

This is to better support internationalization, as the string table will be
replaced by the correct language at runtime.
16 changes: 16 additions & 0 deletions parser/src/line_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ pub fn parse(line: Line) -> Result<LineParserResult, ParseError> {
}
)
}

mod tests {
use super::*;

#[test]
fn test_parse() {
let line = Line {
parsed: false,
text: "Hello, world!",
};

let result = super::parse(line).unwrap();
assert_eq!(result.block, super::Block::String(1));
assert_eq!(result.string, "Hello, world!");
}
}
24 changes: 22 additions & 2 deletions parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ pub enum ParseError {
UnexpectedEndOfFile,
}



impl Parser {
pub fn parse<A>(&mut self, script: A) -> Result<Database, ParseError>
where A: AsRef<str>
Expand All @@ -38,3 +36,25 @@ impl Parser {
Ok(database)
}
}


mod test {
use cuentitos_common::test_case::TestCase;
use super::*;

#[test]
fn test_single_line_script() {
let test_case = TestCase::from_string(
include_str!("../../compatibility-tests/00000000001-single-line-and-end.md"),
"single-line.md"
);

let mut parser = Parser::default();
let database = parser.parse(test_case.script).unwrap();
assert_eq!(database.blocks.len(), 1);
assert_eq!(database.strings.len(), 1);
assert_eq!(database.blocks[0], Block::String(0));
assert_eq!(database.strings[0], "This is a single line");

}
}

0 comments on commit f2c98f0

Please sign in to comment.