From 8807c7660b60bd8148ab9e7db736ffbe8e30e149 Mon Sep 17 00:00:00 2001 From: Nick Quaranto Date: Thu, 5 Mar 2009 20:17:09 -0500 Subject: [PATCH 1/3] Starting conversion to shoulda --- test/helper.rb | 1 + test/test_post.rb | 126 +++++++++++++++++++++------------------------- 2 files changed, 58 insertions(+), 69 deletions(-) diff --git a/test/helper.rb b/test/helper.rb index 611d6d60..ed666df6 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -2,6 +2,7 @@ require File.join(File.dirname(__FILE__), *%w[.. lib jekyll]) require 'test/unit' require 'redgreen' +require 'shoulda' include Jekyll diff --git a/test/test_post.rb b/test/test_post.rb index 0a118df3..c05abfea 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -1,99 +1,87 @@ require File.dirname(__FILE__) + '/helper' class TestPost < Test::Unit::TestCase - def setup - - end - - def test_valid + should "ensure valid posts are valid" do assert Post.valid?("2008-10-19-foo-bar.textile") assert Post.valid?("foo/bar/2008-10-19-foo-bar.textile") - + assert !Post.valid?("lol2008-10-19-foo-bar.textile") assert !Post.valid?("blah") end - - def test_process - p = Post.allocate - p.process("2008-10-19-foo-bar.textile") - - assert_equal Time.parse("2008-10-19"), p.date - assert_equal "foo-bar", p.slug - assert_equal ".textile", p.ext - end - - def test_url - p = Post.allocate - p.categories = [] - p.process("2008-10-19-foo-bar.textile") - - assert_equal "/2008/10/19/foo-bar.html", p.url - end - - def test_permalink - p = Post.allocate - p.process("2008-12-03-permalinked-post.textile") - p.read_yaml(File.join(File.dirname(__FILE__), *%w[source _posts]), "2008-12-03-permalinked-post.textile") - assert_equal "my_category/permalinked-post", p.permalink + context "processing posts" do + setup do + @post = Post.allocate + @real_file = "2008-10-18-foo-bar.textile" + @fake_file = "2008-10-19-foo-bar.textile" + @source = File.join(File.dirname(__FILE__), *%w[source _posts]) + end + + should "keep date, title, and markup type" do + @post.process(@fake_file) + + assert_equal Time.parse("2008-10-19"), @post.date + assert_equal "foo-bar", @post.slug + assert_equal ".textile", @post.ext + end + + should "create url based on date and title" do + @post.categories = [] + @post.process(@fake_file) + assert_equal "/2008/10/19/foo-bar.html", @post.url + end + + should "respect permalink" do + file = "2008-12-03-permalinked-post.textile" + @post.process(file) + @post.read_yaml(@source, file) + + assert_equal "my_category/permalinked-post", @post.permalink + assert_equal "my_category/", @post.dir + assert_equal "my_category/permalinked-post", @post.url + end + + should "read yaml front-matter" do + @post.read_yaml(@source, @real_file) + + assert_equal({"title" => "Foo Bar", "layout" => "default"}, @post.data) + assert_equal "\nh1. {{ page.title }}\n\nBest *post* ever", @post.content + end + + should "transform textile" do + @post.process(@real_file) + @post.read_yaml(@source, @real_file) + @post.transform + + assert_equal "

{{ page.title }}

\n

Best post ever

