Skip to content

Commit

Permalink
Drop options argument in group/kata create methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JonJagger committed Aug 15, 2021
1 parent aa48884 commit bf01b98
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 94 deletions.
22 changes: 10 additions & 12 deletions app/source/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ def initialize(externals)

def group_create_custom(version:, display_name:)
manifest = build_custom_manifest(version, display_name)
group(version).create(manifest, {})
group(version).create(manifest)
end

# :nocov:
def group_create2(version:, ltf_name:, exercise_name:)
manifest = build_manifest(version, ltf_name, exercise_name)
group(version).create(manifest, {})
group(version).create(manifest)
end
# :nocov:

def group_create(version:, ltf_name:, exercise_name:)
manifest = build_manifest(version, ltf_name, exercise_name)
group(version).create(manifest, {})
group(version).create(manifest)
end

def group_exists?(id:)
Expand Down Expand Up @@ -67,17 +69,19 @@ def group_fork(id:, index:)

def kata_create_custom(version:, display_name:)
manifest = build_custom_manifest(version, display_name)
kata(version).create(manifest, {})
kata(version).create(manifest)
end

# :nocov:
def kata_create2(version:, ltf_name:, exercise_name:)
manifest = build_manifest(version, ltf_name, exercise_name)
kata(version).create(manifest, {})
kata(version).create(manifest)
end
# :nocov:

def kata_create(version:, ltf_name:, exercise_name:)
manifest = build_manifest(version, ltf_name, exercise_name)
kata(version).create(manifest, {})
kata(version).create(manifest)
end

def kata_exists?(id:)
Expand Down Expand Up @@ -174,12 +178,6 @@ def from_path(path)
manifest['version'].to_i # nil.to_i == 0
end

def from_manifest(manifest)
# All newly created groups and katas use the current version.
# Allow creation from previous versions for tests.
(manifest['version'] || CURRENT_VERSION).to_i
end

def id?(id)
IdGenerator::id?(id)
end
Expand Down
2 changes: 1 addition & 1 deletion app/source/model/fork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def fork(forker, id, index)
# but leave no trace of that in the manifest.
m.delete('group_id')
m.delete('group_index')
forker.new(@externals).create(m, default_options)
forker.new(@externals).create(m)
end

end
8 changes: 4 additions & 4 deletions app/source/model/group_v0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require_relative 'id_pather'
require_relative 'kata_v0'
require_relative 'liner_v0'
require_relative 'options'
require_relative 'poly_filler'
require_relative '../lib/json_adapter'

Expand All @@ -14,9 +15,7 @@ def initialize(externals)

# - - - - - - - - - - - - - - - - - - - - - -

def create(manifest, _options)
# Groups created in cyber-dojo are now always version 1.
# The ability to create version 0 groups is retained for testing.
def create(manifest)
manifest = manifest.clone
manifest['version'] = 0
manifest['created'] = time.now
Expand Down Expand Up @@ -48,7 +47,7 @@ def join(id, indexes)
else
index = indexes[result_index]
manifest['group_index'] = index
kata_id = @kata.create(manifest, {})
kata_id = @kata.create(manifest)
disk.assert(disk.file_create_command(kata_id_filename(id, index), kata_id))
kata_id
end
Expand Down Expand Up @@ -79,6 +78,7 @@ def joined(id)
include IdPather
include JsonAdapter
include Liner_v0
include Options
include PolyFiller

# - - - - - - - - - - - - - - - - - - -
Expand Down
7 changes: 3 additions & 4 deletions app/source/model/group_v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ def initialize(externals)

# - - - - - - - - - - - - - - - - - - - - - -

def create(manifest, options)
fail_unless_known_options(options)
manifest.merge!(options)
def create(manifest)
manifest.merge!(default_options)
manifest['version'] = 1
manifest['created'] = time.now
id = manifest['id'] = IdGenerator.new(@externals).group_id
Expand Down Expand Up @@ -57,7 +56,7 @@ def join(id, indexes)
else
index = indexes[result_index]
manifest['group_index'] = index
kata_id = @kata.create(manifest, {})
kata_id = @kata.create(manifest)
disk.assert(katas_append_command(id, "#{kata_id} #{index}\n"))
kata_id
end
Expand Down
7 changes: 3 additions & 4 deletions app/source/model/group_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ def initialize(externals)

