Class: Minitest::Stately::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/stately/watcher.rb

Constant Summary collapse

HEADER =
<<-EOS

******************************
Minitest::Stately Changes!!!
******************************
EOS

Instance Method Summary collapse

Constructor Details

#initializeWatcher

Returns a new instance of Watcher.



4
5
6
7
8
9
10
11
# File 'lib/minitest/stately/watcher.rb', line 4

def initialize
  @watch    = {}
  @run      = []
  @failures = {}

  @results = {}
  @report  = []
end

Instance Method Details

#check_failures(result) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/minitest/stately/watcher.rb', line 48

def check_failures(result)
  failed = []
  @failures.each do |name, blk|
    failed << name if blk.call
  end

  result.flunk("#{failed.join(",")}") if failed.any?
end

#fail_if(name, &blk) ⇒ Object



22
23
24
# File 'lib/minitest/stately/watcher.rb', line 22

def fail_if(name,&blk)
  @failures[name] = blk
end

#message(result, name, value) ⇒ Object



61
62
63
# File 'lib/minitest/stately/watcher.rb', line 61

def message(result, name, value)
  "#{result.class.name}\##{result.name}: #{name} changed from #{@results[name].inspect} to #{value.inspect}"
end

#record(result) ⇒ Object



26
27
28
29
30
# File 'lib/minitest/stately/watcher.rb', line 26

def record(result)
  record_changes(result)
  run_blocks()
  check_failures(result)
end

#record_changes(result) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/minitest/stately/watcher.rb', line 32

def record_changes(result)
  @watch.each do |name, blk|
    value = blk.call(result)
    if value_changed?(name, value)
      @report << message(result, name, value)
    end
    @results[name] = value
  end
end

#reportObject



72
73
74
75
76
# File 'lib/minitest/stately/watcher.rb', line 72

def report
  return "" if @report.empty?

  HEADER + @report.join("\n") + "\n\n"
end

#run(&blk) ⇒ Object



18
19
20
# File 'lib/minitest/stately/watcher.rb', line 18

def run(&blk)
  @run << blk
end

#run_blocksObject



42
43
44
45
46
# File 'lib/minitest/stately/watcher.rb', line 42

def run_blocks()
  @run.each do |blk|
    blk.call
  end
end

#value_changed?(name, value) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/minitest/stately/watcher.rb', line 57

def value_changed?(name, value)
  @results[name] != value
end

#watch(name, &blk) ⇒ Object



13
14
15
16
# File 'lib/minitest/stately/watcher.rb', line 13

def watch(name, &blk)
  @watch[name]   = blk
  @results[name] = blk.call(nil)
end