Chervil is a toy Lisp interpreter 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.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. module Chervil
  2. RSpec.describe AST::Conditional do
  3. it 'returns the true branch given #t' do
  4. expect(AST::Conditional.new(
  5. AST::Boolean.new(true),
  6. AST::Number.new(1.0),
  7. AST::Number.new(0.0),
  8. ).evaluate(Env.new)).to eq(
  9. 1.0
  10. )
  11. end
  12. it 'returns the false branch given #f' do
  13. expect(AST::Conditional.new(
  14. AST::Boolean.new(false),
  15. AST::Number.new(1.0),
  16. AST::Number.new(0.0),
  17. ).evaluate(Env.new)).to eq(
  18. 0.0
  19. )
  20. end
  21. it 'evaluates complex predicates' do
  22. expect(AST::Conditional.new(
  23. AST::Application.new(
  24. AST::Identifier.new(">"),
  25. [
  26. AST::Number.new(2.0),
  27. AST::Number.new(1.0),
  28. ]
  29. ),
  30. AST::Number.new(2.0),
  31. AST::Number.new(1.0),
  32. ).evaluate(Env.new)).to eq(
  33. 2.0
  34. )
  35. end
  36. it 'evaluates complex branches' do
  37. expect(AST::Conditional.new(
  38. AST::Boolean.new(true),
  39. AST::Application.new(
  40. AST::Identifier.new("+"),
  41. [
  42. AST::Number.new(2.0),
  43. AST::Number.new(1.0),
  44. ]
  45. ),
  46. AST::Number.new(0.0)
  47. ).evaluate(Env.new)).to eq(
  48. 3.0
  49. )
  50. end
  51. end
  52. end