A toy dynamic programming language written in Ruby
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

conditional_spec.rb 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. RSpec.describe AST::Conditional do
  2. it 'evaluates a true if statement' do
  3. expect(
  4. AST::Conditional.new(
  5. [
  6. AST::Branch.new(
  7. AST::Binary.new(
  8. AST::Operators::GREATER_THAN,
  9. AST::Number.new(5.0),
  10. AST::Number.new(4.0)
  11. ),
  12. AST::Block.new([AST::String.new('5 is greater than 4')])
  13. )
  14. ]
  15. )
  16. .execute(Environment.new)
  17. ).to eq('5 is greater than 4')
  18. end
  19. it 'evaluates an else statement' do
  20. expect(
  21. AST::Conditional.new(
  22. [
  23. AST::Branch.new(
  24. AST::Binary.new(
  25. AST::Operators::GREATER_THAN,
  26. AST::Number.new(3.0),
  27. AST::Number.new(4.0)
  28. ),
  29. AST::Block.new([AST::String.new('3 is greater than 4')])
  30. ),
  31. AST::Branch.new(
  32. AST::Boolean.new(true),
  33. AST::Block.new([AST::String.new('3 is _not_ greater than 4')])
  34. )
  35. ]
  36. )
  37. .execute(Environment.new)
  38. ).to eq('3 is _not_ greater than 4')
  39. end
  40. it 'evaluates an else if statement' do
  41. expect(
  42. AST::Conditional.new(
  43. [
  44. AST::Branch.new(
  45. AST::Binary.new(
  46. AST::Operators::GREATER_THAN,
  47. AST::Number.new(3.0),
  48. AST::Number.new(4.0)
  49. ),
  50. AST::Block.new([AST::String.new('3 is greater than 4')])
  51. ),
  52. AST::Branch.new(
  53. AST::Binary.new(
  54. AST::Operators::GREATER_THAN,
  55. AST::Number.new(3.0),
  56. AST::Number.new(2.0)
  57. ),
  58. AST::Block.new([AST::String.new('3 is greater than 2')])
  59. ),
  60. AST::Branch.new(
  61. AST::Boolean.new(true),
  62. AST::Block.new([AST::String.new('3 is _not_ greater than 4')])
  63. )
  64. ]
  65. )
  66. .execute(Environment.new)
  67. ).to eq('3 is greater than 2')
  68. end
  69. end