def test_render_error_pass_through(): # check that if one template tries to render another, the inner RenderError gets propagated template1 = DefTemplate.from_callable("test1", lambda x, y: "${x} + ${y} + ${z}") template2 = DefTemplate.from_callable( "test2", lambda a, t1: "${a} + ${t1.render(a, 1)}") with pytest.raises(RenderError) as e: template2.render(10, template1, kwd=3) assert e.value.args == (10, 1) assert e.value.globals == dict() assert e.value.source == template1.source assert type(e.value.exception) == NameError assert str(e.value.exception) in str(e.value)
def test_builtin_globals(mock_backend_pycuda): mock_backend_pycuda.add_devices([ PyCUDADeviceInfo(max_threads_per_block=1024), PyCUDADeviceInfo(max_threads_per_block=512) ]) source_template = DefTemplate.from_string( 'mock_source', [], """ KERNEL void test() { int max_total_local_size = ${device_params.max_total_local_size}; } """) api = API.from_api_id(mock_backend_pycuda.api_id) context = Context.from_devices( [api.platforms[0].devices[0], api.platforms[0].devices[1]]) src = MockDefTemplate(kernels=[MockKernel('test', [None])], source_template=source_template) program = Program(context.devices, src) assert 'max_total_local_size = 1024' in program.sources[ context.devices[0]].source assert 'max_total_local_size = 512' in program.sources[ context.devices[1]].source
def test_template_builtins(): # Check for the builtins we add to every template template = DefTemplate.from_callable( "test", lambda numpy_ref: "${numpy == numpy_ref}") assert template.render(numpy).strip() == "True" template = DefTemplate.from_string("test", ['numpy_ref'], "${numpy == numpy_ref}") assert template.render(numpy).strip() == "True" template = Template.from_string( '<%def name="test(numpy_ref)">${numpy == numpy_ref}</%def>') template_def = template.get_def("test") assert template_def.render(numpy).strip() == "True" template = Template.from_associated_file(__file__) assert template.get_def("test_builtins").render(numpy).strip() == "True"
def test_render_error(): template = DefTemplate.from_callable("test", lambda x, y: "${x} + ${y} + ${z}") with pytest.raises(RenderError) as e: template.render(1, 2, kwd=3) assert e.value.args == (1, 2) assert e.value.globals == dict(kwd=3) assert e.value.source == template.source assert type(e.value.exception) == NameError assert str(e.value.exception) in str(e.value)
def test_def_template_from_string(): template = DefTemplate.from_string("test", ['x', 'y'], "${x} + ${y}") assert template.render(1, 2).strip() == "1 + 2"
def test_def_template_from_callable(): template = DefTemplate.from_callable("test", lambda x, y: "${x} + ${y}") assert template.render(1, 2).strip() == "1 + 2"
def test_render_def_template(): tmpl = DefTemplate.from_callable("test", lambda x, y: "${x} + ${y} + ${z}") res = render_with_modules(tmpl, render_args=[1, 2], render_globals=dict(z=3)).strip() assert res == "1 + 2 + 3"