From b318494cfecfe477e719582b7e003f2655bb02f1 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 27 Oct 2011 18:30:53 +0200 Subject: [PATCH] added more unit tests for defined and default --- doc/filters/default.rst | 1 + doc/tests/defined.rst | 4 +++ test/Twig/Tests/Fixtures/filters/default.test | 29 ++++++++++++++++++++++++ test/Twig/Tests/Fixtures/tests/defined.test | 30 +++++++++++++++++++++++- 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/doc/filters/default.rst b/doc/filters/default.rst index 3fc0845..3c1bc5b 100644 --- a/doc/filters/default.rst +++ b/doc/filters/default.rst @@ -9,6 +9,7 @@ undefined or empty, otherwise the value of the variable: {{ var|default('var is not defined') }} {{ var.foo|default('foo item on var is not defined') }} + {{ var['foo']|default('foo item on var is not defined') }} {{ ''|default('passed var is empty') }} diff --git a/doc/tests/defined.rst b/doc/tests/defined.rst index 9e8e228..a6164e5 100644 --- a/doc/tests/defined.rst +++ b/doc/tests/defined.rst @@ -15,3 +15,7 @@ useful if you use the ``strict_variables`` option: {% if foo.bar is defined %} ... {% endif %} + + {% if foo['bar'] is defined %} + ... + {% endif %} diff --git a/test/Twig/Tests/Fixtures/filters/default.test b/test/Twig/Tests/Fixtures/filters/default.test index 66b9ffa..1614a87 100644 --- a/test/Twig/Tests/Fixtures/filters/default.test +++ b/test/Twig/Tests/Fixtures/filters/default.test @@ -9,10 +9,12 @@ Variable: {{ undefinedVar |default('default') is sameas('default') ? 'ok' : 'ko' }} Array access: {{ nested.definedVar |default('default') is sameas('default') ? 'ko' : 'ok' }} +{{ nested['definedVar'] |default('default') is sameas('default') ? 'ko' : 'ok' }} {{ nested.zeroVar |default('default') is sameas('default') ? 'ko' : 'ok' }} {{ nested.emptyVar |default('default') is sameas('default') ? 'ok' : 'ko' }} {{ nested.nullVar |default('default') is sameas('default') ? 'ok' : 'ko' }} {{ nested.undefinedVar |default('default') is sameas('default') ? 'ok' : 'ko' }} +{{ nested['undefinedVar'] |default('default') is sameas('default') ? 'ok' : 'ko' }} {{ undefinedVar.foo |default('default') is sameas('default') ? 'ok' : 'ko' }} Plain values: {{ 'defined' |default('default') is sameas('default') ? 'ko' : 'ok' }} @@ -22,9 +24,16 @@ Plain values: Precedence: {{ 'o' ~ nullVar |default('k') }} {{ 'o' ~ nested.nullVar |default('k') }} +Object methods: +{{ object.foo |default('default') is sameas('default') ? 'ko' : 'ok' }} +{{ object.undefinedMethod |default('default') is sameas('default') ? 'ok' : 'ko' }} Deep nested: {{ nested.undefinedVar.foo.bar |default('default') is sameas('default') ? 'ok' : 'ko' }} {{ nested.definedArray.0 |default('default') is sameas('default') ? 'ko' : 'ok' }} +{{ nested['definedArray'][0] |default('default') is sameas('default') ? 'ko' : 'ok' }} +{{ object.self.foo |default('default') is sameas('default') ? 'ko' : 'ok' }} +{{ object.self.undefinedMethod |default('default') is sameas('default') ? 'ok' : 'ko' }} +{{ object.undefinedMethod.self |default('default') is sameas('default') ? 'ok' : 'ko' }} --DATA-- return array( 'definedVar' => 'defined', @@ -38,6 +47,7 @@ return array( 'nullVar' => null, 'definedArray' => array(0), ), + 'object' => new Foo(), ) --CONFIG-- return array('strict_variables' => false) @@ -55,6 +65,8 @@ ok ok ok ok +ok +ok Plain values: ok ok @@ -63,9 +75,16 @@ ok Precedence: ok ok +Object methods: +ok +ok Deep nested: ok ok +ok +ok +ok +ok --DATA-- return array( 'definedVar' => 'defined', @@ -79,6 +98,7 @@ return array( 'nullVar' => null, 'definedArray' => array(0), ), + 'object' => new Foo(), ) --CONFIG-- return array('strict_variables' => true) @@ -96,6 +116,8 @@ ok ok ok ok +ok +ok Plain values: ok ok @@ -104,6 +126,13 @@ ok Precedence: ok ok +Object methods: +ok +ok Deep nested: ok ok +ok +ok +ok +ok diff --git a/test/Twig/Tests/Fixtures/tests/defined.test b/test/Twig/Tests/Fixtures/tests/defined.test index bb7c60d..186283d 100644 --- a/test/Twig/Tests/Fixtures/tests/defined.test +++ b/test/Twig/Tests/Fixtures/tests/defined.test @@ -8,12 +8,20 @@ {{ zeroVar is defined ? 'ok' : 'ko' }} {{ nullVar is defined ? 'ok' : 'ko' }} {{ nested.definedVar is defined ? 'ok' : 'ko' }} +{{ nested['definedVar'] is defined ? 'ok' : 'ko' }} {{ nested.definedVar is not defined ? 'ko' : 'ok' }} {{ nested.undefinedVar is defined ? 'ko' : 'ok' }} +{{ nested['undefinedVar'] is defined ? 'ko' : 'ok' }} {{ nested.undefinedVar is not defined ? 'ok' : 'ko' }} {{ nested.zeroVar is defined ? 'ok' : 'ko' }} {{ nested.nullVar is defined ? 'ok' : 'ko' }} {{ nested.definedArray.0 is defined ? 'ok' : 'ko' }} +{{ nested['definedArray'][0] is defined ? 'ok' : 'ko' }} +{{ object.foo is defined ? 'ok' : 'ko' }} +{{ object.undefinedMethod is defined ? 'ko' : 'ok' }} +{{ object.self.foo is defined ? 'ok' : 'ko' }} +{{ object.self.undefinedMethod is defined ? 'ko' : 'ok' }} +{{ object.undefinedMethod.self is defined ? 'ko' : 'ok' }} --DATA-- return array( 'definedVar' => 'defined', @@ -24,7 +32,8 @@ return array( 'zeroVar' => 0, 'nullVar' => null, 'definedArray' => array(0), - ) + ), + 'object' => new Foo(), ); --EXPECT-- ok @@ -40,6 +49,14 @@ ok ok ok ok +ok +ok +ok +ok +ok +ok +ok +ok --DATA-- return array( 'definedVar' => 'defined', @@ -50,7 +67,8 @@ return array( 'zeroVar' => 0, 'nullVar' => null, 'definedArray' => array(0), - ) + ), + 'object' => new Foo(), ); --CONFIG-- return array('strict_variables' => false) @@ -68,3 +86,11 @@ ok ok ok ok +ok +ok +ok +ok +ok +ok +ok +ok -- 1.7.2.5