Skip to content

Commit 5b2c7b4

Browse files
committed
DRYing up API
1 parent 6bf6f1d commit 5b2c7b4

File tree

3 files changed

+69
-65
lines changed

3 files changed

+69
-65
lines changed

lib/aruba/api.rb

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -362,43 +362,36 @@ def with_file_content(file, &block)
362362
# @param [String] file
363363
# The file to be checked
364364
#
365-
# @param [String] partial_content
366-
# The content which must/must not be in the file
365+
# @param [String, Regexp] content
366+
# The content which must/must not be in the file. If content is
367+
# a String exact match is done, if content is a Regexp then file
368+
# is matched using regular expression
367369
#
368370
# @param [true, false] expect_match
369371
# Must the content be in the file or not
370-
def check_file_content(file, partial_content, expect_match = true)
371-
regexp = regexp(partial_content)
372+
def check_file_content(file, content, expect_match = true)
373+
match_content =
374+
if(Regexp === content)
375+
match(content)
376+
else
377+
eq(content)
378+
end
372379
prep_for_fs_check do
373380
content = IO.read(File.expand_path(file))
374-
375381
if expect_match
376-
expect(content).to match regexp
382+
expect(content).to match_content
377383
else
378-
expect(content).not_to match regexp
384+
expect(content).not_to match_content
379385
end
380386
end
381387
end
382388

383-
# Check if the exact content can be found in file
384-
#
385-
# @param [String] file
386-
# The file to be checked
387-
#
388-
# @param [String] exact_content_or_file
389-
# The content of the file or an IO
390-
#
391-
# @param [true, false] expect_match
392-
# Must the content be in the file or not
389+
# @private
390+
# @deprecated
393391
def check_exact_file_content(file, exact_content, expect_match = true)
394-
prep_for_fs_check do
395-
content = IO.read(File.expand_path(file))
396-
if expect_match
397-
expect(content).to eq exact_content
398-
else
399-
expect(content).not_to eq exact_content
400-
end
401-
end
392+
warn('The use of "check_exact_file_content" is deprecated. Use "#check_file_content" with a string instead instead.')
393+
394+
check_file_content(file, exact_content, expect_match = true)
402395
end
403396

404397
# Check if the content of file against the content of a reference file
@@ -476,10 +469,6 @@ def unescape(string)
476469
string
477470
end
478471

479-
def regexp(string_or_regexp)
480-
Regexp === string_or_regexp ? string_or_regexp : Regexp.compile(Regexp.escape(string_or_regexp))
481-
end
482-
483472
# Fetch output (stdout, stderr) from command
484473
#
485474
# @param [String] cmd

lib/aruba/cucumber.rb

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -332,36 +332,24 @@
332332
check_directory_presence(directories.raw.map{|directory_row| directory_row[0]}, false)
333333
end
334334

335-
Then /^a directory named "([^"]*)" should exist$/ do |directory|
336-
check_directory_presence([directory], true)
335+
Then /^a directory named "([^"]*)" should (not )?exist$/ do |directory, expect_match|
336+
check_directory_presence([directory], !expect_match)
337337
end
338338

339-
Then /^a directory named "([^"]*)" should not exist$/ do |directory|
340-
check_directory_presence([directory], false)
339+
Then /^the file "([^"]*)" should (not )?contain "([^"]*)"$/ do |file, expect_match, partial_content|
340+
check_file_content(file, Regexp.compile(Regexp.escape(partial_content)), !expect_match)
341341
end
342342

343-
Then /^the file "([^"]*)" should contain "([^"]*)"$/ do |file, partial_content|
344-
check_file_content(file, partial_content, true)
343+
Then /^the file "([^"]*)" should (not )?contain:$/ do |file, expect_match, partial_content|
344+
check_file_content(file, Regexp.compile(Regexp.escape(partial_content)), !expect_match)
345345
end
346346

