OLD | NEW |
(Empty) | |
| 1 overview: | |
| 2 Lambdas are a special-cased data type for use in interpolations and |
| 3 sections. |
| 4 |
| 5 When used as the data value for an Interpolation tag, the lambda MUST be |
| 6 treatable as an arity 0 function, and invoked as such. The returned value |
| 7 MUST be rendered against the default delimiters, then interpolated in place |
| 8 of the lambda. |
| 9 |
| 10 When used as the data value for a Section tag, the lambda MUST be treatable |
| 11 as an arity 1 function, and invoked as such (passing a String containing the |
| 12 unprocessed section contents). The returned value MUST be rendered against |
| 13 the current delimiters, then interpolated in place of the section. |
| 14 tests: |
| 15 - name: Interpolation |
| 16 desc: A lambda's return value should be interpolated. |
| 17 data: |
| 18 lambda: !code |
| 19 ruby: 'proc { "world" }' |
| 20 perl: 'sub { "world" }' |
| 21 js: 'function() { return "world" }' |
| 22 php: 'return "world";' |
| 23 python: 'lambda: "world"' |
| 24 clojure: '(fn [] "world")' |
| 25 template: "Hello, {{lambda}}!" |
| 26 expected: "Hello, world!" |
| 27 |
| 28 - name: Interpolation - Expansion |
| 29 desc: A lambda's return value should be parsed. |
| 30 data: |
| 31 planet: "world" |
| 32 lambda: !code |
| 33 ruby: 'proc { "{{planet}}" }' |
| 34 perl: 'sub { "{{planet}}" }' |
| 35 js: 'function() { return "{{planet}}" }' |
| 36 php: 'return "{{planet}}";' |
| 37 python: 'lambda: "{{planet}}"' |
| 38 clojure: '(fn [] "{{planet}}")' |
| 39 template: "Hello, {{lambda}}!" |
| 40 expected: "Hello, world!" |
| 41 |
| 42 - name: Interpolation - Alternate Delimiters |
| 43 desc: A lambda's return value should parse with the default delimiters. |
| 44 data: |
| 45 planet: "world" |
| 46 lambda: !code |
| 47 ruby: 'proc { "|planet| => {{planet}}" }' |
| 48 perl: 'sub { "|planet| => {{planet}}" }' |
| 49 js: 'function() { return "|planet| => {{planet}}" }' |
| 50 php: 'return "|planet| => {{planet}}";' |
| 51 python: 'lambda: "|planet| => {{planet}}"' |
| 52 clojure: '(fn [] "|planet| => {{planet}}")' |
| 53 template: "{{= | | =}}\nHello, (|&lambda|)!" |
| 54 expected: "Hello, (|planet| => world)!" |
| 55 |
| 56 - name: Interpolation - Multiple Calls |
| 57 desc: Interpolated lambdas should not be cached. |
| 58 data: |
| 59 lambda: !code |
| 60 ruby: 'proc { $calls ||= 0; $calls += 1 }' |
| 61 perl: 'sub { no strict; $calls += 1 }' |
| 62 js: 'function() { return (g=(function(){return this})()).calls=(g.c
alls||0)+1 }' |
| 63 php: 'global $calls; return ++$calls;' |
| 64 python: 'lambda: globals().update(calls=globals().get("calls",0)+1) or
calls' |
| 65 clojure: '(def g (atom 0)) (fn [] (swap! g inc))' |
| 66 template: '{{lambda}} == {{{lambda}}} == {{lambda}}' |
| 67 expected: '1 == 2 == 3' |
| 68 |
| 69 - name: Escaping |
| 70 desc: Lambda results should be appropriately escaped. |
| 71 data: |
| 72 lambda: !code |
| 73 ruby: 'proc { ">" }' |
| 74 perl: 'sub { ">" }' |
| 75 js: 'function() { return ">" }' |
| 76 php: 'return ">";' |
| 77 python: 'lambda: ">"' |
| 78 clojure: '(fn [] ">")' |
| 79 template: "<{{lambda}}{{{lambda}}}" |
| 80 expected: "<>>" |
| 81 |
| 82 - name: Section |
| 83 desc: Lambdas used for sections should receive the raw section string. |
| 84 data: |
| 85 x: 'Error!' |
| 86 lambda: !code |
| 87 ruby: 'proc { |text| text == "{{x}}" ? "yes" : "no" }' |
| 88 perl: 'sub { $_[0] eq "{{x}}" ? "yes" : "no" }' |
| 89 js: 'function(txt) { return (txt == "{{x}}" ? "yes" : "no") }' |
| 90 php: 'return ($text == "{{x}}") ? "yes" : "no";' |
| 91 python: 'lambda text: text == "{{x}}" and "yes" or "no"' |
| 92 clojure: '(fn [text] (if (= text "{{x}}") "yes" "no"))' |
| 93 template: "<{{#lambda}}{{x}}{{/lambda}}>" |
| 94 expected: "<yes>" |
| 95 |
| 96 - name: Section - Expansion |
| 97 desc: Lambdas used for sections should have their results parsed. |
| 98 data: |
| 99 planet: "Earth" |
| 100 lambda: !code |
| 101 ruby: 'proc { |text| "#{text}{{planet}}#{text}" }' |
| 102 perl: 'sub { $_[0] . "{{planet}}" . $_[0] }' |
| 103 js: 'function(txt) { return txt + "{{planet}}" + txt }' |
| 104 php: 'return $text . "{{planet}}" . $text;' |
| 105 python: 'lambda text: "%s{{planet}}%s" % (text, text)' |
| 106 clojure: '(fn [text] (str text "{{planet}}" text))' |
| 107 template: "<{{#lambda}}-{{/lambda}}>" |
| 108 expected: "<-Earth->" |
| 109 |
| 110 - name: Section - Alternate Delimiters |
| 111 desc: Lambdas used for sections should parse with the current delimiters. |
| 112 data: |
| 113 planet: "Earth" |
| 114 lambda: !code |
| 115 ruby: 'proc { |text| "#{text}{{planet}} => |planet|#{text}" }' |
| 116 perl: 'sub { $_[0] . "{{planet}} => |planet|" . $_[0] }' |
| 117 js: 'function(txt) { return txt + "{{planet}} => |planet|" + txt }' |
| 118 php: 'return $text . "{{planet}} => |planet|" . $text;' |
| 119 python: 'lambda text: "%s{{planet}} => |planet|%s" % (text, text)' |
| 120 clojure: '(fn [text] (str text "{{planet}} => |planet|" text))' |
| 121 template: "{{= | | =}}<|#lambda|-|/lambda|>" |
| 122 expected: "<-{{planet}} => Earth->" |
| 123 |
| 124 - name: Section - Multiple Calls |
| 125 desc: Lambdas used for sections should not be cached. |
| 126 data: |
| 127 lambda: !code |
| 128 ruby: 'proc { |text| "__#{text}__" }' |
| 129 perl: 'sub { "__" . $_[0] . "__" }' |
| 130 js: 'function(txt) { return "__" + txt + "__" }' |
| 131 php: 'return "__" . $text . "__";' |
| 132 python: 'lambda text: "__%s__" % (text)' |
| 133 clojure: '(fn [text] (str "__" text "__"))' |
| 134 template: '{{#lambda}}FILE{{/lambda}} != {{#lambda}}LINE{{/lambda}}' |
| 135 expected: '__FILE__ != __LINE__' |
| 136 |
| 137 - name: Inverted Section |
| 138 desc: Lambdas used for inverted sections should be considered truthy. |
| 139 data: |
| 140 static: 'static' |
| 141 lambda: !code |
| 142 ruby: 'proc { |text| false }' |
| 143 perl: 'sub { 0 }' |
| 144 js: 'function(txt) { return false }' |
| 145 php: 'return false;' |
| 146 python: 'lambda text: 0' |
| 147 clojure: '(fn [text] false)' |
| 148 template: "<{{^lambda}}{{static}}{{/lambda}}>" |
| 149 expected: "<>" |
OLD | NEW |