# - - - - - - - - - - - - - - - - - - - - - -

def create(manifest, options)
fail_unless_known_options(options)
manifest.merge!(options)
def create(manifest)
manifest.merge!(default_options)
manifest['version'] = 2
manifest['created'] = time.now
id = manifest['id'] = IdGenerator.new(@externals).group_id
Expand Down Expand Up @@ -57,7 +56,7 @@ def join(id, indexes)
else
index = indexes[result_index]
manifest['group_index'] = index
kata_id = @kata.create(manifest, {})
kata_id = @kata.create(manifest)
disk.assert(katas_append_command(id, "#{kata_id} #{index}\n"))
kata_id
end
Expand Down
5 changes: 2 additions & 3 deletions app/source/model/kata_v0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ def initialize(externals)

# - - - - - - - - - - - - - - - - - - - - - -

def create(manifest, options)
def create(manifest)
manifest = manifest.clone
fail_unless_known_options(options)
manifest.merge!(options)
manifest.merge!(default_options)
manifest['version'] = 0
manifest['created'] = time.now
id = manifest['id'] = IdGenerator.new(@externals).kata_id
Expand Down
5 changes: 2 additions & 3 deletions app/source/model/kata_v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ def initialize(externals)

# - - - - - - - - - - - - - - - - - - - - - -

def create(manifest, options)
fail_unless_known_options(options)
manifest.merge!(options)
def create(manifest)
manifest.merge!(default_options)
manifest['version'] = 1
manifest['created'] = time.now
id = manifest['id'] = IdGenerator.new(@externals).kata_id
Expand Down
6 changes: 2 additions & 4 deletions app/source/model/kata_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def initialize(externals)

# - - - - - - - - - - - - - - - - - - - - - -

def create(manifest, options)
fail_unless_known_options(options)
def create(manifest)
manifest['version'] = 2
manifest['created'] = time.now
events = [{
Expand All @@ -32,13 +31,12 @@ def create(manifest, options)
'time' => manifest['created']
}]
files = manifest.delete('visible_files')
options = default_options.merge(options)

# IdGenerator makes the kata dir, eg /cyber-dojo/katas/Rl/mR/cV
id = manifest['id'] = IdGenerator.new(@externals).kata_id
disk.assert_all([
disk.file_create_command(manifest_filename(id), json_pretty(manifest)),
disk.file_create_command(options_filename(id), json_pretty(options)),
disk.file_create_command(options_filename(id), json_pretty(default_options)),
disk.file_create_command(events_filename(id), json_pretty(events)),
disk.file_create_command(readme_filename(id), readme(manifest))
])
Expand Down
30 changes: 0 additions & 30 deletions app/source/model/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,6 @@ def fail_unless_known_option(name)
end
end

def fail_unless_known_options(options)
unless options.is_a?(Hash)
fail "options is not a Hash"
end
options.each do |key,value|
unless known_option_key?(key)
fail "options:{#{quoted(key)}: #{value}} unknown key: #{quoted(key)}"
end
unless known_option_value?(key, value)
fail "options:{#{quoted(key)}: #{value}} unknown value: #{value}"
end
end
end

def known_option_key?(key)
key.is_a?(String) && KNOWN_KEYS.include?(key)
end

def known_option_value?(key, value)
if key === "theme"
["dark","light"].include?(value)
else
["on","off"].include?(value)
end
end

def quoted(s)
'"' + s + '"'
end

