Chervil is a toy Lisp interpreter written in Ruby
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

core_spec.rb 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. module Chervil
  2. RSpec.describe Core do
  3. it 'does arithmetic' do
  4. expect(Core::CORE['+'].call([1.0, 2.0])).to eq(3.0)
  5. expect(Core::CORE['-'].call([3.0, 1.0])).to eq(2.0)
  6. expect(Core::CORE['*'].call([2.0, 3.0])).to eq(6.0)
  7. expect(Core::CORE['/'].call([6.0, 2.0])).to eq(3.0)
  8. end
  9. it 'compares values' do
  10. expect(Core::CORE['='].call([1.0, 2.0])).to eq(false)
  11. expect(Core::CORE['='].call([1.0, 1.0])).to eq(true)
  12. expect(Core::CORE['<'].call([2.0, 1.0])).to eq(false)
  13. expect(Core::CORE['<'].call([2.0, 3.0])).to eq(true)
  14. expect(Core::CORE['>'].call([2.0, 1.0])).to eq(true)
  15. expect(Core::CORE['>'].call([2.0, 3.0])).to eq(false)
  16. expect(Core::CORE['<='].call([2.0, 1.0])).to eq(false)
  17. expect(Core::CORE['<='].call([2.0, 2.0])).to eq(true)
  18. expect(Core::CORE['>='].call([2.0, 2.0])).to eq(true)
  19. expect(Core::CORE['>='].call([2.0, 3.0])).to eq(false)
  20. expect(Core::CORE['and'].call([2.0, 3.0])).to eq(true)
  21. expect(Core::CORE['and'].call([2.0, 3.0, false])).to eq(false)
  22. expect(Core::CORE['or'].call([2.0, 3.0, false])).to eq(true)
  23. expect(Core::CORE['or'].call([false, false])).to eq(false)
  24. expect(Core::CORE['not'].call([1.0])).to eq(false)
  25. expect(Core::CORE['not'].call([false])).to eq(true)
  26. end
  27. it 'returns an error if argument has the wrong type' do
  28. expect(Core::CORE['+'].call([1.0, "hello"])).to eq(
  29. Error.new("Expected an argument of type number but got string")
  30. )
  31. end
  32. it 'returns an error if given the wrong number of arguments' do
  33. expect(Core::CORE['not'].call([1.0, 2.0])).to eq(
  34. Error.new("Expected 1 argument but received 2")
  35. )
  36. end
  37. end
  38. end