347-
Then /^the file "([^"]*)" should not contain "([^"]*)"$/ do |file, partial_content|
348-
check_file_content(file, partial_content, false)
347+
Then /^the file "([^"]*)" should (not )?contain exactly:$/ do |file, expect_match, exact_content|
348+
check_file_content(file, exact_content, !expect_match)
349349
end
350350

351-
Then /^the file "([^"]*)" should contain:$/ do |file, partial_content|
352-
check_file_content(file, partial_content, true)
353-
end
354-
355-
Then /^the file "([^"]*)" should contain exactly:$/ do |file, exact_content|
356-
check_exact_file_content(file, exact_content)
357-
end
358-
359-
Then /^the file "([^"]*)" should match \/([^\/]*)\/$/ do |file, partial_content|
360-
check_file_content(file, /#{partial_content}/, true)
361-
end
362-
363-
Then /^the file "([^"]*)" should not match \/([^\/]*)\/$/ do |file, partial_content|
364-
check_file_content(file, /#{partial_content}/, false)
351+
Then /^the file "([^"]*)" should (not )?match \/([^\/]*)\/$/ do |file, expect_match, partial_content|
352+
check_file_content(file, /#{partial_content}/, !expect_match)
365353
end
366354

367355
Then /^the file "([^"]*)" should (not )?be equal to file "([^"]*)"/ do |file, expect_match, reference_file|

spec/aruba/api_spec.rb

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -316,26 +316,53 @@ def announce_or_puts(*args)
316316
end
317317
end
318318

319-
context "#check_file_content" do
320-
before :each do
319+
context "check file content" do
320+
before :example do
321321
@aruba.write_file(@file_name, "foo bar baz")
322322
end
323323

324-
it "succeeds if file content matches" do
325-
@aruba.check_file_content(@file_name, "foo bar baz")
326-
@aruba.check_file_content(@file_name, "foo bar baz", true)
327-
end
324+
context "#check_file_content" do
325+
context "with regexp" do
326+
let(:matching_content){/bar/}
327+
let(:non_matching_content){/nothing/}
328+
it "succeeds if file content matches" do
329+
@aruba.check_file_content(@file_name, matching_content)
330+
@aruba.check_file_content(@file_name, matching_content, true)
331+
end
328332

329-
it "succeeds if file content does not match" do
330-
@aruba.check_file_content(@file_name, "hello world", false)
331-
end
333+
it "succeeds if file content does not match" do
334+
@aruba.check_file_content(@file_name, non_matching_content, false)
335+
end
332336

333-
it "works with ~ in path name" do
334-
file_path = File.join('~', random_string)
337+
it "works with ~ in path name" do
338+
file_path = File.join('~', random_string)
335339

336-
with_env 'HOME' => File.expand_path(current_dir) do
337-
@aruba.write_file(file_path, "foo bar baz")
338-
@aruba.check_file_content(file_path, "hello world", false)
340+
with_env 'HOME' => File.expand_path(current_dir) do
341+
@aruba.write_file(file_path, "foo bar baz")
342+
@aruba.check_file_content(file_path, non_matching_content, false)
343+
end
344+
end
345+
end
346+
context "with string" do
347+
let(:matching_content){"foo bar baz"}
348+
let(:non_matching_content){"bar"}
349+
it "succeeds if file content matches" do
350+
@aruba.check_file_content(@file_name, matching_content)
351+
@aruba.check_file_content(@file_name, matching_content, true)
352+
end
353+
354+
it "succeeds if file content does not match" do
355+
@aruba.check_file_content(@file_name, non_matching_content, false)
356+
end
357+
358+
it "works with ~ in path name" do
359+
file_path = File.join('~', random_string)
360+
361+
with_env 'HOME' => File.expand_path(current_dir) do
362+
@aruba.write_file(file_path, "foo bar baz")
363+
@aruba.check_file_content(file_path, non_matching_content, false)
364+
end
365+
end
339366
end
340367
end
341368
end

0 commit comments

Comments
 (0)