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.

variable_declaration_spec.rb 678B

123456789101112131415161718192021222324
  1. RSpec.describe AST::VariableDeclaration do
  2. it 'defines a variable' do
  3. env = Environment.new
  4. AST::VariableDeclaration.new(
  5. AST::Identifier.new('x'),
  6. AST::Number.new(5.0),
  7. ).execute(env)
  8. expect(env.get('x').execute(env)).to eq(5.0)
  9. end
  10. it 'raises if a variable is already defined' do
  11. env = Environment.new
  12. AST::VariableDeclaration.new(
  13. AST::Identifier.new('x'),
  14. AST::Number.new(5.0),
  15. ).execute(env)
  16. expect {
  17. AST::VariableDeclaration.new(
  18. AST::Identifier.new('x'),
  19. AST::Number.new(6.0),
  20. ).execute(env)
  21. }.to raise_error('Invalid declaration of previously declared variable x')
  22. end
  23. end