From ff279fafff06d6eb4fe3683ba683ea13da055f29 Mon Sep 17 00:00:00 2001 From: Jason Garber Date: Thu, 15 Aug 2019 15:23:16 -0400 Subject: [PATCH] Spec cleanup (#110) * Update configuration with lower values * Update spec_helper, disabling WebMock net connections * Clean up and organize spec files --- .rubocop.yml | 7 +- .../absolute_uri_absolutize_spec.rb | 59 +++++++ spec/lib/microformats/absolute_uri_spec.rb | 61 -------- spec/lib/microformats/collection_spec.rb | 28 ++-- spec/lib/microformats/parser_result_spec.rb | 6 +- spec/lib/microformats/parser_spec.rb | 146 +++++++++--------- spec/lib/microformats/property_set_spec.rb | 36 ++--- spec/lib/microformats_parse_spec.rb | 7 + spec/lib/microformats_read_html_spec.rb | 29 ++++ spec/lib/microformats_spec.rb | 31 ---- spec/spec_helper.rb | 4 + 11 files changed, 209 insertions(+), 205 deletions(-) create mode 100644 spec/lib/microformats/absolute_uri_absolutize_spec.rb delete mode 100644 spec/lib/microformats/absolute_uri_spec.rb create mode 100644 spec/lib/microformats_parse_spec.rb create mode 100644 spec/lib/microformats_read_html_spec.rb delete mode 100644 spec/lib/microformats_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index 5928a37..a6fdd9d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -9,14 +9,11 @@ Metrics/BlockLength: Metrics/LineLength: Enabled: false -RSpec/ExampleLength: - Max: 15 - RSpec/MultipleExpectations: - Max: 10 + Max: 5 RSpec/NestedGroups: - Max: 6 + Max: 4 Style/Documentation: Enabled: false diff --git a/spec/lib/microformats/absolute_uri_absolutize_spec.rb b/spec/lib/microformats/absolute_uri_absolutize_spec.rb new file mode 100644 index 0000000..c1989e9 --- /dev/null +++ b/spec/lib/microformats/absolute_uri_absolutize_spec.rb @@ -0,0 +1,59 @@ +describe Microformats::AbsoluteUri, '#absolutize' do + subject { described_class.new(relative, base: base).absolutize } + + context 'when relative is nil' do + let(:relative) { nil } + let(:base) { 'http://example.com' } + + it { is_expected.to eq(base) } + end + + context 'when relative is an empty string' do + let(:relative) { '' } + let(:base) { 'http://example.com' } + + it { is_expected.to eq(base) } + end + + context 'when relative is a valid absolute URI' do + let(:base) { nil } + let(:relative) { 'http://google.com' } + + it { is_expected.to eq('http://google.com') } + end + + context 'when relative is a valid non-absolute URI' do + let(:relative) { 'bar/qux' } + + context 'when base is present but not absolute' do + let(:base) { 'foo' } + + it { is_expected.to eq('bar/qux') } + end + + context 'when base is present and absolute' do + let(:base) { 'http://google.com' } + + it { is_expected.to eq('http://google.com/bar/qux') } + end + + context 'when base is not present' do + let(:base) { nil } + + it { is_expected.to eq('bar/qux') } + end + + context 'when base has a subdir' do + let(:base) { 'http://google.com/asdf.html' } + + it { is_expected.to eq('http://google.com/bar/qux') } + end + end + + context 'when relative is an invalid URI' do + let(:base) { nil } + let(:relative) { 'git@github.com:indieweb/microformats-ruby.git' } + + it { is_expected.to eq(relative) } + end +end diff --git a/spec/lib/microformats/absolute_uri_spec.rb b/spec/lib/microformats/absolute_uri_spec.rb deleted file mode 100644 index 7dcd364..0000000 --- a/spec/lib/microformats/absolute_uri_spec.rb +++ /dev/null @@ -1,61 +0,0 @@ -describe Microformats::AbsoluteUri do - describe '#absolutize' do - subject { described_class.new(relative, base: base).absolutize } - - context 'when relative is nil' do - let(:relative) { nil } - let(:base) { 'http://example.com' } - - it { is_expected.to eq(base) } - end - - context 'when relative is an empty string' do - let(:relative) { '' } - let(:base) { 'http://example.com' } - - it { is_expected.to eq(base) } - end - - context 'when relative is a valid absolute URI' do - let(:base) { nil } - let(:relative) { 'http://google.com' } - - it { is_expected.to eq('http://google.com') } - end - - context 'when relative is a valid non-absolute URI' do - let(:relative) { 'bar/qux' } - - context 'when base is present but not absolute' do - let(:base) { 'foo' } - - it { is_expected.to eq('bar/qux') } - end - - context 'when base is present and absolute' do - let(:base) { 'http://google.com' } - - it { is_expected.to eq('http://google.com/bar/qux') } - end - - context 'when base is not present' do - let(:base) { nil } - - it { is_expected.to eq('bar/qux') } - end - - context 'when base has a subdir' do - let(:base) { 'http://google.com/asdf.html' } - - it { is_expected.to eq('http://google.com/bar/qux') } - end - end - - context 'when relative is an invalid URI' do - let(:base) { nil } - let(:relative) { 'git@github.com:indieweb/microformats-ruby.git' } - - it { is_expected.to eq('git@github.com:indieweb/microformats-ruby.git') } - end - end -end diff --git a/spec/lib/microformats/collection_spec.rb b/spec/lib/microformats/collection_spec.rb index 6427bf2..e61af6b 100644 --- a/spec/lib/microformats/collection_spec.rb +++ b/spec/lib/microformats/collection_spec.rb @@ -3,49 +3,51 @@ describe 'collection to hash or string' do let(:html) { '

Jessica Lynn Suttles

' } + let(:collection) { parser.parse(html) } it 'is accessible as a hash []' do - expect(Microformats.parse(html)['items'][0]['properties']['name'][0]).to eq('Jessica Lynn Suttles') + expect(collection['items'][0]['properties']['name'][0]).to eq('Jessica Lynn Suttles') end it 'can convert to hash' do - expect(Microformats.parse(html).to_hash['items'][0]['properties']['name'][0]).to eq('Jessica Lynn Suttles') + expect(collection.to_hash['items'][0]['properties']['name'][0]).to eq('Jessica Lynn Suttles') end it 'can convert to hash by to_h' do - expect(Microformats.parse(html).to_h['items'][0]['properties']['name'][0]).to eq('Jessica Lynn Suttles') + expect(collection.to_h['items'][0]['properties']['name'][0]).to eq('Jessica Lynn Suttles') end it 'converts to string' do - expect(Microformats.parse(html).to_s).to eq('{"items"=>[{"type"=>["h-card"], "properties"=>{"name"=>["Jessica Lynn Suttles"]}}], "rels"=>{}, "rel-urls"=>{}}') + expect(collection.to_s).to eq('{"items"=>[{"type"=>["h-card"], "properties"=>{"name"=>["Jessica Lynn Suttles"]}}], "rels"=>{}, "rel-urls"=>{}}') end end describe 'collection functions' do let(:html) { '

Jessica Lynn Suttles

homepage

sample

' } + let(:collection) { parser.parse(html) } it 'is has rels function' do - expect(Microformats.parse(html).rels['canonical'][0]).to eq('https://example.com/') + expect(collection.rels['canonical'][0]).to eq('https://example.com/') end it 'is has rel_urls function' do - expect(Microformats.parse(html).rel_urls['https://example.com/']['rels'][0]).to eq('canonical') + expect(collection.rel_urls['https://example.com/']['rels'][0]).to eq('canonical') end it 'has respond_to? function' do - expect(Microformats.parse(html)).to respond_to(:respond_to?) + expect(collection).to respond_to(:respond_to?) end it 'supports old parser function calls by h- name' do - expect(Microformats.parse(html).card.to_hash).to eq(Microformats.parse(html).items[0].to_hash) - expect(Microformats.parse(html).card(:all)[0].to_hash).to eq(Microformats.parse(html).items[0].to_hash) - expect(Microformats.parse(html).card(0).to_hash).to eq(Microformats.parse(html).items[0].to_hash) - expect(Microformats.parse(html).card(3).to_hash).to eq(Microformats.parse(html).items[0].to_hash) - expect(Microformats.parse(html).as_sample.to_hash).to eq(Microformats.parse(html).items[1].to_hash) + expect(collection.card.to_hash).to eq(collection.items[0].to_hash) + expect(collection.card(:all)[0].to_hash).to eq(collection.items[0].to_hash) + expect(collection.card(0).to_hash).to eq(collection.items[0].to_hash) + expect(collection.card(3).to_hash).to eq(collection.items[0].to_hash) + expect(collection.as_sample.to_hash).to eq(collection.items[1].to_hash) end it 'has an items function that returns an array of ParserResult objects' do - expect(Microformats.parse(html).items[0]).to be_kind_of(Microformats::ParserResult) + expect(collection.items[0]).to be_kind_of(Microformats::ParserResult) end end end diff --git a/spec/lib/microformats/parser_result_spec.rb b/spec/lib/microformats/parser_result_spec.rb index e94769e..fa20181 100644 --- a/spec/lib/microformats/parser_result_spec.rb +++ b/spec/lib/microformats/parser_result_spec.rb @@ -3,7 +3,7 @@ describe 'conversion functions' do let(:html) { '

Jessica Lynn Suttles

' } - let(:item) { Microformats.parse(html).items[0] } + let(:item) { parser.parse(html).items[0] } it 'is accessible as a hash []' do expect(item['properties']['name'][0]).to eq('Jessica Lynn Suttles') @@ -28,14 +28,14 @@ describe 'parser result functions' do let(:html) { '

Jessica Lynn Suttles

homepage
' } - let(:item) { Microformats.parse(html).items[0] } + let(:item) { parser.parse(html).items[0] } it 'has respond_to? function' do expect(item).to respond_to(:respond_to?) end it 'returns PropertySets' do - expect(item.properties).to be_kind_of Microformats::PropertySet + expect(item.properties).to be_kind_of(Microformats::PropertySet) end it 'supports old parser function calls by property name' do diff --git a/spec/lib/microformats/parser_spec.rb b/spec/lib/microformats/parser_spec.rb index e763f18..81be597 100644 --- a/spec/lib/microformats/parser_spec.rb +++ b/spec/lib/microformats/parser_spec.rb @@ -1,5 +1,15 @@ describe Microformats::Parser do - let(:parser) { described_class.new } + subject(:parser) { described_class.new } + + let(:base_url) { 'http://www.example.com' } + + let(:http_response_object) do + { + status: 200, + headers: { 'Content-Length': 3 }, + body: 'abc' + } + end describe '#http_headers' do it 'starts as a blank hash' do @@ -22,11 +32,9 @@ describe 'http response' do before do - stub_request(:get, 'http://www.example.com/') - .with(headers: { 'Accept': '*/*', 'User-Agent': 'Ruby' }) - .to_return(status: 200, body: 'abc', headers: { 'Content-Length': 3 }) + stub_request(:get, base_url).to_return(http_response_object) - parser.parse('http://www.example.com') + parser.parse(base_url) end it 'saves #http_headers' do @@ -42,10 +50,9 @@ describe '#bad_input' do describe 'space after url' do before do - stub_request(:get, 'http://www.example.com/') - .with(headers: { 'Accept': '*/*', 'User-Agent': 'Ruby' }) - .to_return(status: 200, body: 'abc', headers: { 'Content-Length': 3 }) - parser.parse('http://www.example.com ') + stub_request(:get, base_url).to_return(http_response_object) + + parser.parse("#{base_url} ") end it 'saves #http_body' do @@ -57,13 +64,10 @@ describe '#frozen_strings' do describe 'frozen url' do before do - stub_request(:get, 'http://www.example.com/') - .with(headers: { 'Accept': '*/*', 'User-Agent': 'Ruby' }) - .to_return(status: 200, body: 'abc', headers: { 'Content-Length': 3 }) + stub_request(:get, base_url).to_return(http_response_object) - url = 'http://www.example.com' - url.freeze - parser.parse(url) + base_url.freeze + parser.parse(base_url) end it 'saves #http_body' do @@ -75,92 +79,90 @@ let(:html) { '

Jessica Lynn Suttles

' } it 'returns Collection' do - expect(Microformats.parse(html)).to be_kind_of Microformats::Collection + expect(parser.parse(html)).to be_kind_of(Microformats::Collection) end end end describe 'edge cases' do - cases_dir = 'spec/support/lib/edge_cases/' + input_file_paths = Dir[File.expand_path('../../support/lib/edge_cases/*.html', __dir__)] - Dir[File.join(cases_dir, '*')].keep_if { |f| f =~ /([.]js$)/ }.each do |json_file| - it json_file.split('/').last.to_s do - html_file = json_file.gsub(/([.]js$)/, '.html') - html = File.read(html_file) - json = File.read(json_file) + input_file_paths.each do |input_file_path| + let(:input_file) { File.read(input_file_path) } + let(:output_file) { File.read(input_file_path.sub(/\.html$/, '.js')) } - expect(JSON.parse(Microformats.parse(html).to_json)).to eq(JSON.parse(json)) + it "parses #{input_file_path.split('/').last}" do + expect(JSON.parse(parser.parse(input_file).to_json)).to eq(JSON.parse(output_file)) end end end - describe 'microformat-tests/tests' do - cases_dir = 'vendor/tests/tests/*' - # cases_dir = 'vendor/tests/tests/microformats-mixed' - # cases_dir = 'vendor/tests/tests/microformats-v1' - # cases_dir = 'vendor/tests/tests/microformats-working' - # cases_dir = 'vendor/tests/tests/microformats-v2' #limit to only v2 for now + # rubocop:disable RSpec/ExampleLength + describe 'microformats test suite' do + let(:base_url) { 'http://example.com' } - 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 =~ /([.]json$)/ }.each do |json_file| - it json_file.split('/').last.to_s do + output_file_paths = Dir[File.expand_path('../../../vendor/tests/tests/**/*.json', __dir__)] - if json_file.match?(%r{/includes/}) - pending 'include-pattern are not implemented' + output_file_paths.each do |output_file_path| + input_file_path = output_file_path.sub(/\.json$/, '.html') - elsif json_file.match?(%r{/h-entry/urlincontent}) - pending 'known issue / this is an aspect of nokogiri / won\'t fix' + it "parses #{input_file_path.split('/')[-3..-1].join('/')}" do + if input_file_path.match?(%r{/includes/}) + pending 'include-pattern are not implemented' - elsif json_file.match?(%r{/rel/duplicate-rels}) - pending 'known issue / this is an aspect of nokogiri / won\'t fix' + elsif input_file_path.match?(%r{/h-entry/urlincontent}) + pending 'known issue / this is an aspect of nokogiri / won\'t fix' - #these tests are failing due to timestamp output not being correct - elsif json_file.match?(%r{/h-feed/simple}) - pending 'Known timestamp issue' + elsif input_file_path.match?(%r{/rel/duplicate-rels}) + pending 'known issue / this is an aspect of nokogiri / won\'t fix' - elsif json_file.match?(%r{/h-feed/implied-title}) - pending 'Known timestamp issue' + # these tests are failing due to timestamp output not being correct + elsif input_file_path.match?(%r{/h-feed/simple}) + pending 'Known timestamp issue' - elsif json_file.match?(%r{/h-entry/summarycontent}) - pending 'Known timestamp issue' + elsif input_file_path.match?(%r{/h-feed/implied-title}) + pending 'Known timestamp issue' - elsif json_file.match?(%r{/h-event/dates}) - pending 'Known timestamp issue' + elsif input_file_path.match?(%r{/h-entry/summarycontent}) + pending 'Known timestamp issue' - #these tests are failing due to whitespace in the test suite not being correct, currently an open PR to fix this - elsif json_file.match?(%r{/h-entry/mixedroots}) - pending 'Test Set whitespace issue' - elsif json_file.match?(%r{/hnews/minimum}) - pending 'Test Set whitespace issue' - elsif json_file.match?(%r{/hnews/all}) - pending 'Test Set whitespace issue' - elsif json_file.match?(%r{/hreview/vcard}) - pending 'Test Set whitespace issue' - elsif json_file.match?(%r{/hentry/summarycontent}) - pending 'Test Set whitespace issue' - elsif json_file.match?(%r{/hfeed/simple}) - pending 'Test Set whitespace issue' + elsif input_file_path.match?(%r{/h-event/dates}) + pending 'Known timestamp issue' + # these tests are failing due to whitespace in the test suite not being correct, currently an open PR to fix this + elsif input_file_path.match?(%r{/h-entry/mixedroots}) + pending 'Test Set whitespace issue' - elsif json_file.match?(%r{/h-card/impliedurlempty}) - pending 'trailing slash on url, need to look at this more' + elsif input_file_path.match?(%r{/hnews/minimum}) + pending 'Test Set whitespace issue' - elsif json_file.match?(%r{/hproduct/aggregate}) - pending 'not entirely sure what is going on here, other parsers all get different results too' + elsif input_file_path.match?(%r{/hnews/all}) + pending 'Test Set whitespace issue' - end + elsif input_file_path.match?(%r{/hreview/vcard}) + pending 'Test Set whitespace issue' - # pending 'These are dynamic tests that are not yet passing so commenting out for now' + elsif input_file_path.match?(%r{/hentry/summarycontent}) + pending 'Test Set whitespace issue' - html_file = json_file.gsub(/([.]json$)/, '.html') - html = File.read(html_file) - json = File.read(json_file) + elsif input_file_path.match?(%r{/hfeed/simple}) + pending 'Test Set whitespace issue' - expect(JSON.parse(Microformats.parse(html, base: 'http://example.com').to_json)).to eq(JSON.parse(json)) - end + elsif input_file_path.match?(%r{/h-card/impliedurlempty}) + pending 'trailing slash on url, need to look at this more' + + elsif input_file_path.match?(%r{/hproduct/aggregate}) + pending 'not entirely sure what is going on here, other parsers all get different results too' end + + # pending 'These are dynamic tests that are not yet passing so commenting out for now' + + input_file = File.read(input_file_path) + output_file = File.read(output_file_path) + + expect(JSON.parse(parser.parse(input_file, base: base_url).to_json)).to eq(JSON.parse(output_file)) end end end + # rubocop:enable RSpec/ExampleLength end diff --git a/spec/lib/microformats/property_set_spec.rb b/spec/lib/microformats/property_set_spec.rb index e312be8..d760418 100644 --- a/spec/lib/microformats/property_set_spec.rb +++ b/spec/lib/microformats/property_set_spec.rb @@ -1,28 +1,24 @@ describe Microformats::PropertySet do - let(:parser) { described_class.new } + let(:html) { '

Jessica Lynn Suttles

' } + let(:property_set) { Microformats.parse(html).items[0].properties } - describe 'conversion functions' do - let(:html) { '

Jessica Lynn Suttles

' } - let(:set) { Microformats.parse(html).items[0].properties } - - it 'is accessible as a hash []' do - expect(set['name'][0]).to eq('Jessica Lynn Suttles') - end + it 'is accessible as a hash []' do + expect(property_set['name'][0]).to eq('Jessica Lynn Suttles') + end - it 'can convert to hash' do - expect(set.to_hash['name'][0]).to eq('Jessica Lynn Suttles') - end + it 'can convert to hash' do + expect(property_set.to_hash['name'][0]).to eq('Jessica Lynn Suttles') + end - it 'can convert to hash by to_h' do - expect(set.to_h['name'][0]).to eq('Jessica Lynn Suttles') - end + it 'can convert to hash by to_h' do + expect(property_set.to_h['name'][0]).to eq('Jessica Lynn Suttles') + end - it 'converts to json' do - expect(set.to_json).to eq('{"name":["Jessica Lynn Suttles"]}') - end + it 'converts to json' do + expect(property_set.to_json).to eq('{"name":["Jessica Lynn Suttles"]}') + end - it 'converts to string' do - expect(set.to_s).to eq(set.to_h.to_s) - end + it 'converts to string' do + expect(property_set.to_s).to eq(property_set.to_h.to_s) end end diff --git a/spec/lib/microformats_parse_spec.rb b/spec/lib/microformats_parse_spec.rb new file mode 100644 index 0000000..ecf4a0b --- /dev/null +++ b/spec/lib/microformats_parse_spec.rb @@ -0,0 +1,7 @@ +describe Microformats, '.parse' do + let(:html) { '

Jessica Lynn Suttles

' } + + it 'returns Collection' do + expect(described_class.parse(html)).to be_kind_of(Microformats::Collection) + end +end diff --git a/spec/lib/microformats_read_html_spec.rb b/spec/lib/microformats_read_html_spec.rb new file mode 100644 index 0000000..f5eecdb --- /dev/null +++ b/spec/lib/microformats_read_html_spec.rb @@ -0,0 +1,29 @@ +describe Microformats, '.read_html' do + context 'when given a string of HTML' do + let(:input) { '

Jessica Lynn Suttles

' } + + it 'returns the HTML' do + expect(described_class.read_html(input)).to include(input) + end + end + + context 'when given a file path' do + let(:input) { 'spec/support/lib/microformats/simple.html' } + + it 'returns the HTML' do + expect(described_class.read_html(input)).to include('
') + end + end + + context 'when given a URL' do + let(:url) { 'https://example.com' } + + before do + stub_request(:get, url).to_return(status: 200, body: 'example') + end + + it 'returns the HTML' do + expect(described_class.read_html(url)).to eq('example') + end + end +end diff --git a/spec/lib/microformats_spec.rb b/spec/lib/microformats_spec.rb deleted file mode 100644 index 5e3b02f..0000000 --- a/spec/lib/microformats_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -describe Microformats do - let(:html) { '

Jessica Lynn Suttles

' } - - describe '::parse' do - it 'returns Collection' do - expect(described_class.parse(html)).to be_kind_of(Microformats::Collection) - end - end - - describe '::read_html' do - it 'can be a string of html' do - expect(described_class.read_html(html)).to include(html) - end - - it 'can be a file path to html' do - html = 'spec/support/lib/microformats/simple.html' - - expect(described_class.read_html(html)).to include('
') - end - - it 'can be a url to html' do - stub_request(:get, 'http://google.com/') - .with(headers: { 'Accept': '*/*', 'User-Agent': 'Ruby' }) - .to_return(status: 200, body: 'google', headers: {}) - - html = 'http://google.com' - - expect(described_class.read_html(html)).to include('google') - end - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 24916cb..eaa6fc6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,8 @@ +$LOAD_PATH.unshift File.expand_path('../lib', __dir__) + require 'simplecov' require 'webmock/rspec' require 'microformats' + +WebMock.disable_net_connect!