Sha256: 549bb70ab20ba8fcb59daba4cab7930e4a7bd11f0c9e867c3970c97c8b80f871

Contents?: true

Size: 1001 Bytes

Versions: 3

Compression:

Stored size: 1001 Bytes

Contents

require 'spec_helper'

require 'dentaku/visitor/infix'

class ArrayProcessor
  attr_reader :expression
  include Dentaku::Visitor::Infix

  def initialize
    @expression = []
  end

  def visit_array(node)
    @expression << "{"

    head, *tail = node.value

    process(head) if head

    tail.each do |v|
      @expression << ","
      process(v)
    end

    @expression << "}"
  end

  def process(node)
    @expression << node.to_s
  end
end

RSpec.describe Dentaku::Visitor::Infix do
  it 'generates array representation of operation' do
    processor = ArrayProcessor.new
    processor.visit(ast('5 + 3'))
    expect(processor.expression).to eq ['5', '+', '3']
  end

  it 'supports array nodes' do
    processor = ArrayProcessor.new
    processor.visit(ast('{1, 2, 3}'))
    expect(processor.expression).to eq ['{', '1', ',', '2', ',', '3', '}']
  end

  private

  def ast(expression)
    tokens = Dentaku::Tokenizer.new.tokenize(expression)
    Dentaku::Parser.new(tokens).parse
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dentaku-3.5.4 spec/visitor/infix_spec.rb
dentaku-3.5.3 spec/visitor/infix_spec.rb
dentaku-3.5.2 spec/visitor/infix_spec.rb