Skip to content

Commit

Permalink
convert MarkDown tests to Cucumber
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Dec 14, 2014
1 parent bbd1738 commit 163912c
Show file tree
Hide file tree
Showing 13 changed files with 584 additions and 922 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ The goal is to create a Rubocop plugin which can check for
covert some ugly code parts introduced by the automatic code conversion done by
[YCP Killer](https://github.com/yast/ycp-killer) (conversion from YCP to Ruby).

Check [the test descriptions](spec/builtins_spec.md) to see the examples of offense
detection and code conversion.
Check [the RSpec tests](spec) and [the Cucumber features](features)to see
the examples of offense detection and code conversion.

*The plugin is currently in early development, always manually check the chages
done by the plugin! It can eat your code... ;-)*
Expand Down Expand Up @@ -135,4 +135,4 @@ and then run:
bundle exec rake release
```

(Note: You need push permissions at Rubygems.org.)
(Note: You need push permissions at [rubygems.org](https://rubygems.org/gems/rubocop-yast).)
27 changes: 3 additions & 24 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,13 @@ rescue Bundler::BundlerError => e
exit e.status_code
end

require "redcarpet"
require_relative "spec/rspec_renderer"

def render_markdown(renderer, task)
puts "Rendering file: #{task.name}"
markdown = Redcarpet::Markdown.new(renderer, fenced_code_blocks: true)

string = markdown.render(File.read(task.prerequisites[0]))
File.write(task.name, string)
end

renderer = "spec/rspec_renderer.rb"

file "spec/builtins_spec.rb" => ["spec/builtins_spec.md", renderer] do |t|
render_markdown(RSpecRenderer.new("RuboCop::Cop::Yast::Builtins"), t)
end

file "spec/builtins_spec.html" => ["spec/builtins_spec.md", renderer] do |t|
render_markdown(Redcarpet::Render::HTML, t)
end
desc "Render the specification to HTML locally"
task html: ["spec/builtins_spec.html"]
require "cucumber/rake/task"
Cucumber::Rake::Task.new(:features)

require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)
task spec: ["spec/builtins_spec.rb"]

require "rubocop/rake_task"
RuboCop::RakeTask.new(:rubocop)

task default: [:spec, :rubocop]
task default: [:spec, :features, :rubocop]
67 changes: 67 additions & 0 deletions features/builtins_cop.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
Feature: Generic Builtins detection and replacement

Some Builtin calls are not detected as an offense and are kept unchanged.
That include the calls which do not have native ruby replacement, like lsort()
or crytp() functions, or the replacement would be too complex (the gettext
builtins).

Only known builtins can be replaced, the rest needs to be kept untouched.


Scenario: y2milestone() builtin call is reported as an offense
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: Builtin with explicit Yast namespace is reported as an offense
Given the original code is
"""
Yast::Builtins.y2milestone("foo")
"""
When I check it using RuboCop::Cop::Yast::Builtins cop
Then offense "Builtin call `y2milestone` is obsolete" is found

Scenario: Builtin with explicit ::Yast namespace is reported as an offense
Given the original code is
"""
::Yast::Builtins.y2milestone("foo")
"""
When I check it using RuboCop::Cop::Yast::Builtins cop
Then offense "Builtin call `y2milestone` is obsolete" is found

Scenario: Builtins with ::Builtins name space are ignored
Given the original code is
"""
::Builtins.y2milestone("foo")
"""
When I check it using RuboCop::Cop::Yast::Builtins cop
Then the code is found correct

Scenario: Builtins in non Yast name space are ignored
Given the original code is
"""
Foo::Builtins.y2milestone("foo")
"""
When I check it using RuboCop::Cop::Yast::Builtins cop
Then the code is found correct

Scenario: lsort(), crypt and gettext builtins are allowed
Given the original code is
"""
Builtins.lsort(["foo"])
Builtins.crypt("foo")
Builtins.dgettext("domain", "foo")
"""
When I check it using RuboCop::Cop::Yast::Builtins cop
Then the code is found correct

Scenario: Unknown builtins are kept unchanged
Given the original code is
"""
Builtins.foo()
"""
When I correct it using RuboCop::Cop::Yast::Builtins cop
Then the code is unchanged
39 changes: 39 additions & 0 deletions features/builtins_env.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Feature: Builtins.getenv()

Builtins.getenv() call can be easily replaced by ENV hash access.
We just need to keep the original parameter.

Scenario: With string literal parameter it is translated to ENV equivalent
Given the original code is
"""
Builtins.getenv("foo")
"""
When I correct it using RuboCop::Cop::Yast::Builtins cop
Then the code is converted to
"""
ENV["foo"]
"""

Scenario: Variable as the parameter is preserved
Given the original code is
"""
foo = bar
Builtins.getenv(foo)
"""
When I correct it using RuboCop::Cop::Yast::Builtins cop
Then the code is converted to
"""
foo = bar
ENV[foo]
"""

Scenario: Any other statement is preserved
Given the original code is
"""
Builtins.getenv(Ops.add(foo, bar))
"""
When I correct it using RuboCop::Cop::Yast::Builtins cop
Then the code is converted to
"""
ENV[Ops.add(foo, bar)]
"""
10 changes: 10 additions & 0 deletions features/builtins_time.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature: Builtins.time()

Builtins.time() call can be easily replaced by native Time.now call.
It has no parameter therefore no extra handling is needed.


Scenario: Builtins.time() is replaced by ::Time.now.to_i
Given the original code is "Builtins.time()"
When I correct it using RuboCop::Cop::Yast::Builtins cop
Then the code is converted to "::Time.now.to_i"
Loading

0 comments on commit 163912c

Please sign in to comment.