1
0

liquid_interpolatable_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 stringify 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. describe 'to_uri' do
  53. before do
  54. @agent = Agents::InterpolatableAgent.new(name: "test", options: { 'foo' => '{% assign u = s | to_uri %}{{ u.path }}' })
  55. @agent.interpolation_context['s'] = 'http://example.com/dir/1?q=test'
  56. end
  57. it 'should parse an abosule URI' do
  58. @filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1').should == URI('http://example.net/index.html')
  59. end
  60. it 'should parse an abosule URI with a base URI specified' do
  61. @filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1').should == URI('http://example.net/index.html')
  62. end
  63. it 'should parse a relative URI with a base URI specified' do
  64. @filter.to_uri('foo/index.html', 'http://example.com/dir/1').should == URI('http://example.com/dir/foo/index.html')
  65. end
  66. it 'should parse an abosule URI with a base URI specified' do
  67. @filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1').should == URI('http://example.net/index.html')
  68. end
  69. it 'should stringify a non-string operand' do
  70. @filter.to_uri(123, 'http://example.com/dir/1').should == URI('http://example.com/dir/123')
  71. end
  72. it 'should return a URI value in interpolation' do
  73. @agent.interpolated['foo'].should == '/dir/1'
  74. end
  75. it 'should return a URI value resolved against a base URI in interpolation' do
  76. @agent.options['foo'] = '{% assign u = s | to_uri:"http://example.com/dir/1" %}{{ u.path }}'
  77. @agent.interpolation_context['s'] = 'foo/index.html'
  78. @agent.interpolated['foo'].should == '/dir/foo/index.html'
  79. end
  80. end
  81. end