Skip to content

Instantly share code, notes, and snippets.

@sanemat sanemat/Gemfile
Last active Aug 29, 2015

Embed
What would you like to do?
# coding: utf-8
# https://gist.github.com/JunichiIto/c548e39fed60bf4bd36a
require 'byebug'
require 'rspec'
class KaraokeMachine
KEYBOARD = %w(C C# D D# E F F# G G# A A# B)
def initialize(melody)
@melody = melody
end
def transpose(amount)
@melody.scan(/.(?:#)?/).map do |ch|
if KEYBOARD.include?(ch)
index = KEYBOARD.index(ch)
(KEYBOARD * 2)[index + amount % 12]
else
ch
end
end.join
end
end
describe KaraokeMachine do
it "メロディ無し" do
melody = ""
answer = melody
expect(KaraokeMachine.new(melody).transpose(0)).to eq answer
end
# ここから下のコメントを外していって、テストを全部パスさせてください!
it "変更無し" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = melody
expect(KaraokeMachine.new(melody).transpose(0)).to eq answer
end
#
it "2上げる" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = "D E F# G |F# E D |F# G A B |A G F# |D D |D D |DDEEF#F#GG|F# E D "
expect(KaraokeMachine.new(melody).transpose(2)).to eq answer
end
#
it "7上げる" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = "G A B C |B A G |B C D E |D C B |G G |G G |GGAABBCC|B A G "
expect(KaraokeMachine.new(melody).transpose(7)).to eq answer
end
#
it "1下げる" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = "B C# D# E |D# C# B |D# E F# G# |F# E D# |B B |B B |BBC#C#D#D#EE|D# C# B "
expect(KaraokeMachine.new(melody).transpose(-1)).to eq answer
end
it "7下げる" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = "F G A A# |A G F |A A# C D |C A# A |F F |F F |FFGGAAA#A#|A G F "
expect(KaraokeMachine.new(melody).transpose(-7)).to eq answer
end
it "1オクターブ上げる" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = melody
expect(KaraokeMachine.new(melody).transpose(12)).to eq answer
end
it "1オクターブ下げる" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = melody
expect(KaraokeMachine.new(melody).transpose(-12)).to eq answer
end
it "14上げる" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = "D E F# G |F# E D |F# G A B |A G F# |D D |D D |DDEEF#F#GG|F# E D "
expect(KaraokeMachine.new(melody).transpose(14)).to eq answer
end
it "13下げる" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = "B C# D# E |D# C# B |D# E F# G# |F# E D# |B B |B B |BBC#C#D#D#EE|D# C# B "
expect(KaraokeMachine.new(melody).transpose(-13)).to eq answer
end
it "2オクターブ上げる" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = melody
expect(KaraokeMachine.new(melody).transpose(24)).to eq answer
end
it "2オクターブ下げる" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = melody
expect(KaraokeMachine.new(melody).transpose(-24)).to eq answer
end
it "26上げる" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = "D E F# G |F# E D |F# G A B |A G F# |D D |D D |DDEEF#F#GG|F# E D "
expect(KaraokeMachine.new(melody).transpose(26)).to eq answer
end
it "25上げる" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
answer = "B C# D# E |D# C# B |D# E F# G# |F# E D# |B B |B B |BBC#C#D#D#EE|D# C# B "
expect(KaraokeMachine.new(melody).transpose(-25)).to eq answer
end
it "6上げる(メロディはF#から開始)" do
melody = "F# G# A# B |A# G# F# |A# B C# D# |C# B A# |F# F# |F# F# |F#F#G#G#A#A#BB|A# G# F# "
answer = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
expect(KaraokeMachine.new(melody).transpose(6)).to eq answer
end
it "6下げる(メロディはF#から開始)" do
melody = "F# G# A# B |A# G# F# |A# B C# D# |C# B A# |F# F# |F# F# |F#F#G#G#A#A#BB|A# G# F# "
answer = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
expect(KaraokeMachine.new(melody).transpose(-6)).to eq answer
end
it "連続してtransposeを呼び出す" do
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
karaoke = KaraokeMachine.new(melody)
answer = melody
expect(karaoke.transpose(0)).to eq answer
answer = "D E F# G |F# E D |F# G A B |A G F# |D D |D D |DDEEF#F#GG|F# E D "
expect(karaoke.transpose(2)).to eq answer
answer = "B C# D# E |D# C# B |D# E F# G# |F# E D# |B B |B B |BBC#C#D#D#EE|D# C# B "
expect(karaoke.transpose(-1)).to eq answer
end
end
# A sample Gemfile
source 'https://rubygems.org'
gem 'rspec'
gem 'byebug'
GEM
remote: https://rubygems.org/
specs:
byebug (3.1.2)
columnize (~> 0.8)
debugger-linecache (~> 1.2)
columnize (0.8.9)
debugger-linecache (1.2.0)
diff-lcs (1.2.5)
rspec (3.0.0)
rspec-core (~> 3.0.0)
rspec-expectations (~> 3.0.0)
rspec-mocks (~> 3.0.0)
rspec-core (3.0.2)
rspec-support (~> 3.0.0)
rspec-expectations (3.0.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.0.0)
rspec-mocks (3.0.2)
rspec-support (~> 3.0.0)
rspec-support (3.0.2)
PLATFORMS
ruby
DEPENDENCIES
byebug
rspec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.