Skip to content

Commit

Permalink
Merge pull request #581 from hilary/move_cases_to_meta
Browse files Browse the repository at this point in the history
generator: move cases to .meta/generator/
  • Loading branch information
Insti authored Apr 30, 2017
2 parents 0fa9171 + b319edc commit db45c0c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<exercise_name>_cases.rb`. Then you can regenerate the exercise with
`bin/generate <exercise_name>`.
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 <exercise_name>`.

#### 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/<exercise_name>_cases.rb`.
An exercise's test case generator class produces the code that goes inside
the minitest `test_<whatever>` methods. An exercise's generator lives in
`exercises/<exercise_name>/.meta/generator/<exercise_name>_cases.rb`, although
some generators have not yet been moved and are in `lib/<exercise_name>_cases.rb`.

`lib/<exercise_name>_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 <ExerciseName>Case < ExerciseCase
Expand Down
22 changes: 18 additions & 4 deletions lib/generator/files/generator_cases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
File renamed without changes.
26 changes: 25 additions & 1 deletion test/generator/files/generate_cases_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit db45c0c

Please sign in to comment.