KNOWN_KEYS = [
"theme",
"fork_button",
Expand Down
3 changes: 3 additions & 0 deletions app/source/model/poly_filler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def polyfill_manifest_defaults(manifest)
manifest['tab_size'] ||= 4
manifest['max_seconds'] ||= 10
manifest['progress_regexs'] ||= []
default_options.each do |key,value|
manifest[key] ||= value
end
end

# - - - - - - - - - - - - - - - - - - - - - -
Expand Down
6 changes: 3 additions & 3 deletions app/test/config/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

test: {
lines: {
total:1929,
total:1838,
missed:0,
},
branches: {
Expand All @@ -24,11 +24,11 @@

code: {
lines: {
total:1398,
total:1371,
missed:0,
},
branches: {
total:149,
total:141,
missed:0,
}
}
Expand Down
24 changes: 0 additions & 24 deletions app/test/data/kata_test_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,6 @@ def kata_event_k5ZTk0_3
}
end

=begin
def manifest_Tennis_refactoring_Python_unitttest
{
"display_name" => "Tennis refactoring, Python unitttest",
"filename_extension" => [".py"],
"image_name" => "cyberdojofoundation/python_unittest:b8333d3",
"visible_files" => {
"cyber-dojo.sh" => {
"content" => "python -m unittest *test*.py\n"
},
"readme.txt" => {
"content" => "Tennis has a rather quirky scoring system, and to newcomers it \ncan be a little difficult to keep track of. The local tennis club\nhas some code that is currently being used to update the scoreboard\nwhen a player scores a point. They has recently acquired two smaller\ntennis clubs, and they two each have a similar piece of code.\n \nYou have just been employed by the tennis club, and your job \nis to refactor all three codebases until you are happy to\nwork with any of them. The future is uncertain, new features may\nbe needed, and you want to be thoroughly on top of your game when\nthat happens.\n \nSummary of Tennis scoring:\n1. A game is won by the first player to have won at least four points \n in total and at least two points more than the opponent.\n2. The running score of each game is described in a manner peculiar \n to tennis: scores from zero to three points are described as “love”, \n “fifteen”, “thirty”, and “forty” respectively.\n3. If at least three points have been scored by each player, and the \n scores are equal, the score is “deuce”.\n4. If at least three points have been scored by each side and a player\n has one more point than his opponent, the score of the game is\n “advantage” for the player in the lead."
},
"tennis.py" => {
"content" => "# -*- coding: utf-8 -*-\n\nclass TennisGame1:\n\n def __init__(self, player1Name, player2Name):\n self.player1Name = player1Name\n self.player2Name = player2Name\n self.p1points = 0\n self.p2points = 0\n \n def won_point(self, playerName):\n if playerName == self.player1Name:\n self.p1points += 1\n else:\n self.p2points += 1\n \n def score(self):\n result = \"\"\n tempScore=0\n if (self.p1points==self.p2points):\n result = {\n 0 : \"Love-All\",\n 1 : \"Fifteen-All\",\n 2 : \"Thirty-All\",\n }.get(self.p1points, \"Deuce\")\n elif (self.p1points>=4 or self.p2points>=4):\n minusResult = self.p1points-self.p2points\n if (minusResult==1):\n result =\"Advantage \" + self.player1Name\n elif (minusResult ==-1):\n result =\"Advantage \" + self.player2Name\n elif (minusResult>=2):\n result = \"Win for \" + self.player1Name\n else:\n result =\"Win for \" + self.player2Name\n else:\n for i in range(1,3):\n if (i==1):\n tempScore = self.p1points\n else:\n result+=\"-\"\n tempScore = self.p2points\n result += {\n 0 : \"Love\",\n 1 : \"Fifteen\",\n 2 : \"Thirty\",\n 3 : \"Forty\",\n }[tempScore]\n return result\n\n\nclass TennisGame2:\n def __init__(self, player1Name, player2Name):\n self.player1Name = player1Name\n self.player2Name = player2Name\n self.p1points = 0\n self.p2points = 0\n \n def won_point(self, playerName):\n if playerName == self.player1Name:\n self.P1Score()\n else:\n self.P2Score()\n \n def score(self):\n result = \"\"\n if (self.p1points == self.p2points and self.p1points < 3):\n if (self.p1points==0):\n result = \"Love\"\n if (self.p1points==1):\n result = \"Fifteen\"\n if (self.p1points==2):\n result = \"Thirty\"\n result += \"-All\"\n if (self.p1points==self.p2points and self.p1points>2):\n result = \"Deuce\"\n \n P1res = \"\"\n P2res = \"\"\n if (self.p1points > 0 and self.p2points==0):\n if (self.p1points==1):\n P1res = \"Fifteen\"\n if (self.p1points==2):\n P1res = \"Thirty\"\n if (self.p1points==3):\n P1res = \"Forty\"\n \n P2res = \"Love\"\n result = P1res + \"-\" + P2res\n if (self.p2points > 0 and self.p1points==0):\n if (self.p2points==1):\n P2res = \"Fifteen\"\n if (self.p2points==2):\n P2res = \"Thirty\"\n if (self.p2points==3):\n P2res = \"Forty\"\n \n P1res = \"Love\"\n result = P1res + \"-\" + P2res\n \n \n if (self.p1points>self.p2points and self.p1points < 4):\n if (self.p1points==2):\n P1res=\"Thirty\"\n if (self.p1points==3):\n P1res=\"Forty\"\n if (self.p2points==1):\n P2res=\"Fifteen\"\n if (self.p2points==2):\n P2res=\"Thirty\"\n result = P1res + \"-\" + P2res\n if (self.p2points>self.p1points and self.p2points < 4):\n if (self.p2points==2):\n P2res=\"Thirty\"\n if (self.p2points==3):\n P2res=\"Forty\"\n if (self.p1points==1):\n P1res=\"Fifteen\"\n if (self.p1points==2):\n P1res=\"Thirty\"\n result = P1res + \"-\" + P2res\n \n if (self.p1points > self.p2points and self.p2points >= 3):\n result = \"Advantage \" + self.player1Name\n \n if (self.p2points > self.p1points and self.p1points >= 3):\n result = \"Advantage \" + self.player2Name\n \n if (self.p1points>=4 and self.p2points>=0 and (self.p1points-self.p2points)>=2):\n result = \"Win for \" + self.player1Name\n if (self.p2points>=4 and self.p1points>=0 and (self.p2points-self.p1points)>=2):\n result = \"Win for \" + self.player2Name\n return result\n \n def SetP1Score(self, number):\n for i in range(number):\n self.P1Score()\n \n def SetP2Score(self, number):\n for i in range(number):\n self.P2Score()\n \n def P1Score(self):\n self.p1points +=1\n \n \n def P2Score(self):\n self.p2points +=1\n \nclass TennisGame3:\n def __init__(self, player1Name, player2Name):\n self.p1N = player1Name\n self.p2N = player2Name\n self.p1 = 0\n self.p2 = 0\n \n def won_point(self, n):\n if n == self.p1N:\n self.p1 += 1\n else:\n self.p2 += 1\n \n def score(self):\n if (self.p1 < 4 and self.p2 < 4) and (self.p1 + self.p2 < 6):\n p = [\"Love\", \"Fifteen\", \"Thirty\", \"Forty\"]\n s = p[self.p1]\n return s + \"-All\" if (self.p1 == self.p2) else s + \"-\" + p[self.p2]\n else:\n if (self.p1 == self.p2):\n return \"Deuce\"\n s = self.p1N if self.p1 > self.p2 else self.p2N\n return \"Advantage \" + s if ((self.p1-self.p2)*(self.p1-self.p2) == 1) else \"Win for \" + s\n"
},
"tennis_unit_test.py" => {
"content" => "# -*- coding: utf-8 -*-\n\nimport unittest\n\nfrom tennis import TennisGame1, TennisGame2, TennisGame3\n\ntest_cases = [\n (0, 0, \"Love-All\", 'player1', 'player2'),\n (1, 1, \"Fifteen-All\", 'player1', 'player2'),\n (2, 2, \"Thirty-All\", 'player1', 'player2'),\n (3, 3, \"Deuce\", 'player1', 'player2'),\n (4, 4, \"Deuce\", 'player1', 'player2'),\n\n (1, 0, \"Fifteen-Love\", 'player1', 'player2'),\n (0, 1, \"Love-Fifteen\", 'player1', 'player2'),\n (2, 0, \"Thirty-Love\", 'player1', 'player2'),\n (0, 2, \"Love-Thirty\", 'player1', 'player2'),\n (3, 0, \"Forty-Love\", 'player1', 'player2'),\n (0, 3, \"Love-Forty\", 'player1', 'player2'),\n (4, 0, \"Win for player1\", 'player1', 'player2'),\n (0, 4, \"Win for player2\", 'player1', 'player2'),\n\n (2, 1, \"Thirty-Fifteen\", 'player1', 'player2'),\n (1, 2, \"Fifteen-Thirty\", 'player1', 'player2'),\n (3, 1, \"Forty-Fifteen\", 'player1', 'player2'),\n (1, 3, \"Fifteen-Forty\", 'player1', 'player2'),\n (4, 1, \"Win for player1\", 'player1', 'player2'),\n (1, 4, \"Win for player2\", 'player1', 'player2'),\n\n (3, 2, \"Forty-Thirty\", 'player1', 'player2'),\n (2, 3, \"Thirty-Forty\", 'player1', 'player2'),\n (4, 2, \"Win for player1\", 'player1', 'player2'),\n (2, 4, \"Win for player2\", 'player1', 'player2'),\n\n (4, 3, \"Advantage player1\", 'player1', 'player2'),\n (3, 4, \"Advantage player2\", 'player1', 'player2'),\n (5, 4, \"Advantage player1\", 'player1', 'player2'),\n (4, 5, \"Advantage player2\", 'player1', 'player2'),\n (15, 14, \"Advantage player1\", 'player1', 'player2'),\n (14, 15, \"Advantage player2\", 'player1', 'player2'),\n\n (6, 4, 'Win for player1', 'player1', 'player2'), \n (4, 6, 'Win for player2', 'player1', 'player2'), \n (16, 14, 'Win for player1', 'player1', 'player2'), \n (14, 16, 'Win for player2', 'player1', 'player2'), \n\n (6, 4, 'Win for One', 'One', 'player2'),\n (4, 6, 'Win for Two', 'player1', 'Two'), \n (6, 5, 'Advantage One', 'One', 'player2'),\n (5, 6, 'Advantage Two', 'player1', 'Two'), \n \n ]\n\ndef play_game(TennisGame, p1Points, p2Points, p1Name, p2Name):\n game = TennisGame(p1Name, p2Name)\n for i in range(max(p1Points, p2Points)):\n if i < p1Points:\n game.won_point(p1Name)\n if i < p2Points:\n game.won_point(p2Name)\n return game\n\nclass TestTennis(unittest.TestCase):\n \n def test_Score_Game1(self):\n for testcase in test_cases:\n (p1Points, p2Points, score, p1Name, p2Name) = testcase\n game = play_game(TennisGame1, p1Points, p2Points, p1Name, p2Name)\n self.assertEqual(score, game.score())\n\n def test_Score_Game2(self):\n for testcase in test_cases:\n (p1Points, p2Points, score, p1Name, p2Name) = testcase\n game = play_game(TennisGame2, p1Points, p2Points, p1Name, p2Name)\n self.assertEqual(score, game.score())\n\n def test_Score_Game3(self):\n for testcase in test_cases:\n (p1Points, p2Points, score, p1Name, p2Name) = testcase\n game = play_game(TennisGame3, p1Points, p2Points, p1Name, p2Name)\n self.assertEqual(score, game.score())\n \nif __name__ == \"__main__\":\n unittest.main() \n "
}
}
}.clone
end
=end

=begin
def print_cmp(expected, actual)
actual.each do |key,value|
Expand Down
2 changes: 1 addition & 1 deletion app/test/group_fork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def self.id58_prefix
end

forked_manifest = group_manifest(fid)
%w( id created theme colour predict revert_red revert_amber revert_green ).each do |key|
%w( id created ).each do |key|
forked_manifest.delete(key)
end
forked_files = forked_manifest.delete('visible_files')
Expand Down
8 changes: 7 additions & 1 deletion app/test/kata_manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ def self.id58_prefix
'id' => id,
'max_seconds' => 10,
'progress_regexs' => [],
'tab_size' => 4
'tab_size' => 4,
'theme' => "light",
'colour' => "on",
'predict' => "off",
'revert_red' => "off",
'revert_amber' => "off",
'revert_green' => "off"
}
assert_equal expected, actual
end
Expand Down

0 comments on commit bf01b98

Please sign in to comment.