public
Description: Bits of code from around the world
Homepage:
Clone URL: git://github.com/mchung/mchung-code.git
mchung-code / hacks / rb / hash_test.rb
100644 75 lines (66 sloc) 1.835 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 'hash'
require 'rubygems'
require 'xmlsimple'
 
class HashTest < Test::Unit::TestCase
 
  def test_should_lookup_when_key_is_symbol
    a = {:foo => "42"}
    assert_equal "42", a.fetch(:foo)
    assert_equal "42", a[:foo]
    assert_equal "42", a.foo
    assert_equal nil, a.foo_no_aqui
  end
 
  def test_should_lookup_when_key_is_string
    a = {"foo" => "fnord"}
    assert_equal "fnord", a["foo"]
    assert_equal "fnord", a.foo
    assert_equal nil, a.foo_no_aqui_tambien
  end
  
  def test_should_query_flat_hash
    a = {:foo => "salsa"}
    assert_equal nil, a.foo?.bar
    assert_equal nil, a.samba?.chacha?.rumba?.salsa
    assert_equal "salsa", a.foo
  end
  
  def test_should_query_nested_hash
    a = {:foo => "jazz", :bar => {:baz => "blues"}}
    b = {:baz => "blues"}
    assert_equal "jazz", a.foo
    assert_equal b, a.bar
    assert_equal "blues", a.bar.baz
    assert_equal "blues", a.bar?.baz
    assert_equal({}, a.bar?.baz?)
    assert_equal nil, a.bar?.baz?.tienes_ganas_una_taza_de_cafe
  end
  
  def test_hash_from_xml
    xml =<<XML
<SyncHdr>
<VerDTD>1.2</VerDTD>
<VerProto>OMA/1.2</VerProto>
<SessionID>1</SessionID>
<MsgID>1</MsgID>
<Target>
<LocURI>https://oma.example.com</LocURI>
</Target>
<Source>
<LocURI>OMA123456</LocURI>
</Source>
<Cred>
<Meta>
<Type>syncml:auth-basic</Type>
<Format>b64</Format>
</Meta>
<Data>ssh_the_pass</Data>
</Cred>
<Meta>
<MaxMsgSize>10700</MaxMsgSize>
</Meta>
</SyncHdr>
XML
    hash = XmlSimple.xml_in(xml, {"forcearray" => false, "keeproot" => true})
    assert_equal "1.2", hash.SyncHdr?.VerDTD
    assert_equal "ssh_the_pass", hash.SyncHdr?.Cred?.Data
    assert_equal nil, hash.SyncHdr?.Cred?.Meta?.MoreData
    assert_equal "OMA123456", hash.SyncHdr?.Source?.LocURI
  end
 
 
end