Skip to content

Commit 23b90d1

Browse files
committed
spec: spec_helpers
1 parent 37a8c29 commit 23b90d1

File tree

1 file changed

+53
-16
lines changed

1 file changed

+53
-16
lines changed

spec/spec_helpers.cr

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,67 @@
1818
# -----------------------------------------------------------------------
1919

2020
def expect_diff(a, b)
21-
a.should eq(b)
21+
a.should eq(b)
2222
rescue error
23-
fail diff(a, b)
23+
fail diff(a, b)
2424
end
2525

2626
def diff(a, b)
27-
file1 = File.tempfile do |file|
28-
file.puts a.strip
29-
file.flush
30-
end
31-
32-
io = IO::Memory.new
27+
file1 = File.tempfile do |file|
28+
file.puts a.strip
29+
file.flush
30+
end
31+
32+
file2 = File.tempfile do |file|
33+
file.puts b
34+
file.flush
35+
end
36+
37+
io = IO::Memory.new
3338

34-
Process.run("git", [
35-
"--no-pages", "diff", "--no-index",
36-
file1.path,
37-
], output: io)
39+
Process.run("git", [
40+
"--no-pager", "diff", "--no-index", "--color=always",
41+
file1.path, file2.path,
42+
], output: io)
3843

39-
io.to_s
44+
io.to_s
45+
ensure
46+
file1.try &.delete
47+
file2.try &.delete
4048
end
4149

4250
def matches_template(template, expected)
43-
expected = expected.uncolarize
51+
expected =
52+
expected.uncolorize
4453

45-
parts = template.split(/x*/)
54+
parts =
55+
template.split(/×+/)
4656

47-
end
57+
fills =
58+
[] of String
59+
60+
parts.reduce(0) do |position, item|
61+
unless index = expected.index(item, position)
62+
raise <<-TEXT
63+
TEMPLATE INVALID!
64+
65+
TEMPLATE:
66+
#{template}
67+
68+
ITEM:
69+
#{item}
70+
71+
EXPECTED:
72+
#{expected}
73+
TEXT
74+
end
75+
76+
fills << expected[position, index - position]
77+
index + item.size
78+
end
79+
80+
replaced =
81+
parts.zip(fills).join { |part, fill| fill + part }
82+
83+
expect_diff(replaced, expected)
84+
end

0 commit comments

Comments
 (0)