liquid_interpolatable.rb 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. require 'spec_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. @checker.interpolate_options(@checker.options, @event).should == {
  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", focus: true do
  29. @checker.options = {"value" => ["{{variable}}", "Much array", "Hey, {{hello_world}}"]}
  30. @checker.interpolate_options(@checker.options, @event).should == {
  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. @checker.interpolate_options(@checker.options, @event).should == {
  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. @checker.interpolate_string("{{variable}}", @event).should == "hello"
  48. @checker.interpolate_string("{{variable}} you", @event).should == "hello you"
  49. end
  50. end
  51. describe "liquid tags" do
  52. it "should work with existing credentials" do
  53. @checker.interpolate_string("{% credential aws_key %}", {}).should == '2222222222-jane'
  54. end
  55. it "should raise an exception for undefined credentials" do
  56. expect {
  57. @checker.interpolate_string("{% credential unknown %}", {})
  58. }.to raise_error
  59. end
  60. end
  61. end