Skip to content

Commit

Permalink
Add no-std and alloc-only tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Dec 21, 2024
1 parent fe36573 commit e7540e1
Show file tree
Hide file tree
Showing 18 changed files with 205 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
matrix:
package: [
examples/actix-web-app, fuzzing, rinja, rinja_derive, rinja_derive_standalone,
rinja_parser, testing,
rinja_parser, testing, testing-alloc, testing-no-std,
]
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:
- run: |
set -eu
for PKG in \
examples/actix-web-app fuzzing rinja rinja_derive rinja_derive_standalone rinja_parser testing
examples/actix-web-app fuzzing rinja rinja_derive rinja_derive_standalone rinja_parser testing testing-alloc testing-no-std
do
cd "$PKG"
echo "Testing: $PKG"
Expand Down Expand Up @@ -144,7 +144,7 @@ jobs:
- run: |
set -eu
for PKG in \
examples/actix-web-app fuzzing rinja rinja_derive rinja_derive_standalone rinja_parser testing
examples/actix-web-app fuzzing rinja rinja_derive rinja_derive_standalone rinja_parser testing testing-alloc testing-no-std
do
cd "$PKG"
cargo sort --check --check-format --grouped
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["rinja", "rinja_derive", "rinja_parser", "testing"]
members = ["rinja", "rinja_derive", "rinja_parser", "testing", "testing-alloc", "testing-no-std"]
resolver = "2"
1 change: 1 addition & 0 deletions testing-alloc/.rustfmt.toml
13 changes: 13 additions & 0 deletions testing-alloc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "rinja_testing-alloc"
version = "0.3.5"
authors = ["rinja-rs developers"]
workspace = ".."
edition = "2021"
rust-version = "1.81"
publish = false

[dev-dependencies]
rinja = { path = "../rinja", version = "0.3.5", default-features = false, features = ["alloc"] }

assert_matches = "1.5.0"
1 change: 1 addition & 0 deletions testing-alloc/LICENSE-APACHE
1 change: 1 addition & 0 deletions testing-alloc/LICENSE-MIT
1 change: 1 addition & 0 deletions testing-alloc/_typos.toml
1 change: 1 addition & 0 deletions testing-alloc/deny.toml
89 changes: 89 additions & 0 deletions testing-alloc/tests/hello-world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#![no_std]

use core::fmt;
use core::str::Utf8Error;

use assert_matches::assert_matches;
use rinja::Template;

#[test]
fn hello_world() {
#[derive(Template)]
#[template(
ext = "html",
source = "Hello {%- if let Some(user) = user? -%} , {{ user }} {%- endif -%}!"
)]
struct Hello<'a> {
user: Result<Option<&'a str>, CustomError>,
}

let mut buffer = [0; 32];

let tmpl = Hello { user: Ok(None) };
let mut cursor = Cursor::new(&mut buffer);
assert_matches!(tmpl.render_into(&mut cursor), Ok(()));
assert_eq!(cursor.finalize(), Ok("Hello!"));

let tmpl = Hello {
user: Ok(Some("user")),
};
let mut cursor = Cursor::new(&mut buffer);
assert_matches!(tmpl.render_into(&mut cursor), Ok(()));
assert_eq!(cursor.finalize(), Ok("Hello, user!"));

let tmpl = Hello {
user: Ok(Some("<user>")),
};
let mut cursor = Cursor::new(&mut buffer);
assert_matches!(tmpl.render_into(&mut cursor), Ok(()));
assert_eq!(cursor.finalize(), Ok("Hello, &#60;user&#62;!"));

let tmpl = Hello {
user: Err(CustomError),
};
let mut cursor = Cursor::new(&mut buffer);
let err = match tmpl.render_into(&mut cursor) {
Err(rinja::Error::Custom(err)) => err,
err => panic!("Expected `Err(Custom(_))`, got {err:#?}"),
};
assert!(err.is::<CustomError>());
}

