xcomponent 0.5.2__tar.gz → 0.6.0__tar.gz
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.
- {xcomponent-0.5.2 → xcomponent-0.6.0}/CHANGELOG.md +5 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/Cargo.lock +1 -1
- {xcomponent-0.5.2 → xcomponent-0.6.0}/Cargo.toml +1 -1
- {xcomponent-0.5.2 → xcomponent-0.6.0}/PKG-INFO +1 -1
- {xcomponent-0.5.2 → xcomponent-0.6.0}/docs/source/user/getting_started.md +45 -10
- {xcomponent-0.5.2 → xcomponent-0.6.0}/pyproject.toml +3 -1
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/context.rs +3 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/expression/ast/eval.rs +9 -4
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/expression/ast/model.rs +4 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/expression/ast/parse.rs +5 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/expression/grammar.pest +5 -3
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/expression/parser.rs +8 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/expression/tokens.rs +5 -0
- xcomponent-0.6.0/tests/unittests/conftest.py +41 -0
- xcomponent-0.6.0/tests/unittests/expression/test_let.py +26 -0
- xcomponent-0.6.0/tests/unittests/expression/test_str_method.py +22 -0
- xcomponent-0.6.0/tests/unittests/test_named_component.py +30 -0
- xcomponent-0.6.0/tests/unittests/test_render_attributes.py +141 -0
- xcomponent-0.6.0/tests/unittests/test_render_context.py +55 -0
- xcomponent-0.6.0/tests/unittests/test_render_globals.py +50 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/test_render_globals_obj.py +18 -19
- xcomponent-0.6.0/tests/unittests/test_render_globals_obj2.py +55 -0
- xcomponent-0.6.0/tests/unittests/test_render_nested_dict.py +24 -0
- xcomponent-0.6.0/tests/unittests/test_render_nested_node.py +58 -0
- xcomponent-0.6.0/tests/unittests/test_render_node.py +167 -0
- xcomponent-0.6.0/tests/unittests/test_render_script.py +22 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/uv.lock +1 -1
- xcomponent-0.5.2/tests/unittests/test_named_component.py +0 -31
- xcomponent-0.5.2/tests/unittests/test_render_attributes.py +0 -158
- xcomponent-0.5.2/tests/unittests/test_render_context.py +0 -50
- xcomponent-0.5.2/tests/unittests/test_render_globals.py +0 -49
- xcomponent-0.5.2/tests/unittests/test_render_globals_obj2.py +0 -54
- xcomponent-0.5.2/tests/unittests/test_render_nested_dict.py +0 -22
- xcomponent-0.5.2/tests/unittests/test_render_nested_node.py +0 -56
- xcomponent-0.5.2/tests/unittests/test_render_node.py +0 -146
- xcomponent-0.5.2/tests/unittests/test_render_script.py +0 -19
- {xcomponent-0.5.2 → xcomponent-0.6.0}/.github/workflows/build-sdist.yml +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/.github/workflows/build-wheels.yml +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/.github/workflows/publish-doc.yml +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/.github/workflows/publish-pypi.yml +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/.github/workflows/release.yml +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/.github/workflows/tests-artifacts.yml +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/.github/workflows/tests.yml +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/.gitignore +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/.python-version +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/Justfile +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/LICENSE +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/README.md +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/docs/Makefile +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/docs/source/conf.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/docs/source/develop/.gitkeep +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/docs/source/index.md +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/docs/source/user/changelog.md +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/scripts/get_version.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/scripts/write_changelog.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/python/xcomponent/__init__.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/python/xcomponent/py.typed +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/python/xcomponent/service/__init__.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/python/xcomponent/service/catalog.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/python/xcomponent/xcore.pyi +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/catalog/catalog.rs +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/catalog/mod.rs +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/expression/ast/mod.rs +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/expression/mod.rs +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/lib.rs +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/markup/grammar.pest +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/markup/mod.rs +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/markup/parser.rs +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/src/rust/markup/tokens.rs +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/__init__.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/__init__.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/expression/test_binary_eq.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/expression/test_binary_op.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/expression/test_binary_op_bool.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/expression/test_default_values.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/expression/test_field_access.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/expression/test_for_expr.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/expression/test_func.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/expression/test_if_expr.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/expression/test_nested.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/expression/test_render_comment.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/expression/test_types.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/expression/test_unary.py +0 -0
- {xcomponent-0.5.2 → xcomponent-0.6.0}/tests/unittests/test_core.py +0 -0
|
@@ -228,7 +228,6 @@ assert HelloWebPage(globals={"title": "my title", "description": ""}) == (
|
|
|
228
228
|
)
|
|
229
229
|
```
|
|
230
230
|
|
|
231
|
-
|
|
232
231
|
The description is passed as an empty string and is mandatory.
|
|
233
232
|
The access to `globals.description` will raise a KeyError if it is not present.
|
|
234
233
|
|
|
@@ -241,7 +240,7 @@ proper documentation.
|
|
|
241
240
|
|
|
242
241
|
Everything in the Markup that has been enclosed by curly braces is an expression.
|
|
243
242
|
An expression enables the dynamic rendering of a page, using variables,
|
|
244
|
-
`if` statements, `for` statements, and operators.
|
|
243
|
+
`if` statements, `for` statements, `let` statements, and operators.
|
|
245
244
|
|
|
246
245
|
### Types
|
|
247
246
|
|
|
@@ -268,8 +267,8 @@ String objects can be enclosed by double quotes or single quotes.
|
|
|
268
267
|
Boolean values are `true` and `false`, like in JavaScript, Rust, and many languages,
|
|
269
268
|
except Python.
|
|
270
269
|
|
|
271
|
-
None value is render has an empty string for a XNode, and
|
|
272
|
-
the rendering of attributes.
|
|
270
|
+
None value is render has an empty string for a XNode, and is used to remove
|
|
271
|
+
the rendering of tag attributes.
|
|
273
272
|
|
|
274
273
|
### Functions
|
|
275
274
|
|
|
@@ -287,6 +286,20 @@ def HelloWorld(name: str) -> str:
|
|
|
287
286
|
|
|
288
287
|
When the functions are called, the simple types are cast to Python types.
|
|
289
288
|
|
|
289
|
+
````{node}
|
|
290
|
+
|
|
291
|
+
It is possible to call the str methods directly in the XComponent expression.
|
|
292
|
+
|
|
293
|
+
The capitalized function was a simple exemple for function explanation.
|
|
294
|
+
|
|
295
|
+
```python
|
|
296
|
+
@catalog.component
|
|
297
|
+
def HelloWorld(name: str) -> str:
|
|
298
|
+
return """<>{"Hello " + name.capitalize()}</>"""
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
````
|
|
302
|
+
|
|
290
303
|
### List Index
|
|
291
304
|
|
|
292
305
|
To access an index in a list, the `[]` must be used.
|
|
@@ -327,7 +340,6 @@ between booleans and integers are permitted.
|
|
|
327
340
|
|
|
328
341
|
Strings can be multiplied by an integer, like in Python, to produce a repeated string.
|
|
329
342
|
|
|
330
|
-
|
|
331
343
|
#### Binary Operators
|
|
332
344
|
|
|
333
345
|
All types support Python truthy/falsey values.
|
|
@@ -336,15 +348,12 @@ The operators include `and` and `or`. The `not` operator is used to reverse a co
|
|
|
336
348
|
|
|
337
349
|
Also, Python functions can be registered to mitigate or implement complex binary operations.
|
|
338
350
|
|
|
339
|
-
|
|
340
351
|
#### Comparison Operators
|
|
341
352
|
|
|
342
353
|
| `==` | `!=` | `>=` | `<=` | `>` | `<` |
|
|
343
354
|
| ------ | ---------- | --------------------- | ------------------ | ------------ | --------- |
|
|
344
355
|
| equals | not equals | greater than or equal | less than or equal | greater than | less than |
|
|
345
356
|
|
|
346
|
-
|
|
347
|
-
|
|
348
357
|
#### Priority
|
|
349
358
|
|
|
350
359
|
The priority of operators follows the mathematical order.
|
|
@@ -352,8 +361,7 @@ Multiplication and division have the highest priority, followed by addition and
|
|
|
352
361
|
subtraction, then greater than or equal to and less than or equal to, next are
|
|
353
362
|
equals and not equals, followed by the and operator, and finally the or operator.
|
|
354
363
|
|
|
355
|
-
The parenthesis, such as **(
|
|
356
|
-
|
|
364
|
+
The parenthesis, such as **( _condition_ )** can be used to override the priority.
|
|
357
365
|
|
|
358
366
|
### If Syntax
|
|
359
367
|
|
|
@@ -398,3 +406,30 @@ this is not python code, you can do
|
|
|
398
406
|
}
|
|
399
407
|
```
|
|
400
408
|
````
|
|
409
|
+
|
|
410
|
+
### Let Syntax
|
|
411
|
+
|
|
412
|
+
The `let` keywork let you declare new constant in the component.
|
|
413
|
+
|
|
414
|
+
The expression cannot have multiple statement separated by indentation or semicolum.
|
|
415
|
+
Since the expression language is minimal, an other expression can be used to display
|
|
416
|
+
a variable that has been set, or reuse it to do a condition.
|
|
417
|
+
The variable will be local to the component.
|
|
418
|
+
Says differently, the `let` keyword declare constant scoped by component.
|
|
419
|
+
|
|
420
|
+
The `let` syntax is like the Rust syntax.
|
|
421
|
+
It means that the binary expression is affected to the variable.
|
|
422
|
+
|
|
423
|
+
Example:
|
|
424
|
+
|
|
425
|
+
```jsx
|
|
426
|
+
{/* simple let statement */}
|
|
427
|
+
{ let x = 42 }
|
|
428
|
+
{/* display the value */}
|
|
429
|
+
{x}
|
|
430
|
+
|
|
431
|
+
{/* if statement with else */}
|
|
432
|
+
{let y = if (x >= 1) {"Yes"} else {"No"}}
|
|
433
|
+
{/* display the value */}
|
|
434
|
+
{y}
|
|
435
|
+
```
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "xcomponent"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.6.0"
|
|
4
4
|
description = "A component base template engige - inspired by JSX"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [{ name = "Guillaume Gauvrit", email = "guillaume@gauvr.it" }]
|
|
@@ -53,6 +53,8 @@ docs = [
|
|
|
53
53
|
]
|
|
54
54
|
|
|
55
55
|
[tool.pyright]
|
|
56
|
+
autoSearchPaths = false
|
|
57
|
+
extraPaths = ["src/python"]
|
|
56
58
|
include = ["src/python", "tests"]
|
|
57
59
|
typeCheckingMode = "strict"
|
|
58
60
|
reportMissingTypeStubs = false
|
|
@@ -302,6 +302,9 @@ impl RenderContext {
|
|
|
302
302
|
d.insert(key, value);
|
|
303
303
|
self.stack.push(d);
|
|
304
304
|
}
|
|
305
|
+
pub fn insert_current(&mut self, key: LiteralKey, value: Literal) {
|
|
306
|
+
self.stack.last_mut().unwrap().insert(key, value);
|
|
307
|
+
}
|
|
305
308
|
pub fn get(&self, key: &LiteralKey) -> Option<&Literal> {
|
|
306
309
|
self.stack.iter().rev().find_map(|scope| scope.get(key))
|
|
307
310
|
}
|
|
@@ -264,10 +264,10 @@ pub fn eval_ast<'py>(
|
|
|
264
264
|
let item = o.obj().getattr(py, field)?.into_bound(py);
|
|
265
265
|
Literal::downcast(py, item)
|
|
266
266
|
}
|
|
267
|
-
_ =>
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
267
|
+
_ => {
|
|
268
|
+
let item = base.into_py(py).getattr(field)?;
|
|
269
|
+
Literal::downcast(py, item)
|
|
270
|
+
}
|
|
271
271
|
}
|
|
272
272
|
}
|
|
273
273
|
|
|
@@ -401,6 +401,11 @@ pub fn eval_ast<'py>(
|
|
|
401
401
|
))),
|
|
402
402
|
}
|
|
403
403
|
}
|
|
404
|
+
AST::LetStatement { ident, expr } => {
|
|
405
|
+
let value = eval_ast(py, expr, catalog, context)?;
|
|
406
|
+
context.insert_current(LiteralKey::Str(ident.clone()), value);
|
|
407
|
+
Ok(Literal::Str("".to_string()))
|
|
408
|
+
}
|
|
404
409
|
}
|
|
405
410
|
}
|
|
406
411
|
|
|
@@ -54,6 +54,11 @@ pub fn token_to_ast(tok: &ExpressionToken, min_prec: u8) -> Result<AST, PyErr> {
|
|
|
54
54
|
iterable: token_to_ast(iterable, min_prec).map(|x| Box::new(x))?,
|
|
55
55
|
body: token_to_ast(body, min_prec).map(|x| Box::new(x))?,
|
|
56
56
|
}),
|
|
57
|
+
|
|
58
|
+
ExpressionToken::LetExpression { ident, expr } => Ok(AST::LetStatement {
|
|
59
|
+
ident: ident.clone(),
|
|
60
|
+
expr: token_to_ast(expr, min_prec).map(|x| Box::new(x))?,
|
|
61
|
+
}),
|
|
57
62
|
// Comment produce a Noop
|
|
58
63
|
ExpressionToken::Noop => Ok(AST::Literal(Literal::Str("".to_string()))),
|
|
59
64
|
_ => Err(PySyntaxError::new_err(format!(
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
expression = { for_expression | if_expression | binary_expression | comment_expression }
|
|
1
|
+
expression = { for_expression | if_expression | let_expression | binary_expression | comment_expression }
|
|
2
2
|
|
|
3
3
|
binary_expression = { comment_expression* ~ whitespace* ~ term ~ (whitespace* ~ operator ~ whitespace* ~ term)* }
|
|
4
4
|
|
|
5
|
-
term = _{ for_expression | if_expression | unary_expression | postfix }
|
|
5
|
+
term = _{ for_expression | if_expression | let_expression | unary_expression | postfix }
|
|
6
6
|
|
|
7
7
|
unary_expression = { "not" ~ whitespace+ ~ binary_expression }
|
|
8
8
|
|
|
@@ -28,7 +28,7 @@ boolean = { "true" | "false" }
|
|
|
28
28
|
integer = @{ (ASCII_DIGIT | "_")+ }
|
|
29
29
|
string = @{ "\"" ~ (escape_sequence | !("\"" | "\\") ~ ANY)* ~ "\"" }
|
|
30
30
|
|
|
31
|
-
keyword = { "for" | "in" | "if" | "else" | "not" | "and" | "or" }
|
|
31
|
+
keyword = { "for" | "in" | "if" | "else" | "not" | "and" | "or" | "let" }
|
|
32
32
|
|
|
33
33
|
keyword_spaced = { keyword ~ (whitespace+ | ".") }
|
|
34
34
|
ident = { !keyword_spaced ~ ident_raw }
|
|
@@ -43,6 +43,8 @@ if_expression = {
|
|
|
43
43
|
|
|
44
44
|
for_expression = { "for" ~ whitespace+ ~ ident ~ whitespace+ ~ "in" ~ whitespace+ ~ expression ~ whitespace* ~ block }
|
|
45
45
|
|
|
46
|
+
let_expression = { "let" ~ whitespace+ ~ ident ~ whitespace* ~ "=" ~ whitespace* ~ expression }
|
|
47
|
+
|
|
46
48
|
block = { whitespace* ~"{" ~ whitespace* ~ expression ~ whitespace* ~ "}" ~ whitespace* }
|
|
47
49
|
comment_expression = _{ "/*" ~ comment ~ "*/" }
|
|
48
50
|
comment = { (!"*/" ~ ANY)* }
|
|
@@ -115,6 +115,14 @@ fn parse_expression_token(pair: Pair<Rule>) -> Result<ExpressionToken, String> {
|
|
|
115
115
|
body,
|
|
116
116
|
})
|
|
117
117
|
}
|
|
118
|
+
Rule::let_expression => {
|
|
119
|
+
let mut inner = pair.into_inner();
|
|
120
|
+
let ident = inner.next().unwrap().as_str().to_string();
|
|
121
|
+
let expr_expr = inner.next().unwrap();
|
|
122
|
+
let expr = Box::new(parse_expression_token(expr_expr)?);
|
|
123
|
+
|
|
124
|
+
Ok(ExpressionToken::LetExpression { ident, expr })
|
|
125
|
+
}
|
|
118
126
|
Rule::ident => {
|
|
119
127
|
let content = pair.as_str();
|
|
120
128
|
debug!("Pushing ident {}", content);
|
|
@@ -148,6 +148,10 @@ pub enum ExpressionToken {
|
|
|
148
148
|
iterable: Box<ExpressionToken>,
|
|
149
149
|
body: Box<ExpressionToken>,
|
|
150
150
|
},
|
|
151
|
+
LetExpression {
|
|
152
|
+
ident: String,
|
|
153
|
+
expr: Box<ExpressionToken>,
|
|
154
|
+
},
|
|
151
155
|
Noop,
|
|
152
156
|
}
|
|
153
157
|
|
|
@@ -200,6 +204,7 @@ impl std::fmt::Display for ExpressionToken {
|
|
|
200
204
|
iterable,
|
|
201
205
|
body,
|
|
202
206
|
} => write!(f, "for {} in {} {{ {} }}", ident, iterable, body),
|
|
207
|
+
ExpressionToken::LetExpression { ident, expr } => write!(f, "let {} = {}", ident, expr),
|
|
203
208
|
ExpressionToken::Noop => write!(f, ""), // ??
|
|
204
209
|
}
|
|
205
210
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from collections.abc import Iterator, Mapping
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
from bs4 import BeautifulSoup, PageElement # type: ignore
|
|
6
|
+
|
|
7
|
+
from xcomponent import Catalog
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@pytest.fixture()
|
|
11
|
+
def catalog() -> Catalog:
|
|
12
|
+
return Catalog()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@pytest.fixture()
|
|
16
|
+
def globals() -> Mapping[str, str]:
|
|
17
|
+
return {
|
|
18
|
+
"RADIO_DIV_CLASS": "RADIO_DIV_CLASS",
|
|
19
|
+
"RADIO_INPUT_CLASS": "RADIO_INPUT_CLASS",
|
|
20
|
+
"RADIO_LABEL_CLASS": "RADIO_LABEL_CLASS",
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@pytest.fixture()
|
|
25
|
+
def soup_rendered(
|
|
26
|
+
catalog: Catalog, template_string: str, globals: Mapping[str, str]
|
|
27
|
+
) -> Iterator[PageElement]:
|
|
28
|
+
rendered = catalog.render(template_string.strip(), globals=globals)
|
|
29
|
+
soup = BeautifulSoup(rendered, "html.parser")
|
|
30
|
+
try:
|
|
31
|
+
yield next(soup.children) # type: ignore
|
|
32
|
+
except Exception as exc:
|
|
33
|
+
# Display the error in the assertion is a compromise
|
|
34
|
+
yield exc # type: ignore
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@pytest.fixture()
|
|
38
|
+
def soup_expected(expected_string: str) -> PageElement:
|
|
39
|
+
expected_string = re.sub(r">\s+<", "><", expected_string).strip()
|
|
40
|
+
expected_soup = BeautifulSoup(expected_string, features="html.parser")
|
|
41
|
+
return next(expected_soup.children) # type: ignore
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from xcomponent import Catalog
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@pytest.fixture(autouse=True)
|
|
7
|
+
def components(catalog: Catalog):
|
|
8
|
+
@catalog.component
|
|
9
|
+
def DummyInt() -> str:
|
|
10
|
+
return """<>{let a = 42}{a}</>"""
|
|
11
|
+
|
|
12
|
+
@catalog.component
|
|
13
|
+
def YesNo(a: int) -> str:
|
|
14
|
+
return """<>{let b = if (a >= 1) {"Yes"} else {"No"}}{b}</>"""
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@pytest.mark.parametrize(
|
|
18
|
+
"template_string,expected",
|
|
19
|
+
[
|
|
20
|
+
pytest.param("<DummyInt />", "42", id="simple let"),
|
|
21
|
+
pytest.param("<YesNo a={0} />", "No", id="let if"),
|
|
22
|
+
pytest.param("<YesNo a={2} />", "Yes", id="let else"),
|
|
23
|
+
],
|
|
24
|
+
)
|
|
25
|
+
def test_nested(catalog: Catalog, template_string: str, expected: str):
|
|
26
|
+
assert catalog.render(template_string) == expected
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from xcomponent import Catalog
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@pytest.fixture(autouse=True)
|
|
7
|
+
def components(catalog: Catalog):
|
|
8
|
+
@catalog.component
|
|
9
|
+
def Dummy() -> str:
|
|
10
|
+
return '<>{"dummy-str".replace("-", "_")}</>'
|
|
11
|
+
|
|
12
|
+
return Dummy
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@pytest.mark.parametrize(
|
|
16
|
+
"template_string,expected",
|
|
17
|
+
[
|
|
18
|
+
pytest.param("<Dummy />", "dummy_str", id="replace"),
|
|
19
|
+
],
|
|
20
|
+
)
|
|
21
|
+
def test_render_nested_property(catalog: Catalog, template_string: str, expected: str):
|
|
22
|
+
assert catalog.render(template_string) == expected
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from xcomponent import Catalog
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@pytest.fixture(autouse=True)
|
|
7
|
+
def components(catalog: Catalog):
|
|
8
|
+
@catalog.component(name="HelloWorld1")
|
|
9
|
+
def hello_world(name="world"):
|
|
10
|
+
return "<>Hello {name}</>"
|
|
11
|
+
|
|
12
|
+
@catalog.component()
|
|
13
|
+
def HelloWorld2(name="world"):
|
|
14
|
+
return "<>Hello {name}</>"
|
|
15
|
+
|
|
16
|
+
@catalog.component
|
|
17
|
+
def HelloWorld3(name="world"):
|
|
18
|
+
return "<>Hello {name}</>"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@pytest.mark.parametrize(
|
|
22
|
+
"markup",
|
|
23
|
+
[
|
|
24
|
+
pytest.param("<HelloWorld1/>", id="explicit name"),
|
|
25
|
+
pytest.param("<HelloWorld2/>", id="called implicit"),
|
|
26
|
+
pytest.param("<HelloWorld3/>", id="implicit"),
|
|
27
|
+
],
|
|
28
|
+
)
|
|
29
|
+
def test_named_component(catalog: Catalog, markup: str):
|
|
30
|
+
assert catalog.render(markup) == "Hello world"
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
from typing import Literal
|
|
2
|
+
|
|
3
|
+
from bs4.element import PageElement
|
|
4
|
+
from xcomponent import Catalog, XNode
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@pytest.fixture(autouse=True)
|
|
10
|
+
def components(catalog: Catalog):
|
|
11
|
+
@catalog.component
|
|
12
|
+
def Form(
|
|
13
|
+
method: Literal["get", "post"] | None = None,
|
|
14
|
+
action: str | None = None,
|
|
15
|
+
hx_target: str | None = None,
|
|
16
|
+
children: XNode | None = None,
|
|
17
|
+
) -> str:
|
|
18
|
+
return """
|
|
19
|
+
<form
|
|
20
|
+
hx-target={hx_target}
|
|
21
|
+
action={action}
|
|
22
|
+
method={method}
|
|
23
|
+
>
|
|
24
|
+
{ children }
|
|
25
|
+
</form>
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
@catalog.component
|
|
29
|
+
def Label(
|
|
30
|
+
for_: str | None = None,
|
|
31
|
+
class_: str | None = None,
|
|
32
|
+
) -> str:
|
|
33
|
+
return """
|
|
34
|
+
<label
|
|
35
|
+
for={for_}
|
|
36
|
+
class={class_}
|
|
37
|
+
>
|
|
38
|
+
{ children }
|
|
39
|
+
</label>
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
@catalog.component
|
|
43
|
+
def Button(hx_vals: str) -> str:
|
|
44
|
+
return """
|
|
45
|
+
<button hx-vals={hx_vals}>Submit</button>
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
@catalog.component
|
|
49
|
+
def Radio(
|
|
50
|
+
globals: dict[str, str],
|
|
51
|
+
label: str,
|
|
52
|
+
name: str,
|
|
53
|
+
value: str,
|
|
54
|
+
id: str | None = None,
|
|
55
|
+
checked: bool = False,
|
|
56
|
+
disabled: bool = False,
|
|
57
|
+
onclick: str | None = None,
|
|
58
|
+
div_class: str | None = None,
|
|
59
|
+
class_: str | None = None,
|
|
60
|
+
label_class: str | None = None,
|
|
61
|
+
) -> str:
|
|
62
|
+
return """
|
|
63
|
+
<div class={div_class or globals.RADIO_DIV_CLASS}>
|
|
64
|
+
<input type="radio" name={name} id={id} value={value}
|
|
65
|
+
class={class_ or globals.RADIO_INPUT_CLASS}
|
|
66
|
+
onclick={onclick}
|
|
67
|
+
checked={checked}
|
|
68
|
+
disabled={disabled} />
|
|
69
|
+
<Label for={id} class={label_class or globals.RADIO_LABEL_CLASS}>
|
|
70
|
+
{label}
|
|
71
|
+
</Label>
|
|
72
|
+
</div>
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@pytest.mark.parametrize(
|
|
77
|
+
"template_string,expected_string",
|
|
78
|
+
[
|
|
79
|
+
pytest.param("<Form />", "<form></form>", id="drop-none"),
|
|
80
|
+
pytest.param(
|
|
81
|
+
"<Form><input/></Form>",
|
|
82
|
+
"<form><input/></form>",
|
|
83
|
+
id="drop-none",
|
|
84
|
+
),
|
|
85
|
+
pytest.param(
|
|
86
|
+
"<Form hx_target='/ajax'><input/></Form>",
|
|
87
|
+
'<form hx-target="/ajax"><input/></form>',
|
|
88
|
+
id="forward hx_target",
|
|
89
|
+
),
|
|
90
|
+
pytest.param(
|
|
91
|
+
"<Form hx-target='/ajax'><input/></Form>",
|
|
92
|
+
'<form hx-target="/ajax"><input/></form>',
|
|
93
|
+
id="forward hx-target",
|
|
94
|
+
),
|
|
95
|
+
# we don't test multiple attributes since rust hashmap are not ordered
|
|
96
|
+
pytest.param(
|
|
97
|
+
"<Form><Label class='p-4'>Name:</Label></Form>",
|
|
98
|
+
'<form><label class="p-4">Name:</label></form>',
|
|
99
|
+
id="forward class and for",
|
|
100
|
+
),
|
|
101
|
+
pytest.param(
|
|
102
|
+
"<Form><Label for='name'>Name:</Label></Form>",
|
|
103
|
+
'<form><label for="name">Name:</label></form>',
|
|
104
|
+
id="forward class and for",
|
|
105
|
+
),
|
|
106
|
+
],
|
|
107
|
+
)
|
|
108
|
+
def test_render_form(soup_rendered: PageElement, soup_expected: PageElement):
|
|
109
|
+
assert soup_rendered == soup_expected
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@pytest.mark.parametrize(
|
|
113
|
+
"template_string,expected_string",
|
|
114
|
+
[
|
|
115
|
+
pytest.param(
|
|
116
|
+
"""<Button hx-vals='{"a":"A"}' />""",
|
|
117
|
+
"""<button hx-vals='{"a":"A"}'>Submit</button>""",
|
|
118
|
+
id="forward class and for",
|
|
119
|
+
)
|
|
120
|
+
],
|
|
121
|
+
)
|
|
122
|
+
def test_render_button(soup_rendered: PageElement, soup_expected: PageElement):
|
|
123
|
+
assert soup_rendered == soup_expected
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@pytest.mark.parametrize(
|
|
127
|
+
"template_string,expected_string",
|
|
128
|
+
[
|
|
129
|
+
pytest.param(
|
|
130
|
+
"""
|
|
131
|
+
<Radio name="n" value="v"
|
|
132
|
+
label="lbl" class="radio" label-class="lbl" div-class="d" />
|
|
133
|
+
""",
|
|
134
|
+
'<div class="d"><input type="radio" name="n" class="radio" value="v"/>'
|
|
135
|
+
'<label class="lbl">lbl</label></div>',
|
|
136
|
+
id="dont alter suffixed by class",
|
|
137
|
+
)
|
|
138
|
+
],
|
|
139
|
+
)
|
|
140
|
+
def test_render_radio(soup_rendered: PageElement, soup_expected: PageElement):
|
|
141
|
+
assert soup_rendered == soup_expected
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from xcomponent import Catalog
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.fixture()
|
|
8
|
+
def BadIndex(catalog: Catalog):
|
|
9
|
+
@catalog.component
|
|
10
|
+
def BadExcerpt(title: str) -> str:
|
|
11
|
+
return "<li>{summary}</li>"
|
|
12
|
+
|
|
13
|
+
@catalog.component
|
|
14
|
+
def BadIndex(summaries: list[str]) -> str:
|
|
15
|
+
return """
|
|
16
|
+
<ul>
|
|
17
|
+
{
|
|
18
|
+
for summary in summaries {
|
|
19
|
+
<BadExcerpt title={summary} />
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
</ul>
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
return BadIndex
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@pytest.fixture()
|
|
29
|
+
def GoodIndex(catalog: Catalog):
|
|
30
|
+
@catalog.component
|
|
31
|
+
def GoodExcerpt(title: str) -> str:
|
|
32
|
+
return "<li>{title}</li>"
|
|
33
|
+
|
|
34
|
+
@catalog.component
|
|
35
|
+
def GoodIndex(summaries: list[str]) -> str:
|
|
36
|
+
return """
|
|
37
|
+
<ul>
|
|
38
|
+
{
|
|
39
|
+
for summary in summaries {
|
|
40
|
+
<GoodExcerpt title={summary} />
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
</ul>
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
return GoodIndex
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_raises(BadIndex: Any):
|
|
50
|
+
with pytest.raises(UnboundLocalError):
|
|
51
|
+
assert BadIndex(summaries=["foo", "bar"])
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_ok(GoodIndex: Any):
|
|
55
|
+
assert GoodIndex(summaries=["foo", "bar"]) == "<ul><li>foo</li><li>bar</li></ul>"
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
from xcomponent import Catalog, Component, XNode
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@pytest.fixture()
|
|
7
|
+
def HelloWebPage(catalog: Catalog):
|
|
8
|
+
@catalog.component
|
|
9
|
+
def Layout(head: XNode, children: XNode) -> str:
|
|
10
|
+
return """
|
|
11
|
+
<>
|
|
12
|
+
<!DOCTYPE html>
|
|
13
|
+
<html>
|
|
14
|
+
<head>
|
|
15
|
+
{head}
|
|
16
|
+
</head>
|
|
17
|
+
<body>
|
|
18
|
+
{children}
|
|
19
|
+
</body>
|
|
20
|
+
</html>
|
|
21
|
+
</>
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
@catalog.component
|
|
25
|
+
def HtmlHead(globals: Any) -> str:
|
|
26
|
+
return """
|
|
27
|
+
<>
|
|
28
|
+
<title>{globals.title}</title>
|
|
29
|
+
{if globals.description {<meta name="description" content={globals.description}/>}}
|
|
30
|
+
<meta charset="UTF-8"/>
|
|
31
|
+
</>
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
@catalog.component()
|
|
35
|
+
def HelloWebPage(globals: Any) -> str:
|
|
36
|
+
return """
|
|
37
|
+
<Layout head={<HtmlHead />}>
|
|
38
|
+
<h1>Hello, world!"</h1>
|
|
39
|
+
</Layout>
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
return HelloWebPage
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_catalog_render(HelloWebPage: Component):
|
|
46
|
+
assert HelloWebPage(globals={"title": "my title", "description": ""}) == (
|
|
47
|
+
"<!DOCTYPE html><html><head><title>my title</title>"
|
|
48
|
+
'<meta charset="UTF-8"/>'
|
|
49
|
+
'</head><body><h1>Hello, world!"</h1></body></html>'
|
|
50
|
+
)
|