Skip to content

Commit

Permalink
Add traffic-light tip test for reverted traffic-light and fix event.r…
Browse files Browse the repository at this point in the history
…evert error
  • Loading branch information
JonJagger committed Aug 6, 2020
1 parent 87467bb commit 334fbe0
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 102 deletions.
2 changes: 1 addition & 1 deletion app/controllers/reverter_controller.rb
Expand Up @@ -10,7 +10,7 @@ def revert
colour = event.colour
index = params[:index].to_i + 1

kata.revert(now_index, index, files, time.now, stdout, stderr, status, colour)
kata.revert(index, files, time.now, stdout, stderr, status, colour, now_index)

visible_files = files.map{ |filename,file| [filename, file['content']] }.to_h

Expand Down
32 changes: 22 additions & 10 deletions app/models/event.rb
Expand Up @@ -2,16 +2,15 @@

class Event

def initialize(kata, hash)
def initialize(kata, summary)
@kata = kata
@hash = hash
@summary = summary
end

attr_reader :kata

def index
@hash['index']
end
# - - - - - - - - - - - - - - - - - - - -
# four core properties

def files
event['files']
Expand All @@ -29,27 +28,40 @@ def status
event['status']
end

def revert
event['revert']
# - - - - - - - - - - - - - - - - - - - -
# summary properties

def index
@summary['index']
end

def time_a
@summary['time']
end

def time
Time.mktime(*@hash['time'])
Time.mktime(*@summary['time'])
end

def predicted
@hash['predicted'] || 'none'
@summary['predicted'] || 'none'
end

def colour
# '' unless light?
(@hash['colour'] || '').to_sym
(@summary['colour'] || '').to_sym
end

def revert
@summary['revert']
end

def light?
colour.to_s != ''
end

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

def manifest
kata.manifest.to_json.merge({'visible_files' => files})
end
Expand Down
170 changes: 86 additions & 84 deletions app/models/kata.rb
Expand Up @@ -55,6 +55,92 @@ def avatar_name

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

def run_tests
Runner.new(@externals).run(@params)
end

def ran_tests(index, files, at, duration, stdout, stderr, status, colour, predicted='none')
kata.ran_tests(id, index, files, at, duration, stdout, stderr, status, colour, predicted)
end

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

def revert(index, files, at, stdout, stderr, status, colour, now_index)
kata.revert(id, index, files, at, stdout, stderr, status, colour, now_index)
end

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

def events
kata.events(id).map.with_index do |h,index|
h['index'] ||= index
Event.new(self, h)
end
end

def events_json
kata.events_json(id)
end

def event(index)
kata.event(id, index)
end

def lights
events.select(&:light?)
end

def active?
lights != []
end

def age
created = Time.mktime(*manifest.created)
(most_recent_event.time - created).to_i # in seconds
end

def files
most_recent_event.files
end

def stdout
most_recent_event.stdout
end

def stderr
most_recent_event.stderr
end

def status
most_recent_event.status
end

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

def diff_info(was_index, now_index)
m,e,was,now = kata.diff_info(id, was_index, now_index)
was_files = diff_files(was)
now_files = diff_files(now)
events = e.map.with_index do |h,index|
h['index'] ||= index
Event.new(self, h)
end
[m,events,was_files,now_files]
end

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

def tipper_info(was_index, now_index)
e,was_files,now_files = kata.tipper_info(id, was_index, now_index)
events = e.map.with_index do |h,index|
h['index'] ||= index
Event.new(self, h)
end
[events,plain(was_files),plain(now_files)]
end

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

def theme=(value)
# value == 'dark'|'light'
filename = kata_id_path(id, 'theme')
Expand Down Expand Up @@ -121,90 +207,6 @@ def predict=(value)

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

def diff_info(was_index, now_index)
m,e,was,now = kata.diff_info(id, was_index, now_index)
was_files = diff_files(was)
now_files = diff_files(now)
events = e.map.with_index do |h,index|
h['index'] ||= index
Event.new(self, h)
end
[m,events,was_files,now_files]
end

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

def tipper_info(was_index, now_index)
e,was_files,now_files = kata.tipper_info(id, was_index, now_index)
events = e.map.with_index do |h,index|
h['index'] ||= index
Event.new(self, h)
end
[events,plain(was_files),plain(now_files)]
end

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

def run_tests
Runner.new(@externals).run(@params)
end