", @post.content + end end - def test_dir_respects_permalink - p = Post.allocate - p.process("2008-12-03-permalinked-post.textile") - p.read_yaml(File.join(File.dirname(__FILE__), *%w[source _posts]), "2008-12-03-permalinked-post.textile") - assert_equal "my_category/", p.dir - end - - def test_url_respects_permalink - p = Post.allocate - p.process("2008-12-03-permalinked-post.textile") - p.read_yaml(File.join(File.dirname(__FILE__), *%w[source _posts]), "2008-12-03-permalinked-post.textile") - assert_equal "my_category/permalinked-post", p.url - end - - def test_read_yaml - p = Post.allocate - p.read_yaml(File.join(File.dirname(__FILE__), *%w[source _posts]), "2008-10-18-foo-bar.textile") - - assert_equal({"title" => "Foo Bar", "layout" => "default"}, p.data) - assert_equal "\nh1. {{ page.title }}\n\nBest *post* ever", p.content - end - - def test_transform - p = Post.allocate - p.process("2008-10-18-foo-bar.textile") - p.read_yaml(File.join(File.dirname(__FILE__), *%w[source _posts]), "2008-10-18-foo-bar.textile") - p.transform - - assert_equal "

{{ page.title }}

\n

Best post ever

", p.content - end - - def test_published + should "RENAME ME: test published" do p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-02-02-published.textile") assert_equal true, p.published end - def test_not_published + should "RENAME ME: test not published" do p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-02-02-not-published.textile") assert_equal false, p.published end - def test_yaml_category + should "RENAME ME: test yaml category" do p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2009-01-27-category.textile") assert p.categories.include?('foo') end - def test_yaml_categories + should "RENAME ME: test yaml categories" do p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2009-01-27-categories.textile") assert p.categories.include?('foo') assert p.categories.include?('bar') assert p.categories.include?('baz') end - def test_render + should "RENAME ME: test render" do p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-10-18-foo-bar.textile") layouts = {"default" => Layout.new(File.join(File.dirname(__FILE__), *%w[source _layouts]), "simple.html")} p.render(layouts, {"site" => {"posts" => []}}) @@ -101,7 +89,7 @@ class TestPost < Test::Unit::TestCase assert_equal "<<<

Foo Bar

\n

Best post ever

>>>", p.output end - def test_write + should "RENAME ME: test write" do clear_dest p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-10-18-foo-bar.textile") @@ -110,7 +98,7 @@ class TestPost < Test::Unit::TestCase p.write(dest_dir) end - def test_data + should "RENAME ME: test data" do p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-11-21-complex.textile") layouts = {"default" => Layout.new(File.join(File.dirname(__FILE__), *%w[source _layouts]), "simple.html")} p.render(layouts, {"site" => {"posts" => []}}) @@ -118,13 +106,13 @@ class TestPost < Test::Unit::TestCase assert_equal "<<<

url: /2008/11/21/complex.html
\ndate: #{Time.parse("2008-11-21")}
\nid: /2008/11/21/complex

>>>", p.output end - def test_categories_and_topics + should "RENAME ME: test categories and topics" do p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), 'foo', 'bar/2008-12-12-topical-post.textile') assert_equal ['foo'], p.categories assert_equal ['bar'], p.topics end - def test_include + should "RENAME ME: test include" do Jekyll.source = File.join(File.dirname(__FILE__), *%w[source]) p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-12-13-include.markdown") layouts = {"default" => Layout.new(File.join(File.dirname(__FILE__), *%w[source _layouts]), "simple.html")} From 0d05f27fe4abd834574292867a9a8c646db49fbc Mon Sep 17 00:00:00 2001 From: Nick Quaranto Date: Thu, 5 Mar 2009 20:54:33 -0500 Subject: [PATCH 2/3] Post conversion complete --- test/test_post.rb | 130 +++++++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 59 deletions(-) diff --git a/test/test_post.rb b/test/test_post.rb index c05abfea..e92618f8 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -52,72 +52,84 @@ class TestPost < Test::Unit::TestCase @post.process(@real_file) @post.read_yaml(@source, @real_file) @post.transform - + assert_equal "

{{ page.title }}

\n

Best post ever

