2009-03-25 21:42:22 -07:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
require File.dirname(__FILE__) + '/../test_helper'
|
2009-04-30 14:22:05 -07:00
|
|
|
require 'pathname'
|
2009-03-25 21:42:22 -07:00
|
|
|
|
|
|
|
class UtilTest < Test::Unit::TestCase
|
|
|
|
include Haml::Util
|
|
|
|
|
2009-04-30 14:22:05 -07:00
|
|
|
def test_scope
|
|
|
|
assert(File.exist?(scope("Rakefile")))
|
|
|
|
end
|
|
|
|
|
2009-03-25 21:42:22 -07:00
|
|
|
def test_to_hash
|
|
|
|
assert_equal({
|
|
|
|
:foo => 1,
|
|
|
|
:bar => 2,
|
|
|
|
:baz => 3
|
|
|
|
}, to_hash([[:foo, 1], [:bar, 2], [:baz, 3]]))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_keys
|
|
|
|
assert_equal({
|
|
|
|
"foo" => 1,
|
|
|
|
"bar" => 2,
|
|
|
|
"baz" => 3
|
|
|
|
}, map_keys({:foo => 1, :bar => 2, :baz => 3}) {|k| k.to_s})
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_vals
|
|
|
|
assert_equal({
|
|
|
|
:foo => "1",
|
|
|
|
:bar => "2",
|
|
|
|
:baz => "3"
|
|
|
|
}, map_vals({:foo => 1, :bar => 2, :baz => 3}) {|k| k.to_s})
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_hash
|
|
|
|
assert_equal({
|
|
|
|
"foo" => "1",
|
|
|
|
"bar" => "2",
|
|
|
|
"baz" => "3"
|
|
|
|
}, map_hash({:foo => 1, :bar => 2, :baz => 3}) {|k, v| [k.to_s, v.to_s]})
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_powerset
|
2009-04-05 00:44:13 -07:00
|
|
|
return unless Set[Set[]] == Set[Set[]] # There's a bug in Ruby 1.8.6 that breaks nested set equality
|
2009-03-25 21:42:22 -07:00
|
|
|
assert_equal([[].to_set].to_set,
|
|
|
|
powerset([]))
|
|
|
|
assert_equal([[].to_set, [1].to_set].to_set,
|
|
|
|
powerset([1]))
|
|
|
|
assert_equal([[].to_set, [1].to_set, [2].to_set, [1, 2].to_set].to_set,
|
|
|
|
powerset([1, 2]))
|
|
|
|
assert_equal([[].to_set, [1].to_set, [2].to_set, [3].to_set,
|
|
|
|
[1, 2].to_set, [2, 3].to_set, [1, 3].to_set, [1, 2, 3].to_set].to_set,
|
|
|
|
powerset([1, 2, 3]))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has
|
|
|
|
assert(has?(:instance_method, String, :chomp!))
|
|
|
|
assert(has?(:private_instance_method, Haml::Engine, :set_locals))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_enum_with_index
|
|
|
|
assert_equal(%w[foo0 bar1 baz2],
|
|
|
|
enum_with_index(%w[foo bar baz]).map {|s, i| "#{s}#{i}"})
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_def_static_method
|
|
|
|
klass = Class.new
|
|
|
|
def_static_method(klass, :static_method, [:arg1, :arg2],
|
|
|
|
:sarg1, :sarg2, <<RUBY)
|
|
|
|
s = "Always " + arg1
|
|
|
|
s << " <% if sarg1 %>and<% else %>but never<% end %> " << arg2
|
|
|
|
|
|
|
|
<% if sarg2 %>
|
|
|
|
s << "."
|
|
|
|
<% end %>
|
|
|
|
RUBY
|
|
|
|
c = klass.new
|
|
|
|
assert_equal("Always brush your teeth and comb your hair.",
|
|
|
|
c.send(static_method_name(:static_method, true, true),
|
|
|
|
"brush your teeth", "comb your hair"))
|
|
|
|
assert_equal("Always brush your teeth and comb your hair",
|
|
|
|
c.send(static_method_name(:static_method, true, false),
|
|
|
|
"brush your teeth", "comb your hair"))
|
|
|
|
assert_equal("Always brush your teeth but never play with fire.",
|
|
|
|
c.send(static_method_name(:static_method, false, true),
|
|
|
|
"brush your teeth", "play with fire"))
|
|
|
|
assert_equal("Always brush your teeth but never play with fire",
|
|
|
|
c.send(static_method_name(:static_method, false, false),
|
|
|
|
"brush your teeth", "play with fire"))
|
|
|
|
end
|
|
|
|
end
|