def ran_tests(index, files, at, duration, stdout, stderr, status, colour, predicted='none')
kata.ran_tests(id, index, files, at, duration, stdout, stderr, status, colour, predicted)
end

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

def revert(now_index, index, files, at, stdout, stderr, status, colour)
kata.revert(id, now_index, index, files, at, stdout, stderr, status, colour)
end

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

def events
kata.events(id).map.with_index do |h,index|
h['index'] ||= index
Event.new(self, h)
end
end

def events_json
kata.events_json(id)
end

def event(index)
kata.event(id, index)
end

def lights
events.select(&:light?)
end

def active?
lights != []
end

def age
created = Time.mktime(*manifest.created)
(most_recent_event.time - created).to_i # in seconds
end

def files
most_recent_event.files
end

def stdout
most_recent_event.stdout
end

def stderr
most_recent_event.stderr
end

def status
most_recent_event.status
end

def manifest
@manifest ||= Manifest.new(kata.manifest(id))
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/kata_v0.rb
Expand Up @@ -68,7 +68,7 @@ def ran_tests(id, index, files, now, duration, stdout, stderr, status, colour, p

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

def revert(id, now_index, index, files, now, stdout, stderr, status, colour)
def revert(id, index, files, now, stdout, stderr, status, colour, now_index)
event_summary = {
'index' => index,
'time' => now,
Expand Down
9 changes: 5 additions & 4 deletions app/models/kata_v1.rb
Expand Up @@ -10,10 +10,11 @@
# 4. No longer stores file contents in lined format.
# 5. Uses Oj as its JSON gem.
# 6. Stores explicit index in events.json summary file.
# This makes using index==-1 robust when traffic-lights
# are lost due to Saver outages.
# This improves robustness when there are Saver outages.
# For example index==-1.
# was { ... } # 0
# { ... } # 1 then 2-23 outage
# { ... } # 1
# then 2-23 outage
# { ... } # 24
# now { ..., "index" => 0 }
# { ..., "index" => 1 }
Expand Down Expand Up @@ -83,7 +84,7 @@ def ran_tests(id, index, files, now, duration, stdout, stderr, status, colour, p

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

def revert(id, now_index, index, files, now, stdout, stderr, status, colour)
def revert(id, index, files, now, stdout, stderr, status, colour, now_index)
event_summary = {
'index' => index,
'time' => now,
Expand Down
43 changes: 41 additions & 2 deletions test/app_helpers/traffic_light_tip_test.rb
Expand Up @@ -213,12 +213,51 @@ def self.hex_prefix

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

=begin
test 'D57', %w(
traffic-light tip for revert
) do
in_new_kata do |kata|
files = kata.files
stdout_1 = file("Expected: 42\nActual: 54")
stderr_1 = file('assert failed')
status_1 = 4
kata.ran_tests(1, files, time.now, duration, stdout_1, stderr_1, status_1, 'red')

filename = 'hiker.rb'
hiker_rb = kata.files[filename]['content']
files[filename] = file(hiker_rb.sub('6 * 9','6 * 7'))
stdout_2 = file('All tests passed')
stderr_2 = file('')
status_2 = 0
kata.ran_tests(2, files, time.now, duration, stdout_2, stderr_2, status_2, 'green')

kata.revert(3, kata.events[1].files, time.now, stdout_1, stderr_1, status_1, 'red', 1)

events = kata.events
was_files = files_for(events, 2)
now_files = files_for(events, 3)
diff = differ.diff(kata.id, was_files, now_files)
number = 3

expected =
'<table>' +
'<tr>' +
"<td><span class='traffic-light-count red'>#{number}</span></td>" +
"<td><img src='/traffic-light/image/red_revert.png' class='traffic-light-diff-tip-traffic-light-image'></td>" +
'</tr>' +
'</table>' +
'<table>' +
'<tr>' +
"<td><div class='traffic-light-diff-tip-line-count-deleted some button'>1</div></td>" +
"<td><div class='traffic-light-diff-tip-line-count-added some button'>1</div></td>" +
"<td>&nbsp;hiker.rb</td>" +
'</tr>' +
'</table>'

actual = traffic_light_tip_html(diff, kata.avatar_index, events, 3, number)
assert_equal expected, actual
end
end
=end

private

Expand Down

0 comments on commit 334fbe0

Please sign in to comment.