Merge pull request #1427 from stevenosloan/add_specs_for_recursively_enhance

add specs for #1426 & reorganize Middleman::Util specs to a single file
This commit is contained in:
Thomas Reynolds 2015-01-15 23:02:29 +00:00
commit 46ebc9d518
4 changed files with 137 additions and 52 deletions

View file

@ -1,15 +0,0 @@
require 'middleman-core/util'
describe "Middleman::Util#binary?" do
%w(plain.txt unicode.txt unicode).each do |file|
it "recognizes #{file} as not binary" do
expect(Middleman::Util.binary?(File.join(File.dirname(__FILE__), "binary_spec/#{file}"))).to be false
end
end
%w(middleman.png middleman stars.svgz).each do |file|
it "recognizes #{file} as binary" do
expect(Middleman::Util.binary?(File.join(File.dirname(__FILE__), "binary_spec/#{file}"))).to be true
end
end
end

View file

@ -0,0 +1,41 @@
require 'spec_helper'
require 'middleman-core/core_extensions'
require 'middleman-core/core_extensions/data'
describe Middleman::CoreExtensions::Data do
end
describe Middleman::CoreExtensions::Data::DataStore do
describe "#key?" do
it "returns true if key included in local_data, local_sources, or callback_sources" do
subject = described_class.new instance_double("Middleman::Application"), Middleman::CoreExtensions::Data::DATA_FILE_MATCHER
subject.store :"foo-store", { foo: "bar" }
subject.callbacks :"foo-callback", Proc.new { "bar" }
subject.instance_variable_get(:@local_data)["foo-local"] = "bar"
expect( subject.key?("foo-store") ).to be_truthy
expect( subject.key?("foo-callback") ).to be_truthy
expect( subject.key?("foo-local") ).to be_truthy
end
it "returns false if key not in local_data, local_sources, or callback_sources" do
subject = described_class.new instance_double("Middleman::Application"), Middleman::CoreExtensions::Data::DATA_FILE_MATCHER
expect( subject.key?("foo-store") ).to be_falsy
expect( subject.key?("foo-callback") ).to be_falsy
expect( subject.key?("foo-local") ).to be_falsy
end
it "doesn't raise a stack error if missing the given key" do
subject = described_class.new instance_double("Middleman::Application"), Middleman::CoreExtensions::Data::DATA_FILE_MATCHER
expect{
subject.respond_to? :test
}.not_to raise_error
end
end
end

View file

@ -1,37 +0,0 @@
require 'middleman-core/util'
describe "Middleman::Util#path_match" do
it "matches a literal string" do
expect(Middleman::Util.path_match '/index.html', '/index.html').to be true
end
it "won't match a wrong string" do
expect(Middleman::Util.path_match '/foo.html', '/index.html').to be false
end
it "won't match a partial string" do
expect(Middleman::Util.path_match 'ind', '/index.html').to be false
end
it "works with a regex" do
expect(Middleman::Util.path_match /\.html$/, '/index.html').to be true
expect(Middleman::Util.path_match /\.js$/, '/index.html').to be false
end
it "works with a proc" do
matcher = lambda {|p| p.length > 5 }
expect(Middleman::Util.path_match matcher, '/index.html').to be true
expect(Middleman::Util.path_match matcher, '/i').to be false
end
it "works with globs" do
expect(Middleman::Util.path_match '/foo/*.html', '/foo/index.html').to be true
expect(Middleman::Util.path_match '/foo/*.html', '/foo/index.js').to be false
expect(Middleman::Util.path_match '/bar/*.html', '/foo/index.js').to be false
expect(Middleman::Util.path_match '/foo/*', '/foo/bar/index.html').to be true
expect(Middleman::Util.path_match '/foo/**/*', '/foo/bar/index.html').to be true
expect(Middleman::Util.path_match '/foo/**', '/foo/bar/index.html').to be true
end
end

View file

@ -0,0 +1,96 @@
require 'spec_helper'
require 'middleman-core/util'
describe Middleman::Util do
describe "::path_match" do
it "matches a literal string" do
expect(Middleman::Util.path_match '/index.html', '/index.html').to be true
end
it "won't match a wrong string" do
expect(Middleman::Util.path_match '/foo.html', '/index.html').to be false
end
it "won't match a partial string" do
expect(Middleman::Util.path_match 'ind', '/index.html').to be false
end
it "works with a regex" do
expect(Middleman::Util.path_match /\.html$/, '/index.html').to be true
expect(Middleman::Util.path_match /\.js$/, '/index.html').to be false
end
it "works with a proc" do
matcher = lambda {|p| p.length > 5 }
expect(Middleman::Util.path_match matcher, '/index.html').to be true
expect(Middleman::Util.path_match matcher, '/i').to be false
end
it "works with globs" do
expect(Middleman::Util.path_match '/foo/*.html', '/foo/index.html').to be true
expect(Middleman::Util.path_match '/foo/*.html', '/foo/index.js').to be false
expect(Middleman::Util.path_match '/bar/*.html', '/foo/index.js').to be false
expect(Middleman::Util.path_match '/foo/*', '/foo/bar/index.html').to be true
expect(Middleman::Util.path_match '/foo/**/*', '/foo/bar/index.html').to be true
expect(Middleman::Util.path_match '/foo/**', '/foo/bar/index.html').to be true
end
end
describe "::binary?" do
%w(plain.txt unicode.txt unicode).each do |file|
it "recognizes #{file} as not binary" do
expect(Middleman::Util.binary?(File.join(File.dirname(__FILE__), "binary_spec/#{file}"))).to be false
end
end
%w(middleman.png middleman stars.svgz).each do |file|
it "recognizes #{file} as binary" do
expect(Middleman::Util.binary?(File.join(File.dirname(__FILE__), "binary_spec/#{file}"))).to be true
end
end
end
describe "::recursively_enhance" do
it "returns HashWithIndifferentAccess if given one" do
input = Middleman::Util::HashWithIndifferentAccess.new({test: "subject"})
subject = Middleman::Util.recursively_enhance input
expect( subject ).to be_a Middleman::Util::HashWithIndifferentAccess
expect( subject.test ).to eq "subject"
end
it "returns HashWithIndifferentAccess if given a hash" do
input = {test: "subject"}
subject = Middleman::Util.recursively_enhance input
expect( subject ).to be_a Middleman::Util::HashWithIndifferentAccess
expect( subject.test ).to eq "subject"
end
it "returns Array with strings, or HashWithIndifferentAccess, true, false" do
indifferent_hash = Middleman::Util::HashWithIndifferentAccess.new({test: "subject"})
regular_hash = {regular: "hash"}
input = [ indifferent_hash, regular_hash, true, false ]
subject = Middleman::Util.recursively_enhance input
expect( subject[0] ).to be_a Middleman::Util::HashWithIndifferentAccess
expect( subject[1] ).to be_a Middleman::Util::HashWithIndifferentAccess
expect( subject[1].regular ).to eq "hash"
expect( subject[2] ).to eq true
expect( subject[3] ).to eq false
end
it "returns duplicated & frozen original object if not special cased" do
input = "foo"
subject = Middleman::Util.recursively_enhance input
expect( subject ).to eq input
expect( subject.object_id ).not_to eq input.object_id
expect( subject ).to be_frozen
end
end
end