spec/model/pitch_spec.rb in music-transcription-0.13.0 vs spec/model/pitch_spec.rb in music-transcription-0.14.0

- old
+ new

@@ -15,15 +15,15 @@ { octave: -2, semitone: 7, :ratio => 0.375, :total_semitone => -17 }, { octave: -1, semitone: 9, :ratio => 0.841, :total_semitone => -3 }, ] end - it "should be constructible with no parameters (no error raised)" do + it "should be constructable with no parameters (no error raised)" do lambda { Pitch.new }.should_not raise_error end - it "should be hash-makeable" do + it "should take keyword args" do obj = Pitch.new octave: 4, semitone: 3 obj.octave.should eq(4) obj.semitone.should eq(3) end @@ -39,31 +39,42 @@ p.ratio.should be_within(0.01).of case_data[:ratio] p.total_semitone.should be case_data[:total_semitone] end end - it "should allow setting by ratio" do - @cases.each do |case_data| - p = Pitch.new - p.ratio = case_data[:ratio] - - p.octave.should eq case_data[:octave] - p.semitone.should eq case_data[:semitone] - p.total_semitone.should eq case_data[:total_semitone] + describe '.from_ratio' do + it 'should return a Pitch with given ratio' do + @cases.each do |case_data| + p = Pitch.from_ratio case_data[:ratio] + + p.octave.should eq case_data[:octave] + p.semitone.should eq case_data[:semitone] + p.total_semitone.should eq case_data[:total_semitone] + end end end - it "should setting by total_semitone" do - @cases.each do |case_data| - p = Pitch.new - p.total_semitone = case_data[:total_semitone] - - p.octave.should eq case_data[:octave] - p.semitone.should eq case_data[:semitone] - p.total_semitone.should eq case_data[:total_semitone] + describe '.from_semitones' do + it 'should return a Pitch with given total semitones' do + @cases.each do |case_data| + p = Pitch.from_semitones case_data[:total_semitone] + + p.octave.should eq case_data[:octave] + p.semitone.should eq case_data[:semitone] + p.total_semitone.should eq case_data[:total_semitone] + end end end + + describe '.from_freq' do + it 'should make a pitch whose freq is approximately the given freq' do + [16.35, 440.0, 987.77].each do |given_freq| + pitch = Pitch.from_freq given_freq + pitch.freq.should be_within(0.01).of(given_freq) + end + end + end it "should be comparable to other pitches" do p1 = Pitch.new semitone: 1 p2 = Pitch.new semitone: 2 p3 = Pitch.new semitone: 3 @@ -94,10 +105,26 @@ (p2 - p3).should eq(Pitch.new semitone: -1) (p3 - p2).should eq(Pitch.new semitone: 1) (p3 - p1).should eq(Pitch.new semitone: 2) end + it "should be addable and subtractable with integers" do + p1 = Pitch.new semitone: 1 + p2 = Pitch.new semitone: 2 + p3 = Pitch.new semitone: 3 + + (p1 + 2).should eq(Pitch.new semitone: 3) + (p1 + 3).should eq(Pitch.new semitone: 4) + (p2 + 3).should eq(Pitch.new semitone: 5) + + (p1 - 2).should eq(Pitch.new semitone: -1) + (p1 - 3).should eq(Pitch.new semitone: -2) + (p2 - 3).should eq(Pitch.new semitone: -1) + (p3 - 2).should eq(Pitch.new semitone: 1) + (p3 - 1).should eq(Pitch.new semitone: 2) + end + it "should have freq of 440 for A4" do a4 = Pitch.new octave: 4, semitone: 9 a4.freq.should be_within(0.01).of(440.0) end @@ -134,28 +161,9 @@ { Db0 => "C#0", Eb1 => "D#1", Gb7 => "F#7", Ab4 => "G#4", Bb1 => "A#1" }.each do |p,s| p.to_s(true).should eq s end end - end - end - end - - describe '.make_from_freq' do - it 'should make a pitch whose freq is approximately the given freq' do - [16.35, 440.0, 987.77].each do |given_freq| - pitch = Pitch.make_from_freq given_freq - pitch.freq.should be_within(0.01).of(given_freq) - end - end - end - - describe '.make_from_semitone' do - context 'given an integer less than 12' do - before(:all) { @pitch = Pitch.make_from_semitone(11) } - - it 'semitone should equal given integer' do - @pitch.semitone.should eq 11 end end end end