Hrb: not as good as I thought

This commit is contained in:
2019-06-18 20:17:37 -04:00
commit 517b911eea
26 changed files with 1152 additions and 0 deletions

23
test/fixtures/basic.html.rb vendored Normal file
View File

@@ -0,0 +1,23 @@
# frozen_string_literal: true
doctype
html do
head do
title "Hello!"
meta charset: "utf-8"
meta name: "viewport", content: "width=device-width, initial-scale=1"
link rel: "stylesheet", href: "/test.css"
meta name: "created", content: created
end
body do
div(id: "content") do
div(class: "container") do
h1("Hello!")
numbers.each do |i|
div("This is div ##{i}")
end
end
end
end
end

45
test/fixtures/complex.html.rb vendored Normal file
View File

@@ -0,0 +1,45 @@
# frozen_string_literal: true
def pet(name:, kind:, favorite_food:)
div(class: "pet") do
strong(name)
text(" the ")
em(kind)
text(" likes ")
em(favorite_food)
end
end
# rubocop:disable Metrics/BlockLength
doctype
html(lang: "en-us") do
head do
title "My pets"
meta "http-equiv" => "Content-Type", content: "text/html; charset=utf-8"
end
body do
header(class: "top") do
h1("My pets")
end
main(class: "content") do
subjects = [
{name: "Orion", kind: "dog", favorite_food: "icing"},
{name: "Finn", kind: "dog", favorite_food: "beef"},
{name: "Mao", kind: "cat", favorite_food: "canned cat food"},
{name: "Stella", kind: "cat", favorite_food: "hate"},
{name: "Teemo", kind: "cat", favorite_food: "tuna"},
{name: "Merlin", kind: "cat", favorite_food: "tuna"},
{name: "Taakoyaki", kind: "axolotl", favorite_food: "earthworms"},
]
subjects.each do |subject|
pet(name: subject[:name], kind: subject[:kind], favorite_food: subject[:favorite_food])
end
text("<script>console.log('This is escape')</script>")
end
end
end
# rubocop:enable Metrics/BlockLength

65
test/hrb/template_test.rb Normal file
View File

@@ -0,0 +1,65 @@
# frozen_string_literal: true
require "test_helper"
module Hrb
class TemplateTest < Minitest::Test
def test_render
assert rendered.css("#test > #top + #content").size == 1
assert rendered.css("#content > p").size == 2
end
def test_locals
html = Oga.parse_html(Hrb::Template.render(locals: {name: "Evan", date: "2019-06-17"}) do
strong("Hi, I am #{name}")
small("The date is #{date}")
end)
assert_equal "Hi, I am Evan", html.css("strong").text
assert_equal "The date is 2019-06-17", html.css("small").text
end
def test_scope
html = Oga.parse_html(Hrb::Template.render(scope: scope) do
h1("Hi, I am #{name}")
h2("The date is #{date}")
end)
assert_equal "Hi, I am Evan", html.css("h1").text
assert_equal "The date is 2019-06-17", html.css("h2").text
end
def test_to_s
template = Hrb::Template.new
template.instance_eval do
div("Hello!")
end
assert_equal "<div>Hello!</div>", template.to_s
end
private
Scope = Struct.new(:name, :date)
def scope
Scope.new("Evan", "2019-06-17")
end
# rubocop:disable Metrics/MethodLength
def rendered
Oga.parse_html(Hrb::Template.render do
div(id: "test") do
header(id: "top") do
h1("Hrb is HTML in Ruby")
end
main(id: "content") do
p("It's not a new idea, but it's neat.")
p("I hope you like it.")
end
end
end)
end
end
# rubocop:enable Metrics/MethodLength
end

35
test/hrb_test.rb Normal file
View File

@@ -0,0 +1,35 @@
# frozen_string_literal: true
require "test_helper"
class HrbTest < Minitest::Test
def test_render
assert_equal "Hello from Hrb!", rendered.css("h1").text
assert_equal "This is great!", rendered.css(".test-div > p").text
end
def test_render_file
assert_equal "2019-06-17", rendered_file.xpath("/html/head/meta[@name='created']/@content").first.value
assert_equal "This is div #1", rendered_file.css(".container > div:nth(1)").text
assert_equal "This is div #100", rendered_file.css(".container > div:last-child").text
end
private
def rendered
html = Hrb.render do
h1 "Hello from Hrb!"
h2("This is a test")
div(class: "test-div") do
p("This is great!")
end
end
Oga.parse_html(html)
end
def rendered_file
file = File.join(__dir__, "fixtures", "basic.html.rb")
Oga.parse_html Hrb.render_file(file, locals: {created: "2019-06-17", numbers: (1..100)})
end
end

9
test/test_helper.rb Normal file
View File

@@ -0,0 +1,9 @@
# frozen_string_literal: true
require "simplecov"
SimpleCov.start
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "hrb"
require "oga"
require "minitest/autorun"