Skip to content

Commit

Permalink
Adding aliases and recoverable steps in method chache getgauge/gauge-…
Browse files Browse the repository at this point in the history
  • Loading branch information
BugDiver committed Jan 13, 2018
1 parent b2a0e4e commit df28451
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 18 deletions.
42 changes: 38 additions & 4 deletions lib/static_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,47 @@ def self.remove_steps(file)
end

def self.step_node?(node)
node.type == :block && node.children[0].children.size > 2 && node.children[0].children[1] == :step
node.type == :block && node.children[0].children[1] == :step
end

def self.process_node(file, node)
step_text = node.children[0].children[2].children[0]
step_value = Gauge::Connector.step_value step_text
si = {location: {file: file, span: node.loc}, step_text: step_text, block: node}
if aliases?(node)
load_aliases(file, node)
else
step_text = node.children[0].children[2].children[0]
step_value = Gauge::Connector.step_value step_text
load_step(file, step_value, step_text, node, {recoverable: recoverable?(node)})
end
end

def self.aliases?(node)
return node.children[0].children.size > 3 && node.children[0].children[3].type == :str
end

def self.load_aliases(file, node)
recoverable = false
if recoverable? node
aliases = node.children[0].children.slice(2, node.children[0].children.length() - 3)
recoverable = true
else
aliases = node.children[0].children.slice(2, node.children[0].children.length() - 2)
end
Gauge::MethodCache.add_step_alias(*aliases.map {|x| x.children[0]})
aliases.each {|x|
sv = Gauge::Connector.step_value x.children[0]
load_step(file, sv, x.children[0], node, {recoverable: recoverable})
}
end

def self.recoverable?(node)
size = node.children[0].children.length
options = node.children[0].children[size - 1]
options.type == :hash && options.children[0].children[0].children[0] == :continue_on_failure
end

def self.load_step(file, step_value, step_text, block, options)
si = {location: {file: file, span: block.loc}, step_text: step_text,
block: block, recoverable: options[:recoverable]}
Gauge::MethodCache.add_step(step_value, si)
end
end
Expand Down
63 changes: 49 additions & 14 deletions spec/static_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,72 @@
describe Gauge::StaticLoader do

context 'load method cache from file content without executing' do
subject { Gauge::MethodCache }
before(:each){
subject {Gauge::MethodCache}
before(:each) {
subject.clear
}

it 'should pupuplate method cache' do
file = 'foo.rb'
content = "@vowels = nil
step 'foo <vowels>' do |v|
@vowels = v
end"
content = "step 'foo <vowels>' do |v|\nend"
ast = Gauge::CodeParser.code_to_ast content
Gauge::StaticLoader.load_steps(file, ast)
expect(subject.valid_step? 'foo {}').to eq true
expect(subject.get_step_text 'foo {}').to eq 'foo <vowels>'
end

it 'should load aliases in method cache' do
file = 'foo.rb'
content = "step 'foo <vowels>','bar <vowels>' do |v|\nend"
ast = Gauge::CodeParser.code_to_ast content
Gauge::StaticLoader.load_steps(file, ast)
expect(subject.valid_step? 'foo {}').to eq true
expect(subject.get_step_text 'foo {}').to eq 'foo <vowels>'
expect(subject.valid_step? 'bar {}').to eq true
expect(subject.get_step_text 'bar {}').to eq 'bar <vowels>'
end

it 'should load recoverable steps' do
file = 'foo.rb'
content = "step 'foo <bar>', :continue_on_failure => true do |v|\nend"
ast = Gauge::CodeParser.code_to_ast content
Gauge::StaticLoader.load_steps(file, ast)
expect(subject.valid_step? 'foo {}').to eq true
expect(subject.recoverable? 'foo {}').to eq true
end

it 'should load recoverable aliases steps' do
file = 'foo.rb'
content = "step 'foo <vowels>','bar <vowels>','hey <vowels>', :continue_on_failure => true do |v|\nend"
ast = Gauge::CodeParser.code_to_ast content
Gauge::StaticLoader.load_steps(file, ast)
expect(subject.valid_step? 'foo {}').to eq true
expect(subject.recoverable? 'foo {}').to eq true
expect(subject.has_alias? 'foo <vowels>').to eq true
expect(subject.valid_step? 'bar {}').to eq true
expect(subject.recoverable? 'bar {}').to eq true
expect(subject.has_alias? 'bar <vowels>').to eq true
expect(subject.valid_step? 'hey {}').to eq true
expect(subject.recoverable? 'hey {}').to eq true
expect(subject.has_alias? 'hey <vowels>').to eq true
end

it 'should not load empty steps' do
file = 'foo.rb'
content = "step '' do |v|\nend"
ast = Gauge::CodeParser.code_to_ast content
Gauge::StaticLoader.load_steps(file, ast)
expect(subject.valid_step? 'foo {}').to eq false
end

it 'reload a given file' do
file = 'foo.rb'
content = "@vowels = nil
step 'foo <vowels>' do |v|
@vowels = v
end"
content = "step 'foo <vowels>' do |v|\nend"
ast = Gauge::CodeParser.code_to_ast content
Gauge::StaticLoader.load_steps(file, ast)
expect(subject.valid_step? 'foo {}').to eq true
expect(subject.get_step_text 'foo {}').to eq 'foo <vowels>'
content = "@vowels = nil
step 'hello <vowels>' do |v|
@vowels = v
end"
content = "step 'hello <vowels>' do |v|\nend"
ast = Gauge::CodeParser.code_to_ast content
Gauge::StaticLoader.reload_steps(file, ast)
expect(subject.valid_step? 'foo {}').to eq false
Expand Down

0 comments on commit df28451

Please sign in to comment.