From b7be92686258cf5f5cb8fd1c673fe5d5fa1cd498 Mon Sep 17 00:00:00 2001 From: Erhard Date: Sun, 25 May 2014 21:15:50 +0200 Subject: [PATCH] added commandline tests with cucumber and aruba --- Gemfile | 6 ++- Gemfile.lock | 19 +++++++- Guardfile | 7 +-- bin/mixit | 35 ++++++++------ features/env.rb | 58 +----------------------- features/mixer.feature | 29 ++++++++++-- features/step_definitions/mixer_steps.rb | 19 +++----- features/testDir/data.json | 0 features/testDir/templates/test.txt.erb | 0 lib/mixer.rb | 50 ++++++++++---------- lib/template.rb | 4 ++ mixItRuby.gemspec | 20 ++++---- readme.md | 5 +- spec/lib/mixer_spec.rb | 36 +++++++-------- spec/spec_helper.rb | 7 ++- {spec => testdata}/testdata.json | 0 testdata/testdata.out | 1 + 17 files changed, 140 insertions(+), 156 deletions(-) delete mode 100644 features/testDir/data.json delete mode 100644 features/testDir/templates/test.txt.erb rename {spec => testdata}/testdata.json (100%) create mode 100644 testdata/testdata.out diff --git a/Gemfile b/Gemfile index 00ea18c..f0e9941 100644 --- a/Gemfile +++ b/Gemfile @@ -1,9 +1,11 @@ source 'http://rubygems.org' -gem 'recursive-open-struct' - group :test do +group :test do + gem "mixItRuby" , path: "." gem 'guard-rspec' + gem 'guard-cucumber' gem 'guard' gem 'rspec' gem 'cucumber' + gem 'aruba' end diff --git a/Gemfile.lock b/Gemfile.lock index eff2314..03ca4b0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,12 +1,23 @@ +PATH + remote: . + specs: + mixItRuby (0.0.0) + GEM remote: http://rubygems.org/ specs: + aruba (0.5.4) + childprocess (>= 0.3.6) + cucumber (>= 1.1.1) + rspec-expectations (>= 2.7.0) builder (3.2.2) celluloid (0.15.2) timers (~> 1.1.0) celluloid-io (0.15.0) celluloid (>= 0.15.0) nio4r (>= 0.5.0) + childprocess (0.5.3) + ffi (~> 1.0, >= 1.0.11) coderay (1.1.0) cucumber (1.3.15) builder (>= 2.1.2) @@ -25,6 +36,9 @@ GEM lumberjack (~> 1.0) pry (>= 0.9.12) thor (>= 0.18.1) + guard-cucumber (1.4.1) + cucumber (>= 1.2.0) + guard (>= 1.1.0) guard-rspec (4.2.8) guard (~> 2.1) rspec (>= 2.14, < 4.0) @@ -45,7 +59,6 @@ GEM rb-fsevent (0.9.4) rb-inotify (0.9.3) ffi (>= 0.5.0) - recursive-open-struct (0.4.5) rspec (2.14.1) rspec-core (~> 2.14.0) rspec-expectations (~> 2.14.0) @@ -62,8 +75,10 @@ PLATFORMS ruby DEPENDENCIES + aruba cucumber guard + guard-cucumber guard-rspec - recursive-open-struct + mixItRuby! rspec diff --git a/Guardfile b/Guardfile index 782fb78..edfcdf7 100644 --- a/Guardfile +++ b/Guardfile @@ -4,9 +4,10 @@ notification :tmux, display_message: true guard :rspec do watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } + watch('bin/mixit') { "spec" } watch(%r{^spec/.+_spec\.rb$}) watch(%r{^spec/spec_helper\.rb}) end - - - +guard :cucumber do + watch(%r{^features/(.+)\.feature$}) { "bundle exec cucumber" } +end diff --git a/bin/mixit b/bin/mixit index d80b333..7610049 100755 --- a/bin/mixit +++ b/bin/mixit @@ -1,35 +1,40 @@ #!/usr/bin/env ruby -require 'mixer' require 'optparse' - - +require "mixer" +require "json" options = {} optparse = OptionParser.new do|opts| opts.banner = "Usage: mixit [-o outputfile -i inputfile -t templatefile]" - opts.on( '-i', '--input FILE', 'json Inputfile' ) do|p| + opts.on( '-i', '--input FILE', 'json inputfile' ) do|p| options[:inputfile] = p end - + opts.on( '-o', '--output FILE', 'mixed outputfile' ) do|p| options[:outputfile] = p end - - opts.on( '-t', '--template directory', 'place where the templates are' ) do|p| + opts.on( '-t', '--templates DIR', 'place where the templates are' ) do|p| options[:templateDir] = p end end.parse! - mixer = MIX::Mixer.new - -input_string = IO.read(options["inputfile"]) -outputfile = File.new(options["outputfile"]) -inputdata = JSON.pars(input_string) - - - +input_filename= options[:inputfile] +if !input_filename + STDOUT.puts "no inputfile given" + exit(1) +end + +inputfile = options[:inputfile] +path = File.dirname(inputfile) +input_string = IO.read(inputfile) +outputfile = options[:outputfile] || File.basename(inputfile,".*") + ".out" +outputfile = File.join(path,outputfile) +inputdata = JSON.parse(input_string, {:symbolize_names => true}) +templateDir = options[:templateDir] +mixer.mixItRuby(inputdata, templateDir) +mixer.writeResult2File(outputfile) diff --git a/features/env.rb b/features/env.rb index 97d0682..7686ba5 100644 --- a/features/env.rb +++ b/features/env.rb @@ -1,56 +1,2 @@ -require 'capybara/cucumber' -require 'capybara/poltergeist' - @processingWorkingDir=File.dirname(__FILE__) - @processingWorkDir=@processingWorkingDir+"/../processing/vert/" -#Start the two servers : -# -#Processing Server : - # @t1 = fork do - # system("cd #{@processingWorkDir} && rvm use jruby-1.7.1@masha_gems && vertx run #{@processingWorkDir}server_app.js -conf test.json") - # end - # sleep(5) - # @siteServerPath=File.dirname(__FILE__) + "/../site/bin/" # This is a Serverinstance which is not deployed. It simulates the server of the provider - # @t2 = fork do - # system("cd #{@siteServerPath} && ruby pageServer.rb -p 4568") - # end - # sleep(5) -if ENV['IN_BROWSER'] - # On demand: non-headless tests via Selenium/WebDriver - # To run the scenarios in browser (default: Firefox), use the following command line: - # IN_BROWSER=true bundle exec cucumber - # or (to have a pause of 1 second between each step): - # IN_BROWSER=true PAUSE=1 bundle exec cucumber - Capybara.default_driver = :selenium - AfterStep do - sleep (ENV['PAUSE'] || 0).to_i - end -else - # DEFAULT: headless tests with poltergeist/PhantomJS - Capybara.register_driver :poltergeist do |app| - Capybara::Poltergeist::Driver.new( - app, - window_size: [1280, 1024]#, - #debug: true - ) - end - Capybara.default_driver = :poltergeist - Capybara.javascript_driver = :poltergeist -end -at_exit do - - puts @t1 - puts @t2 - - - if @t1 - puts "processserver killed" - Process.kill("HUP",@t1) - sleep(10) - end - if @t2 - puts "pageServer killed" - Process.kill("HUP",@t2) - sleep(10) - end - puts "bye" -end +$LOAD_PATH << File.join(File.dirname(__FILE__),'../lib') +require "aruba/cucumber" diff --git a/features/mixer.feature b/features/mixer.feature index 8869781..3857658 100644 --- a/features/mixer.feature +++ b/features/mixer.feature @@ -1,13 +1,32 @@ Feature:Commandline functionality A User should be able to call a command to get a mixed outputfile + @announce + Scenario: CommandlineCall should give an error, when no inputfile + When I run `mixit` + Then the output should contain "no inputfile given" + + -Scenario: CommandlineCall - Given a empty testenvironment with one erb file and one data file - When I enter "mix dataInput dataoutput templateDir" - Then I should get a file with the name dataInput and the suffix mixed - + Scenario: exit status of 0 + When I run `mixit` + Then the exit status should be 1 + @announce + Scenario: Generatining an outputfile + Given the testdatafile is in the aruba filespace + When I run `mixit -i testdata/testdata.json` + When I run `cat testdata/testdata.out` + Then the stdout should contain "Bond" + + + @announce + Scenario: mixIt should also run from another templatepath + Given the testdatafile is in the aruba filespace + Given the templatefiles are in the aruby filespace + When I run `mixit -i testdata/testdata.json -t tmp/aruba` + And I run `cat testdata/testdata.out` + Then the stdout should contain "Bond" diff --git a/features/step_definitions/mixer_steps.rb b/features/step_definitions/mixer_steps.rb index e4833c0..42a6314 100644 --- a/features/step_definitions/mixer_steps.rb +++ b/features/step_definitions/mixer_steps.rb @@ -1,18 +1,13 @@ + Given(/^the testdatafile is in the aruba filespace$/) do + `mkdir tmp/aruba/testdata && cp testdata/testdata.json tmp/aruba/testdata` + end -Given(/^a empty testenvironment with one erb file and one data file$/) do - ###just do something - if File.exists?("features/testDir/output/test.txt") - system("rm /features/testDir/output/*") - end - end + Given(/^the templatefiles are in the aruby filespace$/) do + `mkdir tmp/aruba/templates && cp templates/* tmp/aruba/templates` + end + -When(/^I enter "(.*?)"$/) do |arg1| - system(arg1) -end -Then(/^I should get a file with the name dataInput and the suffix mixed$/) do - File.exists?("features/testDir/output/test.txt").should==true -end diff --git a/features/testDir/data.json b/features/testDir/data.json deleted file mode 100644 index e69de29..0000000 diff --git a/features/testDir/templates/test.txt.erb b/features/testDir/templates/test.txt.erb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/mixer.rb b/lib/mixer.rb index 457066a..03752f0 100644 --- a/lib/mixer.rb +++ b/lib/mixer.rb @@ -1,17 +1,17 @@ -module MIX +require 'ostruct' +require 'erb' +module MIX class Mixer - - def initialize(data) - @data = data + def initialize() @result = [] @deeps = 0 end - #get all variables if the level, binds it and templates it - #this is important to maintain the order of buildingblock for the input + #get all variables if the level, binds it and templates it + #this is important to maintain the order of buildingblock for the input - def premix(data) + def premix(data,templatePath) bind_hash={} data.each do |k,v| if v.kind_of?(Hash) @@ -23,47 +23,47 @@ def premix(data) et = ErbIT.new( bind_hash) template = getTemplate(bind_hash[:id]) filled = et.render(template) - puts filled.inspect @result.push(filled) end - def mixItRuby(data) + def mixItRuby(data,templatePath=nil) ###putting an Index to the elements to find the sequence which has been in the json### @deeps=@deeps + 1 - premix(data) + premix(data,templatePath) bind_hash={} data.each do |k,v| if v.kind_of?(Hash) mixItRuby(v) elsif v.kind_of?(Array) - ar = v - ar.each do |element| - mixItRuby(element) - end + ar = v + ar.each do |element| + mixItRuby(element) + end else - #done in premix + #done in premix end end - puts @deeps.inspect @deeps = @deeps -1 end + def writeResult2File(filename) + File.write(filename,@result) + end - -def getResult - @result -end + def getResult + @result + end - def getTemplate(id) - path = File.join(File.dirname(__FILE__),'../templates') - filename = File.join(path,id) - template = IO.read(filename) - template + def getTemplate(id,path = File.dirname(__FILE__)) + path = File.join(path, '../templates') + filename = File.join(path,id) + template = IO.read(filename) + template end diff --git a/lib/template.rb b/lib/template.rb index a7a874f..fa63e79 100644 --- a/lib/template.rb +++ b/lib/template.rb @@ -1,3 +1,7 @@ + +#This file is for future use to extract the variables from a erb template + + class Template attr_accessor :file_path,:contents diff --git a/mixItRuby.gemspec b/mixItRuby.gemspec index 69e1670..795b502 100644 --- a/mixItRuby.gemspec +++ b/mixItRuby.gemspec @@ -1,15 +1,13 @@ Gem::Specification.new do |s| - s.name = 'mixItRuby' - s.version = '0.0.0' - s.date = '2014-05-29' - s.summary = "MitItRuby!" - s.description = "A gem for mixing a nested json structure in erb files" - s.authors = ["Erhard Karger"] - s.email = 'erhard@kargers.org' - s.files = ["lib/mixer.rb"] - s.homepage = "www.kargers.org" - 'http://rubygems.org/gems/mixItRuby' - s.license = 'MIT' + s.name = 'mixItRuby' + s.version = '0.0.0' + s.date = '2014-05-29' + s.summary = "MitItRuby!" + s.description = "A gem for mixing a nested json structure in erb files" + s.authors = ["Erhard Karger"] + s.email = 'erhard@kargers.org' + s.homepage = 'http://rubygems.org/gems/mixItRuby' + s.license = 'MIT' git_files = `git ls-files`.split("\n") rescue '' s.files = git_files s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") diff --git a/readme.md b/readme.md index a8802c1..7cdfcb2 100644 --- a/readme.md +++ b/readme.md @@ -73,7 +73,10 @@ Then an array follow. An Array must be build of hashes, which can have subarrays What is mixItRuby needed for ? ============================== With that little gem you can build up personalized letters or emails or html pages. -Just imagine the erb file are tex files or xmlfo or html files or they are files with are ruby file again, which produce some output. +Just imagine the erb file are tex files or xml-fo or html files or they are files with are ruby file again, which produce some output. + +Another possibility is create other json files. Imagine the template erbs are templates for json. Then for example in SOA business objects can be chained and enriched from services. + There are a lot of posibilities. diff --git a/spec/lib/mixer_spec.rb b/spec/lib/mixer_spec.rb index 3d94a00..7c2d146 100644 --- a/spec/lib/mixer_spec.rb +++ b/spec/lib/mixer_spec.rb @@ -1,43 +1,39 @@ - +require 'stringio' +require 'spec_helper' require File.join(File.dirname(__FILE__),'../../lib/mixer.rb') require "json" describe "Mixer" do it "should traverse the data tree and contain the data in the result" do - mix = MIX::Mixer.new(data) + mix = MIX::Mixer.new() mix.mixItRuby(data) result = mix.getResult - puts result.inspect array_contains?(result,"Bond").should be_true - end it "should preserver the sequence of building blocks of the json in the result" do - mix = MIX::Mixer.new(data) + mix = MIX::Mixer.new() mix.mixItRuby(data) result = mix.getResult result[0].should include("Bond") end - def array_contains?(array, token) - array.each do |element| - if element =~ /#{token}/ - return true - end - end - return false - end - def data - dat={id: "adress.erb", name: "Bond", prename: "James", items: [{id: "item.erb", product: "car1", prize: "10"},{id: "item.erb" , product: "car2", prize: "11"}]} - f=File.new("spec/testdata.json","w") - f.write(dat.to_json) - f.close - dat - end +end + +def data + dat={id: "adress.erb", name: "Bond", prename: "James", items: [{id: "item.erb", product: "car1", prize: "10"},{id: "item.erb" , product: "car2", prize: "11"}]} +end +def array_contains?(array, token) + array.each do |element| + if element =~ /#{token}/ + return true + end + end + return false end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 03d3fbc..16e9011 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,6 @@ +# spec/spec_helper.rb +Dir[File.join(File.dirname(__FILE__), '../lib/**/*.rb')].each {|f| require f } -puts "im Helper ----------------<<<<<<<<<<<<<>>>>>>>>>>>>>>" -# spec/spec_helper.rb -#Dir[File.join(File.dirname(__FILE__), '../lib/**/*.rb')].each {|f| require f } -require File.join(File.dirname(__FILE__),'../lib/mixer.rb') +#require File.join(File.dirname(__FILE__),'../lib') diff --git a/spec/testdata.json b/testdata/testdata.json similarity index 100% rename from spec/testdata.json rename to testdata/testdata.json diff --git a/testdata/testdata.out b/testdata/testdata.out new file mode 100644 index 0000000..e52bbab --- /dev/null +++ b/testdata/testdata.out @@ -0,0 +1 @@ +["adress.erb\nBond , James\n\n", "item.erb\ncar1 10\n", "item.erb\ncar2 11\n"] \ No newline at end of file