Sha256: a8f798bf5117cf6a0e562f98d7692d694bbbe4f3ab9728fc2512f4f7090ac0ce

Contents?: true

Size: 1.6 KB

Versions: 110

Compression:

Stored size: 1.6 KB

Contents

require 'gherkin/rubify'
require 'gherkin/formatter/model'

module Gherkin
  class SexpRecorder
    include Rubify
    
    def initialize
      @sexps = []
    end

    # We can't use method_missing - therubyracer isn't able to invoke methods like that.
    [:comment, :tag, :feature, :background, :scenario, :scenario_outline, :examples, :step, :doc_string, :row, :eof, :uri, :syntax_error].each do |event|
      define_method(event) do |*args|
        event = :scenario_outline if event == :scenarioOutline # Special Java Lexer handling
        event = :doc_string if event == :docString # Special Java Lexer handling
        event = :syntax_error if event == :syntaxError # Special Java Lexer handling
        args  = rubify(args)
        args  = sexpify(args)
        @sexps << [event] + args
      end
    end

    def to_sexp
      @sexps
    end

    # Useful in IRB
    def reset!
      @sexps = []
    end

    def errors
      @sexps.select { |sexp| sexp[0] == :syntax_error }
    end

    def line(number)
      @sexps.find { |sexp| sexp.last == number }
    end

    def sexpify(o)
      array = (defined?(JRUBY_VERSION) && Java.java.util.Collection === o) || 
              (defined?(V8) && V8::Array === o) ||
              Array === o
      if array
        o.map{|e| sexpify(e)}
      elsif(Formatter::Model::Row === o)
        {
          "cells" => sexpify(o.cells),
          "comments" => sexpify(o.comments),
          "line" => o.line,
        }
      elsif(Formatter::Model::Comment === o)
        o.value
      elsif(Formatter::Model::Tag === o)
        o.name
      else
        o
      end
    end
  end
end

Version data entries

110 entries across 110 versions & 1 rubygems

Version Path
gherkin-2.5.4 spec/gherkin/sexp_recorder.rb
gherkin-2.5.4-x86-mswin32 spec/gherkin/sexp_recorder.rb
gherkin-2.5.4-x86-mingw32 spec/gherkin/sexp_recorder.rb
gherkin-2.5.4-universal-dotnet spec/gherkin/sexp_recorder.rb
gherkin-2.5.4-java spec/gherkin/sexp_recorder.rb
gherkin-2.5.3 spec/gherkin/sexp_recorder.rb
gherkin-2.5.3-x86-mswin32 spec/gherkin/sexp_recorder.rb
gherkin-2.5.3-x86-mingw32 spec/gherkin/sexp_recorder.rb
gherkin-2.5.3-universal-dotnet spec/gherkin/sexp_recorder.rb
gherkin-2.5.3-java spec/gherkin/sexp_recorder.rb
gherkin-2.5.2 spec/gherkin/sexp_recorder.rb
gherkin-2.5.2-x86-mswin32 spec/gherkin/sexp_recorder.rb
gherkin-2.5.2-x86-mingw32 spec/gherkin/sexp_recorder.rb
gherkin-2.5.2-universal-dotnet spec/gherkin/sexp_recorder.rb
gherkin-2.5.2-java spec/gherkin/sexp_recorder.rb
gherkin-2.5.1 spec/gherkin/sexp_recorder.rb
gherkin-2.5.1-x86-mswin32 spec/gherkin/sexp_recorder.rb
gherkin-2.5.1-x86-mingw32 spec/gherkin/sexp_recorder.rb
gherkin-2.5.1-universal-dotnet spec/gherkin/sexp_recorder.rb
gherkin-2.5.1-java spec/gherkin/sexp_recorder.rb