A toy dynamic programming language written in Ruby
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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