Rspec: testing looping arrays , input = [] , output =[] for custom classes. ruby -
i'm trying speed tests development using examples use develop function in output.
i have 2 arrays: input , output.
function(input[0]) == output[0]
.
the loop working, gets stuck in last index. example: if input.length = 10 function(input[10]) == output[10].
describe "multiple_chords" input = ["a7 dmaj79", "e-7 a7", "d-7 g7", "bb-7b5 eb7b9" , "bb-7b5 eb7", "g7 a7", "d-7b5 g7b9", "d-79 g#7913"] output = [[{"root"=>"a", "def"=>"7"}, {"root"=>"d", "def"=>"maj7"}], [{"root"=>"e", "def"=>"-7"}, {"root"=>"a", "def"=>"7"}], [{"root"=>"d", "def"=>"-7"}, {"root"=>"g", "def"=>"7"}], [{"root"=>"bb", "def"=>"-7b5"}, {"root"=>"eb", "tensions"=>"b9", "def"=>"7"}], [{"root"=>"bb", "def"=>"-7b5"}, {"root"=>"eb", "def"=>"7"}], [{"root"=>"g", "def"=>"7"}, {"root"=>"a", "def"=>"7"}], [{"root"=>"d", "def"=>"-7b5"}, {"root"=>"g", "tensions"=>"b9", "def"=>"7"}], [{"root"=>"d", "tensions"=>"9", "def"=>"-7"}, {"root"=>"g#", "tensions"=>["9", "13"], "def"=>"7"}]] in 0..input.length-1 "analyzes correctly #{input[i]}" expect(ipmchords::multiple_chords(input[i])).to eq(output[i]) end end end
the output in console is:
6) ipmchords multiple_chords analyzes correctly bb-7b5 eb7 failure/error: expect(ipmchords::multiple_chords(input[i])).to eq(output[i]) expected: [{"root"=>"d", "tensions"=>"9", "def"=>"-7"}, {"root"=>"g#", "tensions"=>["9", "13"], "def"=>"7"}] got: [{"root"=>"d", "tensions"=>"9", "def"=>"-7"}, {"root"=>"g#", "tensions"=>"13", "def"=>""}] (compared using ==) diff: @@ -1,3 +1,3 @@ [{"root"=>"d", "tensions"=>"9", "def"=>"-7"}, - {"root"=>"g#", "tensions"=>["9", "13"], "def"=>"7"}] + {"root"=>"g#", "tensions"=>"13", "def"=>""}] # ./spec/ipm_classes/ipm_chords_ipm_class_spec.rb:28:in `block (4 levels) in <top (required)>' 7) ipmchords multiple_chords analyzes correctly e-7 a7 failure/error: expect(ipmchords::multiple_chords(input[i])).to eq(output[i]) expected: [{"root"=>"d", "tensions"=>"9", "def"=>"-7"}, {"root"=>"g#", "tensions"=>["9", "13"], "def"=>"7"}] got: [{"root"=>"d", "tensions"=>"9", "def"=>"-7"}, {"root"=>"g#", "tensions"=>"13", "def"=>""}] (compared using ==) diff: @@ -1,3 +1,3 @@ [{"root"=>"d", "tensions"=>"9", "def"=>"-7"}, - {"root"=>"g#", "tensions"=>["9", "13"], "def"=>"7"}] + {"root"=>"g#", "tensions"=>"13", "def"=>""}]
you can see evaluating same array index, test name changing each loop iteration:
6) ipmchords multiple_chords analyzes correctly bb-7b5 eb7 7) ipmchords multiple_chords analyzes correctly e-7 a7
i think great time saver, , should work, no?? hope can me. thanks
you're running bit of problem ruby's block bindings here. what's happening each block going bind i
, remains in scope, means they're invoked, they're going use whatever i
ends being (which last array index).
if instead use ruby's block iterators, it'll work:
describe "multiple_chords" input = ["a7 dmaj79", "e-7 a7", "d-7 g7", "bb-7b5 eb7b9" , "bb-7b5 eb7", "g7 a7", "d-7b5 g7b9", "d-79 g#7913"] output = [[{"root"=>"a", "def"=>"7"}, {"root"=>"d", "def"=>"maj7"}], [{"root"=>"e", "def"=>"-7"}, {"root"=>"a", "def"=>"7"}], [{"root"=>"d", "def"=>"-7"}, {"root"=>"g", "def"=>"7"}], [{"root"=>"bb", "def"=>"-7b5"}, {"root"=>"eb", "tensions"=>"b9", "def"=>"7"}], [{"root"=>"bb", "def"=>"-7b5"}, {"root"=>"eb", "def"=>"7"}], [{"root"=>"g", "def"=>"7"}, {"root"=>"a", "def"=>"7"}], [{"root"=>"d", "def"=>"-7b5"}, {"root"=>"g", "tensions"=>"b9", "def"=>"7"}], [{"root"=>"d", "tensions"=>"9", "def"=>"-7"}, {"root"=>"g#", "tensions"=>["9", "13"], "def"=>"7"}]] input.each_with_index |chord, index| "analyzes correctly #{chord}" expect(ipmchords::multiple_chords(chord)).to eq(output[index]) end end end
the reason works don't end creating variable outside of each block remains in scope, each block ends being invoked correct values. values yielded each loop local loop, don't "bleed over".
as aside, idiomatic ruby expects use of enumerators rather loops.
a note on style; can clean bit defining , iterating on hash of expected inputs , outputs:
describe "multiple_chords" chord_map = { "a7 dmaj79" => [{"root"=>"a", "def"=>"7"}, {"root"=>"d", "def"=>"maj7"}], "e-7 a7" => [{"root"=>"e", "def"=>"-7"}, {"root"=>"a", "def"=>"7"}], # ... } chord_map.each |chord, result| "analyzes correctly #{chord}" expect(ipmchords::multiple_chords(chord)).to eq(result) end end end
Comments
Post a Comment