~lambdas.yml 5.8 KB

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