ankit 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/bin/ankit +6 -0
  2. data/lib/ankit/add_command.rb +28 -0
  3. data/lib/ankit/card.rb +66 -0
  4. data/lib/ankit/card_happening_command.rb +31 -0
  5. data/lib/ankit/challenge_command.rb +444 -0
  6. data/lib/ankit/coming_command.rb +90 -0
  7. data/lib/ankit/command.rb +40 -0
  8. data/lib/ankit/event.rb +62 -0
  9. data/lib/ankit/event_traversing_command.rb +37 -0
  10. data/lib/ankit/fail_command.rb +14 -0
  11. data/lib/ankit/find_command.rb +38 -0
  12. data/lib/ankit/hello_command.rb +22 -0
  13. data/lib/ankit/list_command.rb +26 -0
  14. data/lib/ankit/name_command.rb +16 -0
  15. data/lib/ankit/pass_command.rb +9 -0
  16. data/lib/ankit/round_command.rb +31 -0
  17. data/lib/ankit/runtime.rb +151 -0
  18. data/lib/ankit/score_command.rb +24 -0
  19. data/lib/ankit/text_reading_command.rb +23 -0
  20. data/lib/ankit.rb +3 -0
  21. data/test/card_test.rb +92 -0
  22. data/test/command_test.rb +406 -0
  23. data/test/data/bye_card.card +2 -0
  24. data/test/data/hello_card.card +2 -0
  25. data/test/data/hello_config.rb +4 -0
  26. data/test/data/hello_repo/anemone.journal +2 -0
  27. data/test/data/hello_repo/baobab.journal +2 -0
  28. data/test/data/hello_repo/cards/foo/hello.card +2 -0
  29. data/test/data/hello_repo/cards/foo/this_is_not_a_card.txt +1 -0
  30. data/test/data/hello_repo/cards/foo/vanilla-please.card +2 -0
  31. data/test/data/hope.card +1 -0
  32. data/test/data/luck.card +1 -0
  33. data/test/data/number_repo/anemone.journal +0 -0
  34. data/test/data/number_repo/cards/eight.card +2 -0
  35. data/test/data/number_repo/cards/five.card +2 -0
  36. data/test/data/number_repo/cards/four.card +2 -0
  37. data/test/data/number_repo/cards/one.card +2 -0
  38. data/test/data/number_repo/cards/seven.card +2 -0
  39. data/test/data/number_repo/cards/six.card +2 -0
  40. data/test/data/number_repo/cards/three.card +2 -0
  41. data/test/data/number_repo/cards/two.card +2 -0
  42. data/test/data/vanilla_repo/anemone.journal +0 -0
  43. data/test/event_test.rb +29 -0
  44. data/test/helpers.rb +54 -0
  45. data/test/progress_test.rb +99 -0
  46. data/test/runtime_test.rb +58 -0
  47. metadata +138 -0
