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 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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(
  13. [
  14. AST::String.new("5 is greater than 4")
  15. ]
  16. )
  17. )
  18. ]
  19. ).execute(Environment.new)
  20. ).to eq("5 is greater than 4")
  21. end
  22. it 'evaluates an else statement' do
  23. expect(
  24. AST::Conditional.new(
  25. [
  26. AST::Branch.new(
  27. AST::Binary.new(
  28. AST::Operators::GREATER_THAN,
  29. AST::Number.new(3.0),
  30. AST::Number.new(4.0),
  31. ),
  32. AST::Block.new(
  33. [
  34. AST::String.new("3 is greater than 4")
  35. ]
  36. )
  37. ),
  38. AST::Branch.new(
  39. AST::Boolean.new(true),
  40. AST::Block.new(
  41. [
  42. AST::String.new("3 is _not_ greater than 4")
  43. ]
  44. )
  45. )
  46. ]
  47. ).execute(Environment.new)
  48. ).to eq("3 is _not_ greater than 4")
  49. end
  50. it 'evaluates an else if statement' do
  51. expect(
  52. AST::Conditional.new(
  53. [
  54. AST::Branch.new(
  55. AST::Binary.new(
  56. AST::Operators::GREATER_THAN,
  57. AST::Number.new(3.0),
  58. AST::Number.new(4.0),
  59. ),
  60. AST::Block.new(
  61. [
  62. AST::String.new("3 is greater than 4")
  63. ]
  64. )
  65. ),
  66. AST::Branch.new(
  67. AST::Binary.new(
  68. AST::Operators::GREATER_THAN,
  69. AST::Number.new(3.0),
  70. AST::Number.new(2.0),
  71. ),
  72. AST::Block.new(
  73. [
  74. AST::String.new("3 is greater than 2")
  75. ]
  76. )
  77. ),
  78. AST::Branch.new(
  79. AST::Boolean.new(true),
  80. AST::Block.new(
  81. [
  82. AST::String.new("3 is _not_ greater than 4")
  83. ]
  84. )
  85. )
  86. ]
  87. ).execute(Environment.new)
  88. ).to eq("3 is greater than 2")
  89. end
  90. end