amp / test / test_support.rb

##################################################################
#                  Licensing Information                         #
#                                                                #
#  The following code is licensed, as standalone code, under     #
#  the Ruby License, unless otherwise directed within the code.  #
#                                                                #
#  For information on the license of this code when distributed  #
#  with and used in conjunction with the other modules in the    #
#  Amp project, please see the root-level LICENSE file.          #
#                                                                #
#  © Michael J. Edgar and Ari Brown, 2009-2010                   #
#                                                                #
##################################################################

require File.join(File.expand_path(File.dirname(__FILE__)), 'testutilities')
require File.expand_path(File.join(File.dirname(__FILE__), "../lib/amp"))

class OppositeMethodTestKlass
  def base(input)
    !!input
  end
  opposite_method :opposite, :base
end

class TestSupport < AmpTestCase
  def test_split_newlines
    assert_equal(["hi there what's\n", "\r up there\r kids\n", " lol"], "hi there what's\n\r up there\r kids\n lol".split_newlines)
  end
  
  def test_opposite_method
    obj = OppositeMethodTestKlass.new
    assert_respond_to obj, :opposite
    assert obj.base(true)
    assert_false obj.opposite(true)
    assert_false obj.base(false)
    assert obj.opposite(false)
  end
  
  def test_array_hash
    hash = ArrayHash.new
    assert_equal [], hash[:hello]
    hash[:other] << "hi"
    assert_equal ["hi"], hash[:other]
  end
  
  def test_symbol_to_proc
    assert_equal [1,2,3], ["1","2","3"].map(&:to_i)
  end
  
  def test_time_to_diff
    t = Time.at(1234567890.123456)
    expected = "2009-02-13 18:31:30.123456"
    assert_equal expected, t.to_diff
  end
  
  def test_hide_password
    url = "http://user:password@www.bob.com/"
    expected = "http://user:***@www.bob.com/"
    assert_equal expected, url.hide_password
    
    url = "https://u:somepass@.ru" # weird URL
    expected = "https://u:***@.ru"
    assert_equal expected, url.hide_password
  end
  
  def test_hexlify
    assert_equal "0102dead", "\x01\x02\xde\xad".hexlify
    assert_equal "fffedabb1234", "\xff\xfe\xda\xbb\x12\x34".hexlify
  end
  
  def test_binary_search
    assert_equal 5, [1,2,3,4,4,4,5,5,5,6,7,7,10,13,15,19,19,20].binary_search {|x| x <=> 5}
  end
  
  def test_binary_search_empty
    assert_equal nil, [].binary_search {|x| x <=> 5}
  end
  
  def test_binary_search_not_found
    assert_equal nil, [1,2,3,4,4,4,6,6,7,8,9,10].binary_search {|x| x <=> 5}
  end
  
  def test_binary_search_one_element
    assert_equal 5, [5].binary_search {|x| x <=> 5}
  end
  
  ###
  # File additions. Really it's more just like 
end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.