Sha256: 6c8e98b770ca9a5125b160d5b297ec56668a15f9eacff11c02e0f57dd7411ecc

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

require 'shoulda_runner/ast_builder'

module ShouldaRunner
  class Parser
    def initialize(test_file_path)
      @source = File.read(test_file_path)
    end

    def select_test(linenumber)
      test_names[linenumber]
    end

    private

    def test_names
      @asts ||= build_asts
      hash = {}
      @asts.each do |ast|
        ast.each do |node|
          if node.children.empty?
            from = node.content[:line_range][:from]
            to   = node.content[:line_range][:to]
            test_name = build_test_name(node)

            from.upto(to) do |line|
              hash[line] = test_name
            end
          end
        end
      end

      hash
    end

    def build_test_name(node)
      test_name_string = node.content[:name]
      parent = node.parent
      while parent != nil
        test_name_string = "#{parent.content[:name]}#{test_name_string}"
        parent = parent.parent
      end

      test_name_string
    end

    def build_asts
      ast_builder = AstBuilder.new(@source)
      ast_builder.parse
      @asts = ast_builder.asts
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shoulda_runner-0.2.2 lib/shoulda_runner/parser.rb
shoulda_runner-0.2.1 lib/shoulda_runner/parser.rb