# Tests and Conditionals

39 built-in tests for template conditional logic including type checking, value comparison, and meta-testing. Tests are used in {% if %} statements and can be combined with logical operators.

## Capabilities

### Type Testing

Tests for checking object types and characteristics.

```python { .api }
def boolean(value):
    """
    Check if value is a boolean.
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is boolean
    """

def callable(obj):
    """
    Check if object is callable.
    
    Parameters:
        obj: Input object
        
    Returns:
        bool: True if object is callable
    """

def defined(value):
    """
    Check if value is defined (not undefined).
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is defined
    """

def undefined(value):
    """
    Check if value is undefined.
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is undefined
    """

def float(value):
    """
    Check if value is a float.
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is float
    """

def integer(value):
    """
    Check if value is an integer.
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is integer
    """

def iterable(value):
    """
    Check if value is iterable.
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is iterable
    """

def mapping(value):
    """
    Check if value is a mapping (dict-like).
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is mapping
    """

def none(value):
    """
    Check if value is None.
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is None
    """

def number(value):
    """
    Check if value is a number.
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is number
    """

def sequence(value):
    """
    Check if value is a sequence.
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is sequence
    """

def string(value):
    """
    Check if value is a string.
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is string
    """
```

### Value Testing

Tests for checking specific value characteristics and properties.

```python { .api }
def even(value):
    """
    Check if number is even.
    
    Parameters:
        value: Input number
        
    Returns:
        bool: True if number is even
    """

def odd(value):
    """
    Check if number is odd.
    
    Parameters:
        value: Input number
        
    Returns:
        bool: True if number is odd
    """

def divisibleby(value, num):
    """
    Check if value is divisible by number.
    
    Parameters:
        value: Input number
        num: Divisor number
        
    Returns:
        bool: True if value is divisible by num
    """

def escaped(value):
    """
    Check if value is escaped (Markup object).
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is escaped
    """

def false(value):
    """
    Check if value is falsy (evaluates to False).
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is falsy
    """

def true(value):
    """
    Check if value is truthy (evaluates to True).
    
    Parameters:
        value: Input value
        
    Returns:
        bool: True if value is truthy
    """

def lower(value):
    """
    Check if string is lowercase.
    
    Parameters:
        value: Input string
        
    Returns:
        bool: True if string is lowercase
    """

def upper(value):
    """
    Check if string is uppercase.
    
    Parameters:
        value: Input string
        
    Returns:
        bool: True if string is uppercase
    """
```

### Comparison Tests

Tests for comparing values using various comparison operators.

```python { .api }
def equalto(value, other):
    """
    Check if values are equal (also available as '==' and 'eq').
    
    Parameters:
        value: First value
        other: Second value
        
    Returns:
        bool: True if values are equal
    """

def ne(value, other):
    """
    Check if values are not equal (also available as '!=').
    
    Parameters:
        value: First value
        other: Second value
        
    Returns:
        bool: True if values are not equal
    """

def greaterthan(value, other):
    """
    Check if value is greater than other (also available as '>' and 'gt').
    
    Parameters:
        value: First value
        other: Second value
        
    Returns:
        bool: True if value > other
    """

def ge(value, other):
    """
    Check if value is greater than or equal to other (also available as '>=').
    
    Parameters:
        value: First value
        other: Second value
        
    Returns:
        bool: True if value >= other
    """

def lessthan(value, other):
    """
    Check if value is less than other (also available as '<' and 'lt').
    
    Parameters:
        value: First value
        other: Second value
        
    Returns:
        bool: True if value < other
    """

def le(value, other):
    """
    Check if value is less than or equal to other (also available as '<=').
    
    Parameters:
        value: First value
        other: Second value
        
    Returns:
        bool: True if value <= other
    """

def sameas(value, other):
    """
    Check if values are the same object (identity test).
    
    Parameters:
        value: First value
        other: Second value
        
    Returns:
        bool: True if values are same object
    """

def in_test(value, seq):
    """
    Check if value is in sequence (available as 'in' test).
    
    Parameters:
        value: Value to search for
        seq: Sequence to search in
        
    Returns:
        bool: True if value is in sequence
    """
```

### Meta Tests

Tests for checking the existence of other tests and filters.

```python { .api }
def filter_test(name):
    """
    Check if filter exists.
    
    Parameters:
        name: Filter name
        
    Returns:
        bool: True if filter exists
    """

def test_test(name):
    """
    Check if test exists.
    
    Parameters:
        name: Test name
        
    Returns:
        bool: True if test exists
    """
```

## Usage Examples

### Basic Template Usage

```python
# Template content
template_str = '''
{% if user is defined %}
    {% if user.age is number and user.age is greaterthan(18) %}
        Adult user: {{ user.name }}
    {% elif user.age is number %}
        Minor user: {{ user.name }}
    {% else %}
        User with unknown age: {{ user.name }}
    {% endif %}
{% else %}
    No user defined
{% endif %}

{% for item in items %}
    {% if item is string and item is upper %}
        UPPERCASE: {{ item }}
    {% elif item is number and item is even %}
        EVEN NUMBER: {{ item }}
    {% elif item is iterable %}
        SEQUENCE: {{ item | join(', ') }}
    {% endif %}
{% endfor %}
'''

from jinja2 import Template
template = Template(template_str)

result = template.render(
    user={'name': 'Alice', 'age': 25},
    items=['HELLO', 42, [1, 2, 3], 'world', 17]
)
```

### Programmatic Test Usage

```python
from jinja2 import Environment

env = Environment()

# Test values programmatically
result1 = env.call_test('defined', 'hello')  # True
result2 = env.call_test('number', 42)        # True  
result3 = env.call_test('even', 42)          # True
result4 = env.call_test('in', 'hello', ['hello', 'world'])  # True
```

### Custom Tests

Add custom tests to environments:

```python
def is_email(value):
    """Test if string looks like an email address."""
    return isinstance(value, str) and '@' in value and '.' in value

def is_positive(value):
    """Test if number is positive."""
    try:
        return float(value) > 0
    except (ValueError, TypeError):
        return False

env = Environment()
env.tests['email'] = is_email
env.tests['positive'] = is_positive

# Use in templates: {% if user.email is email %}
```

## Types

```python { .api }
class TestEnvironment:
    """
    Test execution environment providing context and utilities for test functions.
    
    Attributes:
        environment: Jinja2 environment instance
        context: Current template context (if available)
        eval_ctx: Current evaluation context (if available)
    """
```