diff --git a/README.md b/README.md index f4e2d040f2..4f9f093489 100644 --- a/README.md +++ b/README.md @@ -105,21 +105,26 @@ the exercise, which lives in the x-common repository. └── metadata.yml ``` -This change will need to be submitted as a pull request to the x-common repository. This pull -request needs to be merged before you can regenerate the exercise. +This change will need to be submitted as a pull request to the x-common +repository. This pull request needs to be merged before you can regenerate the +exercise. Changes that don't have to do directly with the test inputs and outputs should -be made to `lib/_cases.rb`. Then you can regenerate the exercise with -`bin/generate `. +be made to the exercise's test case generator, discussed +in [implementing a new generator](#implementing-a-generator), next. Then you +can regenerate the exercise with `bin/generate `. #### Implementing a Generator -You will need to write code to produce the code that goes inside the test methods. Your -code will live in `lib/_cases.rb`. +An exercise's test case generator class produces the code that goes inside +the minitest `test_` methods. An exercise's generator lives in +`exercises//.meta/generator/_cases.rb`, although +some generators have not yet been moved and are in `lib/_cases.rb`. -`lib/_cases.rb` contains a derived class of `ExerciseCase` (in -`lib/generator/exercise_cases.rb`) which wraps the JSON for a single test -case. The default version looks something like this: +The test case generator is a derived class of `ExerciseCase` (in +`lib/generator/exercise_cases.rb`). `ExerciseCase` does most of the work of +extracting the canonical data. The derived class wraps the JSON for a single +test case. The default version looks something like this: ``` class Case < ExerciseCase diff --git a/lib/generator/files/generator_cases.rb b/lib/generator/files/generator_cases.rb index 75a5499d8a..3271112d68 100644 --- a/lib/generator/files/generator_cases.rb +++ b/lib/generator/files/generator_cases.rb @@ -4,8 +4,8 @@ module GeneratorCases module_function def available(track_path) - generator_glob = File.join(track_path, 'lib', '*_cases.rb') - Dir[generator_glob].sort.map { |filename| exercise_name(filename) } + generator_glob = File.join(track_path, "{lib,exercises}", '**', '*_cases.rb') + Dir.glob(generator_glob, File::FNM_DOTMATCH).sort.map { |filename| exercise_name(filename) } end def filename(exercise_name) @@ -20,9 +20,23 @@ def exercise_name(filename) %r{([^/]*)_cases\.rb$}.match(filename).captures[0].tr('_', '-') end + # temporary, when we've moved everything, move contents of cases_filename + # to this method def load_filename(track_path, exercise_name) - path = File.join(track_path, 'lib') - "%s/%s.rb" % [ path, filename(exercise_name) ] + File.exist?(cases_filename(track_path, exercise_name)) ? + cases_filename(track_path, exercise_name) : + legacy_cases_filename(track_path, exercise_name) + end + + # this becomes load_filename + def cases_filename(track_path, exercise_name) + "%s.rb" % File.join(track_path, 'exercises', exercise_name, '.meta', 'generator', + filename(exercise_name)) + end + + # this goes away + def legacy_cases_filename(track_path, exercise_name) + "%s.rb" % File.join(track_path, 'lib', filename(exercise_name)) end end end diff --git a/test/fixtures/xruby/lib/beta_cases.rb b/test/fixtures/xruby/exercises/beta/.meta/generator/beta_cases.rb similarity index 100% rename from test/fixtures/xruby/lib/beta_cases.rb rename to test/fixtures/xruby/exercises/beta/.meta/generator/beta_cases.rb diff --git a/test/generator/files/generate_cases_test.rb b/test/generator/files/generate_cases_test.rb index 097f1498ef..b6ff10fc33 100644 --- a/test/generator/files/generate_cases_test.rb +++ b/test/generator/files/generate_cases_test.rb @@ -15,7 +15,7 @@ def test_cases_found def test_available_returns_exercise_names track_path = 'test/fixtures/xruby' - Dir.stub :[], %w(/alpha_cases.rb hy_phen_ated_cases.rb) do + Dir.stub :glob, %w(/alpha_cases.rb hy_phen_ated_cases.rb) do assert_equal %w(alpha hy-phen-ated), GeneratorCases.available(track_path) end end @@ -28,6 +28,30 @@ def test_filename def test_class_name assert_equal 'TwoParterCase', GeneratorCases.class_name('two-parter') end + + def test_load_filename # test the cases_filename alternative + track_path = 'test/fixtures/xruby' + exercise_name = 'foo' + expected_filename = track_path + '/exercises/foo/.meta/generator/foo_cases.rb' + File.stub(:exist?, true) do + assert_equal( + expected_filename, + GeneratorCases.load_filename(track_path, exercise_name) + ) + end + end + + def test_load_legacy_filename # test the cases_filename alternative + track_path = 'test/fixtures/xruby' + exercise_name = 'foo' + expected_filename = track_path + '/lib/foo_cases.rb' + File.stub(:exist?, false) do + assert_equal( + expected_filename, + GeneratorCases.load_filename(track_path, exercise_name) + ) + end + end end end end