changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/lib/spec/runner/spec_parser.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1module Spec
2 module Runner
3 # Parses a spec file and finds the nearest example for a given line number.
4 class SpecParser
5 attr_reader :best_match
6
7 def initialize
8 @best_match = {}
9 end
10
11 def spec_name_for(file, line_number)
12 best_match.clear
13 file = File.expand_path(file)
14 rspec_options.example_groups.each do |example_group|
15 consider_example_groups_for_best_match example_group, file, line_number
16
17 example_group.examples.each do |example|
18 consider_example_for_best_match example, example_group, file, line_number
19 end
20 end
21 if best_match[:example_group]
22 if best_match[:example]
23 "#{best_match[:example_group].description} #{best_match[:example].description}"
24 else
25 best_match[:example_group].description
26 end
27 else
28 nil
29 end
30 end
31
32 protected
33
34 def consider_example_groups_for_best_match(example_group, file, line_number)
35 parsed_backtrace = parse_backtrace(example_group.registration_backtrace)
36 parsed_backtrace.each do |example_file, example_line|
37 if is_best_match?(file, line_number, example_file, example_line)
38 best_match.clear
39 best_match[:example_group] = example_group
40 best_match[:line] = example_line
41 end
42 end
43 end
44
45 def consider_example_for_best_match(example, example_group, file, line_number)
46 parsed_backtrace = parse_backtrace(example.implementation_backtrace)
47 parsed_backtrace.each do |example_file, example_line|
48 if is_best_match?(file, line_number, example_file, example_line)
49 best_match.clear
50 best_match[:example_group] = example_group
51 best_match[:example] = example
52 best_match[:line] = example_line
53 end
54 end
55 end
56
57 def is_best_match?(file, line_number, example_file, example_line)
58 file == File.expand_path(example_file) &&
59 example_line <= line_number &&
60 example_line > best_match[:line].to_i
61 end
62
63 def parse_backtrace(backtrace)
64 backtrace.collect do |trace_line|
65 split_line = trace_line.split(':')
66 [split_line[0], Integer(split_line[1])]
67 end
68 end
69 end
70 end
71end