Skip to content

Commit

Permalink
Make tests magic element centric
Browse files Browse the repository at this point in the history
  • Loading branch information
danott committed Jan 15, 2024
1 parent 42988dd commit f5c572a
Showing 1 changed file with 20 additions and 67 deletions.
87 changes: 20 additions & 67 deletions test/magic_comment_hydrator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,16 @@ class MagicCommentHydratorTest < Minitest::Test
<title>Test</title>
</head>
<body>
<!--IncludeInHeader::Text <meta name="single line text"> -->
<!--IncludeInHeader::Text
<meta name="multi line text">
-->
<!--IncludeInHeader::DummyIntendedForHeader-->
<h1>Test</h1>
<!--Include::DummyIntendedForInline-->
<!--Include::HydraIncludable-->
<h2><eval-ruby>2 + 2</eval-ruby></h2>
<h2><eval-ruby>self.data.fetch("Fetch this!")</eval-ruby></h2>
<include-in-header>
<meta name="include-in-header">
<eval-ruby>'<meta name="eval-ruby"' + ">"</eval-ruby>
<eval-ruby>
%Q[<meta name="\#{data.fetch("Fetch this!")}">]
</eval-ruby>
</include-in-header>
</body>
</html>
HTML
Expand All @@ -32,72 +26,31 @@ def test_everything
data: {
"Fetch this!" => "Return that!"
},
includables: {
"Text" => Includable::Text,
"HydraIncludable" => HydraIncludable,
"DummyIntendedForInline" => DummyIntendedForInline,
"DummyIntendedForHeader" => DummyIntendedForHeader
}
includables: []
)
result = hydrator.hydrate

refute_includes result.content, "<!--IncludeInHeader::Text "
assert_includes result.content, '<meta name="single line text">'
assert_includes result.content, '<meta name="multi line text">'

refute_includes result.content,
"<!--IncludeInHeader:DummyIntendedForHeader-->"
assert_includes result.content, '<meta name="DummyIntendedForHeader">'

refute_includes result.content, "<!--Include::DummyIntendedForInline-->"
assert_includes result.content,
"<h1>DummyIntendedForInline Return that!</h1>"

refute_includes result.content, "<!--Include::HydraIncludable-->"
assert_equal 2,
result
.content
.scan("<h1>DummyIntendedForInline Return that!</h1>")
.size

refute_includes result.content, "<eval-ruby>2 + 2</eval-ruby>"
assert_includes result.content, "<h2>4</h2>"

refute_includes result.content, "Fetch this!"
refute_includes result.content, "2 + 2"
refute_includes result.content, "<eval-ruby>"
refute_includes result.content, "</eval-ruby>"
refute_includes result.content, "<include-in-header>"
assert_includes result.content, '<meta name="include-in-header">'
assert_includes result.content, '<meta name="eval-ruby">'
assert result.content.index('name="eval-ruby"') <
result.content.index("</head>"),
"Should actually be injected into the head!"
end

class DummyIntendedForInline
attr_reader :data
refute_includes result.content, "</include-in-header>"

def initialize(string:, data:)
@data = data
end
assert_body_includes result.content, "<h2>4</h2>"
assert_body_includes result.content, "<h2>Return that!</h2>"

def render
"<h1>DummyIntendedForInline #{data.fetch("Fetch this!")}</h1>"
end
assert_head_includes result.content, '<meta name="include-in-header">'
assert_head_includes result.content, '<meta name="Return that!">'
end

class DummyIntendedForHeader
def initialize(string:, data:)
end

def render
%Q[<meta name="DummyIntendedForHeader">]
end
def assert_body_includes(haystack, needle)
body = haystack.split("<body>").last.split("</body>").first
assert_includes body, needle
end

class HydraIncludable
def initialize(string:, data:)
end

def render
"<!--Include::DummyIntendedForInline-->"
end
def assert_head_includes(haystack, needle)
head = haystack.split("<head>").last.split("</head>").first
assert_includes head, needle
end
end

0 comments on commit f5c572a

Please sign in to comment.