struct Cursor<'a> {
data: &'a mut [u8],
pos: usize,
}

impl<'a> Cursor<'a> {
fn new(data: &'a mut [u8]) -> Self {
Self { data, pos: 0 }
}

fn finalize(self) -> Result<&'a str, Utf8Error> {
core::str::from_utf8(&self.data[..self.pos])
}
}

impl fmt::Write for Cursor<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let data = &mut self.data[self.pos..];
if data.len() >= s.len() {
data[..s.len()].copy_from_slice(s.as_bytes());
self.pos += s.len();
Ok(())
} else {
Err(fmt::Error)
}
}
}

#[derive(Debug, Clone, Copy)]
struct CustomError;

impl fmt::Display for CustomError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}

impl core::error::Error for CustomError {}
1 change: 1 addition & 0 deletions testing-alloc/tomlfmt.toml
1 change: 1 addition & 0 deletions testing-no-std/.rustfmt.toml
13 changes: 13 additions & 0 deletions testing-no-std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "rinja_testing-no-std"
version = "0.3.5"
authors = ["rinja-rs developers"]
workspace = ".."
edition = "2021"
rust-version = "1.81"
publish = false

[dev-dependencies]
rinja = { path = "../rinja", version = "0.3.5", default-features = false }

assert_matches = "1.5.0"
1 change: 1 addition & 0 deletions testing-no-std/LICENSE-APACHE
1 change: 1 addition & 0 deletions testing-no-std/LICENSE-MIT
1 change: 1 addition & 0 deletions testing-no-std/_typos.toml
1 change: 1 addition & 0 deletions testing-no-std/deny.toml
74 changes: 74 additions & 0 deletions testing-no-std/tests/hello-world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#![no_std]

use core::fmt;
use core::str::Utf8Error;

use assert_matches::assert_matches;
use rinja::Template;

#[test]
fn hello_world() {
#[derive(Template)]
#[template(
ext = "html",
source = "Hello {%- if let Some(user) = user? -%} , {{ user }} {%- endif -%}!"
)]
struct Hello<'a> {
user: Result<Option<&'a str>, fmt::Error>,
}

let mut buffer = [0; 32];

let tmpl = Hello { user: Ok(None) };
let mut cursor = Cursor::new(&mut buffer);
assert_matches!(tmpl.render_into(&mut cursor), Ok(()));
assert_eq!(cursor.finalize(), Ok("Hello!"));

let tmpl = Hello {
user: Ok(Some("user")),
};
let mut cursor = Cursor::new(&mut buffer);
assert_matches!(tmpl.render_into(&mut cursor), Ok(()));
assert_eq!(cursor.finalize(), Ok("Hello, user!"));

let tmpl = Hello {
user: Ok(Some("<user>")),
};
let mut cursor = Cursor::new(&mut buffer);
assert_matches!(tmpl.render_into(&mut cursor), Ok(()));
assert_eq!(cursor.finalize(), Ok("Hello, &#60;user&#62;!"));

let tmpl = Hello {
user: Err(fmt::Error),
};
let mut cursor = Cursor::new(&mut buffer);
assert_matches!(tmpl.render_into(&mut cursor), Err(rinja::Error::Fmt));
}

struct Cursor<'a> {
data: &'a mut [u8],
pos: usize,
}

impl<'a> Cursor<'a> {
fn new(data: &'a mut [u8]) -> Self {
Self { data, pos: 0 }
}

fn finalize(self) -> Result<&'a str, Utf8Error> {
core::str::from_utf8(&self.data[..self.pos])
}
}

impl fmt::Write for Cursor<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let data = &mut self.data[self.pos..];
if data.len() >= s.len() {
data[..s.len()].copy_from_slice(s.as_bytes());
self.pos += s.len();
Ok(())
} else {
Err(fmt::Error)
}
}
}
1 change: 1 addition & 0 deletions testing-no-std/tomlfmt.toml

0 comments on commit e7540e1

Please sign in to comment.