num2bool_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. require 'spec_helper_acceptance'
  2. describe 'num2bool function' do
  3. describe 'success' do
  4. pp1 = <<-DOC
  5. $a = 1
  6. $b = "1"
  7. $c = "50"
  8. $ao = num2bool($a)
  9. $bo = num2bool($b)
  10. $co = num2bool($c)
  11. notice(inline_template('a is <%= @ao.inspect %>'))
  12. notice(inline_template('b is <%= @bo.inspect %>'))
  13. notice(inline_template('c is <%= @co.inspect %>'))
  14. DOC
  15. regex_array_true = [%r{a is true}, %r{b is true}, %r{c is true}]
  16. it 'bools positive numbers and numeric strings as true' do
  17. apply_manifest(pp1, :catch_failures => true) do |r|
  18. regex_array_true.each do |i|
  19. expect(r.stdout).to match(i)
  20. end
  21. end
  22. end
  23. pp2 = <<-DOC
  24. $a = 0
  25. $b = -0.1
  26. $c = ["-50","1"]
  27. $ao = num2bool($a)
  28. $bo = num2bool($b)
  29. $co = num2bool($c)
  30. notice(inline_template('a is <%= @ao.inspect %>'))
  31. notice(inline_template('b is <%= @bo.inspect %>'))
  32. notice(inline_template('c is <%= @co.inspect %>'))
  33. DOC
  34. regex_array_false = [%r{a is false}, %r{b is false}, %r{c is false}]
  35. it 'bools negative numbers as false' do
  36. apply_manifest(pp2, :catch_failures => true) do |r|
  37. regex_array_false.each do |i|
  38. expect(r.stdout).to match(i)
  39. end
  40. end
  41. end
  42. end
  43. describe 'failure' do
  44. pp3 = <<-DOC
  45. $a = "a"
  46. $ao = num2bool($a)
  47. notice(inline_template('a is <%= @ao.inspect %>'))
  48. DOC
  49. it 'fails on words' do
  50. expect(apply_manifest(pp3, :expect_failures => true).stderr).to match(%r{not look like a number})
  51. end
  52. pp4 = <<-DOC
  53. $b = "1b"
  54. $bo = num2bool($b)
  55. notice(inline_template('b is <%= @bo.inspect %>'))
  56. DOC
  57. it 'fails on numberwords' do
  58. expect(apply_manifest(pp4, :expect_failures => true).stderr).to match(%r{not look like a number})
  59. end
  60. pp5 = <<-DOC # rubocop:disable Lint/UselessAssignment
  61. $c = {"c" => "-50"}
  62. $co = num2bool($c)
  63. notice(inline_template('c is <%= @co.inspect %>'))
  64. DOC
  65. it 'fails on non-numeric/strings' do
  66. pending "The function will call .to_s.to_i on anything not a Numeric or
  67. String, and results in 0. Is this intended?"
  68. expect(apply_manifest(pp5(:expect_failures => true)).stderr).to match(%r{Unable to parse})
  69. end
  70. end
  71. end