Permalink
Browse files

Adds more cucumber features.

  • Loading branch information...
1 parent 747ce6e commit e4bf6a7602bd24c5e9e30d660ea011c25f73339f @inukshuk committed Apr 30, 2011
View
@@ -0,0 +1,67 @@
+Feature: Parse BibTeX preambles
+ As a hacker who works with bibliographies
+ I want to be able to parse BibTeX files with entries
+ Because that's what BibTeX is all about
+
+ Scenario: A BibTeX file with three books
+ When I parse the following file:
+ """
+ %% This BibTeX bibliography file was created using BibDesk.
+ %% http://bibdesk.sourceforge.net/
+
+
+ %% Created for Sylvester Keil at 2010-08-05 10:07:07 +0200
+
+
+ %% Saved with string encoding Unicode (UTF-8)
+
+
+
+ @book{rails,
+ Address = {Raleigh, North Carolina},
+ Author = {Ruby, Sam, and Thomas, Dave, and Hansson Heinemeier, David},
+ Booktitle = {Agile Web Development with Rails},
+ Date-Added = {2010-08-05 10:01:36 +0200},
+ Date-Modified = {2010-08-05 10:06:46 +0200},
+ Edition = {third},
+ Isbn = {978-1-9343561-6-6},
+ Keywords = {ruby, rails},
+ Publisher = {The Pragmatic Bookshelf},
+ Series = {The Facets of Ruby},
+ Title = {Agile Web Development with Rails},
+ Year = {2009}
+ }
+
+ @book{dragon,
+ Address = {Boston},
+ Author = {Aho, Alfred V., and Lam, Monica S., and Ullman, Jeffrey D.},
+ Booktitle = {Compilers: Principles, Techniques, and Tools},
+ Date-Added = {2010-08-05 09:57:15 +0200},
+ Date-Modified = {2010-08-05 10:06:32 +0200},
+ Edition = {second},
+ Keywords = {compiler, lex, yacc},
+ Publisher = {Addison Wesley},
+ Title = {Compilers: Principles, Techniques, and Tools},
+ Year = {2007}
+ }
+
+ @book{pickaxe,
+ Address = {Raleigh, North Carolina},
+ Author = {Thomas, Dave, and Fowler, Chad, and Hunt, Andy},
+ Date-Added = {2010-08-05 09:54:07 +0200},
+ Date-Modified = {2010-08-05 10:07:01 +0200},
+ Keywords = {ruby},
+ Publisher = {The Pragmatic Bookshelf},
+ Series = {The Facets of Ruby},
+ Title = {Programming Ruby 1.9: The Pragmatic Programmer's Guide},
+ Year = {2009}
+ }
+ """
+ Then my bibliography should contain these books:
+ | title | author | publisher | year |
+ | Agile Web Development with Rails | Ruby, Sam, and Thomas, Dave, and Hansson Heinemeier, David | The Pragmatic Bookshelf | 2009 |
+ | Compilers: Principles, Techniques, and Tools | Aho, Alfred V., and Lam, Monica S., and Ullman, Jeffrey D. | Addison Wesley | 2007 |
+ | Programming Ruby 1.9: The Pragmatic Programmer's Guide | Thomas, Dave, and Fowler, Chad, and Hunt, Andy | The Pragmatic Bookshelf | 2009 |
+ And my bibliography should contain 2 books published in 2009
+ And my bibliography should contain a book with id "pickaxe"
+ And my bibliography should contain a book with id "dragon"
@@ -0,0 +1,21 @@
+Feature: Keys containing '/' symbols
+ As a hacker who works with bibliographies
+ I want to parse BibTeX entries with keys containing slashes
+ Because they frequently occur in key naming schemes
+
+ Scenario: An entry taken from the DBLP
+ When I parse the following file:
+ """
+ @article{DBLP:journals/dq/RossinK99,
+ author = {Donald F. Rossin and
+ Barbara D. Klein},
+ title = {Data Errors in Neural Network and Linear Regression Models:
+ An Experimental Comparison},
+ journal = {Data Quality Journal},
+ volume = {5},
+ year = {1999},
+ ee = {http://www.dataquality.com/999KR.htm},
+ bibsource = {DBLP, http://dblp.uni-trier.de}
+ }
+ """
+ Then my bibliography should contain an article with id "DBLP:journals/dq/RossinK99"
@@ -26,6 +26,14 @@
end
end
+Then /^my bibliography should contain these (\w+):$/ do |type, table|
+ @bibliography.q("@#{type.chomp!('s')}").zip(table.hashes).each do |object, expected|
+ expected.each_pair do |key, value|
+ assert_equal value, object.send(key).to_s
+ end
+ end
+end
+
Then /^my bibliography should contain the following numbers of elements:$/ do |table|
counts = table.hashes.first
counts[[]] = counts.delete('total') if counts.has_key?('total')
@@ -44,4 +52,13 @@
Then /^there should be exactly (\d+) match(?:es)?$/ do |matches|
assert_equal matches.to_i, @result.length
+end
+
+
+Then /^my bibliography should contain (\d+) (\w+) published in (\d+)$/ do |count, type, year|
+ assert_equal @bibliography.q("@#{type.chomp!('s')}[year=#{year}]").length, count.to_i
+end
+
+Then /^my bibliography should contain an? (\w+) with id "([^"]*)"$/ do |type, id|
+ assert_equal @bibliography[id.to_sym].type, type.to_sym
end
@@ -103,8 +103,7 @@ def save_to(path)
# if the object was not part of the bibliography.
#
def delete(*arguments, &block)
- objects = select(*arguments, &block)
- objects.each { |object| object.removed_from_bibliography(self) }
+ objects = q(*arguments, &block).map { |o| o.removed_from_bibliography(self) }
@data = @data - objects
objects.length == 1 ? objects[0] : objects
end
View
@@ -49,13 +49,14 @@ def initialize(hash = {})
self.type = hash.delete(:type) if hash.has_key?(:type)
self.key = hash.delete(:key) if hash.has_key?(:key)
- hash.each { |k, v| add(k.to_sym, v) }
+ hash.each { |k,v| add(k.to_sym, v) }
yield self if block_given?
end
# Sets the key of the entry
def key=(key)
+ raise(ArgumentError, "keys must be convertible to Symbol; was: #{type.class.name}.") unless type.respond_to?(:to_sym)
@key = key.to_sym
end
@@ -68,7 +69,7 @@ def key
# Sets the type of the entry.
def type=(type)
- raise(ArgumentError, "BibTeX::Entry type must be convertible to Symbol; was: #{type.class.name}.") unless type.respond_to?(:to_sym)
+ raise(ArgumentError, "types must be convertible to Symbol; was: #{type.class.name}.") unless type.respond_to?(:to_sym)
@type = type.to_sym
end
@@ -126,22 +127,22 @@ def empty?
# Returns false if the entry is one of the standard entry types and does not have
# definitions of all the required fields for that type.
def valid?
- REQUIRED_FIELDS[@type].all? { |f|
+ REQUIRED_FIELDS[@type].all? do |f|
f.is_a?(Array) ? !(f & @fields.keys).empty? : !@fields[f].nil?
- }
+ end
end
# Called when the element was added to a bibliography.
def added_to_bibliography(bibliography)
- super(bibliography)
- bibliography.entries[@key] = self
+ super
+ bibliography.entries[key] = self
self
end
# Called when the element was removed from a bibliography.
def removed_from_bibliography(bibliography)
- super(bibliography)
- bibliography.entries[@key] = nil
+ super
+ bibliography.entries[key] = nil
self
end
View
@@ -1,6 +1,5 @@
require File.expand_path('../../lib/bibtex.rb', __FILE__)
-require 'rubygems'
require 'mini_shoulda'
require 'minitest/autorun'
require 'redgreen'

0 comments on commit e4bf6a7

Please sign in to comment.