Skip to content

Instantly share code, notes, and snippets.

@bitops
Forked from practicingruby/1_rcat_tests.rb
Last active August 29, 2015 14:16
Show Gist options
  • Save bitops/dd010da87c2829ae562f to your computer and use it in GitHub Desktop.
Save bitops/dd010da87c2829ae562f to your computer and use it in GitHub Desktop.
# Task: Implement the rcat utility and get these tests to pass on a system
# which has the UNIX cat command present
# To see Gregory Brown's solution, see http://github.com/elm-city-craftworks/rcat
# Feel free to publicly share your own solutions
rrequire "open3"
working_dir = File.dirname(__FILE__)
gettysburg_file = "#{working_dir}/data/gettysburg.txt"
spaced_file = "#{working_dir}/data/spaced_out.txt"
############################################################################
cat_output = `cat #{gettysburg_file}`
rcat_output = `rcat #{gettysburg_file}`
fail "Failed 'cat == rcat'" unless cat_output == rcat_output
############################################################################
cat_output = `cat #{gettysburg_file} #{spaced_file}`
rcat_output = `rcat #{gettysburg_file} #{spaced_file}`
fail "Failed 'cat [f1 f2] == rcat [f1 f2]'" unless cat_output == rcat_output
############################################################################
cat_output = `cat < #{spaced_file}`
rcat_output = `rcat < #{spaced_file}`
unless cat_output == rcat_output
fail "Failed 'cat < file == rcat < file"
end
############################################################################
cat_output = `cat -n #{gettysburg_file}`
rcat_output = `rcat -n #{gettysburg_file}`
fail "Failed 'cat -n == rcat -n'" unless cat_output == rcat_output
############################################################################
cat_output = `cat -b #{gettysburg_file}`
rcat_output = `rcat -b #{gettysburg_file}`
fail "Failed 'cat -b == rcat -b'" unless cat_output == rcat_output
############################################################################
cat_output = `cat -s #{spaced_file}`
rcat_output = `rcat -s #{spaced_file}`
fail "Failed 'cat -s == rcat -s'" unless cat_output == rcat_output
############################################################################
cat_output = `cat -bs #{spaced_file}`
rcat_output = `rcat -bs #{spaced_file}`
fail "Failed 'cat -bns == rcat -bns'" unless cat_output == rcat_output
############################################################################
cat_output = `cat -bns #{spaced_file}`
rcat_output = `rcat -bns #{spaced_file}`
fail "Failed 'cat -bns == rcat -bns'" unless cat_output == rcat_output
############################################################################
cat_output = `cat -nbs #{spaced_file}`
rcat_output = `rcat -nbs #{spaced_file}`
fail "Failed 'cat -nbs == rcat -nbs'" unless cat_output == rcat_output
############################################################################
cat_output = `cat -ns #{gettysburg_file} #{spaced_file}`
rcat_output = `rcat -ns #{gettysburg_file} #{spaced_file}`
unless cat_output == rcat_output
fail "Failed 'cat -ns [f1 f2] == rcat -ns [f1 f2]'"
end
############################################################################
cat_output = `cat -bs #{gettysburg_file} #{spaced_file}`
rcat_output = `rcat -bs #{gettysburg_file} #{spaced_file}`
unless cat_output == rcat_output
fail "Failed 'cat -bs [f1 f2] == rcat -bs [f1 f2]'"
end
############################################################################
`cat #{gettysburg_file}`
cat_success = $?
`rcat #{gettysburg_file}`
rcat_success = $?
unless cat_success.exitstatus == 0 && rcat_success.exitstatus == 0
fail "Failed 'cat and rcat success exit codes match"
end
############################################################################
cat_out, cat_err, cat_process = Open3.capture3("cat some_invalid_file")
rcat_out, rcat_err, rcat_process = Open3.capture3("rcat some_invalid_file")
unless cat_process.exitstatus == 1 && rcat_process.exitstatus == 1
fail "Failed 'cat and rcat exit codes match on bad file"
end
unless rcat_err =~ /No such file or directory.*some_invalid_file/
fail "Failed 'cat and rcat error messages match on bad file'"
end
############################################################################
cat_out, cat_err, cat_proccess = Open3.capture3("cat -x #{gettysburg_file}")
rcat_out,rcat_err, rcat_process = Open3.capture3("rcat -x #{gettysburg_file}")
unless cat_process.exitstatus == 1 && rcat_process.exitstatus == 1
fail "Failed 'cat and rcat exit codes match on bad switch"
end
unless rcat_err == "rcat: invalid option: -x\nusage: rcat [-bns] [file ...]\n"
fail "Failed 'rcat provides usage instructions when given invalid option"
end
############################################################################
puts "You passed the tests, yay!"
Four score and seven years ago our fathers brought forth on this continent, a
new nation, conceived in Liberty, and dedicated to the proposition that all men
are created equal.
Now we are engaged in a great civil war, testing whether that nation, or any
nation so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as a
final resting place for those who here gave their lives that that nation might
live. It is altogether fitting and proper that we should do this.
But, in a larger sense, we can not dedicate -- we can not consecrate -- we can
not hallow -- this ground. The brave men, living and dead, who struggled here,
have consecrated it, far above our poor power to add or detract. The world will
little note, nor long remember what we say here, but it can never forget what
they did here. It is for us the living, rather, to be dedicated here to the
unfinished work which they who fought here have thus far so nobly advanced. It
is rather for us to be here dedicated to the great task remaining before us --
that from these honored dead we take increased devotion to that cause for which
they gave the last full measure of devotion -- that we here highly resolve that
these dead shall not have died in vain -- that this nation, under God, shall
have a new birth of freedom -- and that government of the people, by the people,
for the people, shall not perish from the earth.
Now we are engaged in a great civil war, testing whether that nation, or any
nation so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as a
final resting place for those who here gave their lives that that nation might
live. It is altogether fitting and proper that we should do this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment