# tests

URL: /kida/api/environment/tests/
Section: environment
Description: Built-in tests for Kida templates.

Tests are boolean predicates used with `is` in conditionals:
`{% if value is test %}` or `{% if value is test(arg) %}`

Categories:
**Type Tests**:
    - `defined`: Value is not None
    - `undefined`: Value is None
    - `none`: Value is None (alias)
    - `string`: Value is a string
    - `number`: Value is int or float (not bool)
    - `sequence`: Value is list, tuple, or string
    - `mapping`: Value is a dict
    - `iterable`: Value supports iteration
    - `callable`: Value is callable

**Boolean Tests**:
    - `true`: Value is exactly True
    - `false`: Value is exactly False

**Number Tests**:
    - `odd`: Integer is odd
    - `even`: Integer is even
    - `divisibleby(n)`: Integer is divisible by n

**Comparison Tests**:
    - `eq(other)` / `equalto(other)`: Equal to other
    - `ne(other)`: Not equal to other
    - `lt(other)` / `lessthan(other)`: Less than other
    - `le(other)`: Less than or equal
    - `gt(other)` / `greaterthan(other)`: Greater than other
    - `ge(other)`: Greater than or equal
    - `sameas(other)`: Identity comparison (is)
    - `in(seq)`: Value is in sequence

**String Tests**:
    - `lower`: String is all lowercase
    - `upper`: String is all uppercase

**HTMX Tests** (Feature 1.2):
    - `hx_request`: Request is from HTMX (checks HX-Request header)
    - `hx_target(id)`: HTMX target matches element ID
    - `hx_boosted`: Request is HTMX-boosted

Negation:
Use `is not` for negated tests:
`{% if user is not defined %}` or `{% if count is not even %}`

Example:
    ```jinja
    {% if posts is defined and posts is iterable %}
        {% for post in posts %}
            {% if loop.index is odd %}
                <div class="odd">{{ post.title }}</div>
            {% endif %}
        {% endfor %}
    {% endif %}
    ```

Custom Tests:
    >>> env.add_test('prime', lambda n: n > 1 and all(n % i for i in range(2, n)))
    >>> # {% if 17 is prime %}Yes{% endif %}

---

> For a complete page index, fetch /kida/llms.txt.

Open LLM text
(/kida/api/environment/tests/index.txt)

Share with AI

