liquid_interpolatable.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. require 'rails_helper'
  2. shared_examples_for LiquidInterpolatable do
  3. before(:each) do
  4. @valid_params = {
  5. "normal" => "just some normal text",
  6. "variable" => "{{variable}}",
  7. "text" => "Some test with an embedded {{variable}}",
  8. "escape" => "This should be {{hello_world | uri_escape}}"
  9. }
  10. @checker = new_instance
  11. @checker.name = "somename"
  12. @checker.options = @valid_params
  13. @checker.user = users(:jane)
  14. @event = Event.new
  15. @event.agent = agents(:bob_weather_agent)
  16. @event.payload = { :variable => 'hello', :hello_world => "Hello world"}
  17. @event.save!
  18. end
  19. describe "interpolating liquid templates" do
  20. it "should work" do
  21. expect(@checker.interpolate_options(@checker.options, @event)).to eq({
  22. "normal" => "just some normal text",
  23. "variable" => "hello",
  24. "text" => "Some test with an embedded hello",
  25. "escape" => "This should be Hello+world"
  26. })
  27. end
  28. it "should work with arrays" do
  29. @checker.options = {"value" => ["{{variable}}", "Much array", "Hey, {{hello_world}}"]}
  30. expect(@checker.interpolate_options(@checker.options, @event)).to eq({
  31. "value" => ["hello", "Much array", "Hey, Hello world"]
  32. })
  33. end
  34. it "should work recursively" do
  35. @checker.options['hash'] = {'recursive' => "{{variable}}"}
  36. @checker.options['indifferent_hash'] = ActiveSupport::HashWithIndifferentAccess.new({'recursive' => "{{variable}}"})
  37. expect(@checker.interpolate_options(@checker.options, @event)).to eq({
  38. "normal" => "just some normal text",
  39. "variable" => "hello",
  40. "text" => "Some test with an embedded hello",
  41. "escape" => "This should be Hello+world",
  42. "hash" => {'recursive' => 'hello'},
  43. "indifferent_hash" => {'recursive' => 'hello'},
  44. })
  45. end
  46. it "should work for strings" do
  47. expect(@checker.interpolate_string("{{variable}}", @event)).to eq("hello")
  48. expect(@checker.interpolate_string("{{variable}} you", @event)).to eq("hello you")
  49. end
  50. it "should use local variables while in a block" do
  51. @checker.options['locals'] = '{{_foo_}} {{_bar_}}'
  52. @checker.interpolation_context.tap { |context|
  53. expect(@checker.interpolated['locals']).to eq(' ')
  54. context.stack {
  55. context['_foo_'] = 'This is'
  56. context['_bar_'] = 'great.'
  57. expect(@checker.interpolated['locals']).to eq('This is great.')
  58. }
  59. expect(@checker.interpolated['locals']).to eq(' ')
  60. }
  61. end
  62. it "should use another self object while in a block" do
  63. @checker.options['properties'] = '{{_foo_}} {{_bar_}}'
  64. expect(@checker.interpolated['properties']).to eq(' ')
  65. @checker.interpolate_with({ '_foo_' => 'That was', '_bar_' => 'nice.' }) {
  66. expect(@checker.interpolated['properties']).to eq('That was nice.')
  67. }
  68. expect(@checker.interpolated['properties']).to eq(' ')
  69. end
  70. end
  71. describe "liquid tags" do
  72. context "%credential" do
  73. it "should work with existing credentials" do
  74. expect(@checker.interpolate_string("{% credential aws_key %}", {})).to eq('2222222222-jane')
  75. end
  76. it "should not raise an exception for undefined credentials" do
  77. expect {
  78. result = @checker.interpolate_string("{% credential unknown %}", {})
  79. expect(result).to eq('')
  80. }.not_to raise_error
  81. end
  82. end
  83. context '%line_break' do
  84. it 'should convert {% line_break %} to actual line breaks' do
  85. expect(@checker.interpolate_string("test{% line_break %}second line", {})).to eq("test\nsecond line")
  86. end
  87. end
  88. end
  89. end