Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 940b3f94f0
Fetching contributors…

Cannot retrieve contributors at this time

file 66 lines (53 sloc) 1.405 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
require 'test/unit'
require 'quick_parse_tree_oh'
require 'activesupport'

class QuickParseTreeOrderedHashTest < Test::Unit::TestCase
  class ::OrderedHash < ActiveSupport::OrderedHash
    def inspect
      super =~ /\A.+?(\{.+\})[^}]+\Z/
      $1
    end
  end

  def test_locals
    a = 5
    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{{ :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{{ 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
Something went wrong with that request. Please try again.