liquid_interpolatable_spec.rb 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. require 'spec_helper'
  2. require 'nokogiri'
  3. describe LiquidInterpolatable::Filters do
  4. before do
  5. @filter = Class.new do
  6. include LiquidInterpolatable::Filters
  7. end.new
  8. end
  9. describe 'uri_escape' do
  10. it 'should escape a string for use in URI' do
  11. @filter.uri_escape('abc:/?=').should == 'abc%3A%2F%3F%3D'
  12. end
  13. it 'should not raise an error when an operand is nil' do
  14. @filter.uri_escape(nil).should be_nil
  15. end
  16. end
  17. describe 'validations' do
  18. class Agents::InterpolatableAgent < Agent
  19. include LiquidInterpolatable
  20. def check
  21. create_event :payload => {}
  22. end
  23. def validate_options
  24. interpolated['foo']
  25. end
  26. end
  27. it "should finish without raising an exception" do
  28. agent = Agents::InterpolatableAgent.new(name: "test", options: { 'foo' => '{{bar}' })
  29. agent.valid?.should == false
  30. agent.errors[:options].first.should =~ /not properly terminated/
  31. end
  32. end
  33. describe 'to_xpath' do
  34. before do
  35. def @filter.to_xpath_roundtrip(string)
  36. Nokogiri::XML('').xpath(to_xpath(string))
  37. end
  38. end
  39. it 'should escape a string for use in XPath expression' do
  40. [
  41. %q{abc}.freeze,
  42. %q{'a"bc'dfa""fds''fa}.freeze,
  43. ].each { |string|
  44. @filter.to_xpath_roundtrip(string).should == string
  45. }
  46. end
  47. it 'should stringize a non-string operand' do
  48. @filter.to_xpath_roundtrip(nil).should == ''
  49. @filter.to_xpath_roundtrip(1).should == '1'
  50. end
  51. end
  52. end