@@ -0,0 +1,406 @@
1
+
2
+ require 'ankit/runtime'
3
+
4
+ require 'test/unit'
5
+ require 'fileutils'
6
+ require 'tmpdir'
7
+ require 'mocha'
8
+
9
+ class CommandTest < Test::Unit::TestCase
10
+ include Ankit
11
+
12
+ def test_available
13
+ assert(Command::COMMANDS.include?(HelloCommand))
14
+ end
15
+
16
+ def test_command_names
17
+ assert_equal(Command.by_name["hello"], HelloCommand)
18
+ end
19
+ end
20
+
21
+ class ListTest < Test::Unit::TestCase
22
+ include Ankit
23
+ include Ankit::TestHelper
24
+
25
+ def test_hello
26
+ lines = make_runtime.dispatch_then(["hello"]).printed_lines
27
+ a_subpath = File.join(HELLO_REPO, "cards", "foo")
28
+ assert(lines.include?("card_search_paths: #{a_subpath}"))
29
+ end
30
+
31
+ def test_list
32
+ lines = make_runtime.dispatch_then(["list"]).printed_lines
33
+ assert( lines.include?(File.join(HELLO_REPO, "cards", "foo", "hello.card")))
34
+ assert(!lines.include?(File.join(HELLO_REPO, "cards", "foo")))
35
+ not_a_card = File.join(HELLO_REPO, "cards", "foo", "this_is_not_a_card.txt")
36
+ assert( File.file?(not_a_card))
37
+ assert(!lines.include?(not_a_card))
38
+ end
39
+ end
40
+
41
+ class FindTest < Test::Unit::TestCase
42
+ include Ankit
43
+ include Ankit::TestHelper
44
+
45
+ def test_hello
46
+ assert_equal(make_runtime.dispatch_then(["find", "vanilla-please", "hello", "no-such-card"]).printed_lines,
47
+ [repo_data_at("cards/foo/vanilla-please.card"),
48
+ repo_data_at("cards/foo/hello.card")])
49
+ end
50
+ end
51
+
52
+ class NameTest < Test::Unit::TestCase
53
+ include Ankit
54
+ include Ankit::TestHelper
55
+
56
+ def test_bad_options
57
+ assert_raise(BadOptions) do
58
+ target = make_runtime
59
+ target.dispatch(["name", "--stdin", "hello"])
60
+ end
61
+
62
+ assert_raise(BadOptions) do
63
+ target = make_runtime
64
+ target.dispatch(["name"])
65
+ end
66
+ end
67
+
68
+ def test_stdin
69
+ target = make_runtime
70
+ target.stdin.string << "O: Hello!\n\n"
71
+ assert_equal(target.dispatch_then(["name", "--stdin"]).printed_line, "hello")
72
+ end
73
+
74
+ def test_hello
75
+ target = make_runtime
76
+ target.dispatch(["name", test_data_at("hello_card.card"), test_data_at("bye_card.card")])
77
+ assert_equal(target.printed_lines, ["hello-world", "bye-universe"])
78
+ end
79
+ end
80
+
81
+ class ScoreTest < Test::Unit::TestCase
82
+ include Ankit
83
+ include Ankit::TestHelper
84
+
85
+ def test_each_event
86
+ target = ScoreCommand.new(make_runtime, [])
87
+ all = target.to_enum(:each_event).to_a
88
+ assert_equal(all.size, 4)
89
+ assert_equal(all[0].class, Event)
90
+ assert_equal(target.to_enum(:each_event, "hello").to_a.size, 2)
91
+ end
92
+
93
+ def test_hello
94
+ target = make_runtime
95
+ target.dispatch(["score", repo_data_at("to_cards/foo/hello.card")])
96
+ assert_equal(2, target.printed_lines.size)
97
+ end
98
+
99
+ def test_find_dup
100
+ target = make_runtime
101
+ latest = EventTraversing.find_latest_event_for(target, repo_data_at("to_cards/foo/hello.card"))
102
+ assert_equal(latest.round, 6)
103
+ end
104
+ end
105
+
106
+
107
+ class RoundTest < Test::Unit::TestCase
108
+ include Ankit
109
+ include Ankit::TestHelper
110
+
111
+ def test_hello
112
+ assert_equal(make_runtime.dispatch_then(["round"]).printed_line, "6 9")
113
+ end
114
+
115
+ def test_vanilla
116
+ assert_equal(make_vanilla_runtime.dispatch_then(["round"]).printed_line, "0 0")
117
+ end
118
+ end
119
+
120
+ class AddTest < Test::Unit::TestCase
121
+ include Ankit
122
+ include Ankit::TestHelper
123
+
124
+ THE_TEXT = "O:There is no Frigate like a Book\n"
125
+ THE_NAME = "there-is-no-frigate-like-a-book.card"
126
+
127
+ def assert_written(runtime, paths)
128
+ assert_equal(paths.size, runtime.printed_lines.size)
129
+ runtime.printed_lines.zip(paths).each do |line, path|
130
+ assert_equal(path, line)
131
+ assert(File.file?(path))
132
+ end
133
+ end
134
+
135
+ def test_hello_stdin
136
+ with_runtime_on_temp_repo do |target|
137
+ target.stdin.string << THE_TEXT
138
+ assert_written(target.dispatch_then(["add", "--stdin"]),
139
+ [File.join(target.config.card_paths[0], THE_NAME)])
140
+ end
141
+ end
142
+
143
+ def test_hello_stdin_dir
144
+ with_runtime_on_temp_repo do |target|
145
+ target.stdin.string << THE_TEXT
146
+ dst_dir = target.config.card_search_paths[1]
147
+ assert_written(target.dispatch_then(["add", "--stdin", "--dir", dst_dir]),
148
+ [File.join(dst_dir, THE_NAME)])
149
+ end
150
+ end
151
+
152
+ def test_hello_two_file
153
+ with_runtime_on_temp_repo do |target|
154
+ dst_dir = target.config.card_search_paths[1]
155
+ assert_written(target.dispatch_then(["add", test_data_at("hope.card"), test_data_at("luck.card")]),
156
+ [File.join(target.config.card_paths[0], "hope-is-the-thing-with-feathers.card"),
157
+ File.join(target.config.card_paths[0], "luck-is-not-chance.card")])
158
+ end
159
+ end
160
+ end
161
+
162
+ class ComingTest < Test::Unit::TestCase
163
+ include Ankit
164
+ include Ankit::TestHelper
165
+ include Ankit::CardNaming
166
+
167
+ def test_name
168
+ with_runtime_on_temp_repo do |target|
169
+ assert_equal(target.dispatch_then(["coming", "--name"]).printed_lines,
170
+ ["vanilla-please", "bye", "how-are-you", "hello"])
171
+ end
172
+ end
173
+
174
+ def test_limit
175
+ with_runtime_on_temp_repo do |target|
176
+
177
+ assert_equal(target.dispatch_then(["coming", "--name", "1"]).printed_lines,
178
+ ["vanilla-please"])
179
+ end
180
+ end
181
+
182
+ def test_hello
183
+ with_runtime_on_temp_repo do |target|
184
+ dir = File.join(target.config.repo, "cards/foo")
185
+ assert_equal(target.dispatch_then(["coming"]).printed_lines,
186
+ [to_card_path(dir, "vanilla-please"),
187
+ to_card_path(dir, "hello")])
188
+ end
189
+ end
190
+ end
191
+
192
+ class FailPassTest < Test::Unit::TestCase
193
+ include Ankit
194
+ include Ankit::TestHelper
195
+ include Ankit::CardNaming, Ankit::Coming
196
+
197
+ VANILLA = "vanilla-please"
198
+ JUNIOR = "bye"
199
+ MIDDLE = "how-are-you"
200
+ VINTAGE = "hello"
201
+
202
+ def path_for(runtime, name)
203
+ dir = File.join(runtime.config.repo, "cards/foo")
204
+ to_card_path(dir, name)
205
+ end
206
+
207
+ def run_test_against(command, card, &block)
208
+ with_runtime_on_temp_repo do |target|
209
+ path = path_for(target, card)
210
+ target.dispatch([command, path])
211
+ assert_equal(target.printed_lines.size, 1)
212
+ events = Coming.coming_events(target)
213
+ block.call(events)
214
+ end
215
+ end
216
+
217
+ def test_pass_vanilla
218
+ run_test_against("pass", VANILLA) do |events|
219
+ assert_equal(events[0].name, VANILLA)
220
+ assert_equal(events[0].maturity, 1)
221
+ end
222
+ end
223
+
224
+ def test_pass_vintage
225
+ run_test_against("pass", VINTAGE) do |events|
226
+ assert_equal(events.map(&:name), [VANILLA, JUNIOR, MIDDLE, VINTAGE])
227
+ end
228
+ end
229
+
230
+ def test_pass_junior
231
+ run_test_against("pass", JUNIOR) do |events|
232
+ assert_equal(events.map(&:name), [VANILLA, MIDDLE, JUNIOR, VINTAGE])
233
+ assert_equal(events[2].maturity, 3)
234
+ end
235
+ end
236
+
237
+ def test_fail_vanilla
238
+ run_test_against("fail", VANILLA) do |events|
239
+ assert_equal(events[0].name, VANILLA)
240
+ assert_equal(events[0].maturity, 0)
241
+ end
242
+ end
243
+
244
+ def test_fail_vintage
245
+ run_test_against("fail", VINTAGE) do |events|
246
+ assert_equal(events.map(&:name), [VANILLA, VINTAGE, JUNIOR, MIDDLE])
247
+ assert_equal(events[0].maturity, 0)
248
+ end
249
+ end
250
+ end
251
+
252
+ class ChallengeTest < Test::Unit::TestCase
253
+ include Ankit
254
+ include Ankit::TestHelper
255
+ include Ankit::CardNaming, Ankit::Coming
256
+
257
+ def test_initial_state
258
+ with_runtime_on_temp_repo do |target|
259
+ actual = ChallengeCommand.new(target).initial_state
260
+ assert_instance_of(Challenge::QuestionState, actual)
261
+ assert_equal(2, actual.progress.slots.size)
262
+ end
263
+ end
264
+
265
+ def test_initial_state_limit_default
266
+ runtime = make_runtime(NUMBER_REPO)
267
+ actual = runtime.make_command(["challenge"]).initial_state
268
+ assert_equal(actual.progress.size, ChallengeCommand::DEFAULT_COUNT)
269
+ end
270
+
271
+ def test_initial_state_limit_args
272
+ runtime = make_runtime(NUMBER_REPO)
273
+ actual = runtime.make_command(["challenge", "--limit", "2"]).initial_state
274
+ assert_equal(actual.progress.size, 2)
275
+ end
276
+
277
+ FIRST_CORRECT_ANSWER = "Vanilla, Please?"
278
+ SECOND_CORRECT_ANSWER = "Hello"
279
+ FIRST_WRONG_ANSWER = "Doesn't Match"
280
+ FIRST_TYPO_ANSWER = "Vanilla, Please!"
281
+
282
+ def hit_return_pump(state)
283
+ state.progress.runtime.line = HighLine.new
284
+ state.progress.runtime.line.stubs(:say).at_least(0)
285
+ state.progress.runtime.line.stubs(:ask).once()
286
+ state.pump
287
+ end
288
+
289
+ def enter_text_pump(state, text)
290
+ state.progress.runtime.line = HighLine.new
291
+ state.progress.runtime.line.stubs(:say).at_least(1)
292
+ state.progress.runtime.line.stubs(:ask).once().returns(text)
293
+ state.pump
294
+ end
295
+
296
+ def agree_pump(state, that)
297
+ state.progress.runtime.line = HighLine.new
298
+ state.progress.runtime.line.stubs(:say).at_least(0)
299
+ state.progress.runtime.line.stubs(:ask).once().returns(that)
300
+ state.pump
301
+ end
302
+
303
+ def test_question_to_fail
304
+ with_runtime_on_temp_repo do |runtime|
305
+ actual = ChallengeCommand.new(runtime).initial_state
306
+ actual_next = enter_text_pump(actual, FIRST_WRONG_ANSWER)
307
+ assert_instance_of(Challenge::FailedState, actual_next)
308
+ actual_next = hit_return_pump(actual_next)
309
+ assert_instance_of(Challenge::QuestionState, actual_next)
310
+ assert_equal(actual_next.progress.npassed, 0)
311
+ assert_equal(actual_next.progress.nfailed, 1)
312
+ end
313
+ end
314
+
315
+ def test_question_to_typo
316
+ with_runtime_on_temp_repo do |runtime|
317
+ actual = ChallengeCommand.new(runtime).initial_state
318
+ actual_next = enter_text_pump(actual, FIRST_TYPO_ANSWER)
319
+ assert_instance_of(Challenge::TypoState, actual_next)
320
+ actual_next = hit_return_pump(actual_next)
321
+ assert_instance_of(Challenge::QuestionState, actual_next)
322
+ assert_equal(actual_next.progress.npassed, 0)
323
+ assert_equal(actual_next.progress.nfailed, 0)
324
+ actual_next = enter_text_pump(actual_next, FIRST_CORRECT_ANSWER)
325
+ assert_instance_of(Challenge::PassedState, actual_next)
326
+ hit_return_pump(actual_next)
327
+ assert_equal(actual_next.progress.npassed, 1)
328
+ assert_equal(actual_next.progress.nfailed, 0)
329
+ end
330
+ end
331
+
332
+ def test_question_to_pass
333
+ with_runtime_on_temp_repo do |runtime|
334
+ actual = ChallengeCommand.new(runtime).initial_state
335
+ actual_next = enter_text_pump(actual, FIRST_CORRECT_ANSWER)
336
+ assert_instance_of(Challenge::PassedState, actual_next)
337
+ actual_next = hit_return_pump(actual_next)
338
+ assert_instance_of(Challenge::QuestionState, actual_next)
339
+ assert_equal(actual_next.progress.npassed, 1)
340
+ assert_equal(actual_next.progress.nfailed, 0)
341
+ end
342
+ end
343
+
344
+ def pass_two(state)
345
+ state = enter_text_pump(state, FIRST_CORRECT_ANSWER)
346
+ assert_instance_of(Challenge::PassedState, state)
347
+ state = hit_return_pump(state)
348
+ assert_instance_of(Challenge::QuestionState, state)
349
+ state = enter_text_pump(state, SECOND_CORRECT_ANSWER)
350
+ assert_instance_of(Challenge::PassedState, state)
351
+ hit_return_pump(state)
352
+ end
353
+
354
+ def test_to_breaking
355
+ with_runtime_on_temp_repo do |runtime|
356
+ actual = ChallengeCommand.new(runtime).initial_state
357
+ actual_next = pass_two(actual)
358
+ assert_instance_of(Challenge::BreakingState, actual_next)
359
+ end
360
+ end
361
+
362
+ def test_to_breaking_to_over
363
+ with_runtime_on_temp_repo do |runtime|
364
+ actual = ChallengeCommand.new(runtime).initial_state
365
+ actual = pass_two(actual)
366
+ actual = agree_pump(actual, "n")
367
+ assert_instance_of(Challenge::OverState, actual)
368
+ end
369
+ end
370
+
371
+ def test_to_breaking_to_more
372
+ with_runtime_on_temp_repo do |runtime|
373
+ actual = ChallengeCommand.new(runtime).initial_state
374
+ actual = pass_two(actual)
375
+ actual = agree_pump(actual, "y")
376
+ assert_instance_of(Challenge::QuestionState, actual)
377
+ end
378
+ end
379
+
380
+ def test_to_breaking_to_more
381
+ with_runtime_on_temp_repo do |runtime|
382
+ actual = ChallengeCommand.new(runtime).initial_state
383
+ actual = pass_two(actual)
384
+ actual = agree_pump(actual, "?")
385
+ assert_instance_of(Challenge::BreakingState, actual)
386
+ end
387
+ end
388
+ end
389
+
390
+ class StylableTextTest < Test::Unit::TestCase
391
+ include Ankit
392
+ include Ankit::TestHelper
393
+
394
+ def test_hello
395
+ assert_equal("Hello, *****!",
396
+ Card.new(o: "Hello, [World]!").hidden_original)
397
+ assert_equal("*****, *****!",
398
+ Card.new(o: "Hello, World!").hidden_original)
399
+ end
400
+
401
+ def test_diff
402
+ StylableText.new("hello").diff("helo")
403
+ StylableText.new("helo").diff("hello")
404
+ StylableText.new("helxo").diff("hello")
405
+ end
406
+ end
@@ -0,0 +1,2 @@
1
+ O:Bye, Universe!
2
+ T:Sayonara Uchu!!
@@ -0,0 +1,2 @@
1
+ O:Hello, World!
2
+ T:Konichiwa Sekai!!
@@ -0,0 +1,4 @@
1
+
2
+ self.repo = "hello_repo"
3
+ self.location = "hello"
4
+ self.card_paths << "#{repo}/more"
@@ -0,0 +1,2 @@
1
+ {"envelope":{"at":"2001-02-03T04:05:06+00:00","round":5}, "values":{"type":"card","verb":"add","name":"hello","maturity":1}}
2
+ {"envelope":{"at":"2001-02-03T04:05:06+00:00","round":5}, "values":{"type":"card","verb":"add","name":"bye","maturity":2}}
@@ -0,0 +1,2 @@
1
+ {"envelope":{"at":"2001-02-03T04:05:06+00:00","round":6}, "values":{"type":"card","verb":"add","name":"hello","maturity":4}}
2
+ {"envelope":{"at":"2001-02-03T04:05:06+00:00","round":5}, "values":{"type":"card","verb":"add","name":"how-are-you","maturity":3}}
@@ -0,0 +1,2 @@
1
+ O: Hello
2
+ T: Konnichiwa
@@ -0,0 +1 @@
1
+ This isn't a card.
@@ -0,0 +1,2 @@
1
+ O: [Vanilla], Please?
2
+ T: Konnichiwa
@@ -0,0 +1 @@
1
+ O: Hope is the thing with feathers
@@ -0,0 +1 @@
1
+ O: Luck is not chance
File without changes
@@ -0,0 +1,2 @@
1
+ O: Eight
2
+ T: Hachi
@@ -0,0 +1,2 @@
1
+ O: Five
2
+ T: Go
@@ -0,0 +1,2 @@
1
+ O: Four
2
+ T: Shi
@@ -0,0 +1,2 @@
1
+ O: One
2
+ T: Ichi
@@ -0,0 +1,2 @@
1
+ O: Seven
2
+ T: Nana
@@ -0,0 +1,2 @@
1
+ O: Six
2
+ T: Roku
@@ -0,0 +1,2 @@
1
+ O: Three
2
+ T: San
@@ -0,0 +1,2 @@
1
+ O: Two
2
+ T: Ni
File without changes
@@ -0,0 +1,29 @@
1
+
2
+ require 'ankit/event'
3
+ require 'test/unit'
4
+
5
+ class EventTest < Test::Unit::TestCase
6
+ include Ankit
7
+
8
+ def setup
9
+ @target = Event.parse('{"envelope":{"at":"2001-02-03T04:05:06+00:00","round":1},' +
10
+ '"values":{"type":"card","verb":"add","name":"hello","maturity":1}}')
11
+ @next_envelope = Envelope.parse('{"at":"2002-03-04T05:06:07+00:00","round":2}')
12
+ end
13
+
14
+ def test_to_passed
15
+ expected = Event.new(@next_envelope, JSON.parse('{"type":"card","verb":"passed","name":"hello","maturity":2}'))
16
+ actual = @target.to_passed(@next_envelope)
17
+ assert_equal(actual, expected)
18
+ end
19
+
20
+ def test_to_failed
21
+ expected = Event.new(@next_envelope, JSON.parse('{"type":"card","verb":"failed","name":"hello","maturity":0}'))
22
+ actual = @target.to_failed(@next_envelope)
23
+ assert_equal(actual, expected)
24
+ end
25
+
26
+ def test_next_round
27
+ assert_equal(@target.next_round, 3)
28
+ end
29
+ end
data/test/helpers.rb ADDED
@@ -0,0 +1,54 @@
1
+
2
+ require 'ankit/runtime'
3
+ require 'stringio'
4
+ require 'fileutils'
5
+
6
+ module Ankit
7
+ TEST_DATA_BASE = File.join(File.dirname(__FILE__), "data")
8
+ HELLO_REPO = File.join(TEST_DATA_BASE, "hello_repo")
9
+ VANILLA_REPO = File.join(TEST_DATA_BASE, "vanilla_repo")
10
+ NUMBER_REPO = File.join(TEST_DATA_BASE, "number_repo")
11
+
12
+ class RuntimeWithMockedIO < Runtime
13
+ def initialize(config)
14
+ super
15
+ supress_io
16
+ end
17
+
18
+ def printed_line; self.stdout.string.strip; end
19
+ def printed_lines; self.stdout.string.split("\n").map(&:strip); end
20
+
21
+ def self.prepare_default; end
22
+ end
23
+
24
+ module TestHelper
25
+ def test_data_at(*args) File.join(TEST_DATA_BASE, *args); end
26
+ def repo_data_at(*args) File.join(HELLO_REPO, *args); end
27
+
28
+ def copy_hello_repo_to(dst)
29
+ FileUtils.cp_r(HELLO_REPO, dst)
30
+ File.join(dst, File.basename(HELLO_REPO))
31
+ end
32
+
33
+ def make_config(repo_dir=HELLO_REPO)
34
+ config = Config.new
35
+ config.repo = repo_dir
36
+ config.location = "anomone"
37
+ config
38
+ end
39
+
40
+ def make_runtime(repo_dir=HELLO_REPO)
41
+ RuntimeWithMockedIO.new(make_config(repo_dir))
42
+ end
43
+
44
+ def make_vanilla_runtime()
45
+ make_runtime(VANILLA_REPO)
46
+ end
47
+
48
+ def with_runtime_on_temp_repo(&block)
49
+ Dir.mktmpdir do |temp_repo|
50
+ block.call(make_runtime(copy_hello_repo_to(temp_repo)))
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,99 @@
1
+
2
+ require 'ankit/runtime'
3
+ require 'test/unit'
4
+
5
+ class ProgressTest < Test::Unit::TestCase
6
+ include Ankit
7
+ include Ankit::TestHelper
8
+ include Ankit::CardNaming
9
+ include Challenge
10
+
11
+ def make_target(runtime)
12
+ Progress.new(Session.new(runtime), [Slot.new("path1"), Slot.new("path2"), Slot.new("path3")])
13
+ end
14
+
15
+ def first_slot_event_of(progress)
16
+ EventTraversing.find_latest_event_for(progress.runtime, progress.slots[0].path)
17
+ end
18
+
19
+ def test_fail
20
+ with_runtime_on_temp_repo do |runtime|
21
+ target = make_target(runtime)
22
+ target.fail
23
+ fetched_event = first_slot_event_of(target)
24
+ assert_equal(target.index, 0)
25
+ assert_equal(target.slots[0].rating, :failed)
26
+ assert_equal(target.slots[0].event, fetched_event)
27
+ assert_equal(0, fetched_event.maturity)
28
+ assert(runtime.printed_line.empty?)
29
+ assert_equal(1, target.nfailed)
30
+ assert_equal(0, target.npassed)
31
+ end
32
+ end
33
+
34
+ def test_pass
35
+ with_runtime_on_temp_repo do |runtime|
36
+ target = make_target(runtime)
37
+ target.pass
38
+ fetched_event = first_slot_event_of(target)
39
+ assert_equal(target.index, 1)
40
+ assert_equal(target.slots[0].rating, :passed)
41
+ assert_equal(target.slots[0].event, fetched_event)
42
+ assert_equal(1, fetched_event.maturity)
43
+ assert(runtime.printed_line.empty?)
44
+
45
+ assert_equal(0, target.nfailed)
46
+ assert_equal(1, target.npassed)
47
+ end
48
+ end
49
+
50
+ def test_fail_then_pass
51
+ with_runtime_on_temp_repo do |runtime|
52
+ target = make_target(runtime)
53
+ target.fail
54
+ target.pass
55
+ fetched_event = first_slot_event_of(target)
56
+ assert_equal(target.index, 1)
57
+ assert_equal(target.slots[0].rating, :failed)
58
+ assert_equal(target.slots[0].event, fetched_event)
59
+ assert_equal(0, fetched_event.maturity)
60
+
61
+ assert_equal(1, target.nfailed)
62
+ assert_equal(0, target.npassed)
63
+ end
64
+ end
65
+
66
+ def test_round_during_progress
67
+ with_runtime_on_temp_repo do |runtime|
68
+ target = make_target(runtime)
69
+ target.pass
70
+ e1 = EventTraversing.find_latest_event_named(runtime, "path1")
71
+ target.pass
72
+ e2 = EventTraversing.find_latest_event_named(runtime, "path2")
73
+ assert_equal(target.this_round, e1.round)
74
+ assert_equal(target.this_round, e2.round)
75
+ end
76
+ end
77
+
78
+ def test_indicator
79
+ with_runtime_on_temp_repo do |runtime|
80
+ target = make_target(runtime)
81
+ assert_equal("---", target.indicator)
82
+ target.pass
83
+ assert_equal("o--", target.indicator)
84
+ target.attack
85
+ assert_equal("o*-", target.indicator)
86
+ target.fail
87
+ assert_equal("ox-", target.indicator)
88
+ end
89
+ end
90
+
91
+ def test_round_delta
92
+ with_runtime_on_temp_repo do |runtime|
93
+ target = make_target(runtime).pass.pass.pass
94
+ assert_equal(1, target.round_delta)
95
+ end
96
+ end
97
+
98
+ end
99
+