Skip to content

Commit

Permalink
Use sgml helper
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Sep 23, 2024
1 parent 410af90 commit 0224f9f
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 163 deletions.
9 changes: 9 additions & 0 deletions fixtures/sgml_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module SGMLHelper
def phlex(component = Phlex::HTML, *, **, &)
component.new(*, **).call do |e|
e.instance_exec(&)
end
end
end
8 changes: 3 additions & 5 deletions quickdraw/sgml/attributes.test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# frozen_string_literal: true

def phlex(component = Phlex::HTML, *, **, &)
component.new(*, **).call do |e|
e.instance_exec(&)
end
end
require "sgml_helper"

include SGMLHelper

test "id attributes must be lower case symbols" do
expect { phlex { div("id" => "abc") } }.to_raise(Phlex::ArgumentError)
Expand Down
8 changes: 3 additions & 5 deletions quickdraw/sgml/comment.test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# frozen_string_literal: true

def phlex(component = Phlex::HTML, *, **, &)
component.new(*, **).call do |e|
e.instance_exec(&)
end
end
require "sgml_helper"

include SGMLHelper

test "text comment" do
expect(
Expand Down
3 changes: 1 addition & 2 deletions quickdraw/sgml/element_clobbering_guard.test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

class Example < Phlex::HTML
end
Example = Class.new(Phlex::HTML)

test "raises if you try to redefine an underscored method" do
expect { Example.define_method(:_h1) { nil } }.to_raise(Phlex::NameError)
Expand Down
64 changes: 21 additions & 43 deletions quickdraw/sgml/plain.test.rb
Original file line number Diff line number Diff line change
@@ -1,61 +1,39 @@
# frozen_string_literal: true

test "with string" do
component = Class.new(Phlex::HTML) do
define_method(:view_template) do
plain "Hello, World!"
end
end
require "sgml_helper"

include SGMLHelper

expect(component.new.call) == "Hello, World!"
test "with string" do
expect(
phlex { plain "Hello, World!" },
) == "Hello, World!"
end

test "with symbol" do
component = Class.new(Phlex::HTML) do
define_method(:view_template) do
plain :hello_world
end
end

expect(component.new.call) == "hello_world"
expect(
phlex { plain :hello_world },
) == "hello_world"
end

test "with integer" do
component = Class.new(Phlex::HTML) do
define_method(:view_template) do
plain 42
end
end

expect(component.new.call) == "42"
expect(
phlex { plain 42 },
) == "42"
end

test "with float" do
component = Class.new(Phlex::HTML) do
define_method(:view_template) do
plain 3.14
end
end

expect(component.new.call) == "3.14"
expect(
phlex { plain 3.14 },
) == "3.14"
end

test "with nil" do
component = Class.new(Phlex::HTML) do
define_method(:view_template) do
plain nil
end
end

expect(component.new.call) == ""
expect(
phlex { plain nil },
) == ""
end

test "with array" do
component = Class.new(Phlex::HTML) do
define_method(:view_template) do
plain [1, 2, 3]
end
end

expect { component.new.call }.to_raise(Phlex::ArgumentError)
test "with invalid arguments" do
expect { phlex { plain [] } }.to_raise(Phlex::ArgumentError)
end
36 changes: 7 additions & 29 deletions quickdraw/sgml/raw.test.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,23 @@
# frozen_string_literal: true

def render(&)
Phlex::HTML.new.call do |e|
e.instance_exec(&)
end
end
require "sgml_helper"

include SGMLHelper

test "with an unsafe object" do
expect {
render do
raw "<div></div>"
end
}.to_raise(Phlex::ArgumentError) do |error|
expect { phlex { raw "<div></div>" } }.to_raise(Phlex::ArgumentError) do |error|
expect(error.message) == "You passed an unsafe object to `raw`."
end
end

test "with a safe object" do
expect(
render do
raw safe %(<div class="hello">&</div>)
end,
) == %(<div class="hello">&</div>)
expect(phlex { raw safe %(<div class="hello">&</div>) }) == %(<div class="hello">&</div>)
end

test "with nil" do
expect(
render do
div do
raw nil
end
end,
) == %(<div></div>)
expect(phlex { div { raw nil } }) == %(<div></div>)
end

test "with empty string" do
expect(
render do
div do
raw ""
end
end,
) == %(<div></div>)
expect(phlex { div { raw "" } }) == %(<div></div>)
end
36 changes: 0 additions & 36 deletions quickdraw/sgml/render_enumerable.test.rb

This file was deleted.

8 changes: 3 additions & 5 deletions quickdraw/sgml/safe.test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# frozen_string_literal: true

def phlex(component = Phlex::HTML, *, **, &)
component.new(*, **).call do |e|
e.instance_exec(&)
end
end
require "sgml_helper"

include SGMLHelper

test "safe attribute values" do
expect(
Expand Down
24 changes: 0 additions & 24 deletions quickdraw/sgml/safe_value.test.rb

This file was deleted.

19 changes: 10 additions & 9 deletions quickdraw/sgml/to_proc.test.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
# frozen_string_literal: true

class App < Phlex::HTML
def view_template
render Example do |e|
e.slot(&Sub.new)
end
end
end
require "sgml_helper"
include SGMLHelper

class Example < Phlex::HTML
def view_template(&)
Expand All @@ -24,6 +19,12 @@ def view_template
end
end

test do
expect(App.new.call) == "<article><h1>Sub</h1></article>"
test "rendering components via #to_proc" do
expect(
phlex {
render Example do |e|
e.slot(&Sub.new)
end
},
) == "<article><h1>Sub</h1></article>"
end
8 changes: 3 additions & 5 deletions quickdraw/sgml/whitespace.test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# frozen_string_literal: true

def phlex(component = Phlex::HTML, *, **, &)
component.new(*, **).call do |e|
e.instance_exec(&)
end
end
require "sgml_helper"

include SGMLHelper

test "whitespae between" do
expect(
Expand Down

0 comments on commit 0224f9f

Please sign in to comment.