Skip to content

Commit

Permalink
Added some missing tests to minijinja (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jun 23, 2024
1 parent f01084d commit fd6d7ba
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to MiniJinja are documented here.
- Added new methods to pycompat: `str.endswith`, `str.rfind`,
`str.isalnum`, `str.isalpha`, `str.isascii`, `str.isdigit`,
`str.isnumeric`, `str.join`, `str.startswith`. #522
- Added the missing tests `boolean`, `divisibleby`, `lower` and `upper`. #592

## 2.0.2

Expand Down
4 changes: 4 additions & 0 deletions minijinja/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ pub(crate) fn get_builtin_tests() -> BTreeMap<Cow<'static, str>, BoxedTest> {
rv.insert("escaped".into(), is_safe);
#[cfg(feature = "builtins")]
{
rv.insert("boolean".into(), BoxedTest::new(tests::is_boolean));
rv.insert("odd".into(), BoxedTest::new(tests::is_odd));
rv.insert("even".into(), BoxedTest::new(tests::is_even));
rv.insert("divisibleby".into(), BoxedTest::new(tests::is_divisibleby));
rv.insert("number".into(), BoxedTest::new(tests::is_number));
rv.insert("integer".into(), BoxedTest::new(tests::is_integer));
rv.insert("int".into(), BoxedTest::new(tests::is_integer));
Expand All @@ -136,6 +138,8 @@ pub(crate) fn get_builtin_tests() -> BTreeMap<Cow<'static, str>, BoxedTest> {
BoxedTest::new(tests::is_startingwith),
);
rv.insert("endingwith".into(), BoxedTest::new(tests::is_endingwith));
rv.insert("lower".into(), BoxedTest::new(tests::is_lower));
rv.insert("upper".into(), BoxedTest::new(tests::is_upper));

// operators
let is_eq = BoxedTest::new(tests::is_eq);
Expand Down
47 changes: 47 additions & 0 deletions minijinja/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,19 @@ mod builtins {

use std::borrow::Cow;

use crate::value::ops::{coerce, CoerceResult};
use crate::value::ValueKind;

/// Return true if the object is a boolean value.
///
/// ```jinja
/// {{ true is boolean }} -> true
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "builtins")))]
pub fn is_boolean(v: Value) -> bool {
v.kind() == ValueKind::Bool
}

/// Checks if a value is odd.
///
/// ```jinja
Expand All @@ -263,6 +274,20 @@ mod builtins {
i128::try_from(v).ok().map_or(false, |x| x % 2 == 0)
}

/// Return true if the value is divisible by another one.
///
/// ```jinja
/// {{ 42 is divisibleby(2) }} -> true
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "builtins")))]
pub fn is_divisibleby(v: Value, other: Value) -> bool {
match coerce(&v, &other) {
Some(CoerceResult::I128(a, b)) => (a % b) == 0,
Some(CoerceResult::F64(a, b)) => (a % b) == 0.0,
_ => false,
}
}

/// Checks if this value is a number.
///
/// ```jinja
Expand Down Expand Up @@ -524,6 +549,28 @@ mod builtins {
pub fn is_test(state: &State, name: &str) -> bool {
state.env.get_test(name).is_some()
}

/// Checks if a string is all lowercase.
///
/// ```jinja
/// {{ 'foo' is lower }} -> true
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "builtins")))]
#[cfg(feature = "builtins")]
pub fn is_lower(name: &str) -> bool {
name.chars().all(|x| x.is_lowercase())
}

/// Checks if a string is all uppercase.
///
/// ```jinja
/// {{ 'FOO' is upper }} -> true
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "builtins")))]
#[cfg(feature = "builtins")]
pub fn is_upper(name: &str) -> bool {
name.chars().all(|x| x.is_uppercase())
}
}

