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



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

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

.cleanupObject



49
50
51
52
# File 'lib/step_sequencer/tests.rb', line 49

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

.play_sounds(sounds, tempo: 800) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/step_sequencer/tests.rb', line 81

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



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

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



16
17
18
19
20
21
22
23
24
25
# File 'lib/step_sequencer/tests.rb', line 16

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



28
29
30
31
32
33
# File 'lib/step_sequencer/tests.rb', line 28

def self.run_test_case(test_class, fn_name, test_case)
  speak_fn_name fn_name
  puts fn_name
  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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/step_sequencer/tests.rb', line 65

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



78
79
80
# File 'lib/step_sequencer/tests.rb', line 78

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

.speak_fn_name(fn_name) ⇒ Object



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

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