From 6cfe925aaed1a8441faf3ca088c52de171ec7810 Mon Sep 17 00:00:00 2001 From: thatrubylove Date: Sun, 1 Dec 2013 22:48:21 -0800 Subject: [PATCH 1/2] Refactored Parser#run to be idiomatic ruby Used the extract method pattern to pull out 2 new helper methods 2 new tests in parser to test the methods I extracted. The tests were written against the original code before I made the refactor. * removal of _ prefix on local variables * #run is now shorter and more expressive * opt for reduce over each instead of declaring a local collection and then 'stuffing' it on each iteration * opt for an inline hash and declarations --- lib/watson/parser.rb | 55 ++++++++++++++++++++++++++------------------ spec/parser_spec.rb | 26 +++++++++++++++++++++ 2 files changed, 59 insertions(+), 22 deletions(-) diff --git a/lib/watson/parser.rb b/lib/watson/parser.rb index 8c5f81a..2de15c6 100755 --- a/lib/watson/parser.rb +++ b/lib/watson/parser.rb @@ -34,32 +34,17 @@ def run # Identify method entry debug_print "#{ self } : #{ __method__ }\n" - # Go through all files added from CL (sort them first) - # If empty, sort and each will do nothing, no errors - _completed_dirs = Array.new() - _completed_files = Array.new() - if @config.cl_entry_set - @config.file_list.sort.each do |_file| - _completed_files.push(parse_file(_file)) - end - end - - # Then go through all the specified directories - # Initial parse depth to parse_dir is 0 (unlimited) - @config.dir_list.sort.each do |_dir| - _completed_dirs.push(parse_dir(_dir, 0)) - end - # Create overall hash for parsed files - _structure = Hash.new() - _structure[:files] = _completed_files - _structure[:subdirs] = _completed_dirs + structure = { + files: get_completed_files, + dirs: get_completed_dirs + } - debug_print "_structure dump\n\n" - debug_print PP.pp(_structure, '') + debug_print "structure dump\n\n" + debug_print PP.pp(structure, '') debug_print "\n\n" - _structure + structure end @@ -432,6 +417,32 @@ def get_comment_type(filename) end + private + + + # Private + # Used by #run to parse and return an array of completed files + def get_completed_files + # Go through all files added from CL (sort them first) + # If empty, sort and reduce will do nothing, no error + return [] unless @config.cl_entry_set + sorted_files = @config.file_list.sort + sorted_files.reduce([]) do |completed, file| + completed << parse_file(file) + end + end + + + # Private + # Used by #run to parse and return an array of completed dirs + def get_completed_dirs + # Go through all the specified directories + # Initial parse depth to parse_dir is 0 (unlimited) + sorted_dirs = @config.dir_list.sort + sorted_dirs.reduce([]) do |completed, dir| + completed << parse_dir(dir, 0) + end + end end end diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb index 7a6bd98..8aeb0c1 100755 --- a/spec/parser_spec.rb +++ b/spec/parser_spec.rb @@ -15,7 +15,33 @@ module Watson enable_output end + describe "#get_completed_files" do + before(:each) do + config_msgs = { + cl_entry_set: true, + file_list: ["file1.rb"] + } + @parser = Parser.new(double(:config, config_msgs)) + allow(@parser).to receive(:parse_file) { "file" } + end + it "returns only completed files" do + @parser.send(:get_completed_files).should eql ["file"] + end + end + describe "#get_completed_dirs" do + before(:each) do + config_msgs = { + dir_list: ["dir"] + } + @parser = Parser.new(double(:config, config_msgs)) + allow(@parser).to receive(:parse_dir) { "dir" } + end + it "returns only completed dirs" do + @parser.send(:get_completed_dirs).should eql ["dir"] + end + end + describe '#get_comment_type' do context 'known extension' do it 'return correct extension for c++' do From 033775b134adf875c7d30dd7e24009ec8562266a Mon Sep 17 00:00:00 2001 From: thatrubylove Date: Sun, 1 Dec 2013 23:01:22 -0800 Subject: [PATCH 2/2] moved tests to the bottom as they are on privates --- spec/parser_spec.rb | 54 ++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb index 8aeb0c1..e28764f 100755 --- a/spec/parser_spec.rb +++ b/spec/parser_spec.rb @@ -15,32 +15,6 @@ module Watson enable_output end - describe "#get_completed_files" do - before(:each) do - config_msgs = { - cl_entry_set: true, - file_list: ["file1.rb"] - } - @parser = Parser.new(double(:config, config_msgs)) - allow(@parser).to receive(:parse_file) { "file" } - end - it "returns only completed files" do - @parser.send(:get_completed_files).should eql ["file"] - end - end - - describe "#get_completed_dirs" do - before(:each) do - config_msgs = { - dir_list: ["dir"] - } - @parser = Parser.new(double(:config, config_msgs)) - allow(@parser).to receive(:parse_dir) { "dir" } - end - it "returns only completed dirs" do - @parser.send(:get_completed_dirs).should eql ["dir"] - end - end describe '#get_comment_type' do context 'known extension' do @@ -216,7 +190,33 @@ module Watson end end + end + + describe "#get_completed_files" do + before(:each) do + config_msgs = { + cl_entry_set: true, + file_list: ["file1.rb"] + } + @parser = Parser.new(double(:config, config_msgs)) + allow(@parser).to receive(:parse_file) { "file" } + end + it "returns only completed files" do + @parser.send(:get_completed_files).should eql ["file"] + end + end -end + describe "#get_completed_dirs" do + before(:each) do + config_msgs = { + dir_list: ["dir"] + } + @parser = Parser.new(double(:config, config_msgs)) + allow(@parser).to receive(:parse_dir) { "dir" } + end + it "returns only completed dirs" do + @parser.send(:get_completed_dirs).should eql ["dir"] + end + end end