From 531384b453c9ee1273aa550d80a7e1b42daed707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20Slez=C3=A1k?= Date: Fri, 12 Dec 2014 19:20:26 +0100 Subject: [PATCH] initial support for writing tests in cucumber --- Rakefile | 5 ++- features/builtins_cop.feature | 21 +++++++++++++ features/builtins_time.feature | 5 +++ features/builtins_y2log.feature | 15 +++++++++ features/step_definitions/cop_steps.rb | 42 ++++++++++++++++++++++++++ features/support/env.rb | 36 ++++++++++++++++++++++ rubocop-yast.gemspec | 1 + spec/builtins_spec.md | 19 ------------ spec/spec_helper.rb | 12 -------- 9 files changed, 124 insertions(+), 32 deletions(-) create mode 100644 features/builtins_cop.feature create mode 100644 features/builtins_time.feature create mode 100644 features/builtins_y2log.feature create mode 100644 features/step_definitions/cop_steps.rb create mode 100644 features/support/env.rb diff --git a/Rakefile b/Rakefile index 6dfe755..71f9b03 100644 --- a/Rakefile +++ b/Rakefile @@ -10,6 +10,9 @@ rescue Bundler::BundlerError => e exit e.status_code end +require "cucumber/rake/task" +Cucumber::Rake::Task.new(:features) + require "redcarpet" require_relative "spec/rspec_renderer" @@ -40,4 +43,4 @@ task spec: ["spec/builtins_spec.rb"] require "rubocop/rake_task" RuboCop::RakeTask.new(:rubocop) -task default: [:spec, :rubocop] +task default: [:spec, :features, :rubocop] diff --git a/features/builtins_cop.feature b/features/builtins_cop.feature new file mode 100644 index 0000000..a932822 --- /dev/null +++ b/features/builtins_cop.feature @@ -0,0 +1,21 @@ +Feature: Builtins Cop Features + + Scenario: Builtins.y2milestone() is reported + Given the original code is "Builtins.y2milestone('foo')" + When I check it using RuboCop::Cop::Yast::Builtins cop + Then offense "Builtin call `y2milestone` is obsolete" is found + + Scenario: Allowed Builtins.lsort() call is accepted + Given the original code is "Builtins.lsort([])" + When I check it using RuboCop::Cop::Yast::Builtins cop + Then the code is found correct + + Scenario: Builtins in the ::Builtins name space are ignored + Given the original code is "::Builtins.lsort([])" + When I check it using RuboCop::Cop::Yast::Builtins cop + Then the code is found correct + + Scenario: Unknown Builtins call is not changed + Given the original code is "Builtins.foo()" + When I correct it using RuboCop::Cop::Yast::Builtins cop + Then the code is unchanged diff --git a/features/builtins_time.feature b/features/builtins_time.feature new file mode 100644 index 0000000..19a86c6 --- /dev/null +++ b/features/builtins_time.feature @@ -0,0 +1,5 @@ +Feature: Builtins.time() + Scenario: Replace Builtins.time() with ::Time.now.to_i + Given the original code is "Builtins.time()" + When I correct it using RuboCop::Cop::Yast::Builtins cop + Then the code should be converted to "::Time.now.to_i" diff --git a/features/builtins_y2log.feature b/features/builtins_y2log.feature new file mode 100644 index 0000000..4821051 --- /dev/null +++ b/features/builtins_y2log.feature @@ -0,0 +1,15 @@ +Feature: Logging Builtins + + Scenario: The include statement is added only once + Given the original code is + """ + Builtins.y2milestone("foo") + Builtins.y2milestone("foo") + """ + When I correct it using RuboCop::Cop::Yast::Builtins cop + Then the code should be converted to + """ + include Yast::Logger + log.info "foo" + log.info "foo" + """ diff --git a/features/step_definitions/cop_steps.rb b/features/step_definitions/cop_steps.rb new file mode 100644 index 0000000..b6b9443 --- /dev/null +++ b/features/step_definitions/cop_steps.rb @@ -0,0 +1,42 @@ + +# inline code +Given(/^the original code is "(.*)"$/) do |original_code| + @original_code = original_code +end + +# multiline code passed via docstring +Given(/^the original code is$/) do |original_code| + @original_code = original_code +end + +When(/^I correct it using (.*) cop$/) do |cop| + @cop = Object.const_get(cop).new + @corrected = autocorrect_source(@cop, @original_code.split("\n")) +end + +# inline code +Then(/^the code should be converted to "(.*)"$/) do |expected_code| + expect(@corrected).to eq(expected_code) +end + +# multiline code passed via docstring +Then(/^the code should be converted to$/) do |expected_code| + expect(@corrected).to eq(expected_code) +end + +Then(/^the code is unchanged$/) do + expect(@corrected).to eq(@original_code) +end + +When(/^I check it using (.*) cop$/) do |cop| + @cop = Object.const_get(cop).new + inspect_source(@cop, @original_code.split("\n")) +end + +Then(/^the code is found correct$/) do + expect(@cop.offenses).to be_empty +end + +Then(/^offense "(.*)" is found$/) do |offense| + expect(@cop.offenses.first.message).to include(offense) +end diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000..b14d742 --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1,36 @@ +# encoding: utf-8 + +require "simplecov" +require "rspec" + +# use coveralls for on-line code coverage reporting at Travis CI +if ENV["TRAVIS"] + require "coveralls" + + SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ + SimpleCov::Formatter::HTMLFormatter, + Coveralls::SimpleCov::Formatter + ] +end + +SimpleCov.start do + # don't check code coverage in these subdirectories + add_filter "/vendor/" + add_filter "/features/" +end + +# allow only the new "expect" RSpec syntax +RSpec.configure do |config| + config.expect_with :rspec do |c| + c.syntax = :expect + end +end + +# reuse the Rubocop helper, provides some nice methods used in tests +require File.join(Gem::Specification.find_by_name("rubocop").gem_dir, "spec", + "support", "cop_helper.rb") +include CopHelper + +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) + +require "rubocop-yast" diff --git a/rubocop-yast.gemspec b/rubocop-yast.gemspec index 104f9dc..70251bc 100644 --- a/rubocop-yast.gemspec +++ b/rubocop-yast.gemspec @@ -33,5 +33,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency("rake") spec.add_development_dependency("redcarpet", "~> 3") spec.add_development_dependency("rspec", "~> 3.1.0") + spec.add_development_dependency("cucumber") spec.add_development_dependency("simplecov") end diff --git a/spec/builtins_spec.md b/spec/builtins_spec.md index a6ab0bd..b2a63b2 100644 --- a/spec/builtins_spec.md +++ b/spec/builtins_spec.md @@ -5,7 +5,6 @@ Table Of Contents ----------------- 1. [Description](#description) -1. [Builtins.time()](#builtinstime) 1. [Builtins.getenv()](#builtinsgetenv) 1. [Logging - Builtins.y2debug(),...](#logging---builtinsy2debug-) @@ -82,24 +81,6 @@ It does not change unknown builtins Builtins.foo() ``` - -Builtins.time() ---------------- - -Is trivially converted to `::Time.now.to_i` - -**Original** - -```ruby -Builtins.time() -``` - -**Translated** - -```ruby -::Time.now.to_i -``` - Builtins.getenv() ----------------- diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 39b00d8..4d4006a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,18 +2,6 @@ require "simplecov" -# use coveralls for on-line code coverage reporting at Travis CI -if ENV["TRAVIS"] - require "coveralls" - - SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ - SimpleCov::Formatter::HTMLFormatter, - Coveralls::SimpleCov::Formatter - ] -end - -SimpleCov.minimum_coverage 95 - SimpleCov.start do # don't check code coverage in these subdirectories add_filter "/vendor/"