", @post.content end end + context "initializing posts" do + setup do + @setup_post = lambda do |file| + Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', file) + end + end + should "publish when published yaml is no specified" do + post = @setup_post.call("2008-02-02-published.textile") + assert_equal true, post.published + end - should "RENAME ME: test published" do - p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-02-02-published.textile") - assert_equal true, p.published + should "not published when published yaml is false" do + post = @setup_post.call("2008-02-02-not-published.textile") + assert_equal false, post.published + end + + should "recognize category in yaml" do + post = @setup_post.call("2009-01-27-category.textile") + assert post.categories.include?('foo') + end + + should "recognize several categories in yaml" do + post = @setup_post.call("2009-01-27-categories.textile") + assert post.categories.include?('foo') + assert post.categories.include?('bar') + assert post.categories.include?('baz') + end + + context "rendering" do + setup do + clear_dest + @render = lambda do |post| + layouts = {"default" => Layout.new(File.join(File.dirname(__FILE__), *%w[source _layouts]), "simple.html")} + post.render(layouts, {"site" => {"posts" => []}}) + end + end + + should "render properly" do + post = @setup_post.call("2008-10-18-foo-bar.textile") + @render.call(post) + assert_equal "<<<

Foo Bar

\n

Best post ever

>>>", post.output + end + + should "write properly" do + post = @setup_post.call("2008-10-18-foo-bar.textile") + @render.call(post) + post.write(dest_dir) + + assert File.directory?(dest_dir) + assert File.exists?(File.join(dest_dir, '2008', '10', '18', 'foo-bar.html')) + end + + should "insert data" do + post = @setup_post.call("2008-11-21-complex.textile") + @render.call(post) + + assert_equal "<<<

url: /2008/11/21/complex.html
\ndate: #{Time.parse("2008-11-21")}
\nid: /2008/11/21/complex

>>>", post.output + end + + should "include templates" do + Jekyll.source = File.join(File.dirname(__FILE__), 'source') + post = @setup_post.call("2008-12-13-include.markdown") + @render.call(post) + + assert_equal "<<<
\n

Tom Preston-Werner github.com/mojombo

\n\n

This is cool

>>>", post.output + end + end end - should "RENAME ME: test not published" do - p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-02-02-not-published.textile") - assert_equal false, p.published - end - - should "RENAME ME: test yaml category" do - p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2009-01-27-category.textile") - assert p.categories.include?('foo') - end - - should "RENAME ME: test yaml categories" do - p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2009-01-27-categories.textile") - assert p.categories.include?('foo') - assert p.categories.include?('bar') - assert p.categories.include?('baz') - end - - should "RENAME ME: test render" do - p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-10-18-foo-bar.textile") - layouts = {"default" => Layout.new(File.join(File.dirname(__FILE__), *%w[source _layouts]), "simple.html")} - p.render(layouts, {"site" => {"posts" => []}}) - - assert_equal "<<<

Foo Bar

\n

Best post ever

>>>", p.output - end - - should "RENAME ME: test write" do - clear_dest - - p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-10-18-foo-bar.textile") - layouts = {"default" => Layout.new(File.join(File.dirname(__FILE__), *%w[source _layouts]), "simple.html")} - p.render(layouts, {"site" => {"posts" => []}}) - p.write(dest_dir) - end - - should "RENAME ME: test data" do - p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-11-21-complex.textile") - layouts = {"default" => Layout.new(File.join(File.dirname(__FILE__), *%w[source _layouts]), "simple.html")} - p.render(layouts, {"site" => {"posts" => []}}) - - assert_equal "<<<

url: /2008/11/21/complex.html
\ndate: #{Time.parse("2008-11-21")}
\nid: /2008/11/21/complex

>>>", p.output - end - - should "RENAME ME: test categories and topics" do - p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), 'foo', 'bar/2008-12-12-topical-post.textile') - assert_equal ['foo'], p.categories - assert_equal ['bar'], p.topics - end - - should "RENAME ME: test include" do - Jekyll.source = File.join(File.dirname(__FILE__), *%w[source]) - p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2008-12-13-include.markdown") - layouts = {"default" => Layout.new(File.join(File.dirname(__FILE__), *%w[source _layouts]), "simple.html")} - p.render(layouts, {"site" => {"posts" => []}}) - - assert_equal "<<<
\n

Tom Preston-Werner github.com/mojombo

\n\n

This is cool

