From df2845194365f0bfbad8fe1a0ed44fac5c4f1f05 Mon Sep 17 00:00:00 2001 From: BugDiver Date: Sat, 13 Jan 2018 14:30:12 +0530 Subject: [PATCH] Adding aliases and recoverable steps in method chache getgauge/gauge-vscode#85 #47 --- lib/static_loader.rb | 42 ++++++++++++++++++++++--- spec/static_loader_spec.rb | 63 +++++++++++++++++++++++++++++--------- 2 files changed, 87 insertions(+), 18 deletions(-) diff --git a/lib/static_loader.rb b/lib/static_loader.rb index cccadf1..a80c644 100644 --- a/lib/static_loader.rb +++ b/lib/static_loader.rb @@ -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 diff --git a/spec/static_loader_spec.rb b/spec/static_loader_spec.rb index 4166a9a..007de3c 100644 --- a/spec/static_loader_spec.rb +++ b/spec/static_loader_spec.rb @@ -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 ' do |v| - @vowels = v -end" + content = "step 'foo ' 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 ' end + it 'should load aliases in method cache' do + file = 'foo.rb' + content = "step 'foo ','bar ' 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 ' + expect(subject.valid_step? 'bar {}').to eq true + expect(subject.get_step_text 'bar {}').to eq 'bar ' + end + + it 'should load recoverable steps' do + file = 'foo.rb' + content = "step 'foo ', :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 ','bar ','hey ', :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 ').to eq true + expect(subject.valid_step? 'bar {}').to eq true + expect(subject.recoverable? 'bar {}').to eq true + expect(subject.has_alias? 'bar ').to eq true + expect(subject.valid_step? 'hey {}').to eq true + expect(subject.recoverable? 'hey {}').to eq true + expect(subject.has_alias? 'hey ').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 ' do |v| - @vowels = v -end" + content = "step 'foo ' 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 ' - content = "@vowels = nil -step 'hello ' do |v| - @vowels = v -end" + content = "step 'hello ' do |v|\nend" ast = Gauge::CodeParser.code_to_ast content Gauge::StaticLoader.reload_steps(file, ast) expect(subject.valid_step? 'foo {}').to eq false