Class: Nice::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/nice/diff.rb

Defined Under Namespace

Classes: FileDoesNotExist, FormatNotSpecified

Constant Summary collapse

@@hash_print =
nil
@@key =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format, expected_file, actual_file) ⇒ Diff

Returns a new instance of Diff.



17
18
19
20
21
22
23
24
25
# File 'lib/nice/diff.rb', line 17

def initialize(format, expected_file, actual_file)        
  raise Nice::Diff::FileDoesNotExist unless File.exists?(expected_file)
  raise Nice::Diff::FileDoesNotExist unless File.exists?(actual_file)
  raise Nice::Diff::FormatNotSpecified if format.nil?
    
  @expected_file = File.read expected_file
  @actual_file = File.read actual_file
    @format = format.upcase
end

Instance Attribute Details

#actual_fileObject

Returns the value of attribute actual_file.



12
13
14
# File 'lib/nice/diff.rb', line 12

def actual_file
  @actual_file
end

#expected_fileObject

Returns the value of attribute expected_file.



12
13
14
# File 'lib/nice/diff.rb', line 12

def expected_file
  @expected_file
end

#formatObject

Returns the value of attribute format.



12
13
14
# File 'lib/nice/diff.rb', line 12

def format
  @format
end

Class Method Details

.hash_printObject



32
33
34
# File 'lib/nice/diff.rb', line 32

def self.hash_print
  @@hash_print
end

Instance Method Details

#key=(key) ⇒ Object



36
37
38
# File 'lib/nice/diff.rb', line 36

def key=(key)
  @@key = key
end

#parse_to_arrayObject



27
28
29
30
# File 'lib/nice/diff.rb', line 27

def parse_to_array        
  parser = eval("#{@format}Parser").new
    parser.parse_to_array(self) #passing reference to itself to the parsing strategy
end


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
# File 'lib/nice/diff.rb', line 40

def print
  expected_arr, actual_arr = parse_to_array
  headers = Array.new     

  expected_arr.each_with_index { |expected_hash_of_obj, i|
  expected_arr[0].each_with_index { |(k,v), i| headers[i] = k} 
  actual_hash_of_obj = actual_arr[i]      

  expected_keys = expected_hash_of_obj.keys.to_set
  actual_keys = actual_hash_of_obj.keys.to_set

  unless expected_keys == actual_keys
    unwanted = actual_keys - expected_keys
    missing = expected_keys - actual_keys

    STDOUT.puts "\Actual #{@format} file contain #{unwanted.size} unwanted #{@@key}(s): #{actual_hash_of_obj.keys - expected_hash_of_obj.keys}" unless unwanted.empty?
    STDOUT.puts "\Actual #{@format} file is missing #{missing.size} #{@@key}(s): #{expected_hash_of_obj.keys - actual_hash_of_obj.keys}" unless missing.empty?
    return
    end

    unless expected_hash_of_obj.eql?(actual_hash_of_obj) 
      STDOUT.puts "\nDiff for object\##{i}:\n"
      self.class.print_hash_diff(expected_hash: expected_hash_of_obj, actual_hash: actual_hash_of_obj, headers: headers)
    end
  }  
end