Add tests and a Sizable trait for ContextElement.
This commit is contained in:
parent
76193bf806
commit
1a54e35736
9
js/test_cases/helpers_size/README.md
Normal file
9
js/test_cases/helpers_size/README.md
Normal file
@ -0,0 +1,9 @@
|
||||
Excerpt from [the DustJS tutorial](https://github.com/linkedin/dustjs/wiki/Dust-Tutorial#size_keyxxx___size_helper_Available_in_Dust_V11_release):
|
||||
|
||||
Array - number of elements, [1,2,3,4] has size=4
|
||||
String - length of the string, "abcdef" has size=6
|
||||
Object - Number of properties in the object, {a:4, b:8, c:15, d:16} has size=4
|
||||
Number - Value of the number, 23 has size 23 and 3.14 has size 3.14
|
||||
Undefined, 0, empty string - zero
|
||||
Any other value - length after conversion to string
|
||||
|
7
js/test_cases/helpers_size/inputarray.json
Normal file
7
js/test_cases/helpers_size/inputarray.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"val": [
|
||||
"Alice",
|
||||
"Bob",
|
||||
"Chris"
|
||||
]
|
||||
}
|
3
js/test_cases/helpers_size/inputemptystring.json
Normal file
3
js/test_cases/helpers_size/inputemptystring.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"val": ""
|
||||
}
|
3
js/test_cases/helpers_size/inputfalse.json
Normal file
3
js/test_cases/helpers_size/inputfalse.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"val": false
|
||||
}
|
3
js/test_cases/helpers_size/inputmissing.json
Normal file
3
js/test_cases/helpers_size/inputmissing.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"notval": "Alice"
|
||||
}
|
3
js/test_cases/helpers_size/inputnumber.json
Normal file
3
js/test_cases/helpers_size/inputnumber.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"val": 7.21
|
||||
}
|
6
js/test_cases/helpers_size/inputobject.json
Normal file
6
js/test_cases/helpers_size/inputobject.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"val": {
|
||||
"pet": "cat",
|
||||
"name": "fluffy"
|
||||
}
|
||||
}
|
3
js/test_cases/helpers_size/inputstring.json
Normal file
3
js/test_cases/helpers_size/inputstring.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"val": "Alice"
|
||||
}
|
3
js/test_cases/helpers_size/inputtrue.json
Normal file
3
js/test_cases/helpers_size/inputtrue.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"val": true
|
||||
}
|
3
js/test_cases/helpers_size/inputzero.json
Normal file
3
js/test_cases/helpers_size/inputzero.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"val": 0
|
||||
}
|
3
js/test_cases/helpers_size/main.dust
Normal file
3
js/test_cases/helpers_size/main.dust
Normal file
@ -0,0 +1,3 @@
|
||||
The size of val ({val|js|s}) is {@size key=val /}{~n}
|
||||
The size of "{val}" is {@size key="{val}" /}{~n}
|
||||
The size with no key is {@size /}{~n}
|
@ -15,6 +15,7 @@ use renderer::Loopable;
|
||||
use renderer::MathNumber;
|
||||
use renderer::RenderError;
|
||||
use renderer::Renderable;
|
||||
use renderer::Sizable;
|
||||
use renderer::Truthiness;
|
||||
use renderer::WalkError;
|
||||
use renderer::Walkable;
|
||||
@ -313,6 +314,12 @@ impl Loopable for serde_json::Value {
|
||||
}
|
||||
}
|
||||
|
||||
impl Sizable for serde_json::Value {
|
||||
fn get_size<'a>(&'a self) -> Option<IceResult<'a>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Castable for serde_json::Value {
|
||||
fn cast_to_type<'a>(&'a self, target: &str) -> Option<IceResult<'a>> {
|
||||
match (self, target) {
|
||||
|
@ -22,6 +22,7 @@ pub trait ContextElement:
|
||||
+ FromContextElement
|
||||
+ IntoRcIce
|
||||
+ Castable
|
||||
+ Sizable
|
||||
{
|
||||
}
|
||||
|
||||
@ -63,6 +64,10 @@ pub trait Castable {
|
||||
fn cast_to_type<'a>(&'a self, target: &str) -> Option<IceResult<'a>>;
|
||||
}
|
||||
|
||||
pub trait Sizable {
|
||||
fn get_size<'a>(&'a self) -> Option<IceResult<'a>>;
|
||||
}
|
||||
|
||||
pub trait CastToAny {
|
||||
fn to_any(&self) -> &dyn Any;
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ pub use context_element::IceResult;
|
||||
pub use context_element::IntoContextElement;
|
||||
pub use context_element::Loopable;
|
||||
pub use context_element::Renderable;
|
||||
pub use context_element::Sizable;
|
||||
pub use context_element::Truthiness;
|
||||
pub use context_element::Walkable;
|
||||
pub use errors::CompileError;
|
||||
|
@ -14,6 +14,7 @@ use crate::renderer::DustRenderer;
|
||||
use crate::renderer::Loopable;
|
||||
use crate::renderer::RenderError;
|
||||
use crate::renderer::Renderable;
|
||||
use crate::renderer::Sizable;
|
||||
use crate::renderer::Truthiness;
|
||||
use crate::renderer::WalkError;
|
||||
use crate::renderer::Walkable;
|
||||
@ -189,6 +190,12 @@ impl Walkable for OwnedLiteral {
|
||||
}
|
||||
}
|
||||
|
||||
impl Sizable for OwnedLiteral {
|
||||
fn get_size<'a>(&'a self) -> Option<IceResult<'a>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Castable for OwnedLiteral {
|
||||
fn cast_to_type<'a>(&'a self, target: &str) -> Option<IceResult<'a>> {
|
||||
match (self, target) {
|
||||
|
Loading…
Reference in New Issue
Block a user