Class: DiffTool

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/acceptance_test/diff_tool.rb

Instance Method Summary collapse

Instance Method Details

#diff(source, target) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/acceptance_test/diff_tool.rb', line 6

def diff source, target
  phrases1 = []

  keywords_exp1 = /^(Given|When|Then|And|But)/

  File.open(source).each_line do |line|
    line = line.strip

    if line !~ /^#/ and line =~ keywords_exp1
      word = line.scan(keywords_exp1)[0][0]

      phrases1 << line[word.size..-1].strip
    end
  end

  phrases2 = []

  keywords_exp2 = /step\s+('|")(.*)('|")/

  File.open(target).each_line do |line|
    line = line.strip

    if line !~ /^#/ and line =~ keywords_exp2
      phrases2 << line.scan(keywords_exp2)[0][1]
    end
  end

  ok = true

  phrases1.each_with_index do |phrase, index|
    phrase1 = phrase.clone.gsub("\"", "'")
    phrase2 = phrases2[index]

    if phrase1.nil? or phrase2.nil?
      puts "Different amount of steps:"
      puts "  source: #{phrases1.size}"
      puts "  target: #{phrases2.size}"
      ok = false
      break
    end

    params = phrase2.gsub(/:\w+\S/).to_a

    params.each do |param|
      new_param = param.gsub(":", "")

      phrase1.gsub!(%r{<#{new_param}>}, param)
    end

    params.each do |param|
      phrase1.gsub!(/'(\w|\s)*'/, param)
    end

    params.each do |param|
      phrase1.gsub!(/'(.)*'/, param)
    end

    if phrase1 != phrase2
      puts "Fail:"
      puts "  #{phrase}"
      puts "  #{phrases2[index]}"
      ok = false
    else
      puts "OK:"
      puts "  #{phrase}"
      puts "  #{phrases2[index]}"
    end
  end

  if ok
    puts "Everything is OK."
  else
    puts "Some errors exist!"
  end
end