>>>", p.output + should "generate categories and topics" do + post = Post.new(File.join(File.dirname(__FILE__), *%w[source]), 'foo', 'bar/2008-12-12-topical-post.textile') + assert_equal ['foo'], post.categories + assert_equal ['bar'], post.topics end end From 0e132bf2cbbb8dd00da297a6e992d4f501c1fd10 Mon Sep 17 00:00:00 2001 From: Nick Quaranto Date: Thu, 5 Mar 2009 21:43:59 -0500 Subject: [PATCH 3/3] Upgrading the rest of the tests to shoulda --- test/test_filters.rb | 60 ++++++++++++++++++------------------- test/test_generated_site.rb | 51 +++++++++++++++---------------- test/test_jekyll.rb | 0 test/test_site.rb | 57 +++++++++++++++++------------------ test/test_tags.rb | 26 ++++++++-------- 5 files changed, 96 insertions(+), 98 deletions(-) delete mode 100644 test/test_jekyll.rb diff --git a/test/test_filters.rb b/test/test_filters.rb index e6700191..e0893f03 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -1,41 +1,41 @@ require File.dirname(__FILE__) + '/helper' class TestFilters < Test::Unit::TestCase - class JekyllFilter include Jekyll::Filters end - - def setup - @filter = JekyllFilter.new - end - def test_textilize_with_simple_string - assert_equal "

something really simple

", @filter.textilize("something *really* simple") - end + context "filters" do + setup do + @filter = JekyllFilter.new + end - def test_array_to_sentence_string_with_no_args - assert_equal "", @filter.array_to_sentence_string([]) - end + should "textilize with simple string" do + assert_equal "

something really simple

