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, &preparator) ⇒ 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))

    preparator the block, which transforms 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, &preparator)
  @expected = expected
  @produced = produced
  @relative_path = relative_path
  preparator ||= Proc.new { |x| x } #id
  self.preparator = preparator
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

#preparatorProc<(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 preparator
  @preparator
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 prepared 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?
  prepared_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(prepared_expected.to_s, prepared_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(prepared_expected, prepared_produced)
  else
    prepared_expected == prepared_produced
  end
end

#prepared_expectedObject



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

def prepared_expected
  @prepared_expected ||= preparator.call(expected)
end

#prepared_producedObject



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

def prepared_produced
  @prepared_produced ||= preparator.call(produced)
end