require 'rubygems' require 'test/unit' require 'test/libxml_test_helper' # Thought a mock library will be useful for testing out any stuff that # does any http-ing, but have no strong feelings about which one to use. # Looking at the syntax of each of the libraries, I liked the look of the # mocha syntax more, so was thinking to use that when/if required. # # require 'flexmock' require 'mocha' #consider adding custom assertions to clean code up #http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html # Tiny patch to Test::Unit that lets us write our tests in a slightly # more rspec style way. Similar thing is used in jeremymcanally's context # gem (http://github.com/jeremymcanally/context/tree/master), but thought # could do without the extra dependency class Test::Unit::TestCase def self.test(name, &block) test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym defined = instance_method(test_name) rescue false raise "#{test_name} is already defined in #{self}" if defined if block_given? define_method(test_name, &block) else define_method(test_name) do flunk "No implementation provided for #{name}" end end end def self.implement(name, &block) puts "UNIMPLEMENTED: #{name}" end def add_line_numbers(str) return nil if str.nil? out = '' str.split(/\n/).each_with_index do |line, i| out << "#{i+1}: #{line}\n" end out end #compare not the given json strings directly but their hash structures after parsing. #also try to give a more concise diff, if they are different, using Hash.diff from activesupport def assert_json_strings_equal(expected_string, actual_string, message=nil) expected_structure = JSON.parse(expected_string) actual_structure = JSON.parse(actual_string) diff = expected_structure.diff(actual_structure) diff_string = diff.inspect.gsub(/"/, "'") full_message = build_message(message, "Parsed JSON structure doesn't match expected. Hash Diff (quotes changed): \n" + "Full text: Expected: \n\nbut was: .\n", diff_string, expected_string, actual_string) assert_block(full_message) do expected_structure == actual_structure end #File.open('tmp.json', 'w') {|f| f.write output_json } #File.open('tmp_split.json', 'w') {|f| f.write prep_json_for_reading(output_json) } #File.open('tmp_expected_split.json', 'w') {|f| f.write prep_json_for_reading(expected_json)} #TODO: consider allowing submission of both to a json visual diff app, too. (only if switched on by an environment variable). end def assert_json_hashes_equal(expected_structure, actual_structure, message=nil) diff = expected_structure.diff(actual_structure) diff_string = diff.inspect.gsub(/"/, "'") full_message = build_message(message, "JSON structure doesn't match expected. Hash Diff (quotes changed): \n" + "Full values: Expected: \n\nbut was: .\n", diff_string, expected_structure.inspect, actual_structure.inspect) assert_block(full_message) do expected_structure == actual_structure end end end #TODO: don't add this Hash.diff(h2) for all hashes, only for ones we want to diff (e.g. on the fly during assert_json_strings_equal) #TODO: check rails license module ASCoreExtensions #was ActiveSupport module Hash #:nodoc: module Diff # Returns a hash that represents the difference between two hashes. # # Examples: # # {1 => 2}.diff(1 => 2) # => {} # {1 => 2}.diff(1 => 3) # => {1 => 2} # {}.diff(1 => 2) # => {1 => 2} # {1 => 2, 3 => 4}.diff(1 => 2) # => {3 => 4} def diff(h2) self.dup.delete_if { |k, v| h2[k] == v }.merge(h2.dup.delete_if { |k, v| self.has_key?(k) }) end end end end class Hash #:nodoc: include ASCoreExtensions::Hash::Diff end $:.unshift(File.join(File.dirname(__FILE__), "..", "lib")) require 'eeml' include Eeml include Exceptions