@@ -37,6 +37,70 @@ def append_to(array)
37
37
def to_json ( options )
38
38
raw . to_json ( options )
39
39
end
40
+
41
+ def diff! ( other_table )
42
+ other_table = ensure_table ( other_table )
43
+ other_table_cell_matrix = other_table . cell_matrix
44
+
45
+ ensure_green!
46
+
47
+ require_diff_lcs
48
+ cell_matrix . extend ( Diff ::LCS )
49
+
50
+ changes = cell_matrix . diff ( other_table_cell_matrix ) . flatten
51
+
52
+ return if changes . empty?
53
+
54
+ inserted = 0
55
+ missing = 0
56
+
57
+ changes . each do |change |
58
+ if ( change . action == '-' )
59
+ missing_row_pos = change . position + inserted
60
+ cell_matrix [ missing_row_pos ] . each { |cell | cell . status = :undefined }
61
+
62
+ missing += 1
63
+ else # '+'
64
+ inserted_row = change . element
65
+ inserted_row . each { |cell | cell . status = :comment }
66
+
67
+ insert_row_pos = change . position + missing
68
+ cell_matrix . insert ( insert_row_pos , inserted_row )
69
+
70
+ inserted += 1
71
+ end
72
+ end
73
+
74
+ raise Different . new ( self )
75
+ end
76
+
77
+ def ensure_green! #:nodoc:
78
+ each_cell { |cell | cell . status = :passed }
79
+ end
80
+
81
+ private
82
+
83
+ def ensure_table ( table_or_array ) #:nodoc:
84
+ return table_or_array if DataTable === table_or_array
85
+ DataTable . new ( table_or_array )
86
+ end
87
+
88
+ def require_diff_lcs #:nodoc:
89
+ begin
90
+ require 'diff/lcs'
91
+ rescue LoadError => e
92
+ e . message << "\n Please gem install diff-lcs\n "
93
+ raise e
94
+ end
95
+ end
96
+
97
+ class Different < StandardError
98
+ attr_reader :table
99
+ def initialize ( table )
100
+ @table = table
101
+ super ( "Tables were not identical" )
102
+ end
103
+ end
40
104
end
41
105
42
106
class None
@@ -46,7 +110,6 @@ def append_to(array)
46
110
def describe_to ( visitor )
47
111
end
48
112
end
49
-
50
113
end
51
114
end
52
115
0 commit comments