async def test_url_without_plus_escape(self) -> None: skt = Sketch( "https://www.example.com/?user=<%url_without_plus= name %>", skt_ctx=default_skt_ctx, ) assert (await skt.draw(name="John Smith" ) == "https://www.example.com/?user=John%20Smith")
async def test_async_for(self) -> None: time_iter = _AsyncTimeIterator() skt = Sketch( "<% async for i in time_iter %><%r= str(i) %>, <% end %>", skt_ctx=default_skt_ctx, ) assert (await skt.draw(time_iter=time_iter ) == str(time_iter._iterated_time)[1:-1] + ", ")
async def test_url_escape(self) -> None: skt = Sketch( "https://www.example.com/?user=<%u= name %>", skt_ctx=default_skt_ctx, ) assert await skt.draw( name="a&redirect=https://www.example2.com/" ) == ("https://www.example.com/?user=a%26redirect%3Dhttps%3A%2F%2F" "www.example2.com%2F")
async def test_stmts_escape(self) -> None: skt = Sketch( '<%% is the begin mark, and <%r= "%%> is the end mark. " %>' '<%r= "<% and" %> %> only need to be escaped whenever they ' "have ambiguity of the templating system.", skt_ctx=default_skt_ctx, ) assert await skt.draw() == ( "<% is the begin mark, and %> is the end mark. " "<% and %> only need to be escaped whenever they " "have ambiguity of the templating system.")
async def test_if_elif_else(self) -> None: skt = Sketch( "<% if cond %>cond_str<% elif sub_cond %>sub_cond_str" "<% else %>else_str<% end %>", skt_ctx=default_skt_ctx, ) assert await skt.draw(cond=True, sub_cond=True) == "cond_str" assert await skt.draw(cond=False, sub_cond=True) == "sub_cond_str" assert await skt.draw(cond=False, sub_cond=False) == "else_str"
async def test_multiline_stmts(self) -> None: skt = Sketch( """\ <% if a == \ True %> Hello, it's me! <% else %> No, it's not me! <% end %>""", skt_ctx=default_skt_ctx, ) assert await skt.draw(a=True) == "\nHello, it's me!\n" assert await skt.draw(a=False) == "\nNo, it's not me!\n"
async def test_custom_escape_fn(self) -> None: def _number_fn(i: int) -> str: return str(i + 1) if _TEST_CURIO: skt_ctx = CurioSketchContext( custom_escape_fns={"number_plus_one": _number_fn}) else: skt_ctx = AsyncioSketchContext( custom_escape_fns={"number_plus_one": _number_fn}) skt = Sketch("The result is <% number_plus_one= a %>.", skt_ctx=skt_ctx) assert await skt.draw(a=12345) == "The result is 12346."
def test_missing_end_mark(self) -> None: with pytest.raises(SketchSyntaxError): Sketch("<% while True %>", skt_ctx=default_skt_ctx)
def test_malformed_stmts(self) -> None: with pytest.raises(SketchSyntaxError): Sketch("<% <%", skt_ctx=default_skt_ctx)
async def test_assign_var(self): skt = Sketch("<% let a = b %><%= a %>", skt_ctx=default_skt_ctx) assert await skt.draw(b="I am b!") == "I am b!"
async def test_json_escape(self) -> None: skt = Sketch('{"name": <%json= name %>}', skt_ctx=default_skt_ctx) assert (await skt.draw(name='{"name": "admin"}' ) == '{"name": "{\\"name\\": \\"admin\\"}"}')
async def test_no_escape(self) -> None: skt = Sketch("Hello, <%r= a %>!", skt_ctx=default_skt_ctx) assert await skt.draw(a="<h1>world</h1>") == "Hello, <h1>world</h1>!"
async def test_html_escape(self) -> None: skt = Sketch("Hello, <%html= a %>!", skt_ctx=default_skt_ctx) assert (await skt.draw(a="<h1>world</h1>" ) == "Hello, <h1>world</h1>!")
async def test_assign_var_with_extra_equal(self): skt = Sketch('<% let a = "--=--" %><%= a %>', skt_ctx=default_skt_ctx) assert await skt.draw() == "--=--"
def test_redundant_end_mark(self) -> None: with pytest.raises(SketchSyntaxError): Sketch("<% if False %><% end %><% end %>", skt_ctx=default_skt_ctx)
def test_unknown_stmt(self) -> None: with pytest.raises(UnknownStatementError): Sketch("<% if anyways %><% fi %>", skt_ctx=default_skt_ctx)
async def test_raise_error(self): skt = Sketch("<% raise RuntimeError %>", skt_ctx=default_skt_ctx) with pytest.raises(RuntimeError): await skt.draw()