Skip to content

Commit

Permalink
added specs for ABC::File and added tune_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
domgetter committed Aug 6, 2014
1 parent b8a57c4 commit 9ff40d6
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 38 deletions.
3 changes: 2 additions & 1 deletion abc_parser.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Gem::Specification.new do |spec|
spec.version = '0.0.1'
spec.date = '2014-08-05'
spec.summary = 'ABC file parser'
spec.description = 'Converts ABC music files into Ruby objects for programatic use.'
spec.description = 'Converts ABC music files into Ruby objects for ' +
'programatic use.'
spec.authors = ["Dominic Muller"]
spec.email = '[email protected]'
spec.files = [
Expand Down
50 changes: 38 additions & 12 deletions lib/abc/file.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@

module ABC
# An abc file consists of one or more abc tune transcriptions,
# optionally interspersed with free text and typeset text
# annotations. It may optionally start with a file header to
# set up default values for processing the file.
#
# The file header, abc tunes and text annotations are separated
# from each other by empty lines (also known as blank lines).
#
# An abc file with more than one tune in it is called an abc
# tunebook.
#
class File

attr_reader :parsed, :version, :tunes, :header
def initialize(filepath)
@filepath = filepath
@file = ::File.open(@filepath, "r") {|f| f.read}
@parsed = false
parse
end
def parsed?
parsed
end
private
def parse
@sections = @file.split(/\n(?=\n)/)
@sections.each do |section|
case section_type(section)
when :tune
@tunes ||= []
@tunes << Tune.new(section)
when :file_header
@header = FileHeader.new(section)
else
end
end
@parsed = true
end
def section_type(section)
type = :unknown
section.each_line do |line|
if m = line.match(/%abc(-(?<version>[\d.]+))?/)
@version ||= m[:version].to_f
end
case line
when /^H:|^O:/ then type = :file_header
when /^X:|^T:|^K:/ then type = :tune
end
end
type
end
end
end
4 changes: 3 additions & 1 deletion lib/abc/file_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ module ABC
# distributed.
#
class FileHeader

def initialize(header)

end
end

end
24 changes: 2 additions & 22 deletions lib/abc/tune.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@

module ABC
# An abc tune itself consists of a tune header and a tune body,
# terminated by an empty line or the end of the file. It may
# also contain comment lines or stylesheet directives.
#
# The tune header is composed of several information field lines,
# which are further discussed in information fields. The tune
# header should start with an X:(reference number) field followed
# by a T:(title) field and finish with a K:(key) field.
#
# The tune body, which contains the music code, follows immediately
# after. Certain fields may also be used inside the tune body - see
# use of fields within the tune body.
#
# It is legal to write an abc tune without a tune body. This
# feature can be used to document tunes without transcribing them.
#
# Abc music code lines are those lines in the tune body which give
# notes, bar lines and other musical symbols - see the tune body
# for details. In effect, music code is the contents of any line
# which is not an information field, stylesheet directive or
# comment line.
#
class Tune
def initialize(tune)

end
end
end
2 changes: 1 addition & 1 deletion lib/abc_parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

require 'pry'
require 'abc/file'
require 'abc/file_header'
require 'abc/tune'
28 changes: 27 additions & 1 deletion spec/abc/file_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
require 'spec_helper'

# An abc file consists of one or more abc tune transcriptions,
# optionally interspersed with free text and typeset text
# annotations. It may optionally start with a file header to
# set up default values for processing the file.
#
# The file header, abc tunes and text annotations are separated
# from each other by empty lines (also known as blank lines).
#
# An abc file with more than one tune in it is called an abc
# tunebook.
#
describe ABC::File do
it "loads the file without error"
let(:file) {ABC::File.new("spec/fixtures/english.abc")}
it "should load without error" do
expect {file}.not_to raise_error
end
it "should be parsed" do
expect(file).to be_parsed
end
it "should have a format version of 2.1" do
expect(file.version).to eq 2.1
end
it "should have a file header" do
expect(file.header)
end
it "should have 3 tunes" do
expect(file.tunes.count).to eq 3
end
end
27 changes: 27 additions & 0 deletions spec/abc/tune_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

# An abc tune itself consists of a tune header and a tune body,
# terminated by an empty line or the end of the file. It may
# also contain comment lines or stylesheet directives.
#
# The tune header is composed of several information field lines,
# which are further discussed in information fields. The tune
# header should start with an X:(reference number) field followed
# by a T:(title) field and finish with a K:(key) field.
#
# The tune body, which contains the music code, follows immediately
# after. Certain fields may also be used inside the tune body - see
# use of fields within the tune body.
#
# It is legal to write an abc tune without a tune body. This
# feature can be used to document tunes without transcribing them.
#
# Abc music code lines are those lines in the tune body which give
# notes, bar lines and other musical symbols - see the tune body
# for details. In effect, music code is the contents of any line
# which is not an information field, stylesheet directive or
# comment line.
#
describe ABC::Tune do

end

0 comments on commit 9ff40d6

Please sign in to comment.