Class: StepSequencer::Tests

Inherits:
Object
  • Object
show all
Defined in:
lib/step_sequencer/tests.rb

Overview

A custom test runner. The tests themselves are in test_cases.rb Usage: StepSequencer::Tests.run The public method run_test_collection can also be used; it’s more modular.

Defined Under Namespace

Modules: TestCaseHelpers Classes: TestCases

Class Method Summary collapse

Class Method Details

.builder_testsObject



35
36
37
# File 'lib/step_sequencer/tests.rb', line 35

def builder_tests
  StepSequencer::Tests::TestCases::Builder
end

.cleanupObject



46
47
48
49
# File 'lib/step_sequencer/tests.rb', line 46

def cleanup
  dir = StepSequencer::SoundBuilder::OutputDir
  Dir.glob("#{dir}/*.mp3").each &File.method(:delete)
end

.play_sounds(sounds, tempo: 800) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/step_sequencer/tests.rb', line 78

def play_sounds(sounds, tempo: 800)
  sleep_time = 60.0 / tempo.to_f
  sounds.flatten.each do |path|
    `mpg123 #{path} 2> /dev/null`
    sleep sleep_time
  end
end

.player_testsObject



38
39
40
# File 'lib/step_sequencer/tests.rb', line 38

def player_tests
  StepSequencer::Tests::TestCases::Player
end

.runObject

Runs all the test cases, speaking the method name and playing the result.

Returns the results from the tests, for manual inspection from Pry



13
14
15
16
17
18
19
20
21
22
# File 'lib/step_sequencer/tests.rb', line 13

def self.run
  cleanup
  run_test_collection(builder_tests) do |fn_name, test_case|
    result = run_test_case(builder_tests, fn_name, test_case)
    result.tap &method(:play_sounds)
  end
  run_test_collection(player_tests) do |fn_name, test_case|
    run_test_case(player_tests, fn_name, test_case)
  end
end

.run_test_case(test_class, fn_name, test_case) ⇒ Object

shared ‘around’ hook for tests



25
26
27
28
29
30
# File 'lib/step_sequencer/tests.rb', line 25

def self.run_test_case(test_class, fn_name, test_case)
  speak_fn_name fn_name
  puts fn_name.yellow
  test_class.method(fn_name).source.display
  test_case.call
end

.run_test_collection(klass, only: nil, except: nil, &blk) ⇒ Object

Runs all the methods in a class, optionally filtered via :only and :accept options (arrays of symbols referencing methods of klass).

If a block is provided, then it is passed the name as a symbol and the test case as a proc. The proc will need to be called manually from the block, and the block should return the result. This allows a before/after hook to be inserted. With no given block, the test case is automatically run.

Returns a hash mapping test case names (symbols) to results



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/step_sequencer/tests.rb', line 62

def run_test_collection(klass, only: nil, except: nil, &blk)
  klass.methods(false).reduce({}) do |memo, fn_name|
    next memo if only && !only.include?(fn_name)
    next memo if except && except.include?(fn_name)
    memo[fn_name] = if blk
      blk.call(fn_name, ->{klass.send fn_name})
    else
      klass.send(fn_name)
    end
    memo
  end
end

.say(phrase) ⇒ Object



75
76
77
# File 'lib/step_sequencer/tests.rb', line 75

def say(phrase)
  ESpeak::Speech.new(phrase).speak
end

.speak_fn_name(fn_name) ⇒ Object



42
43
44
# File 'lib/step_sequencer/tests.rb', line 42

def speak_fn_name fn_name
  say fn_name.to_s.gsub("_", " ")
end