Ask Claude
(https://claude.ai/new?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fenvironment%2Ftests%2Findex.txt)

Ask ChatGPT
(https://chatgpt.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fenvironment%2Ftests%2Findex.txt)

Ask Gemini
(https://gemini.google.com/app?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fenvironment%2Ftests%2Findex.txt)

Ask Copilot
(https://copilot.microsoft.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fenvironment%2Ftests%2Findex.txt)

Module

#
`environment.tests`

Built-in tests for Kida templates.

Tests are boolean predicates used with`is`in conditionals:
`{% if value is test %}` or `{% if value is test(arg) %}`

Categories:

Type Tests:

- `defined`: Value is not None

- `undefined`: Value is None

- `none`: Value is None (alias)

- `string`: Value is a string

- `number`: Value is int or float (not bool)

- `sequence`: Value is list, tuple, or string

- `mapping`: Value is a dict

- `iterable`: Value supports iteration

- `callable`: Value is callable

Boolean Tests:

- `true`: Value is exactly True

- `false`: Value is exactly False

Number Tests:

- `odd`: Integer is odd

- `even`: Integer is even

- `divisibleby(n)`: Integer is divisible by n

Comparison Tests:

- `eq(other)` / `equalto(other)`: Equal to other

- `ne(other)`: Not equal to other

- `lt(other)` / `lessthan(other)`: Less than other

- `le(other)`: Less than or equal

- `gt(other)` / `greaterthan(other)`: Greater than other

- `ge(other)`: Greater than or equal

- `sameas(other)`: Identity comparison (is)

- `in(seq)`: Value is in sequence

String Tests:

- `lower`: String is all lowercase

- `upper`: String is all uppercase

HTMX Tests (Feature 1.2):

- `hx_request`: Request is from HTMX (checks HX-Request header)

- `hx_target(id)`: HTMX target matches element ID

- `hx_boosted`: Request is HTMX-boosted

Negation:

Use`is not`for negated tests:
`{% if user is not defined %}` or `{% if count is not even %}`

Example:

```
```jinja
{% if posts is defined and posts is iterable %}
    {% for post in posts %}
        {% if loop.index is odd %}
            <div class="odd">{{ post.title }}</div>
        {% endif %}
    {% endfor %}
{% endif %}
```
```

Custom Tests:


```
>>> env.add_test('prime', lambda n: n > 1 and all(n % i for i in range(2, n)))
>>> # {% if 17 is prime %}Yes{% endif %}
```







25Functions










## Functions











        `_apply_test`

3


          `bool`


▼


Apply a test to a value.









      `def _apply_test(value: Any, test_name: str, *args: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`






  `test_name`
`str`






  `*args`
`Any`











##### Returns

      `bool`















        `_test_callable`

1


          `bool`


▼


Test if value is callable.









      `def _test_callable(value: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`











##### Returns

      `bool`















        `_test_defined`

1


          `bool`


▼


Test if value is defined (not None and not the Undefined sentinel).









      `def _test_defined(value: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`











##### Returns

      `bool`















        `_test_divisible_by`

2


          `bool`


▼


Test if value is divisible by num.









      `def _test_divisible_by(value: int, num: int) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`int`






  `num`
`int`











##### Returns

      `bool`















        `_test_eq`

2


          `bool`


▼


Test equality.









      `def _test_eq(value: Any, other: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`






  `other`
`Any`











##### Returns

      `bool`















        `_test_even`

1


          `bool`


▼


Test if value is even.









      `def _test_even(value: int) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`int`











##### Returns

      `bool`















        `_test_ge`

2


          `bool`


▼


Test greater than or equal.









      `def _test_ge(value: Any, other: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`






  `other`
`Any`











##### Returns

      `bool`















        `_test_gt`

2


          `bool`


▼


Test greater than.









      `def _test_gt(value: Any, other: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`






  `other`
`Any`











##### Returns

      `bool`















        `_test_in`

2


          `bool`


▼


Test if value is in sequence.









      `def _test_in(value: Any, seq: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`






  `seq`
`Any`











##### Returns

      `bool`















        `_test_iterable`

1


          `bool`


▼


Test if value is iterable.









      `def _test_iterable(value: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`











##### Returns

      `bool`















        `_test_le`

2


          `bool`


▼


Test less than or equal.









      `def _test_le(value: Any, other: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`






  `other`
`Any`











##### Returns

      `bool`















        `_test_lower`

1


          `bool`


▼


Test if string is lowercase.









      `def _test_lower(value: str) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`str`











##### Returns

      `bool`















        `_test_lt`

2


          `bool`


▼


Test less than.









      `def _test_lt(value: Any, other: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`






  `other`
`Any`











##### Returns

      `bool`















        `_test_mapping`

1


          `bool`


▼


Test if value is a mapping.









      `def _test_mapping(value: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`











##### Returns

      `bool`















        `_test_ne`

2


          `bool`


▼


Test inequality.









      `def _test_ne(value: Any, other: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`






  `other`
`Any`











##### Returns

      `bool`















        `_test_none`

1


          `bool`


▼


Test if value is None.









      `def _test_none(value: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`











##### Returns

      `bool`















        `_test_number`

1


          `bool`


▼


Test if value is a number.









      `def _test_number(value: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`











##### Returns

      `bool`















        `_test_odd`

1


          `bool`


▼


Test if value is odd.









      `def _test_odd(value: int) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`int`











##### Returns

      `bool`















        `_test_sequence`

1


          `bool`


▼


Test if value is a sequence.









      `def _test_sequence(value: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`











##### Returns

      `bool`















        `_test_string`

1


          `bool`


▼


Test if value is a string.









      `def _test_string(value: Any) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`Any`











##### Returns

      `bool`















        `_test_upper`

1


          `bool`


▼


Test if string is uppercase.









      `def _test_upper(value: str) -> bool`




##### Parameters




        Name
        Type
        Description





        `value`
`str`











##### Returns

      `bool`















        `_test_match`

2


          `bool`


▼


Test if string matches regex pattern.

Used by rejectattr/selectattr for filter…









      `def _test_match(value: Any, pattern: str) -> bool`




Test if string matches regex pattern.


Used by rejectattr/selectattr for filtering by regex pattern.






##### Parameters




        Name
        Type
        Description





        `value`
`Any`






  `pattern`
`str`











##### Returns

      `bool`















        `_test_hx_request`

1


          `bool`


▼


Test if value indicates an HTMX request.

Works with request objects that have …









      `def _test_hx_request(value: Any) -> bool`




Test if value indicates an HTMX request.


Works with request objects that have a headers dict/attribute,
  or with boolean values directly.


Usage:
  {% if request is hx_request %}
  {# Render fragment for HTMX #}
  {% else %}
  {# Render full page #}
  {% end %}






##### Parameters




        Name
        Type
        Description





        `value`
`Any`


Request object with headers, or boolean value











##### Returns

      `bool`















        `_test_hx_target`

2


          `bool`


▼


Test if HTMX target matches expected element ID.

**Usage:**
{% if request is h…









      `def _test_hx_target(value: Any, target: str) -> bool`




Test if HTMX target matches expected element ID.


Usage:
  {% if request is hx_target("user-list") %}
  {# Rendering into user-list element #}
  {% end %}






##### Parameters




        Name
        Type
        Description





        `value`
`Any`


Request object with headers, or string target value






  `target`
`str`


Expected target element ID











##### Returns

      `bool`















        `_test_hx_boosted`

1


          `bool`


▼


Test if request is HTMX-boosted.

Boosted requests are regular links/forms enha…









      `def _test_hx_boosted(value: Any) -> bool`




Test if request is HTMX-boosted.


Boosted requests are regular links/forms enhanced by hx-boost="true".


Usage:
  {% if request is hx_boosted %}
  {# Progressive enhancement - AJAX navigation #}
  {% end %}






##### Parameters




        Name
        Type
        Description





        `value`
`Any`


Request object with headers, or boolean value











##### Returns

      `bool`
