diff options
author | René Kijewski <kijewski@library.vetmed.fu-berlin.de> | 2021-07-30 17:04:30 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2021-08-30 22:54:32 +0200 |
commit | 726ca1cc3356f7aa59d7c2730c9f3b3588236cb0 (patch) | |
tree | 6ebdfeb9a0b4af290af3faa42b3a09abcb71440d /testing/tests | |
parent | ed2e640dbdff88fe118d943fa0ea9ae00eb11555 (diff) | |
download | askama-726ca1cc3356f7aa59d7c2730c9f3b3588236cb0.tar.gz askama-726ca1cc3356f7aa59d7c2730c9f3b3588236cb0.tar.bz2 askama-726ca1cc3356f7aa59d7c2730c9f3b3588236cb0.zip |
Add {% break %} and {% continue %}
This PR adds `{% break %}` and `{% continue %}` statements to break out
of a loop, or continue with the next element of the iterator.
Diffstat (limited to 'testing/tests')
-rw-r--r-- | testing/tests/loops.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/testing/tests/loops.rs b/testing/tests/loops.rs index fffb39d..49c37e0 100644 --- a/testing/tests/loops.rs +++ b/testing/tests/loops.rs @@ -304,3 +304,71 @@ fn test_for_enumerate() { }; assert_eq!(t.render().unwrap(), "0=hello-1=world-2=!-"); } + +#[derive(Template)] +#[template( + source = "{% for v in values.iter() %}x{{v}}{% if matches!(v, x if *x==3) %}{% break %}{% endif %}y{% endfor %}", + ext = "txt" +)] +struct Break<'a> { + values: &'a [i32], +} + +#[test] +fn test_loop_break() { + let t = Break { + values: &[1, 2, 3, 4, 5], + }; + assert_eq!(t.render().unwrap(), "x1yx2yx3"); + + let t = Break { + values: &[1, 2, 4, 5], + }; + assert_eq!(t.render().unwrap(), "x1yx2yx4yx5y"); +} + +#[derive(Template)] +#[template( + source = "{% for v in values %}x{{v}}{% if matches!(v, x if *x==3) %}{% continue %}{% endif %}y{% endfor %}", + ext = "txt" +)] +struct Continue<'a> { + values: &'a [i32], +} + +#[test] +fn test_loop_continue() { + let t = Continue { + values: &[1, 2, 3, 4, 5], + }; + assert_eq!(t.render().unwrap(), "x1yx2yx3x4yx5y"); + + let t = Continue { + values: &[1, 2, 4, 5], + }; + assert_eq!(t.render().unwrap(), "x1yx2yx4yx5y"); +} + +#[derive(Template)] +#[template(path = "for-break-continue.html")] +struct BreakContinue<'a> { + values: &'a [i32], +} + +#[test] +fn test_loop_break_continue() { + let t = BreakContinue { + values: &[1, 2, 3, 4, 5], + }; + assert_eq!(t.render().unwrap(), "x1yx2yx3yx4yx5y"); + + let t = BreakContinue { + values: &[1, 2, 3, 10, 4, 5], + }; + assert_eq!(t.render().unwrap(), "x1yx2yx3yx10"); + + let t = BreakContinue { + values: &[1, 2, 3, 11, 4, 5], + }; + assert_eq!(t.render().unwrap(), "x1yx2yx3yx11x4yx5y"); +} |