zip_spec.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. require 'spec_helper_acceptance'
  2. describe 'zip function' do
  3. describe 'success' do
  4. pp1 = <<-DOC
  5. $one = [1,2,3,4]
  6. $two = [5,6,7,8]
  7. $output = zip($one,$two)
  8. notice(inline_template('<%= @output.inspect %>'))
  9. DOC
  10. it 'zips two arrays of numbers together' do
  11. expect(apply_manifest(pp1, :catch_failures => true).stdout).to match(%r{\[\[1, 5\], \[2, 6\], \[3, 7\], \[4, 8\]\]})
  12. end
  13. pp2 = <<-DOC
  14. $one = [1,2,"three",4]
  15. $two = [true,true,false,false]
  16. $output = zip($one,$two)
  17. notice(inline_template('<%= @output.inspect %>'))
  18. DOC
  19. it 'zips two arrays of numbers & bools together' do
  20. expect(apply_manifest(pp2, :catch_failures => true).stdout).to match(%r{\[\[1, true\], \[2, true\], \["three", false\], \[4, false\]\]})
  21. end
  22. # XXX This only tests the argument `true`, even though the following are valid:
  23. # 1 t y true yes
  24. # 0 f n false no
  25. # undef undefined
  26. pp3 = <<-DOC
  27. $one = [1,2,3,4]
  28. $two = [5,6,7,8]
  29. $output = zip($one,$two,true)
  30. notice(inline_template('<%= @output.inspect %>'))
  31. DOC
  32. it 'zips two arrays of numbers together and flattens them' do
  33. expect(apply_manifest(pp3, :catch_failures => true).stdout).to match(%r{\[1, 5, 2, 6, 3, 7, 4, 8\]})
  34. end
  35. # XXX Is this expected behavior?
  36. pp4 = <<-DOC
  37. $one = [1,2]
  38. $two = [5,6,7,8]
  39. $output = zip($one,$two)
  40. notice(inline_template('<%= @output.inspect %>'))
  41. DOC
  42. it 'handles unmatched length' do
  43. expect(apply_manifest(pp4, :catch_failures => true).stdout).to match(%r{\[\[1, 5\], \[2, 6\]\]})
  44. end
  45. end
  46. describe 'failure' do
  47. pp5 = <<-DOC
  48. $one = [1,2]
  49. $output = zip($one)
  50. notice(inline_template('<%= @output.inspect %>'))
  51. DOC
  52. it 'handles improper number of arguments' do
  53. expect(apply_manifest(pp5, :expect_failures => true).stderr).to match(%r{Wrong number of arguments})
  54. end
  55. pp6 = <<-DOC
  56. $one = "a string"
  57. $two = [5,6,7,8]
  58. $output = zip($one,$two)
  59. notice(inline_template('<%= @output.inspect %>'))
  60. DOC
  61. it 'handles improper argument types' do
  62. expect(apply_manifest(pp6, :expect_failures => true).stderr).to match(%r{Requires array})
  63. end
  64. end
  65. end