From 8344b859383ee9a36d63b0372b43f1d8caae1aae Mon Sep 17 00:00:00 2001 From: Jessica Lynn Suttles Date: Fri, 15 Feb 2013 10:53:40 -0800 Subject: [PATCH] makes specs way more readable --- .rspec | 2 + lib/microformats2/property_parser.rb | 2 +- spec/lib/microformats2/collection_spec.rb | 253 +++++++++--------- .../implied_property/name_spec.rb | 40 +-- .../implied_property/photo_spec.rb | 40 +-- .../implied_property/url_spec.rb | 40 +-- spec/lib/microformats2_spec.rb | 2 +- spec/spec_helper.rb | 3 +- .../{unimplemented => }/microformats-2-2.html | 0 .../{unimplemented => }/microformats-2-2.js | 0 .../{unimplemented => }/microformats-2-4.html | 0 .../{unimplemented => }/microformats-2-4.js | 0 .../{unimplemented => }/microformats-2-8.html | 0 .../{unimplemented => }/microformats-2-8.js | 0 .../{unimplemented => }/microformats-2-9.html | 0 .../{unimplemented => }/microformats-2-9.js | 0 .../nested-format-with-property.html} | 0 .../nested-format-with-property.js | 15 ++ .../nested-format-without-property.html} | 0 .../microformats2/nested-property.html} | 0 .../lib/microformats2/nested-property.js | 9 + .../microformats2/simple.html} | 0 spec/support/lib/microformats2/simple.js | 10 + 23 files changed, 234 insertions(+), 182 deletions(-) create mode 100644 .rspec rename spec/support/cases/microformats.org/microformats-2/{unimplemented => }/microformats-2-2.html (100%) rename spec/support/cases/microformats.org/microformats-2/{unimplemented => }/microformats-2-2.js (100%) rename spec/support/cases/microformats.org/microformats-2/{unimplemented => }/microformats-2-4.html (100%) rename spec/support/cases/microformats.org/microformats-2/{unimplemented => }/microformats-2-4.js (100%) rename spec/support/cases/microformats.org/microformats-2/{unimplemented => }/microformats-2-8.html (100%) rename spec/support/cases/microformats.org/microformats-2/{unimplemented => }/microformats-2-8.js (100%) rename spec/support/cases/microformats.org/microformats-2/{unimplemented => }/microformats-2-9.html (100%) rename spec/support/cases/microformats.org/microformats-2/{unimplemented => }/microformats-2-9.js (100%) rename spec/support/{hentry-pauthor-hcard-nested.html => lib/microformats2/nested-format-with-property.html} (100%) create mode 100644 spec/support/lib/microformats2/nested-format-with-property.js rename spec/support/{hcard-hcard-nested.html => lib/microformats2/nested-format-without-property.html} (100%) rename spec/support/{hcard-pname-pnickname-nested.html => lib/microformats2/nested-property.html} (100%) create mode 100644 spec/support/lib/microformats2/nested-property.js rename spec/support/{hcard-simple.html => lib/microformats2/simple.html} (100%) create mode 100644 spec/support/lib/microformats2/simple.js diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..9d52c56 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format doc diff --git a/lib/microformats2/property_parser.rb b/lib/microformats2/property_parser.rb index 2f71d49..a51f473 100644 --- a/lib/microformats2/property_parser.rb +++ b/lib/microformats2/property_parser.rb @@ -34,7 +34,7 @@ module Microformats2 property = klass.new(element, property_class).parse properties = format_classes(element).empty? ? PropertyParser.parse(element.children) : [] - properties << property + [property].concat properties end end diff --git a/spec/lib/microformats2/collection_spec.rb b/spec/lib/microformats2/collection_spec.rb index 2aeeb49..6f12c6a 100644 --- a/spec/lib/microformats2/collection_spec.rb +++ b/spec/lib/microformats2/collection_spec.rb @@ -2,134 +2,145 @@ require "spec_helper" require "microformats2" describe Microformats2::Collection do - describe "case" do - cases_dir = "spec/support/cases/*" - Dir[File.join(cases_dir, "*")].each do |page_dir| - Dir[File.join(page_dir, "*")].keep_if { |f| f =~ /([.]html$)/ }.each do |html_file| - it "#{html_file}" do - json_file = html_file.gsub(/([.]html$)/, ".js") - html = open(html_file).read - json = open(json_file).read + describe "spec/support/lib/microformats2" do - JSON.parse(Microformats2.parse(html).to_json).should == JSON.parse(json) + describe "simple.html" do + before do + html = "spec/support/lib/microformats2/simple.html" + @collection = Microformats2.parse(html) + end + describe "#to_json" do + it "returns the correct JSON" do + json = "spec/support/lib/microformats2/simple.js" + json = open(json).read + JSON.parse(@collection.to_json).should == JSON.parse(json) + end + end + describe "#card" do + it "returns array of HCard objects" do + @collection.card.first.should be_kind_of HCard + end + end + describe "HCard#name parsed from '.h-card .p-name'" do + it "assigns Property from '.h-card .p-name' to HCard#name[]" do + @collection.first.name.first.should be_kind_of Microformats2::Property::Text + end + it "assigns inner_text to Property#value" do + @collection.first.name.first.value.should == "Jessica Lynn Suttles" + end + end + describe "HCard#url parsed from '.h-card .p-url'" do + it "assigns Property from '.h-card .p-url' to HCard#url[]" do + @collection.first.url.first.should be_kind_of Microformats2::Property::Url + end + it "assigns inner_text to Property#value" do + urls = ["http://flickr.com/jlsuttles", "http://twitter.com/jlsuttles"] + @collection.first.url.map(&:value).should == urls + end + end + describe "HCard#bday parsed from '.h-card .p-bday'" do + it "assigns Property from '.h-card .p-bday' to HCard#bday[]" do + @collection.first.bday.first.should be_kind_of Microformats2::Property::DateTime + end + it "assigns datetime attribute to Property#string_value" do + @collection.first.bday.first.value.to_s.should == "1990-10-15T20:45:33-08:00" + end + it "assigns DateTime object to Property#value" do + @collection.first.bday.first.value.should be_kind_of DateTime + end + end + describe "HCard#content parsed from '.h-card .p-content'" do + it "assigns Property from '.h-card .p-content' to HCard#content[]" do + @collection.first.content.first.should be_kind_of Microformats2::Property::Embedded + end + it "assigns inner_text to Property#value" do + @collection.first.content.first.value.should == "Vegan. Cat lover. Coder." + end + end + end + + describe "nested-property.html" do + before do + html = "spec/support/lib/microformats2/nested-property.html" + @collection = Microformats2.parse(html) + end + describe "#to_json" do + it "returns the correct JSON" do + json = "spec/support/lib/microformats2/nested-property.js" + json = open(json).read + JSON.parse(@collection.to_json).should == JSON.parse(json) + end + end + describe "#card" do + it "returns array of HCard objects" do + @collection.card.first.should be_kind_of HCard + end + end + describe "HCard#name parsed from '.h-card .p-name'" do + it "assigns Property from '.h-card .p-name' to HCard#name[]" do + @collection.first.name.first.should be_kind_of Microformats2::Property::Text + end + it "assigns inner_text to Property#value" do + @collection.first.name.first.value.should == "jlsuttles" + end + end + describe "HCard#nickname parsed from '.h-card .p-nickname'" do + it "assigns Property from '.h-card .p-nickname' to HCard#nickname[]" do + @collection.first.nickname.first.should be_kind_of Microformats2::Property::Text + end + it "assigns inner_text to Property#value" do + @collection.first.nickname.first.value.should == "jlsuttles" + end + end + end + + describe "nested-format-with-property.html" do + before do + html = "spec/support/lib/microformats2/nested-format-with-property.html" + @collection = Microformats2.parse(html) + end + describe "#to_json" do + it "returns the correct JSON" do + json = "spec/support/lib/microformats2/nested-format-with-property.js" + json = open(json).read + JSON.parse(@collection.to_json).should == JSON.parse(json) + end + end + describe "#entry" do + it "returns array of HEntry objects" do + @collection.entry.first.should be_kind_of HEntry + end + end + describe "HEntry#author parsed from '.h-entry .p-author.h-card'" do + it "assigns Property to HEntry#author[]" do + @collection.first.author.first.should be_kind_of Microformats2::Property::Text + end + it "assigns inner_text to Property#value" do + @collection.first.author.first.value.should == "Jessica Lynn Suttles" + end + it "assigns HCard to Property#formats[]" do + @collection.first.author.first.formats.first.should be_kind_of HCard end end end end - describe ".h-card simple" do - before do - html = "spec/support/hcard-simple.html" - @collection = Microformats2.parse(html) - end + # + # these cases were scraped from the internet using `rake specs:update` + # + describe "spec/support/cases" do + cases_dir = "spec/support/cases/*" + Dir[File.join(cases_dir, "*")].each do |page_dir| + describe page_dir.split("/")[-2..-1].join("/") do + Dir[File.join(page_dir, "*")].keep_if { |f| f =~ /([.]html$)/ }.each do |html_file| + it "#{html_file.split("/").last}" do + json_file = html_file.gsub(/([.]html$)/, ".js") + html = open(html_file).read + json = open(json_file).read - describe "#parse" do - it "creates ruby class HCard" do - @collection.all.first.should be_kind_of HCard - @collection.first.should be_kind_of HCard - @collection.card.first.should be_kind_of HCard - end - it "assigns .h-card .p-name to HCard#name" do - @collection.first.name.first.value.should == "Jessica Lynn Suttles" - end - it "assigns both .h-card .u-url to HCard#url" do - urls = ["http://flickr.com/jlsuttles", "http://twitter.com/jlsuttles"] - @collection.first.url.map(&:value).should == urls - end - it "assings .h-card .dt-bday to HCard#bday" do - @collection.first.bday.first.value.should be_kind_of DateTime - @collection.first.bday.first.value.to_s.should == "1990-10-15T20:45:33-08:00" - end - it "assigns .h-card .e-content to HCard#content" do - @collection.first.content.first.value.should == "Vegan. Cat lover. Coder." - end - end - - describe "#to_hash" do - it "returns the correct Hash" do - hash = { - :items => [{ - :type => ["h-card"], - :properties => { - :url => ["http://flickr.com/jlsuttles", "http://twitter.com/jlsuttles"], - :name => ["Jessica Lynn Suttles"], - :bday => ["1990-10-15T20:45:33-08:00"], - :content => ["Vegan. Cat lover. Coder."] - } - }] - } - @collection.to_hash.should == hash - end - end - end - - describe ".h-entry .p-author.h-card nested" do - before do - html = "spec/support/hentry-pauthor-hcard-nested.html" - @collection = Microformats2.parse(html) - end - - describe "#parse" do - it "creates ruby class HEntry" do - @collection.all.first.should be_kind_of HEntry - @collection.first.should be_kind_of HEntry - @collection.entry.first.should be_kind_of HEntry - end - it "assigns .h-entry .p-author to HEntry#author" do - @collection.first.author.first.value.should == "Jessica Lynn Suttles" - end - end - - describe "#to_hash" do - it "returns the correct Hash" do - hash = { - :items => [{ - :type => ["h-entry"], - :properties => { - :author => [{ - :value => "Jessica Lynn Suttles", - :type => ["h-card", "h-org"], - :properties => { - :url => ["http://twitter.com/jlsuttles"], - :name => ["Jessica Lynn Suttles"] - } - }], - :name => ["Jessica Lynn Suttles"] - } - }] - } - @collection.to_hash.should == hash - end - end - end - - describe ".h-card .p-name .p-nickname nested" do - before do - html = "spec/support/hcard-pname-pnickname-nested.html" - @collection = Microformats2.parse(html) - end - - describe "#parse" do - it "assigns .h-card .p-name to HCard#name" do - @collection.first.name.first.value.should == "jlsuttles" - end - it "assigns .h-card .p-nickname to HCard#nickname" do - @collection.first.nickname.first.value.should == "jlsuttles" - end - end - - describe "#to_hash" do - it "returns the correct Hash" do - hash = { - :items => [{ - :type => ["h-card"], - :properties => { - :name => ["jlsuttles"], - :nickname => ["jlsuttles"] - } - }] - } - @collection.to_hash.should == hash + JSON.parse(Microformats2.parse(html).to_json).should == JSON.parse(json) + end + end end end end diff --git a/spec/lib/microformats2/implied_property/name_spec.rb b/spec/lib/microformats2/implied_property/name_spec.rb index 7241406..8cdb500 100644 --- a/spec/lib/microformats2/implied_property/name_spec.rb +++ b/spec/lib/microformats2/implied_property/name_spec.rb @@ -2,27 +2,29 @@ require "spec_helper" require "microformats2" describe Microformats2::ImpliedProperty::Name do - describe "name-pass.html" do - html = "spec/support/lib/microformats2/implied_property/name-pass.html" - collection = Microformats2.parse(html) - it "should have the correct number of formats" do - collection.all.length.should == 6 - end - collection.all.each_with_index do |format, index| - it "passes case #{index+1}" do - format.name.first.value.should == "Jessica" + describe "spec/support/lib/microformats/implied_property" do + describe "name-pass.html" do + html = "spec/support/lib/microformats2/implied_property/name-pass.html" + collection = Microformats2.parse(html) + it "should have 6 microformats" do + collection.all.length.should == 6 + end + collection.all.each_with_index do |format, index| + it "implies name to be 'Jessica' in case #{index+1}" do + format.name.first.value.should == "Jessica" + end end end - end - describe "name-fail.html" do - html = "spec/support/lib/microformats2/implied_property/name-fail.html" - collection = Microformats2.parse(html) - it "should have the correct number of formats" do - collection.all.length.should == 8 - end - collection.all.each_with_index do |format, index| - it "fails case #{index+1}" do - format.name.first.value.should == "" + describe "name-fail.html" do + html = "spec/support/lib/microformats2/implied_property/name-fail.html" + collection = Microformats2.parse(html) + it "should have 8 microformats" do + collection.all.length.should == 8 + end + collection.all.each_with_index do |format, index| + it "implies name to be '' in case #{index+1}" do + format.name.first.value.should == "" + end end end end diff --git a/spec/lib/microformats2/implied_property/photo_spec.rb b/spec/lib/microformats2/implied_property/photo_spec.rb index 0636934..b5c9afe 100644 --- a/spec/lib/microformats2/implied_property/photo_spec.rb +++ b/spec/lib/microformats2/implied_property/photo_spec.rb @@ -2,27 +2,29 @@ require "spec_helper" require "microformats2" describe Microformats2::ImpliedProperty::Photo do - describe "photo-pass.html" do - html = "spec/support/lib/microformats2/implied_property/photo-pass.html" - collection = Microformats2.parse(html) - it "should have the correct number of formats" do - collection.all.length.should == 6 - end - collection.all.each_with_index do |format, index| - it "passes case #{index+1}" do - format.photo.first.value.should == "http://gravatar.com/jlsuttles" + describe "spec/support/lib/microformats/implied_property" do + describe "photo-pass.html" do + html = "spec/support/lib/microformats2/implied_property/photo-pass.html" + collection = Microformats2.parse(html) + it "should have 6 microformats" do + collection.all.length.should == 6 + end + collection.all.each_with_index do |format, index| + it "implies photo to be 'http://gravatar.com/jlsuttles' in case #{index+1}" do + format.photo.first.value.should == "http://gravatar.com/jlsuttles" + end end end - end - describe "photo-fail.html" do - html = "spec/support/lib/microformats2/implied_property/photo-fail.html" - collection = Microformats2.parse(html) - it "should have the correct number of formats" do - collection.all.length.should == 8 - end - collection.all.each_with_index do |format, index| - it "fails case #{index+1}" do - format.photo.should be_nil + describe "photo-fail.html" do + html = "spec/support/lib/microformats2/implied_property/photo-fail.html" + collection = Microformats2.parse(html) + it "should have 8 microformats" do + collection.all.length.should == 8 + end + collection.all.each_with_index do |format, index| + it "implies photo to be nil in case #{index+1}" do + format.photo.should be_nil + end end end end diff --git a/spec/lib/microformats2/implied_property/url_spec.rb b/spec/lib/microformats2/implied_property/url_spec.rb index a729093..b8a39cf 100644 --- a/spec/lib/microformats2/implied_property/url_spec.rb +++ b/spec/lib/microformats2/implied_property/url_spec.rb @@ -2,27 +2,29 @@ require "spec_helper" require "microformats2" describe Microformats2::ImpliedProperty::Url do - describe "url-pass.html" do - html = "spec/support/lib/microformats2/implied_property/url-pass.html" - collection = Microformats2.parse(html) - it "should have the correct number of formats" do - collection.all.length.should == 2 - end - collection.all.each_with_index do |format, index| - it "passes case #{index+1}" do - format.url.first.value.should == "http://github.com/jlsuttles" + describe "spec/support/lib/microformats/implied_property" do + describe "url-pass.html" do + html = "spec/support/lib/microformats2/implied_property/url-pass.html" + collection = Microformats2.parse(html) + it "should have 2 microformats" do + collection.all.length.should == 2 + end + collection.all.each_with_index do |format, index| + it "implies url to be 'http://github.com/jlsuttles' in case #{index+1}" do + format.url.first.value.should == "http://github.com/jlsuttles" + end end end - end - describe "url-fail.html" do - html = "spec/support/lib/microformats2/implied_property/url-fail.html" - collection = Microformats2.parse(html) - it "should have the correct number of formats" do - collection.all.length.should == 2 - end - collection.all.each_with_index do |format, index| - it "fails case #{index+1}" do - format.url.should be_nil + describe "url-fail.html" do + html = "spec/support/lib/microformats2/implied_property/url-fail.html" + collection = Microformats2.parse(html) + it "should have 2 microformats" do + collection.all.length.should == 2 + end + collection.all.each_with_index do |format, index| + it "implies url to be nil in case #{index+1}" do + format.url.should be_nil + end end end end diff --git a/spec/lib/microformats2_spec.rb b/spec/lib/microformats2_spec.rb index 3b2ca52..613ac75 100644 --- a/spec/lib/microformats2_spec.rb +++ b/spec/lib/microformats2_spec.rb @@ -19,7 +19,7 @@ describe Microformats2 do Microformats2.read_html(@html).should include @html end it "can be a file path to html" do - html = "spec/support/hcard-simple.html" + html = "spec/support/lib/microformats2/simple.html" Microformats2.read_html(html).should include "
" end it "can be a url to html" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 23dfd5e..7034fa3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,6 +6,5 @@ require 'rspec' require 'rspec/autorun' RSpec.configure do |config| - # config.order = "random" - config.color = true + config.order = "random" end diff --git a/spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-2.html b/spec/support/cases/microformats.org/microformats-2/microformats-2-2.html similarity index 100% rename from spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-2.html rename to spec/support/cases/microformats.org/microformats-2/microformats-2-2.html diff --git a/spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-2.js b/spec/support/cases/microformats.org/microformats-2/microformats-2-2.js similarity index 100% rename from spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-2.js rename to spec/support/cases/microformats.org/microformats-2/microformats-2-2.js diff --git a/spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-4.html b/spec/support/cases/microformats.org/microformats-2/microformats-2-4.html similarity index 100% rename from spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-4.html rename to spec/support/cases/microformats.org/microformats-2/microformats-2-4.html diff --git a/spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-4.js b/spec/support/cases/microformats.org/microformats-2/microformats-2-4.js similarity index 100% rename from spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-4.js rename to spec/support/cases/microformats.org/microformats-2/microformats-2-4.js diff --git a/spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-8.html b/spec/support/cases/microformats.org/microformats-2/microformats-2-8.html similarity index 100% rename from spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-8.html rename to spec/support/cases/microformats.org/microformats-2/microformats-2-8.html diff --git a/spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-8.js b/spec/support/cases/microformats.org/microformats-2/microformats-2-8.js similarity index 100% rename from spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-8.js rename to spec/support/cases/microformats.org/microformats-2/microformats-2-8.js diff --git a/spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-9.html b/spec/support/cases/microformats.org/microformats-2/microformats-2-9.html similarity index 100% rename from spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-9.html rename to spec/support/cases/microformats.org/microformats-2/microformats-2-9.html diff --git a/spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-9.js b/spec/support/cases/microformats.org/microformats-2/microformats-2-9.js similarity index 100% rename from spec/support/cases/microformats.org/microformats-2/unimplemented/microformats-2-9.js rename to spec/support/cases/microformats.org/microformats-2/microformats-2-9.js diff --git a/spec/support/hentry-pauthor-hcard-nested.html b/spec/support/lib/microformats2/nested-format-with-property.html similarity index 100% rename from spec/support/hentry-pauthor-hcard-nested.html rename to spec/support/lib/microformats2/nested-format-with-property.html diff --git a/spec/support/lib/microformats2/nested-format-with-property.js b/spec/support/lib/microformats2/nested-format-with-property.js new file mode 100644 index 0000000..f38e461 --- /dev/null +++ b/spec/support/lib/microformats2/nested-format-with-property.js @@ -0,0 +1,15 @@ +{ "items": [{ + "type": ["h-entry"], + "properties": { + "author": [{ + "value": "Jessica Lynn Suttles", + "type": ["h-card", "h-org"], + "properties": { + "url": ["http://twitter.com/jlsuttles"], + "name": ["Jessica Lynn Suttles"] + } + }], + "name": ["Jessica Lynn Suttles"] + } + }] +} diff --git a/spec/support/hcard-hcard-nested.html b/spec/support/lib/microformats2/nested-format-without-property.html similarity index 100% rename from spec/support/hcard-hcard-nested.html rename to spec/support/lib/microformats2/nested-format-without-property.html diff --git a/spec/support/hcard-pname-pnickname-nested.html b/spec/support/lib/microformats2/nested-property.html similarity index 100% rename from spec/support/hcard-pname-pnickname-nested.html rename to spec/support/lib/microformats2/nested-property.html diff --git a/spec/support/lib/microformats2/nested-property.js b/spec/support/lib/microformats2/nested-property.js new file mode 100644 index 0000000..b29a1d8 --- /dev/null +++ b/spec/support/lib/microformats2/nested-property.js @@ -0,0 +1,9 @@ +{ "items": [{ + "type": ["h-card"], + "properties": { + "name": ["jlsuttles"], + "nickname": ["jlsuttles"] + } + }] +} + diff --git a/spec/support/hcard-simple.html b/spec/support/lib/microformats2/simple.html similarity index 100% rename from spec/support/hcard-simple.html rename to spec/support/lib/microformats2/simple.html diff --git a/spec/support/lib/microformats2/simple.js b/spec/support/lib/microformats2/simple.js new file mode 100644 index 0000000..47e4b09 --- /dev/null +++ b/spec/support/lib/microformats2/simple.js @@ -0,0 +1,10 @@ +{ "items": [{ + "type": ["h-card"], + "properties": { + "url": ["http://flickr.com/jlsuttles", "http://twitter.com/jlsuttles"], + "name": ["Jessica Lynn Suttles"], + "bday": ["1990-10-15T20:45:33-08:00"], + "content": ["Vegan. Cat lover. Coder."] + } + }] +}