Skip to content

Commit

Permalink
Include full Spec DSL in Bridgetown::Test
Browse files Browse the repository at this point in the history
transition some "shoulda" style tests to Minitest spec syntax
  • Loading branch information
jaredcwhite committed Dec 17, 2024
1 parent 2bcfba9 commit 25377a2
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 97 deletions.
2 changes: 2 additions & 0 deletions bridgetown-builder/test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

require_relative "../../bridgetown-core/test/helper.rb"
require_relative "../lib/bridgetown-builder.rb"

Bridgetown::Builder # fix autoload weirdness when running certain tests
14 changes: 7 additions & 7 deletions bridgetown-builder/test/test_filters_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ def build
end

class TestFilterDSL < BridgetownUnitTest
context "adding a Liquid filter" do
setup do
describe "adding a Liquid filter" do
before do
@site = Site.new(site_configuration)
@builder = FiltersBuilder.new("FiltersDSL", @site).build_with_callbacks
end

should "output the filter result" do
it "output the filter result" do
content = "2 times 2 equals {{ 2 | multiply_by_2 }}"
result = Liquid::Template.parse(content).render
assert_equal "2 times 2 equals 4", result
end

should "output the filter result based on argument" do
it "output the filter result based on argument" do
content = "5 times 10 equals {{ 5 | multiply_by_anything:10 }}"
result = Liquid::Template.parse(content).render
assert_equal "5 times 10 equals 50", result
end

should "support optional arguments" do
it "support optional arguments" do
content = "5 times 10 equals {{ 5 | multiply_and_optionally_add:10 }}"
result = Liquid::Template.parse(content).render
assert_equal "5 times 10 equals 50", result
Expand All @@ -59,13 +59,13 @@ class TestFilterDSL < BridgetownUnitTest
assert_equal "5 times 10 plus 3 equals 53", result
end

should "allow access to local builder scope" do
it "allow access to local builder scope" do
content = "root_dir: {{ 'is' | site_config }}"
result = Liquid::Template.parse(content).render
assert_equal "root_dir: is #{@site.root_dir}", result
end

should "allow access to filters scope" do
it "allow access to filters scope" do
content = "Scope? {{ 'howdy howdy' | within_filters_scope }}"
result = Liquid::Template.parse(content).render({}, registers: { site: @site })
assert_equal "Scope? Within Filters Scope: howdy-howdy #{@site.root_dir} 1", result
Expand Down
4 changes: 2 additions & 2 deletions bridgetown-builder/test/test_generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def build
end

class TestGenerators < BridgetownUnitTest
context "creating a generator" do
should "be loaded on site setup" do
describe "creating a generator" do
it "be loaded on site setup" do
@builders = [GeneratorBuilder, GeneratorBuilder2].sort
@site = Site.new(site_configuration)
@site.signals[:site_metadata] = { title: "Initial Value" }
Expand Down
9 changes: 5 additions & 4 deletions bridgetown-builder/test/test_helpers_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ def method_based(something)
class TestHelpers < BridgetownUnitTest
attr_reader :site

context "adding helpers" do
setup do
describe "adding helpers" do
before do
@site = Site.new(site_configuration)
@builder = HelpersBuilder.new("HelpersBuilder", @site).build_with_callbacks
self.class.instance_variable_set(:@name, "TestHelpers") # reset so this works:
@resource = Bridgetown::Model::Base.build(self, :posts, "im-a-post.md", {
title: "I'm a post!",
date: "2019-05-01",
}).as_resource_in_collection
@erb_view = Bridgetown::ERBView.new(@resource)
end

