-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Exercise generator * spec and crystal tool format * flip name logic
- Loading branch information
1 parent
154ed07
commit b38194d
Showing
11 changed files
with
177 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -16,3 +16,4 @@ bin/configlet | |
bin/configlet.exe | ||
CHECKLIST | ||
tmp/ | ||
bin/generate |
This file contains 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 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 |
---|---|---|
|
@@ -118,7 +118,8 @@ | |
"ignored": [ | ||
"bin", | ||
"docs", | ||
"img" | ||
"img", | ||
"src" | ||
], | ||
"foregone": [ | ||
] | ||
|
This file contains 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 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 @@ | ||
require "ecr" | ||
require "json" | ||
|
||
abstract class ExerciseGenerator | ||
METADATA_REPOSITORY = "x-common" | ||
|
||
def self.generate | ||
new.generate | ||
end | ||
|
||
def generate | ||
File.write(test_file, to_s) | ||
end | ||
|
||
def describe_name | ||
exercise_name.split('-').map(&.capitalize).join | ||
end | ||
|
||
abstract def exercise_name : String | ||
abstract def test_cases : Array(ExerciseTestCase) | ||
|
||
private def root | ||
File.expand_path(File.join("..", "..", "..", ".."), __FILE__) | ||
end | ||
|
||
private def test_file | ||
File.expand_path(File.join("exercises", exercise_name, "spec", "#{exercise_name.tr("-", "_")}_spec.cr"), root) | ||
end | ||
|
||
private def metadata_dir | ||
File.expand_path(File.join("..", METADATA_REPOSITORY, "exercises", exercise_name), root) | ||
end | ||
|
||
private def data | ||
File.read(File.join(metadata_dir, "canonical-data.json")) | ||
end | ||
|
||
ECR.def_to_s "#{__DIR__}/templates/example.tt" | ||
end |
This file contains 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,8 @@ | ||
abstract class ExerciseTestCase | ||
abstract def workload | ||
abstract def test_name | ||
|
||
def pending?(index) | ||
index == 0 ? "it" : "pending" | ||
end | ||
end |
This file contains 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,34 @@ | ||
require "./exercise_generator" | ||
require "./exercise_test_case" | ||
|
||
class HelloWorldGenerator < ExerciseGenerator | ||
def exercise_name | ||
"hello-world" | ||
end | ||
|
||
def test_cases | ||
JSON.parse(data)["cases"].map do |test_case| | ||
HelloWorldTestCase.from_json(test_case.to_json) | ||
end | ||
end | ||
end | ||
|
||
class HelloWorldTestCase < ExerciseTestCase | ||
JSON.mapping( | ||
description: String, | ||
name: String | Nil, | ||
expected: String | ||
) | ||
|
||
def workload | ||
if name | ||
"HelloWorld.hello(\"#{name}\").should eq(\"#{expected}\")" | ||
else | ||
"HelloWorld.hello.should eq(\"#{expected}\")" | ||
end | ||
end | ||
|
||
def test_name | ||
description | ||
end | ||
end |
This file contains 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,10 @@ | ||
require "spec" | ||
require "../src/*" | ||
|
||
describe <%= "#{describe_name.inspect} do" %> | ||
<% test_cases.each_with_index do |test_case, index| %> | ||
<%= test_case.pending?(index) %> "tests <%= test_case.test_name %>" do | ||
<%= test_case.workload %> | ||
end | ||
<% end -%> | ||
end |
This file contains 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,15 @@ | ||
require "./exercises/*" | ||
|
||
if ARGV.empty? | ||
STDERR.puts "Exercise name required!\n" | ||
exit | ||
end | ||
|
||
exercise = ARGV[0] | ||
|
||
klass = {{ExerciseGenerator.subclasses}}.find do |generator| | ||
generator.to_s == "#{exercise.split('-').map(&.capitalize).join}Generator" | ||
end | ||
|
||
raise "Undefined Generator" unless klass | ||
klass.generate |
This file contains 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,27 @@ | ||
require "spec" | ||
require "../exercises/exercise_generator" | ||
require "../exercises/exercise_test_case" | ||
|
||
class DummyGenerator < ExerciseGenerator | ||
def exercise_name | ||
"dummy" | ||
end | ||
|
||
def test_cases | ||
[] of ExerciseTestCase | ||
end | ||
end | ||
|
||
describe "ExerciseGenerator" do | ||
describe "#describe_name" do | ||
it "will return the name of the exercise in camel case" do | ||
DummyGenerator.new.describe_name.should eq("Dummy") | ||
end | ||
end | ||
|
||
describe "#to_s" do | ||
it "will output the generator test file using the example.tt" do | ||
DummyGenerator.new.to_s.should eq("require \"spec\"\nrequire \"../src/*\"\n\ndescribe \"Dummy\" do\nend\n") | ||
end | ||
end | ||
end |
This file contains 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,27 @@ | ||
require "spec" | ||
require "json" | ||
require "../exercises/exercise_test_case" | ||
|
||
class DummyTestCase < ExerciseTestCase | ||
JSON.mapping( | ||
description: String | ||
) | ||
|
||
def workload; end | ||
|
||
def test_name; end | ||
end | ||
|
||
describe "DummyTestCase" do | ||
describe "#pending" do | ||
it "outputs 'it' if the given integer is 0" do | ||
dummy_test_case = DummyTestCase.from_json("{\"description\": \"hello\"}") | ||
dummy_test_case.pending?(0).should eq("it") | ||
end | ||
|
||
it "outputs 'pending' if the given integer is greater than 0" do | ||
dummy_test_case = DummyTestCase.from_json("{\"description\": \"hello\"}") | ||
dummy_test_case.pending?(1).should eq("pending") | ||
end | ||
end | ||
end |