Module minijinja::tests[][src]

Expand description

Test functions and abstractions.

Test functions in MiniJinja are like (filters)crate::filters but a different syntax is used to invoke them and they have to return boolean values. For instance the expression {% if foo is odd %} invokes the is_odd test to check if the value is indeed an odd number.

MiniJinja comes with some built-in test functions that are listed below. To create a custom test write a function that takes at least a &State and value argument and returns a boolean result, then register it with add_filter.

Custom Tests

A custom test function is just a simple function which accepts inputs as parameters and then returns a bool wrapped in a result. For instance the following shows a test function which takes an input value and checks if it’s lowercase:

fn is_lowercase(_state: &State, value: String) -> Result<bool, Error> {
   Ok(value.chars().all(|x| x.is_lowercase()))
}

env.add_test("lowercase", is_lowercase);

MiniJinja will perform the necessary conversions automatically via the FunctionArgs trait.

Traits

A utility trait that represents filters.

Functions

is_definedbuiltin_tests

Checks if a value is defined.

is_endingwithbuiltin_tests

Checks if the value is ending with a string.

is_evenbuiltin_tests

Checks if a value is even.

is_mappingbuiltin_tests

Checks if this value is a mapping

is_numberbuiltin_tests

Checks if this value is a number.

is_oddbuiltin_tests

Checks if a value is odd.

is_sequencebuiltin_tests

Checks if this value is a sequence

is_startingwithbuiltin_tests

Checks if the value is starting with a string.

is_stringbuiltin_tests

Checks if this value is a string.

is_undefinedbuiltin_tests

Checks if a value is undefined.