Skip to content

Commit

Permalink
test(template.mustache): nullish coalescing operator
Browse files Browse the repository at this point in the history
  • Loading branch information
loqusion committed Apr 7, 2024
1 parent cbf8c87 commit 64d50f2
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions tests/template/test_mustache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
from hyprshade.template.mustache import render
from typing import Any

import pytest

from hyprshade.template.mustache import NULLISH_COALESCE_LAMBDA_NAME, render


def nc(text: str) -> str:
return f"{{{{#{NULLISH_COALESCE_LAMBDA_NAME}}}}}{text}{{{{/{NULLISH_COALESCE_LAMBDA_NAME}}}}}"


def test_chevron_enabled():
Expand All @@ -15,8 +23,29 @@ def test_variable():
assert render("Hello, {{name}}!", {"name": "world"}) == "Hello, world!"


def test_default_variable():
template = "Hello, {{#name}}{{.}}{{/name}}{{^name}}world{{/name}}!"
class TestNullishCoalesce:
def test_nullish_coalesce(self):
template = f"Hello, {nc('{{name}} ? world')}!"

assert render(template) == "Hello, world!"
assert render(template, {"name": "planet"}) == "Hello, planet!"

@pytest.mark.parametrize("falsy", [0, 0.0])
def test_falsy_values(self, falsy: Any):
template = f"Hello, {nc('{{name}} ? world')}!"

assert render(template, {"name": falsy}) == f"Hello, {falsy}!"

@pytest.mark.parametrize("nullish", [None, ""])
def test_nullish_values(self, nullish: Any):
template = f"Hello, {nc('{{name}} ? world')}!"

assert render(template, {"name": nullish}) == "Hello, world!"

def test_no_default_value(self):
with pytest.raises(ValueError, match="requires a default value"):
render(f"Hello, {nc('{{name}}')}!")

assert render(template, {"name": "planet"}) == "Hello, planet!"
assert render(template) == "Hello, world!"
def test_multiple_operators(self):
with pytest.raises(ValueError, match="must occur only once"):
render(f"Hello, {nc('{{name}} ? world ? planet')}!")

0 comments on commit 64d50f2

Please sign in to comment.