Class: CLIntegracon::Diff

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/CLIntegracon/diff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected, produced, relative_path = nil, &preprocessor) ⇒ Diff

Init a new diff

Parameters:

  • expected (Pathname)

    the expected file

  • produced (Pathname)

    the produced file

  • relative_path (Pathname) (defaults to: nil)

    the relative path to the expected file

  • ] (Block<(Pathname)->(to_s))

    preprocessor the block, which preprocess the files in a better comparable form


39
40
41
42
43
44
45
# File 'lib/CLIntegracon/diff.rb', line 39

def initialize(expected, produced, relative_path=nil, &preprocessor)
  @expected = expected
  @produced = produced
  @relative_path = relative_path
  preprocessor ||= Proc.new { |x| x } #id
  self.preprocessor = preprocessor
end

Instance Attribute Details

#expectedPathname (readonly)

Returns the expected file

Returns:

  • (Pathname)

    the expected file


11
12
13
# File 'lib/CLIntegracon/diff.rb', line 11

def expected
  @expected
end

#preprocessorProc<(Pathname)->(to_s)

Returns ] the proc, which transforms the files in a better comparable form

Returns:

  • (Proc<(Pathname)->(to_s))

    ] the proc, which transforms the files in a better comparable form


23
24
25
# File 'lib/CLIntegracon/diff.rb', line 23

def preprocessor
  @preprocessor
end

#producedPathname (readonly)

Returns the produced file

Returns:

  • (Pathname)

    the produced file


15
16
17
# File 'lib/CLIntegracon/diff.rb', line 15

def produced
  @produced
end

#relative_pathPathname (readonly)

Returns the relative path to the expected file

Returns:

  • (Pathname)

    the relative path to the expected file


19
20
21
# File 'lib/CLIntegracon/diff.rb', line 19

def relative_path
  @relative_path
end

Instance Method Details

#compares_files?Bool

Check if the preprocessed inputs are files or need to be dumped first to temporary files to be compared.

Returns:

  • (Bool)

60
61
62
# File 'lib/CLIntegracon/diff.rb', line 60

def compares_files?
  preprocessed_expected.is_a? Pathname
end

#each(options = {}, &block) ⇒ Diffy::Diff

Enumerate all lines which differ.

Parameters:

  • options (Hash) (defaults to: {})

    see Diffy#initialize for help.

Returns:

  • (Diffy::Diff)

84
85
86
87
88
89
90
# File 'lib/CLIntegracon/diff.rb', line 84

def each(options = {}, &block)
  options = {
    :source  => compares_files? ? 'files' : 'strings',
    :context => 3
  }.merge options
  Diffy::Diff.new(preprocessed_expected.to_s, preprocessed_produced.to_s, options).each &block
end

#is_equal?Bool

Check if the produced output equals the expected

Returns:

  • (Bool)

    whether the expected is equal to the produced


69
70
71
72
73
74
75
# File 'lib/CLIntegracon/diff.rb', line 69

def is_equal?
  @is_equal ||= if compares_files?
    FileUtils.compare_file(preprocessed_expected, preprocessed_produced)
  else
    preprocessed_expected == preprocessed_produced
  end
end

#preprocessed_expectedObject


47
48
49
# File 'lib/CLIntegracon/diff.rb', line 47

def preprocessed_expected
  @preprocessed_expected ||= preprocessor.call(expected)
end

#preprocessed_producedObject


51
52
53
# File 'lib/CLIntegracon/diff.rb', line 51

def preprocessed_produced
  @preprocessed_produced ||= preprocessor.call(produced)
end