forked from exercism/ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
[WIP] Test generation refactor #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
9e9977a
lib/generator.rb: Ignore blank template lines. (#479)
Insti 2098525
Regenerated based on updated xcommon data.
Insti ea9dc16
Don't increment the version here.
Insti a140290
Generate whole test body in generator.
Insti 3e0d0e5
Add spaces around bookkeeping comment.
Insti f9d2ba0
Generate the whole test in the generator
Insti 3d616a9
Remove redundant code.
Insti c78a049
Move bookkeeping comment into generator.
Insti a38141c
Move Bookeeping gerneration into generator.
Insti 22697ca
Rename assertion to workload
Insti dbd5210
add canonical_data alias
Insti 671643d
Add to_s to TestCase that returns the full test
Insti 5ea23bf
Rename data to canonical data
Insti cc9af61
Move the index out of the canonical data
Insti c127328
Make IsogramCases a class rather than a proc
Insti 44bad1c
Added generic TestCases class
Insti a3587f5
Add test failure message.
Insti d2acb90
Rename method to `isogram?`
Insti 091bf9f
Created tests
Insti a7f013d
Get coverage info on everything in lib
Insti 5d77f2e
Tidy up ExerciseTestCase a bit.
Insti 68a5dec
Group test cases in coverage report.
Insti b23e0b6
Add rake and simplecov for beter testing.
Insti 3f25835
Update method name.
Insti fe35f2d
Only test /exercises/* tests for executability.
Insti 1e54c30
Add 'Guard' for automatically running tests.
Insti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| source 'https://rubygems.org' | ||
|
|
||
| ruby '2.2.5' | ||
|
|
||
| gem 'minitest' | ||
| gem 'rubocop', '0.36.0' | ||
| gem 'simplecov' | ||
| gem 'rake' | ||
|
|
||
| # These are not strictly necessary, but I use them to automatically run the | ||
| # tests whenever I edit a file. | ||
| gem 'guard' | ||
| gem 'guard-shell' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # A sample Guardfile | ||
| # More info at https://github.com/guard/guard#readme | ||
|
|
||
| ## Uncomment and set this to only include directories you want to watch | ||
| # directories %w(app lib config test spec features) \ | ||
| # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")} | ||
|
|
||
| ## Note: if you are using the `directories` clause above and you are not | ||
| ## watching the project directory ('.'), then you will want to move | ||
| ## the Guardfile to a watched dir and symlink it back, e.g. | ||
| # | ||
| # $ mkdir config | ||
| # $ mv Guardfile config/ | ||
| # $ ln -s config/Guardfile . | ||
| # | ||
| # and, you'll have to watch "config/Guardfile" instead of "Guardfile" | ||
|
|
||
| interactor :off | ||
|
|
||
| # Add files and commands to this file, like the example: | ||
| # watch(%r{file/path}) { `command(s)` } | ||
| # | ||
| guard :shell do | ||
| watch(/(lib|tests\/)(.*)\.rb$/) do |m| | ||
| file = m[0] | ||
| puts "File changed: #{file}" | ||
| test_file = file[/_test\.rb/] ? file : file.sub(/\.rb/, '_test.rb') | ||
| commands = | ||
| "bundle exec rake test", | ||
| "bundle exec rubocop -D #{file}" | ||
| generator = | ||
| "bin/generate isogram", | ||
| "git --no-pager diff exercises/isogram/isogram_test.rb" | ||
| command = commands.join(' && ') | ||
| p command | ||
| system(command) | ||
| system(generator.join(' && ')) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| require 'rake' | ||
| require 'rake/testtask' | ||
|
|
||
| Rake::TestTask.new do |task| | ||
| task.pattern = 'tests/*_test.rb' | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 1 | ||
| 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| class ExerciseTestCase | ||
| attr_reader :canonical_data | ||
|
|
||
| def initialize(canonical_data) | ||
| @canonical_data = OpenStruct.new(canonical_data) | ||
| end | ||
|
|
||
| def render(index = -1) | ||
| @index = index | ||
| [comment, *full_method].compact.join("\n") + "\n" | ||
| end | ||
|
|
||
| def full_method | ||
| indent( [ | ||
| method_definition, | ||
| *method_body, | ||
| method_end | ||
| ], 2) | ||
| end | ||
|
|
||
| def comment | ||
| nil | ||
| end | ||
|
|
||
| def description | ||
| canonical_data.description || '' | ||
| end | ||
|
|
||
| def method_definition | ||
| "def #{test_name}" | ||
| end | ||
|
|
||
| def method_body | ||
| indent( [skip, *workload], 2) | ||
| end | ||
|
|
||
| def method_end | ||
| "end" | ||
| end | ||
|
|
||
| def skip | ||
| @index.zero? ? '# skip' : 'skip' | ||
| end | ||
|
|
||
| def test_name | ||
| "test_#{description_as_test_name}" | ||
| end | ||
|
|
||
| def description_as_test_name | ||
| description.downcase.tr_s(' -','_') | ||
| end | ||
|
|
||
| def workload | ||
| "assert #{@canonical_data.expected.inspect}, Subject.method #{@canonical_data.input.inspect}" | ||
| end | ||
|
|
||
| def indent(lines, count) | ||
| lines.compact.map { |line| ' ' * count + line } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| class ExerciseTestCases | ||
| attr_reader :cases_key | ||
| def initialize(json_data) | ||
| @data = json_data | ||
| @cases_key = 'cases' | ||
| end | ||
|
|
||
| def case_classname | ||
| classname = self.class.to_s.sub(/Cases$/,'Case') | ||
| Object.const_get(classname) | ||
| end | ||
|
|
||
| def parsed_json_cases | ||
| JSON.parse(@data)[cases_key] | ||
| end | ||
|
|
||
| def to_a | ||
| parsed_json_cases.map { |test_case| case_classname.new(test_case) } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,26 @@ | ||
| class IsogramCase < OpenStruct | ||
| require_relative 'exercise_testcases' | ||
| require_relative 'exercise_testcase' | ||
|
|
||
| def name | ||
| format('test_%s', description) | ||
| end | ||
| class IsogramCases < ExerciseTestCases | ||
| end | ||
|
|
||
| def description | ||
| input.downcase.gsub(/[ -]/,'_') | ||
| class IsogramCase < ExerciseTestCase | ||
| def workload | ||
| [ | ||
| "string = '#{canonical_data.input}'", | ||
| "#{assertion} Isogram.isogram?(string), '#{failure_message}'" | ||
| ] | ||
| end | ||
|
|
||
| def assertion | ||
| expected ? 'assert' : 'refute' | ||
| def failure_message | ||
| "#{canonical_data.input.inspect} #{is_or_isnt} an isogram" | ||
| end | ||
|
|
||
| def skip | ||
| index.zero? ? '# skip' : 'skip' | ||
| def is_or_isnt | ||
| canonical_data.expected ? 'is' : 'is NOT' | ||
| end | ||
| end | ||
|
|
||
| IsogramCases = proc do |data| | ||
| JSON.parse(data)['cases'].map.with_index do |row, i| | ||
| IsogramCase.new(row.merge('index' => i)) | ||
| def assertion | ||
| canonical_data.expected ? 'assert' : 'refute' | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we insert the bookkeeping test case into the cases array to always generate it at the end of the view? This would allows us to render it just like all the other test cases, since it is a ExerciseTestCase