Skip to content

Commit

Permalink
wordwrap filter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Nov 30, 2024
1 parent b81855b commit e829c30
Showing 1 changed file with 79 additions and 1 deletion.
80 changes: 79 additions & 1 deletion minijinja-contrib/tests/filters.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use minijinja::{context, Environment};
use minijinja_contrib::filters::{pluralize, wordcount};
use minijinja_contrib::filters::{pluralize, wordcount, wordwrap};
use similar_asserts::assert_eq;

#[test]
Expand Down Expand Up @@ -267,3 +267,81 @@ fn test_wordcount() {
"3"
);
}

#[test]
fn test_wordwrap() {
let mut env = minijinja::Environment::new();
env.add_filter("wordwrap", wordwrap);

// Test basic wrapping
assert_eq!(
env.render_str(
"{{ text|wordwrap(width=20) }}",
context! {
text => "This is a long piece of text that should be wrapped at a specific width."
}
)
.unwrap(),
"This is a long piece\nof text that should\nbe wrapped at a\nspecific width."
);

// Test custom wrap string
assert_eq!(
env.render_str(
"{{ text|wordwrap(width=10, wrapstring=' <br> ') }}",
context! {
text => "This is a test of custom wrap strings."
}
)
.unwrap(),
"This is <br> a test <br> of custom <br> wrap <br> strings."
);

// Test preserving newlines
assert_eq!(
env.render_str(
"{{ text|wordwrap(width=20) }}",
context! {
text => "First paragraph.\n\nSecond paragraph."
}
)
.unwrap(),
"First paragraph.\n\nSecond paragraph."
);

// Test breaking long words
assert_eq!(
env.render_str(
"{{ text|wordwrap(width=10, break_long_words=true) }}",
context! {
text => "ThisIsAVeryLongWordThatShouldBeBroken"
}
)
.unwrap(),
"ThisIsAVer\nyLongWordT\nhatShouldB\neBroken"
);

// Test not breaking long words
assert_eq!(
env.render_str(
"{{ text|wordwrap(width=10, break_long_words=false) }}",
context! {
text => "ThisIsAVeryLongWordThatShouldBeBroken"
}
)
.unwrap(),
"ThisIsAVeryLongWordThatShouldBeBroken"
);

// Test breaking on hyphens
assert_eq!(
env.render_str(
"{{ text|wordwrap(width=10, break_on_hyphens=true) }}",
context! {
text => "This-is-a-hyphenated-word"
}
)
.unwrap(),
"This-is-a-\nhyphenated\n-word"
);
}

0 comments on commit e829c30

Please sign in to comment.