", @filter.textilize("something *really* simple") + end - def test_array_to_sentence_string_with_one_arg - assert_equal "1", @filter.array_to_sentence_string([1]) - assert_equal "chunky", @filter.array_to_sentence_string(["chunky"]) + should "convert array to sentence string with no args" do + assert_equal "", @filter.array_to_sentence_string([]) + end + + should "convert array to sentence string with one arg" do + assert_equal "1", @filter.array_to_sentence_string([1]) + assert_equal "chunky", @filter.array_to_sentence_string(["chunky"]) + end + + should "convert array to sentence string with two args" do + assert_equal "1 and 2", @filter.array_to_sentence_string([1, 2]) + assert_equal "chunky and bacon", @filter.array_to_sentence_string(["chunky", "bacon"]) + end + + should "convert array to sentence string with multiple args" do + assert_equal "1, 2, 3, and 4", @filter.array_to_sentence_string([1, 2, 3, 4]) + assert_equal "chunky, bacon, bits, and pieces", @filter.array_to_sentence_string(["chunky", "bacon", "bits", "pieces"]) + end + + should "escape xml with ampersands" do + assert_equal "AT&T", @filter.xml_escape("AT&T") + assert_equal "<code>command &lt;filename&gt;</code>", @filter.xml_escape("command <filename>") + end end - - def test_array_to_sentence_string_with_two_args - assert_equal "1 and 2", @filter.array_to_sentence_string([1, 2]) - assert_equal "chunky and bacon", @filter.array_to_sentence_string(["chunky", "bacon"]) - end - - def test_array_to_sentence_string_with_multiple_args - assert_equal "1, 2, 3, and 4", @filter.array_to_sentence_string([1, 2, 3, 4]) - assert_equal "chunky, bacon, bits, and pieces", @filter.array_to_sentence_string(["chunky", "bacon", "bits", "pieces"]) - end - - def test_xml_escape_with_ampersands - assert_equal "AT&T", @filter.xml_escape("AT&T") - assert_equal "<code>command &lt;filename&gt;</code>", @filter.xml_escape("command <filename>") - end - end diff --git a/test/test_generated_site.rb b/test/test_generated_site.rb index 0762d299..a24584b7 100644 --- a/test/test_generated_site.rb +++ b/test/test_generated_site.rb @@ -1,32 +1,33 @@ require File.dirname(__FILE__) + '/helper' class TestGeneratedSite < Test::Unit::TestCase - def setup - clear_dest - @source = File.join(File.dirname(__FILE__), *%w[source]) - @s = Site.new(@source, dest_dir) - @s.process - @index = File.read(File.join(dest_dir, 'index.html')) - end - - def test_site_posts_in_index - # confirm that {{ site.posts }} is working - assert @index.include?("#{@s.posts.size} Posts") - end + context "generated sites" do + setup do + clear_dest + @source = File.join(File.dirname(__FILE__), *%w[source]) + @s = Site.new(@source, dest_dir) + @s.process + @index = File.read(File.join(dest_dir, 'index.html')) + end - def test_post_content_in_index - # confirm that the {{ post.content }} is rendered OK - latest_post = Dir[File.join(@source, '_posts/*')].last - post = Post.new(@source, '', File.basename(latest_post)) - Jekyll.content_type = post.determine_content_type - post.transform - assert @index.include?(post.content) - end + should "insert site.posts into the index" do + assert @index.include?("#{@s.posts.size} Posts") + end + + should "render post.content" do + latest_post = Dir[File.join(@source, '_posts/*')].last + post = Post.new(@source, '', File.basename(latest_post)) + Jekyll.content_type = post.determine_content_type + post.transform + assert @index.include?(post.content) + end + + should "hide unpublished posts" do + published = Dir[File.join(dest_dir, 'publish_test/2008/02/02/*.html')].map {|f| File.basename(f)} + + assert_equal 1, published.size + assert_equal "published.html", published.first + end - def test_unpublished_posts_are_hidden - published = Dir[File.join(dest_dir, 'publish_test/2008/02/02/*.html')].map {|f| File.basename(f)} - - assert_equal 1, published.size - assert_equal "published.html", published.first end end diff --git a/test/test_jekyll.rb b/test/test_jekyll.rb deleted file mode 100644 index e69de29b..00000000 diff --git a/test/test_site.rb b/test/test_site.rb index e19eefcd..8f32379c 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -1,36 +1,33 @@ require File.dirname(__FILE__) + '/helper' class TestSite < Test::Unit::TestCase - def setup - @source = File.join(File.dirname(__FILE__), *%w[source]) - @s = Site.new(@source, dest_dir) - end - - def test_site_init - - end - - def test_read_layouts - @s.read_layouts - - assert_equal ["default", "simple"].sort, @s.layouts.keys.sort - end - - def test_read_posts - @s.read_posts('') - posts = Dir[File.join(@source, '_posts/*')] - assert_equal posts.size - 1, @s.posts.size - end - - def test_site_payload - clear_dest - @s.process - - posts = Dir[File.join(@source, "**", "_posts/*")] - categories = %w(bar baz category foo z_category publish_test).sort + context "creating sites" do + setup do + @source = File.join(File.dirname(__FILE__), 'source') + @s = Site.new(@source, dest_dir) + end - assert_equal posts.size - 1, @s.posts.size - assert_equal categories, @s.categories.keys.sort - assert_equal 3, @s.categories['foo'].size + should "read layouts" do + @s.read_layouts + assert_equal ["default", "simple"].sort, @s.layouts.keys.sort + end + + should "read posts" do + @s.read_posts('') + posts = Dir[File.join(@source, '_posts/*')] + assert_equal posts.size - 1, @s.posts.size + end + + should "deploy payload" do + clear_dest + @s.process + + posts = Dir[File.join(@source, "**", "_posts/*")] + categories = %w(bar baz category foo z_category publish_test).sort + + assert_equal posts.size - 1, @s.posts.size + assert_equal categories, @s.categories.keys.sort + assert_equal 3, @s.categories['foo'].size + end end end diff --git a/test/test_tags.rb b/test/test_tags.rb index 4e6ad795..e400b469 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -1,9 +1,9 @@ require File.dirname(__FILE__) + '/helper' class TestTags < Test::Unit::TestCase - - def setup - @content = <