pyglove 0.4.5.dev202410020809__py3-none-any.whl → 0.4.5.dev202410100808__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. pyglove/core/__init__.py +9 -0
  2. pyglove/core/object_utils/__init__.py +1 -0
  3. pyglove/core/object_utils/value_location.py +10 -0
  4. pyglove/core/object_utils/value_location_test.py +22 -0
  5. pyglove/core/symbolic/base.py +40 -2
  6. pyglove/core/symbolic/base_test.py +67 -0
  7. pyglove/core/symbolic/diff.py +190 -1
  8. pyglove/core/symbolic/diff_test.py +290 -0
  9. pyglove/core/symbolic/object_test.py +3 -8
  10. pyglove/core/symbolic/ref.py +29 -0
  11. pyglove/core/symbolic/ref_test.py +143 -0
  12. pyglove/core/typing/__init__.py +4 -0
  13. pyglove/core/typing/callable_ext.py +240 -1
  14. pyglove/core/typing/callable_ext_test.py +255 -0
  15. pyglove/core/typing/inspect.py +63 -0
  16. pyglove/core/typing/inspect_test.py +39 -0
  17. pyglove/core/views/__init__.py +30 -0
  18. pyglove/core/views/base.py +906 -0
  19. pyglove/core/views/base_test.py +615 -0
  20. pyglove/core/views/html/__init__.py +27 -0
  21. pyglove/core/views/html/base.py +529 -0
  22. pyglove/core/views/html/base_test.py +804 -0
  23. pyglove/core/views/html/tree_view.py +1052 -0
  24. pyglove/core/views/html/tree_view_test.py +748 -0
  25. {pyglove-0.4.5.dev202410020809.dist-info → pyglove-0.4.5.dev202410100808.dist-info}/METADATA +1 -1
  26. {pyglove-0.4.5.dev202410020809.dist-info → pyglove-0.4.5.dev202410100808.dist-info}/RECORD +29 -21
  27. {pyglove-0.4.5.dev202410020809.dist-info → pyglove-0.4.5.dev202410100808.dist-info}/LICENSE +0 -0
  28. {pyglove-0.4.5.dev202410020809.dist-info → pyglove-0.4.5.dev202410100808.dist-info}/WHEEL +0 -0
  29. {pyglove-0.4.5.dev202410020809.dist-info → pyglove-0.4.5.dev202410100808.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,804 @@
1
+ # Copyright 2024 The Langfun Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ import inspect
15
+ import unittest
16
+
17
+ from pyglove.core.views.html import base
18
+
19
+ Html = base.Html
20
+
21
+ # pylint: disable=line-too-long
22
+
23
+
24
+ class TestCase(unittest.TestCase):
25
+
26
+ def assert_html(self, actual, expected):
27
+ expected = inspect.cleandoc(expected).strip()
28
+ actual = actual.strip()
29
+ if actual != expected:
30
+ print(actual)
31
+ self.assertEqual(actual.strip(), expected)
32
+
33
+
34
+ class SharedPartTest(TestCase):
35
+
36
+ def test_styles(self):
37
+ self.assert_html(Html.Styles().content, '')
38
+ styles = Html.Styles('h1 {color: red;}', 'h2 {color: blue;}')
39
+ self.assert_html(
40
+ styles.content,
41
+ """
42
+ <style>
43
+ h1 {color: red;}
44
+ h2 {color: blue;}
45
+ </style>
46
+ """
47
+ )
48
+ self.assertTrue(styles.add('h3 {color: green;}'))
49
+ self.assertFalse(styles.add('h1 {color: red;}'))
50
+ self.assertEqual(
51
+ styles.parts,
52
+ {
53
+ 'h1 {color: red;}': 2,
54
+ 'h2 {color: blue;}': 1,
55
+ 'h3 {color: green;}': 1,
56
+ },
57
+ )
58
+ self.assert_html(
59
+ styles.content,
60
+ """
61
+ <style>
62
+ h1 {color: red;}
63
+ h2 {color: blue;}
64
+ h3 {color: green;}
65
+ </style>
66
+ """
67
+ )
68
+ styles2 = Html.Styles('h1 {color: red;}', 'h4 {color: yellow;}')
69
+ styles.add(styles2)
70
+ self.assert_html(
71
+ styles.content,
72
+ """
73
+ <style>
74
+ h1 {color: red;}
75
+ h2 {color: blue;}
76
+ h3 {color: green;}
77
+ h4 {color: yellow;}
78
+ </style>
79
+ """
80
+ )
81
+ self.assertEqual(
82
+ styles.parts,
83
+ {
84
+ 'h1 {color: red;}': 3,
85
+ 'h2 {color: blue;}': 1,
86
+ 'h3 {color: green;}': 1,
87
+ 'h4 {color: yellow;}': 1,
88
+ },
89
+ )
90
+ self.assertTrue(styles)
91
+ self.assertFalse(Html.Styles())
92
+ self.assertIn('h1 {color: red;}', styles)
93
+ self.assertNotIn('h3 {color: red;}', styles)
94
+ self.assertEqual(
95
+ list(styles),
96
+ [
97
+ 'h1 {color: red;}',
98
+ 'h2 {color: blue;}',
99
+ 'h3 {color: green;}',
100
+ 'h4 {color: yellow;}',
101
+ ]
102
+ )
103
+ self.assertEqual(
104
+ styles,
105
+ Html.Styles(
106
+ 'h1 {color: red;}',
107
+ 'h2 {color: blue;}',
108
+ 'h3 {color: green;}',
109
+ 'h4 {color: yellow;}'
110
+ )
111
+ )
112
+ self.assertNotEqual(
113
+ styles,
114
+ Html.Styles(
115
+ 'h1 {color: red;}',
116
+ )
117
+ )
118
+ self.assertNotEqual(styles, Html.Scripts())
119
+ self.assertEqual(
120
+ repr(styles),
121
+ """Styles(parts={'h1 {color: red;}': 3, 'h2 {color: blue;}': 1, 'h3 {color: green;}': 1, 'h4 {color: yellow;}': 1})"""
122
+ )
123
+ self.assert_html(
124
+ str(styles),
125
+ """
126
+ <style>
127
+ h1 {color: red;}
128
+ h2 {color: blue;}
129
+ h3 {color: green;}
130
+ h4 {color: yellow;}
131
+ </style>
132
+ """
133
+ )
134
+
135
+ def test_style_files(self):
136
+ self.assert_html(Html.StyleFiles().content, '')
137
+ style_files = Html.StyleFiles('./a.css', 'https://x/y.css')
138
+ self.assert_html(
139
+ style_files.content,
140
+ """
141
+ <link rel="stylesheet" href="./a.css">
142
+ <link rel="stylesheet" href="https://x/y.css">
143
+ """
144
+ )
145
+ self.assertTrue(style_files.add('./b.css'))
146
+ self.assertFalse(style_files.add('./a.css'))
147
+ self.assertEqual(
148
+ style_files.parts,
149
+ {
150
+ './a.css': 2,
151
+ 'https://x/y.css': 1,
152
+ './b.css': 1,
153
+ },
154
+ )
155
+ self.assert_html(
156
+ style_files.content,
157
+ """
158
+ <link rel="stylesheet" href="./a.css">
159
+ <link rel="stylesheet" href="https://x/y.css">
160
+ <link rel="stylesheet" href="./b.css">
161
+ """
162
+ )
163
+ style_files2 = Html.StyleFiles('./a.css', './c.css')
164
+ style_files.add(style_files2)
165
+ self.assert_html(
166
+ style_files.content,
167
+ """
168
+ <link rel="stylesheet" href="./a.css">
169
+ <link rel="stylesheet" href="https://x/y.css">
170
+ <link rel="stylesheet" href="./b.css">
171
+ <link rel="stylesheet" href="./c.css">
172
+ """
173
+ )
174
+ self.assertEqual(
175
+ style_files.parts,
176
+ {
177
+ './a.css': 3,
178
+ 'https://x/y.css': 1,
179
+ './b.css': 1,
180
+ './c.css': 1,
181
+ },
182
+ )
183
+ self.assertTrue(style_files)
184
+ self.assertFalse(Html.StyleFiles())
185
+ self.assertIn('./a.css', style_files)
186
+ self.assertNotIn('./d.css}', style_files)
187
+ self.assertEqual(
188
+ list(style_files),
189
+ [
190
+ './a.css',
191
+ 'https://x/y.css',
192
+ './b.css',
193
+ './c.css',
194
+ ]
195
+ )
196
+ self.assertEqual(
197
+ style_files,
198
+ Html.StyleFiles(
199
+ './a.css',
200
+ 'https://x/y.css',
201
+ './b.css',
202
+ './c.css',
203
+ )
204
+ )
205
+ self.assertNotEqual(
206
+ style_files,
207
+ Html.StyleFiles(
208
+ './a.css',
209
+ )
210
+ )
211
+ self.assertNotEqual(style_files, Html.Scripts())
212
+ self.assertEqual(
213
+ repr(style_files),
214
+ """StyleFiles(parts={'./a.css': 3, 'https://x/y.css': 1, './b.css': 1, './c.css': 1})"""
215
+ )
216
+ self.assert_html(
217
+ str(style_files),
218
+ """
219
+ <link rel="stylesheet" href="./a.css">
220
+ <link rel="stylesheet" href="https://x/y.css">
221
+ <link rel="stylesheet" href="./b.css">
222
+ <link rel="stylesheet" href="./c.css">
223
+ """
224
+ )
225
+
226
+ def test_scripts(self):
227
+ self.assert_html(Html.Scripts().content, '')
228
+ scripts = Html.Scripts(
229
+ 'function myFun1(p1, p2) { return p1 * p2; }',
230
+ 'console.log("hi");'
231
+ )
232
+ self.assert_html(
233
+ scripts.content,
234
+ """
235
+ <script>
236
+ function myFun1(p1, p2) { return p1 * p2; }
237
+ console.log("hi");
238
+ </script>
239
+ """
240
+ )
241
+ self.assertTrue(scripts.add('function myFun2(p1, p2) { return p1 * p2; }'))
242
+ self.assertFalse(scripts.add('console.log("hi");'))
243
+ self.assertEqual(
244
+ scripts.parts,
245
+ {
246
+ 'function myFun1(p1, p2) { return p1 * p2; }': 1,
247
+ 'function myFun2(p1, p2) { return p1 * p2; }': 1,
248
+ 'console.log("hi");': 2,
249
+ },
250
+ )
251
+ self.assert_html(
252
+ scripts.content,
253
+ """
254
+ <script>
255
+ function myFun1(p1, p2) { return p1 * p2; }
256
+ console.log("hi");
257
+ function myFun2(p1, p2) { return p1 * p2; }
258
+ </script>
259
+ """
260
+ )
261
+ scripts2 = Html.Scripts(
262
+ 'function myFun3(p1, p2) { return p1 * p2; }',
263
+ 'console.log("hi");'
264
+ )
265
+ self.assertTrue(scripts.add(scripts2))
266
+ self.assert_html(
267
+ scripts.content,
268
+ """
269
+ <script>
270
+ function myFun1(p1, p2) { return p1 * p2; }
271
+ console.log("hi");
272
+ function myFun2(p1, p2) { return p1 * p2; }
273
+ function myFun3(p1, p2) { return p1 * p2; }
274
+ </script>
275
+ """
276
+ )
277
+ self.assertEqual(
278
+ scripts.parts,
279
+ {
280
+ 'function myFun1(p1, p2) { return p1 * p2; }': 1,
281
+ 'console.log("hi");': 3,
282
+ 'function myFun2(p1, p2) { return p1 * p2; }': 1,
283
+ 'function myFun3(p1, p2) { return p1 * p2; }': 1,
284
+ },
285
+ )
286
+ self.assertTrue(scripts)
287
+ self.assertFalse(Html.Scripts())
288
+ self.assertIn('function myFun1(p1, p2) { return p1 * p2; }', scripts)
289
+ self.assertNotIn('function myFun4(p1, p2) { return p1 * p2; }', scripts)
290
+ self.assertEqual(
291
+ list(scripts),
292
+ [
293
+ 'function myFun1(p1, p2) { return p1 * p2; }',
294
+ 'console.log("hi");',
295
+ 'function myFun2(p1, p2) { return p1 * p2; }',
296
+ 'function myFun3(p1, p2) { return p1 * p2; }',
297
+ ]
298
+ )
299
+ self.assertEqual(
300
+ scripts,
301
+ Html.Scripts(
302
+ 'function myFun1(p1, p2) { return p1 * p2; }',
303
+ 'console.log("hi");',
304
+ 'function myFun2(p1, p2) { return p1 * p2; }',
305
+ 'function myFun3(p1, p2) { return p1 * p2; }',
306
+ )
307
+ )
308
+ self.assertNotEqual(
309
+ scripts,
310
+ Html.Scripts(
311
+ 'function myFun1(p1, p2) { return p1 * p2; }'
312
+ )
313
+ )
314
+ self.assertNotEqual(scripts, Html.ScriptFiles())
315
+ self.assertEqual(
316
+ repr(scripts),
317
+ """Scripts(parts={'function myFun1(p1, p2) { return p1 * p2; }': 1, 'console.log("hi");': 3, 'function myFun2(p1, p2) { return p1 * p2; }': 1, 'function myFun3(p1, p2) { return p1 * p2; }': 1})"""
318
+ )
319
+ self.assert_html(
320
+ str(scripts),
321
+ """
322
+ <script>
323
+ function myFun1(p1, p2) { return p1 * p2; }
324
+ console.log("hi");
325
+ function myFun2(p1, p2) { return p1 * p2; }
326
+ function myFun3(p1, p2) { return p1 * p2; }
327
+ </script>
328
+ """
329
+ )
330
+
331
+ def test_script_files(self):
332
+ self.assert_html(Html.ScriptFiles().content, '')
333
+ script_files = Html.ScriptFiles('./a.js', 'https://x/y.js')
334
+ self.assert_html(
335
+ script_files.content,
336
+ """
337
+ <script src="./a.js"></script>
338
+ <script src="https://x/y.js"></script>
339
+ """
340
+ )
341
+ self.assertTrue(script_files.add('./b.js'))
342
+ self.assertFalse(script_files.add('./a.js'))
343
+ self.assertEqual(
344
+ script_files.parts,
345
+ {
346
+ './a.js': 2,
347
+ 'https://x/y.js': 1,
348
+ './b.js': 1,
349
+ },
350
+ )
351
+ self.assert_html(
352
+ script_files.content,
353
+ """
354
+ <script src="./a.js"></script>
355
+ <script src="https://x/y.js"></script>
356
+ <script src="./b.js"></script>
357
+ """
358
+ )
359
+ script_files2 = Html.ScriptFiles('./a.js', './c.js')
360
+ script_files.add(script_files2)
361
+ self.assert_html(
362
+ script_files.content,
363
+ """
364
+ <script src="./a.js"></script>
365
+ <script src="https://x/y.js"></script>
366
+ <script src="./b.js"></script>
367
+ <script src="./c.js"></script>
368
+ """
369
+ )
370
+ self.assertEqual(
371
+ script_files.parts,
372
+ {
373
+ './a.js': 3,
374
+ 'https://x/y.js': 1,
375
+ './b.js': 1,
376
+ './c.js': 1,
377
+ },
378
+ )
379
+ self.assertTrue(script_files)
380
+ self.assertFalse(Html.StyleFiles())
381
+ self.assertIn('./a.js', script_files)
382
+ self.assertNotIn('./d.js}', script_files)
383
+ self.assertEqual(
384
+ list(script_files),
385
+ [
386
+ './a.js',
387
+ 'https://x/y.js',
388
+ './b.js',
389
+ './c.js',
390
+ ]
391
+ )
392
+ self.assertEqual(
393
+ script_files,
394
+ Html.ScriptFiles(
395
+ './a.js',
396
+ 'https://x/y.js',
397
+ './b.js',
398
+ './c.js',
399
+ )
400
+ )
401
+ self.assertNotEqual(
402
+ script_files,
403
+ Html.StyleFiles(
404
+ './a.js',
405
+ )
406
+ )
407
+ self.assertNotEqual(script_files, Html.Scripts())
408
+ self.assertEqual(
409
+ repr(script_files),
410
+ """ScriptFiles(parts={'./a.js': 3, 'https://x/y.js': 1, './b.js': 1, './c.js': 1})"""
411
+ )
412
+ self.assert_html(
413
+ str(script_files),
414
+ """
415
+ <script src="./a.js"></script>
416
+ <script src="https://x/y.js"></script>
417
+ <script src="./b.js"></script>
418
+ <script src="./c.js"></script>
419
+ """
420
+ )
421
+
422
+
423
+ class HtmlTest(TestCase):
424
+
425
+ def test_content_init(self):
426
+ html = Html()
427
+ self.assertEqual(html, Html())
428
+
429
+ html = Html('abc')
430
+ self.assertEqual(html, Html('abc'))
431
+
432
+ html = Html(None)
433
+ self.assertEqual(html, Html())
434
+
435
+ html = Html(lambda: 'abc')
436
+ self.assertEqual(html, Html('abc'))
437
+
438
+ html = Html(Html('abc'))
439
+ self.assertEqual(html, Html('abc'))
440
+
441
+ html = Html(
442
+ 'abc',
443
+ lambda: 'def',
444
+ None,
445
+ Html('ghi')
446
+ )
447
+ self.assertEqual(html, Html('abcdefghi'))
448
+
449
+ def test_basics(self):
450
+ html = Html(
451
+ '<h1>foo</h1>',
452
+ styles=['h1 {color: red;}'],
453
+ scripts=['function myFun1(p1, p2) { return p1 * p2; }']
454
+ )
455
+ self.assert_html(html.content, '<h1>foo</h1>')
456
+ self.assert_html(
457
+ html.style_section,
458
+ """
459
+ <style>
460
+ h1 {color: red;}
461
+ </style>
462
+ """,
463
+ )
464
+ self.assert_html(
465
+ html.script_section,
466
+ """
467
+ <script>
468
+ function myFun1(p1, p2) { return p1 * p2; }
469
+ </script>
470
+ """,
471
+ )
472
+
473
+ # Adding the same style.
474
+ html.styles.add('h1 {color: red;}')
475
+ self.assertEqual(list(html.styles), ['h1 {color: red;}'])
476
+
477
+ html.add_style_file('./style1.css')
478
+ self.assertEqual(list(html.style_files), ['./style1.css'])
479
+ self.assert_html(
480
+ html.style_section,
481
+ """
482
+ <link rel="stylesheet" href="./style1.css">
483
+ <style>
484
+ h1 {color: red;}
485
+ </style>
486
+ """,
487
+ )
488
+
489
+ html.scripts.add('function myFun1(p1, p2) { return p1 * p2; }')
490
+ self.assertEqual(
491
+ list(html.scripts),
492
+ ['function myFun1(p1, p2) { return p1 * p2; }']
493
+ )
494
+ html.add_script_file('./script1.js')
495
+ self.assertEqual(list(html.script_files), ['./script1.js'])
496
+ self.assert_html(
497
+ html.script_section,
498
+ """
499
+ <script src="./script1.js"></script>
500
+ <script>
501
+ function myFun1(p1, p2) { return p1 * p2; }
502
+ </script>
503
+ """,
504
+ )
505
+
506
+ html.write('<h2>bar</h2>')
507
+ html.add_style('h2 {color: blue;}')
508
+ html.add_script('function myFun2(p1, p2) { return p1 + p2; }')
509
+ self.assert_html(
510
+ html._repr_html_(),
511
+ """
512
+ <html>
513
+ <head>
514
+ <link rel="stylesheet" href="./style1.css">
515
+ <style>
516
+ h1 {color: red;}
517
+ h2 {color: blue;}
518
+ </style>
519
+ <script src="./script1.js"></script>
520
+ <script>
521
+ function myFun1(p1, p2) { return p1 * p2; }
522
+ function myFun2(p1, p2) { return p1 + p2; }
523
+ </script>
524
+ </head>
525
+ <body>
526
+ <h1>foo</h1><h2>bar</h2>
527
+ </body>
528
+ </html>
529
+ """,
530
+ )
531
+ self.assertEqual(
532
+ repr(html),
533
+ """Html(content='<h1>foo</h1><h2>bar</h2>', style_files=StyleFiles(parts={'./style1.css': 1}), styles=Styles(parts={'h1 {color: red;}': 2, 'h2 {color: blue;}': 1}), script_files=ScriptFiles(parts={'./script1.js': 1}), scripts=Scripts(parts={'function myFun1(p1, p2) { return p1 * p2; }': 2, 'function myFun2(p1, p2) { return p1 + p2; }': 1}))"""
534
+ )
535
+ self.assert_html(
536
+ str(html),
537
+ """
538
+ <html>
539
+ <head>
540
+ <link rel="stylesheet" href="./style1.css">
541
+ <style>
542
+ h1 {color: red;}
543
+ h2 {color: blue;}
544
+ </style>
545
+ <script src="./script1.js"></script>
546
+ <script>
547
+ function myFun1(p1, p2) { return p1 * p2; }
548
+ function myFun2(p1, p2) { return p1 + p2; }
549
+ </script>
550
+ </head>
551
+ <body>
552
+ <h1>foo</h1><h2>bar</h2>
553
+ </body>
554
+ </html>
555
+ """,
556
+ )
557
+
558
+ def test_eq(self):
559
+ def make_test_html(style_file):
560
+ return Html(
561
+ '<h1>foo</h1>',
562
+ styles=['h1 {color: red;}'],
563
+ style_files=[f'./{style_file}.css'],
564
+ scripts=['function myFun1(p1, p2) { return p1 * p2; }'],
565
+ script_files=['./script1.js'],
566
+ )
567
+ html = make_test_html('style1')
568
+ self.assertEqual(html, make_test_html('style1'))
569
+ self.assertNotEqual(html, make_test_html('style2'))
570
+ self.assertEqual(hash(html), hash(make_test_html('style1')))
571
+
572
+ def test_write(self):
573
+ html1 = Html()
574
+ html1.scripts.add('function myFun1(p1, p2) { return p1 * p2; }')
575
+ html1.styles.add('div.a { color: red; }')
576
+ html1.styles.add('div.b { color: red; }')
577
+
578
+ html2 = Html()
579
+ html2.styles.add('div.a { color: red; }')
580
+ html2.styles.add('div.b { color: green; }')
581
+ html2.styles.add('div.c { color: blue; }')
582
+ html1.scripts.add('function myFun1(p1, p2) { return p1 * p2; }')
583
+ html2.write('<div class="c">bar</div>')
584
+ html2.write('\n<script>\nconsole.log("bar");\n</script>')
585
+
586
+ html1.write('<div class="a">foo</div>')
587
+ html1.write('\n<script>\nconsole.log("foo");\n</script>\n')
588
+ html1.write(html2)
589
+
590
+ self.assert_html(
591
+ html1._repr_html_(),
592
+ """
593
+ <html>
594
+ <head>
595
+ <style>
596
+ div.a { color: red; }
597
+ div.b { color: red; }
598
+ div.b { color: green; }
599
+ div.c { color: blue; }
600
+ </style>
601
+ <script>
602
+ function myFun1(p1, p2) { return p1 * p2; }
603
+ </script>
604
+ </head>
605
+ <body>
606
+ <div class="a">foo</div>
607
+ <script>
608
+ console.log("foo");
609
+ </script>
610
+ <div class="c">bar</div>
611
+ <script>
612
+ console.log("bar");
613
+ </script>
614
+ </body>
615
+ </html>
616
+ """,
617
+ )
618
+ self.assert_html(
619
+ html1.to_str(content_only=True),
620
+ """
621
+ <div class="a">foo</div>
622
+ <script>
623
+ console.log("foo");
624
+ </script>
625
+ <div class="c">bar</div>
626
+ <script>
627
+ console.log("bar");
628
+ </script>
629
+ """
630
+ )
631
+
632
+ def test_from_value(self):
633
+ html = Html(
634
+ 'hi', styles=['h1 {color: red;}'], scripts=['console.log("hi");']
635
+ )
636
+ self.assertIs(Html.from_value(html), html)
637
+ self.assertIsNone(Html.from_value(None))
638
+ self.assertEqual(
639
+ Html.from_value('abc'), Html('abc')
640
+ )
641
+ html2 = Html.from_value(html, copy=True)
642
+ self.assertIsNot(html2, html)
643
+ self.assertEqual(html2, html)
644
+
645
+ with self.assertRaises(TypeError):
646
+ Html.from_value(1)
647
+
648
+ def test_add_and_radd(self):
649
+ s1 = Html('<h1>foo</h1>', styles=['h1 {color: red;}'])
650
+ s2 = Html('<h2>bar</h2>', styles=['h2 {color: blue;}'])
651
+ s3 = s1 + s2
652
+ self.assertIsNot(s3, s1)
653
+ self.assertIsNot(s3, s2)
654
+ self.assertEqual(
655
+ s3,
656
+ Html(
657
+ '<h1>foo</h1><h2>bar</h2>',
658
+ styles=['h1 {color: red;}', 'h2 {color: blue;}'],
659
+ )
660
+ )
661
+ s4 = s1 + '<h3>baz</h3>'
662
+ self.assertEqual(
663
+ s4,
664
+ Html(
665
+ '<h1>foo</h1><h3>baz</h3>',
666
+ styles=['h1 {color: red;}'],
667
+ )
668
+ )
669
+ s5 = '<h3>baz</h3>' + s1
670
+ self.assertIsNot(s5, s1)
671
+ self.assertEqual(
672
+ s5,
673
+ Html(
674
+ '<h3>baz</h3><h1>foo</h1>',
675
+ styles=['h1 {color: red;}'],
676
+ )
677
+ )
678
+
679
+ s6 = Html('<hr>')
680
+ self.assertIs(s6 + None, s6)
681
+ self.assertIs(s6, s6 + None)
682
+
683
+ self.assertEqual(
684
+ Html('<hr>') + (lambda: '<div>bar</div>'),
685
+ Html('<hr><div>bar</div>')
686
+ )
687
+ self.assertEqual(
688
+ (lambda: '<div>bar</div>') + Html('<hr>'),
689
+ Html('<div>bar</div><hr>')
690
+ )
691
+
692
+ def test_escape(self):
693
+ self.assertIsNone(Html.escape(None))
694
+ self.assertEqual(Html.escape('foo'), 'foo')
695
+ self.assertEqual(Html.escape('foo"bar'), 'foo&quot;bar')
696
+ self.assertEqual(Html.escape(Html('foo"bar')), Html('foo&quot;bar'))
697
+ self.assertEqual(Html.escape(lambda: 'foo"bar'), 'foo&quot;bar')
698
+
699
+ def test_element(self):
700
+ # Empty element.
701
+ self.assertEqual(Html.element('div').content, '<div></div>')
702
+ # CSS class as list.
703
+ self.assertEqual(
704
+ Html.element('div', css_class=['a', 'b', None]).content,
705
+ '<div class="a b"></div>',
706
+ )
707
+ self.assertEqual(
708
+ Html.element('div', css_class=[None, None]).content,
709
+ '<div></div>',
710
+ )
711
+ # Style as string.
712
+ self.assertEqual(
713
+ Html.element('div', style='color:red;').content,
714
+ '<div style="color:red;"></div>',
715
+ )
716
+ # Style as dictionary.
717
+ self.assertEqual(
718
+ Html.element(
719
+ 'div',
720
+ style=dict(
721
+ color='red', background_color='blue', width=None,
722
+ )
723
+ ).content,
724
+ '<div style="color:red;background-color:blue;"></div>',
725
+ )
726
+ self.assertEqual(
727
+ Html.element(
728
+ 'div',
729
+ style=dict(
730
+ color=None,
731
+ )
732
+ ).content,
733
+ '<div></div>',
734
+ )
735
+ # Properties as kwargs
736
+ self.assertEqual(
737
+ Html.element(
738
+ 'details',
739
+ options='open',
740
+ css_class='my_class',
741
+ id='my_id',
742
+ custom_property='1'
743
+ ).content,
744
+ (
745
+ '<details open class="my_class" id="my_id" custom-property="1">'
746
+ '</details>'
747
+ )
748
+ )
749
+ self.assertEqual(
750
+ Html.element(
751
+ 'details',
752
+ options=[None],
753
+ css_class='my_class',
754
+ id='my_id',
755
+ custom_property='1'
756
+ ).content,
757
+ (
758
+ '<details class="my_class" id="my_id" custom-property="1">'
759
+ '</details>'
760
+ )
761
+ )
762
+ # Child.
763
+ self.assertEqual(
764
+ Html.element(
765
+ 'div',
766
+ css_class='my_class',
767
+ inner_html='<h1>foo</h1>'
768
+ ).content,
769
+ '<div class="my_class"><h1>foo</h1></div>',
770
+ )
771
+ # Children.
772
+ self.assert_html(
773
+ str(Html.element(
774
+ 'div',
775
+ [
776
+ '<hr>',
777
+ lambda: '<div>bar</div>',
778
+ None,
779
+ Html.element(
780
+ 'div',
781
+ css_class='my_class',
782
+ ).add_style('div.my_class { color: red; }')
783
+ ]
784
+ )),
785
+ """
786
+ <html>
787
+ <head>
788
+ <style>
789
+ div.my_class { color: red; }
790
+ </style>
791
+ </head>
792
+ <body>
793
+ <div><hr><div>bar</div><div class="my_class"></div></div>
794
+ </body>
795
+ </html>
796
+ """
797
+ )
798
+
799
+
800
+ # pylint: enable=line-too-long
801
+
802
+
803
+ if __name__ == '__main__':
804
+ unittest.main()