Skip to content

Commit

Permalink
aggregate spec tree by indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
koffeinfrei committed Nov 6, 2013
1 parent 21b4471 commit 1e2892b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
26 changes: 16 additions & 10 deletions lib/guard/jasmine/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,17 @@ def query_string_for_suite_from_line_number(file, options)
line_number ||= options[:line_number]

if line_number
groups = it_and_describe_lines(file_name)[0, line_number].
group_by { |x| x[0,2] }
lines = it_and_describe_lines(file_name, 0, line_number)
last = lines.pop

specs = groups["de"]
specs << groups["it"][-1] if groups["it"]
specs.map { |x| spec_title(x) }.join(' ')
last_indentation = last[/^\s*/].length
# keep only lines with lower indentation
lines.delete_if { |x| x[/^\s*/].length >= last_indentation }
# remove all 'it'
lines.delete_if { |x| x =~ /^\s*it/ }

lines << last
lines.map { |x| spec_title(x) }.join(' ')
end
end

Expand Down Expand Up @@ -213,20 +218,21 @@ def query_string_for_suite_from_first_describe(file, options)
# @return [Array] `[file_name, line_number]`
#
def file_and_line_number_parts(file)
match = file.match(/\A(.+?)(?::(\d+))?$/)
match = file.match(/^(.+?)(?::(\d+))?$/)
[match[1], match[2].nil? ? nil : match[2].to_i]
end

# Returns all lines of the file that are either a
# 'describe' or a 'it' declaration.
#
# @param [String] file the spec file
# @param [Numeric] from the first line in the range
# @param [Numeric] to the last line in the range
# @Return [Array] the line contents
#
def it_and_describe_lines(file)
File.readlines(file).
map(&:strip).
select { |x| x.start_with?('it') || x.start_with?('describe') }
def it_and_describe_lines(file, from, to)
File.readlines(file)[from, to].
select { |x| x =~ /^\s*(it|describe)/ }
end

# Extracts the title of a 'description' or a 'it' declaration.
Expand Down
27 changes: 16 additions & 11 deletions spec/guard/jasmine/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,23 +214,28 @@
context 'when passed a line number' do
before do
File.stub(:readlines).and_return([
'describe "TestContext", ->',
' it "does something", ->',
' # some assertion',
' it "does something else", ->',
' # some assertion'
'describe "TestContext", ->', # 1
' describe "Inner TestContext", ->', # 2
' describe "Unrelated TestContext", ->', # 3
' it "does something", ->', # 4
' # some code', # 5
' # some assertion', # 6
' it "does something else", ->', # 7
' # some assertion', # 8
' it "does something a lot else", ->', # 9
' # some assertion' # 10
])
end

context 'with the spec file name' do
it 'executes the example for line number on example' do
IO.should_receive(:popen).with("#{ phantomjs_command } \"http://localhost:8888/jasmine?spec=TestContext%20does%20something%20else\" 60000 failure true failure failure false true ''", "r:UTF-8")
runner.run(['spec/javascripts/a.js.coffee:4'], defaults)
IO.should_receive(:popen).with("#{ phantomjs_command } \"http://localhost:8888/jasmine?spec=TestContext%20Inner%20TestContext%20does%20something%20else\" 60000 failure true failure failure false true ''", "r:UTF-8")
runner.run(['spec/javascripts/a.js.coffee:7'], defaults)
end

it 'executes the example for line number within example' do
IO.should_receive(:popen).with("#{ phantomjs_command } \"http://localhost:8888/jasmine?spec=TestContext%20does%20something%20else\" 60000 failure true failure failure false true ''", "r:UTF-8")
runner.run(['spec/javascripts/a.js.coffee:5'], defaults)
IO.should_receive(:popen).with("#{ phantomjs_command } \"http://localhost:8888/jasmine?spec=TestContext%20Inner%20TestContext%20does%20something%20else\" 60000 failure true failure failure false true ''", "r:UTF-8")
runner.run(['spec/javascripts/a.js.coffee:8'], defaults)
end

it 'executes all examples within describe' do
Expand All @@ -241,8 +246,8 @@

context 'with the cli argument' do
it 'executes the example for line number on example' do
IO.should_receive(:popen).with("#{ phantomjs_command } \"http://localhost:8888/jasmine?spec=TestContext%20does%20something%20else\" 60000 failure true failure failure false true ''", "r:UTF-8")
runner.run(['spec/javascripts/a.js.coffee'], defaults.merge(line_number: 4))
IO.should_receive(:popen).with("#{ phantomjs_command } \"http://localhost:8888/jasmine?spec=TestContext%20Inner%20TestContext%20does%20something%20else\" 60000 failure true failure failure false true ''", "r:UTF-8")
runner.run(['spec/javascripts/a.js.coffee'], defaults.merge(line_number: 7))
end
end
end
Expand Down

0 comments on commit 1e2892b

Please sign in to comment.