Permalink
Browse files

filling out tests with helper assert

  • Loading branch information...
1 parent 89c6899 commit 98b214442e720356c5b6d93418e350f4048a76b7 @JackDanger committed Dec 22, 2009
Showing with 63 additions and 11 deletions.
  1. +7 −1 test/helper.rb
  2. +56 −10 test/test_inline_styles.rb
View
@@ -7,4 +7,10 @@
require 'inline_styles'
class Test::Unit::TestCase
-end
+ def assert_has_style(element, style)
+ assert element['style'].include?(style), "Expected #{element['style'].inspect} to include #{style.inspect}"
+ end
+ def assert_does_not_have_style(element, style)
+ assert !element['style'].include?(style), "Expected #{element['style'].inspect} to not include #{style.inspect}"
+ end
+end
View
@@ -4,25 +4,71 @@ class TestInlineStyles < Test::Unit::TestCase
CSS = <<-EOCSS
div {display: block;}
-div small {font-size: 0.6em}
+div small {font-size: 14px}
small {font-size: 0.7em}
img {border: none}
div small img {border: 1px solid #000}
EOCSS
- HTML - <<-EOHTML
-<div> Welcome </div>
-<small> stay awhile! </small>
-<div>
- <small>
- <img src='i.png'>
+ HTML = <<-EOHTML
+<body>
+ <div> Welcome </div>
+ <small> stay awhile! </small>
+ <div>
+ <small>
+ <img src='i.png'>
+ </small>
</small>
-</small>
+</body>
EOHTML
context "Applying CSS to HTML" do
- setup { }
- sh
+ setup {
+ @inline = InlineStyles::Page.new(HTML).apply(CSS)
+ @tree = Hpricot(@inline)
+ }
+ should "apply <div> style to each <div>" do
+ (@tree/:div).each do |element|
+ assert_has_style element, "display: block;"
+ end
+ end
+ should "apply specific style to nested <small>" do
+ assert_has_style(
+ @tree.at('div small'),
+ "font-size: 14px;"
+ )
+ end
+ should "apply generic <small> style to top-nested <small>" do
+ assert_has_style(
+ @tree.at('body > small'),
+ "font-size: 0.7em;"
+ )
+ end
+ should "not apply generic <small> style to nested <small>" do
+ assert_does_not_have_style(
+ @tree.at('div small'),
+ "font-size: 0.7em;"
+ )
+ end
+ should "not apply nested <small> style to top-level <small>" do
+ assert_does_not_have_style(
+ @tree.at('body > small'),
+ "font-size: 14px;"
+ )
+ end
+ should "apply border to deeply nested <img>" do
+ assert_has_style(
+ @tree.at('img'),
+ "border: 1px solid #000;"
+ )
+ end
+ should "not apply generic border style to <img>" do
+ puts @inline
+ assert_does_not_have_style(
+ @tree.at('img'),
+ "border: none;"
+ )
+ end
end
end

0 comments on commit 98b2144

Please sign in to comment.