public
Description: various ordered hash syntax experiments for ruby 1.8
Homepage:
Clone URL: git://github.com/coderrr/ordered_hash_syntax.git
ordered_hash_syntax / parse_tree_oh_test.rb
100644 75 lines (59 sloc) 1.604 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'test/unit'
require 'parse_tree_oh'
require 'activesupport'
 
class ParseTreeOrderedHashTest < Test::Unit::TestCase
  class OrderedHash < ActiveSupport::OrderedHash
    def inspect
      super =~ /\A.+?(\{.+\})[^}]+\Z/
      $1
    end
  end
 
  def test_locals
    a = 5
    h = H{{ a => :hi }}
    assert_equal({5 => :hi}, h)
    h = H{{ :hi => a }}
    assert_equal({:hi => 5}, h)
  end
 
  def meth(*a)
    if a.empty?
      :meth
    else
      :"meth#{a.inspect}"
    end
  end
 
  def hash(*hashes)
    hashes.map {|h| h.keys*',' }*','
  end
 
  def test_methods
    h = H{{ meth => :hi }}
    assert_equal({:meth => :hi}, h)
 
    h = H{{ :hi => meth }}
    assert_equal({:hi => :meth}, h)
 
    h = H{{ :hi => meth(1,2,3) }}
    assert_equal({:hi => :"meth[1, 2, 3]"}, h)
 
    h = H{{ :hi => meth(1=>2) }}
    assert_equal({:hi => :"meth[{1=>2}]"}, h)
 
    h = H{{ :hi => meth(*[1, 2, 3]) }}
    assert_equal({:hi => :"meth[1, 2, 3]"}, h)
  end
 
  def test_methods_with_hash_params
    h = H{{ hash(1=>2, 3=>4) => hash(3=>4, 1=>2) }}
    assert_equal({ '1,3' => '3,1'}, h)
 
    h = H{{ 0 => hash({1=>2, 3=>4}, {5=>6, 7=>8}) }}
    assert_equal({ 0 => '1,3,5,7'}, h)
  end
 
  def test_plain
    h = H{{ 1 => :two, 'three' => 4, :five => :six }}
    assert_equal({1=>:two, 'three'=>4, :five => :six}, h)
  end
 
  def test_nested_objects
    h = H{{ :a => [1, 2, 3] }}
    assert_equal({ :a => [1,2,3] }, h)
 
    h = H{{ :a => {1=>2} }}
    assert_equal({ :a => {1=>2} }, h)
 
    a = 55
    h = H{{ :a => {1=>[2,3,4,{a=>meth}]} }}
    assert_equal({ :a => {1=>[2,3,4,{55=>:meth}]} }, h)
  end
end