diff --git a/.rubocop.yml b/.rubocop.yml index 8192bf0b..7c34ff50 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -6,4 +6,7 @@ AllCops: TargetRubyVersion: 2.7 Style/ExplicitBlockArgument: - Enabled: false \ No newline at end of file + Enabled: false + +Style/MixinUsage: + Enabled: false diff --git a/gd/phlex/sgml/attributes.rb b/gd/phlex/sgml/attributes.rb new file mode 100644 index 00000000..81f16aec --- /dev/null +++ b/gd/phlex/sgml/attributes.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +include TestHelper + +class ToStrable + def to_str + "foo" + end +end + +test "with symbol-keyed hash attributes" do + component = build_component_with_template do + div data: { name: { first_name: "Joel" } } + end + + expect(component.new).to_render %(
) +end + +test "with string-keyed hash attributes" do + component = build_component_with_template do + div data: { "name" => { "first_name" => "Joel" } } + end + + expect(component.new).to_render %() +end + +test "with an array of symbols and strings" do + component = build_component_with_template do + div class: ["bg-red-500", :rounded] + end + + expect(component.new).to_render %() +end + +test "with a set of symbols and strings" do + component = build_component_with_template do + div class: Set.new(["bg-red-500", :rounded]) + end + + expect(component.new).to_render %() +end + +test "with a to_str-able object" do + component = build_component_with_template do + div class: ToStrable.new + end + + expect(component.new).to_render %() +end + +test "with numeric integer/float" do + component = build_component_with_template do + input type: "range", min: 0, max: 10, step: 0.5 + end + + expect(component.new).to_render %() +end + +if RUBY_ENGINE == "ruby" + context "with unique tag attributes" do + let def component + build_component_with_template do + div class: SecureRandom.hex + end + end + + let def report + component.call + + MemoryProfiler.report do + 2.times { component.call } + end + end + + test "doesn't leak memory" do + expect(report.total_retained) == 0 + end + end +end diff --git a/gd/support/helper.rb b/gd/support/helper.rb index 91ec90a9..292d1725 100644 --- a/gd/support/helper.rb +++ b/gd/support/helper.rb @@ -1,3 +1,23 @@ # frozen_string_literal: true require "phlex" +require "bundler" + +Bundler.require :test + +module TestHelper + def build_component_with_template(&block) + Class.new(Phlex::HTML) { define_method(:template, &block) } + end +end + +module ToRender + def to_render(expected_output) + output = subject.call + assert(output == expected_output) { "Expected `#{output.inspect}` to equal `#{expected_output.inspect}`." } + end +end + +GreenDots.configure do |config| + config.register_matcher ToRender, Phlex::SGML +end diff --git a/test/phlex/view/attributes.rb b/test/phlex/view/attributes.rb deleted file mode 100644 index dfcd217c..00000000 --- a/test/phlex/view/attributes.rb +++ /dev/null @@ -1,129 +0,0 @@ -# frozen_string_literal: true - -class ToStrable - def to_str - "foo" - end -end - -describe Phlex::HTML do - extend ViewHelper - - with "hash attributes" do - view do - def template - div data: { name: { first_name: "Joel" } } - end - end - - it "flattens the attributes" do - expect(output).to be == %() - end - end - - with "string keyed hash attributes" do - view do - def template - div data: { "name_first_name" => "Joel" } - end - end - - it "flattens the attributes without dasherizing them" do - expect(output).to be == %() - end - end - - with "an array of string attributes" do - view do - def template - div(class: %w(bg-red-500 rounded)) - end - end - - it "joins the array with a space" do - expect(output).to be == %() - end - end - - with "an array of symbol attributes" do - view do - def template - div(class: %i(bg-red-500 rounded)) - end - end - - it "joins the array with a space" do - expect(output).to be == %() - end - end - - with "an array of symbol and string attributes" do - view do - def template - div(class: ["bg-red-500", :rounded]) - end - end - - it "joins the array with a space" do - expect(output).to be == %() - end - end - - with "a set of string attributes" do - view do - def template - div(class: Set["bg-red-500", "rounded"]) - end - end - - it "joins the array with a space" do - expect(output).to be == %() - end - end - - with "an object that is not a boolean, String, Symbol, Array, or Hash" do - view do - def template - div(class: ToStrable.new) - end - end - - it "coerces the object to a string" do - expect(output).to be == %() - end - end - - with "an integer and a float" do - view do - def template - input type: "range", min: 0, max: 10, step: 0.5 - end - end - - it "converts the attribute values to strings" do - expect(output).to be == %() - end - end - - if RUBY_ENGINE == "ruby" - with "unique tag attributes" do - view do - def template - div class: SecureRandom.hex - end - end - - let :report do - view.new.call - - MemoryProfiler.report do - 2.times { view.new.call } - end - end - - it "doesn't leak memory" do - expect(report.total_retained).to be == 0 - end - end - end -end