#[cfg(feature = "builtins")]
Expand Down
4 changes: 4 additions & 0 deletions minijinja/tests/inputs/tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ is-true: {{ true is true }} | {{ 42 is true }}
is-false: {{ false is false }} | {{ 0 is false }}
is-filter: {{ 'escape' is filter }} | {{ 'unknown-filter' is filter }}
is-test: {{ 'safe' is test }} | {{ 'unknown-test' is test }}
is-boolean: {{ true is boolean }} | {{ 42 is boolean }}
is-divisibleby: {{ 42 is divisibleby(2) }} | {{ 41 is divisibleby(2) }}
is-lower: {{ "foo" is lower }} | {{ "FOO" is lower }}
is-upper: {{ "foo" is upper }} | {{ "FOO" is upper }}
4 changes: 4 additions & 0 deletions minijinja/tests/snapshots/test_templates__vm@debug.txt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ State {
"==",
">",
">=",
"boolean",
"defined",
"divisibleby",
"endingwith",
"eq",
"equalto",
Expand All @@ -54,6 +56,7 @@ State {
"iterable",
"le",
"lessthan",
"lower",
"lt",
"mapping",
"ne",
Expand All @@ -67,6 +70,7 @@ State {
"test",
"true",
"undefined",
"upper",
],
filters: [
"abs",
Expand Down
14 changes: 9 additions & 5 deletions minijinja/tests/snapshots/test_templates__vm@tests.txt.snap
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
---
source: minijinja/tests/test_templates.rs
description: "even: {{ two is even }}\nodd: {{ two is odd }}\nundefined: {{ two is undefined }}\ndefined: {{ two is defined }}\nundefined2: {{ ohwell is undefined }}\ndefined2: {{ ohwell is defined }}\nnone: {{ none is none }}\nnot-none: {{ 42 is not none }}\nnumber-int: {{ two is number }}\nnumber-float: {{ two_dot_two is number }}\ninteger-int: {{ 42 is integer }}\ninteger-float: {{ 42.0 is integer }}\nfloat-int: {{ 42 is float }}\nfloat-float: {{ 42.0 is float }}\nnot-seq: {{ two is sequence }}\nseq: {{ seq is sequence }}\nreverse-not-seq: {{ seq|reverse is sequence }}\niterable: {{ seq is iterable }}\niterable-reverse: {{ seq|reverse is iterable }}\nstring-iterable: {{ string is iterable }}\nnot-iterable: {{ two is iterable }}\nnot-map: {{ two is mapping }}\nmap: {{ map is mapping }}\nstring: {{ string is string }}\nnot-string: {{ mapping is string }}\nstarts-with-a: {{ string is startingwith('a') }}\nends-with-ha: {{ string is endingwith('ha') }}\nnot-safe: {{ \"foo\" is safe }}\nsafe: {{ \"foo\"|escape is safe }}\nis-true: {{ true is true }} | {{ 42 is true }}\nis-false: {{ false is false }} | {{ 0 is false }}\nis-filter: {{ 'escape' is filter }} | {{ 'unknown-filter' is filter }}\nis-test: {{ 'safe' is test }} | {{ 'unknown-test' is test }}"
description: "even: {{ two is even }}\nodd: {{ two is odd }}\nundefined: {{ two is undefined }}\ndefined: {{ two is defined }}\nundefined2: {{ ohwell is undefined }}\ndefined2: {{ ohwell is defined }}\nnone: {{ none is none }}\nnot-none: {{ 42 is not none }}\nnumber-int: {{ two is number }}\nnumber-float: {{ two_dot_two is number }}\ninteger-int: {{ 42 is integer }}\ninteger-float: {{ 42.0 is integer }}\nfloat-int: {{ 42 is float }}\nfloat-float: {{ 42.0 is float }}\nnot-seq: {{ two is sequence }}\nseq: {{ seq is sequence }}\nreverse-not-seq: {{ seq|reverse is sequence }}\niterable: {{ seq is iterable }}\niterable-reverse: {{ seq|reverse is iterable }}\nstring-iterable: {{ string is iterable }}\nnot-iterable: {{ two is iterable }}\nnot-map: {{ two is mapping }}\nmap: {{ map is mapping }}\nstring: {{ string is string }}\nnot-string: {{ mapping is string }}\nstarts-with-a: {{ string is startingwith('a') }}\nends-with-ha: {{ string is endingwith('ha') }}\nnot-safe: {{ \"foo\" is safe }}\nsafe: {{ \"foo\"|escape is safe }}\nis-true: {{ true is true }} | {{ 42 is true }}\nis-false: {{ false is false }} | {{ 0 is false }}\nis-filter: {{ 'escape' is filter }} | {{ 'unknown-filter' is filter }}\nis-test: {{ 'safe' is test }} | {{ 'unknown-test' is test }}\nis-boolean: {{ true is boolean }} | {{ 42 is boolean }}\nis-divisibleby: {{ 42 is divisibleby(2) }} | {{ 41 is divisibleby(2) }}\nis-lower: {{ \"foo\" is lower }} | {{ \"FOO\" is lower }}\nis-upper: {{ \"foo\" is upper }} | {{ \"FOO\" is upper }}"
info:
map:
foo: bar
two: 2
two_dot_two: 2.2
seq:
- 1
- 2
- 3
map:
foo: bar
string: aha
two: 2
two_dot_two: 2.2
input_file: minijinja/tests/inputs/tests.txt
---
even: true
Expand Down Expand Up @@ -46,3 +46,7 @@ is-true: true | false
is-false: true | false
is-filter: true | false
is-test: true | false
is-boolean: true | false
is-divisibleby: true | false
is-lower: true | false
is-upper: false | true

0 comments on commit fd6d7ba

Please sign in to comment.