Coverage for tests/test_prompt_helpers.py: 55%
75 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 15:39 -0800
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 15:39 -0800
1from typing import Dict, Optional
3import appl
4import pytest
5from appl import BracketedDefinition as Def
6from appl import define, define_bracketed, ppl, records
7from appl.compositor import *
10def test_compositor():
11 @ppl
12 def func():
13 with NumberedList(indent=INDENT, sep="???\n"):
14 with LetterList(indent=INDENT, sep="!!!\n"):
15 with DashList(indent=INDENT, sep="|||\n"):
16 "a"
17 "b"
18 "c"
19 return records()
21 assert str(func()) == f"{INDENT * 3}- a!!!\n{INDENT * 2}A. b???\n{INDENT}1. c"
24def test_iter():
25 @ppl
26 def func():
27 for i in iter(range(3), comp=RomanList()):
28 f"item {i}"
29 return records()
31 assert str(func()) == f"I. item 0\nII. item 1\nIII. item 2"
34def test_logged():
35 @ppl
36 def func():
37 with Logged(prolog="start", epilog="end"):
38 "a"
39 "b"
40 return records()
42 assert str(func()) == f"start\na\nb\nend"
45def test_tagged():
46 @ppl
47 def func(attrs: Optional[Dict] = None):
48 with Tagged("tag1", attrs=attrs, indent_inside=4):
49 "a"
50 "b"
51 return records()
53 @ppl
54 def inline():
55 with InlineTagged("tag2"):
56 "hello"
57 "world"
58 return records()
60 content = " a\n b\n"
61 assert str(func()) == f"<tag1>\n{content}</tag1>"
62 assert str(func({"x": "1"})) == f'<tag1 x="1">\n{content}</tag1>'
63 assert str(inline()) == f"<tag2>helloworld</tag2>"
66def test_definition():
67 # recommended way to define
68 class ADef(Def):
69 name = "A"
70 fstr = "({})" # overwrite the format string
72 # A shorter way to define
73 BDef = define_bracketed("B")
74 DDef = define("D")
76 @ppl
77 def func():
78 ADef(desc="This is A.")
79 BDef(details="This is B.")
80 (c := Def("C", "This is C.")) # inline definition
81 DDef(desc="This is D.", sep=":: ")
82 f"{ADef}{BDef}{c}{DDef}"
83 return records()
85 target = [
86 "A: This is A.",
87 "B: \nThis is B.",
88 "C: This is C.",
89 "D:: This is D.",
90 "(A)[B][C]D",
91 ]
92 assert str(func()) == "\n".join(target)
95def test_compositor_decorator():
96 @ppl(comp=NumberedList())
97 def func():
98 f"first line"
99 f"second line"
100 with IndentedList():
101 "third line"
102 "fourth line"
103 return records()
105 assert (
106 str(func())
107 == f"1. first line\n2. second line\n{INDENT}third line\n{INDENT}fourth line"
108 )