should "allow execution with provided helpers scope" do
it "allow execution with provided helpers scope" do
content = "This is the <%= block_helper page.data[:title] %> helper"
tmpl = Tilt::ErubiTemplate.new(
outvar: "@_erbout"
Expand All @@ -41,7 +42,7 @@ class TestHelpers < BridgetownUnitTest
"Bridgetown::Site helper", result
end

should "work with methods" do
it "work with methods" do
content = "This is the <%= method_based page.data[:title] %> helper"
tmpl = Tilt::ErubiTemplate.new(
outvar: "@_erbout"
Expand Down
12 changes: 6 additions & 6 deletions bridgetown-builder/test/test_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ def run_after
end

class TestHooks < BridgetownUnitTest
context "builder hooks" do
setup do
describe "builder hooks" do
before do
@site = Site.new(site_configuration)
@builder = HooksBuilder.new("Hooks Test", @site).build_with_callbacks
end

should "be triggered" do
it "be triggered" do
@site.reset
@site.loaders_manager.unload_loaders
@site.setup
Expand All @@ -55,12 +55,12 @@ class TestHooks < BridgetownUnitTest
end
end

context "SiteBuilder" do
setup do
describe "SiteBuilder" do
before do
@site = Site.new(site_configuration)
end

should "be loaded" do
it "be loaded" do
@site.reset
@site.loaders_manager.unload_loaders
@site.setup
Expand Down
16 changes: 8 additions & 8 deletions bridgetown-builder/test/test_http_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ def test_redirect
end

class TestHTTPDSL < BridgetownUnitTest
context "dsl for http requests" do
setup do
describe "dsl for http requests" do
before do
@site = Site.new(site_configuration)
@builder = HTTPBuilder.new("Hooks Test", @site).build_with_callbacks
end

should "add data from external API" do
it "add data from external API" do
@builder.stubs.get("/test.json") do |_env|
[
200,
Expand All @@ -74,7 +74,7 @@ class TestHTTPDSL < BridgetownUnitTest
assert_equal "received", @site.config[:received_data]["data"]["was"].first
end

should "not add data from bad external API" do
it "not add data from bad external API" do
@builder.stubs.get("/test_bad.json") do |_env|
[
200,
Expand All @@ -92,7 +92,7 @@ class TestHTTPDSL < BridgetownUnitTest
"Faraday::ParsingError The response from /test_bad.json did not contain valid JSON"
end

should "not parse JSON if parse_json is false" do
it "not parse JSON if parse_json is false" do
@builder.stubs.get("/test_not_parsing.html") do |_env|
[
200,
Expand All @@ -106,7 +106,7 @@ class TestHTTPDSL < BridgetownUnitTest
assert_equal '[1, 2, ["three"]]', @site.config[:received_data]
end

should "redirect automatically" do
it "redirect automatically" do
@builder.stubs.get("/test.json") do |_env|
[
200,
Expand All @@ -126,7 +126,7 @@ class TestHTTPDSL < BridgetownUnitTest
assert_equal "received", @site.config[:received_data]["data"]["was"].first
end

should "correctly pass headers to the GET request" do
it "correctly pass headers to the GET request" do
@builder.stubs.get("/test_headers.json") do |env|
[
200,
Expand All @@ -140,7 +140,7 @@ class TestHTTPDSL < BridgetownUnitTest
assert_equal "hello, world", @site.config[:received_headers]["X-Test"]
end

should "allows passing parameters to the GET request" do
it "allows passing parameters to the GET request" do
@builder.stubs.get("/test_parameters.json") do |env|
[
200,
Expand Down
24 changes: 13 additions & 11 deletions bridgetown-builder/test/test_inspectors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ def functions # stub to get hooks working

attr_reader :site

context "a resource after being transformed" do
setup do
describe "a resource after being transformed" do
before do
self.class.instance_variable_set(:@name, "TestInspectors") # reset
@site = Site.new(site_configuration)
@_test_functions = []

Expand All @@ -35,12 +36,12 @@ def functions # stub to get hooks working
end
end

teardown do
after do
@_html_inspectors = nil
@_xml_inspectors = nil
end

should "allow manipulation via Nokogiri" do
it "allow manipulation via Nokogiri" do
add_resource :posts, "html-inspectors.md" do
title "I'm a Markdown post!"
content <<~MARKDOWN
Expand All @@ -56,7 +57,7 @@ def functions # stub to get hooks working
resource.output.strip
end

should "bypass inspectors with special front matter variable" do
it "bypass inspectors with special front matter variable" do
add_resource :posts, "html-inspectors-bypass.md" do
title "I'm a Markdown post!"
bypass_inspectors true
Expand All @@ -73,7 +74,7 @@ def functions # stub to get hooks working
resource.output.strip
end

should "not mess up non-HTML resources" do
it "not mess up non-HTML resources" do
add_resource :posts, "no-html-inspectors.json" do
content <<~JSON
{ a: 1, b: "2" }
Expand All @@ -88,7 +89,7 @@ def functions # stub to get hooks working
resource.output.strip
end

should "work with XML resources too" do
it "work with XML resources too" do
add_resource :pages, "sample-feed.atom" do
content <<~XML
<?xml version="1.0" encoding="utf-8"?>
Expand Down Expand Up @@ -122,8 +123,9 @@ def functions # stub to get hooks working
end
end

context "a resource to transform using Nokolexbor" do
setup do
describe "a resource to transform using Nokolexbor" do
before do
self.class.instance_variable_set(:@name, "TestInspectors") # reset
@site = Site.new(site_configuration({ "html_inspector_parser" => "nokolexbor" }))
@_test_functions = []

Expand All @@ -142,12 +144,12 @@ def functions # stub to get hooks working
end
end

teardown do
after do
@_html_inspectors = nil
@_xml_inspectors = nil
end

should "allow manipulation via Nokolexbor" do
it "allow manipulation via Nokolexbor" do
add_resource :posts, "html-inspectors.md" do
title "I'm a Markdown post!"
content <<~MARKDOWN
Expand Down
12 changes: 6 additions & 6 deletions bridgetown-builder/test/test_method_symbols.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ def reset_hook(site)
end

class TestMethodSymbols < BridgetownUnitTest
context "adding tags, filters, generators, and hooks using method symbols" do
setup do
describe "adding tags, filters, generators, and hooks using method symbols" do
before do
@site = Site.new(site_configuration)
@builder = MethodSymbolsBuilder.new("MethodSymbols", @site).build_with_callbacks
end

should "load generator on site generate" do
it "load generator on site generate" do
@site.reset
@site.signals[:site_metadata] = { title: "Initial Value in Method Symbols" }
@site.loaders_manager.unload_loaders
Expand All @@ -46,19 +46,19 @@ class TestMethodSymbols < BridgetownUnitTest
assert_equal "Test Title in Method Symbols", @site.metadata[:title]
end

should "work with tags" do
it "work with tags" do
content = "This is the {% upcase_tag yay %}upcase{% endupcase_tag %} tag"
result = Liquid::Template.parse(content).render
assert_equal "This is the UPCASEYAY tag", result
end

should "output the filter result" do
it "output the filter result" do
content = "5 times 10 equals {{ 5 | multiply_by_anything:10 }}"
result = Liquid::Template.parse(content).render
assert_equal "5 times 10 equals 50", result
end

should "trigger hook" do
it "trigger hook" do
@site.reset
assert @site.config[:after_reset_hook_ran]
end
Expand Down
Loading

0 comments on commit 25377a2

Please sign in to comment.