forked from cleveragents/cleveragents-core
73 lines
2.2 KiB
Python
73 lines
2.2 KiB
Python
from behave import given
|
|
from behave import then
|
|
from behave import when
|
|
|
|
from cleveragents.core.exceptions import TemplateError
|
|
from cleveragents.templates.renderer import TemplateRenderer
|
|
|
|
|
|
@given("a template with placeholders")
|
|
def step_impl(context):
|
|
context.template_renderer = TemplateRenderer()
|
|
context.template_name = "test_template"
|
|
context.template_content = "Hello, {name}! Your age is {age}."
|
|
context.template_renderer.register_template(
|
|
context.template_name, context.template_content
|
|
)
|
|
|
|
|
|
@when("I render the template with context data")
|
|
def step_impl(context):
|
|
context.context_data = {"name": "John", "age": 30}
|
|
context.rendered = context.template_renderer.render(
|
|
context.template_name, context.context_data
|
|
)
|
|
|
|
|
|
@then("the placeholders should be replaced with the context values")
|
|
def step_impl(context):
|
|
assert context.rendered == "Hello, John! Your age is 30."
|
|
|
|
|
|
@given("multiple templates are registered")
|
|
def step_impl(context):
|
|
context.template_renderer = TemplateRenderer()
|
|
context.templates = {
|
|
"template1": "This is template 1: {value}",
|
|
"template2": "This is template 2: {value}",
|
|
"template3": "This is template 3: {value}",
|
|
}
|
|
|
|
for name, content in context.templates.items():
|
|
context.template_renderer.register_template(name, content)
|
|
|
|
|
|
@when("I request a specific template by name")
|
|
def step_impl(context):
|
|
context.requested_template = "template2"
|
|
context.template_content = context.template_renderer.get_template(
|
|
context.requested_template
|
|
)
|
|
|
|
|
|
@then("I should receive the correct template")
|
|
def step_impl(context):
|
|
assert context.template_content == context.templates[context.requested_template]
|
|
|
|
|
|
@when("I render the template with missing context data")
|
|
def step_impl(context):
|
|
context.context_data = {"name": "John"} # Missing 'age'
|
|
try:
|
|
context.rendered = context.template_renderer.render(
|
|
context.template_name, context.context_data
|
|
)
|
|
context.error = None
|
|
except Exception as e:
|
|
context.error = e
|
|
|
|
|
|
@then("a template error should be present")
|
|
def step_impl(context):
|
|
assert context.error is not None, "Expected an error but none was raised"
|