Created
April 7, 2022 17:50
-
-
Save dav/e140bd479a5b4c52ee1530e040254c7a to your computer and use it in GitHub Desktop.
A simple ruby test system (made for coderpad interviews)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Coderpad with simple test system | |
# provides a few basic assertions and a test harness so you can at least test drive | |
# those silly interview puzzles that only ever come up in interviews and almost | |
# never in real work. | |
class Code | |
def sut(param1 = nil, param2 = nil) | |
return true | |
end | |
def xtest_equality | |
param1 = nil | |
param2 = nil | |
result = sut(param1, param2) | |
assertEqual(true, result) | |
end | |
def xtest_truth | |
param1 = nil | |
param2 = nil | |
result = sut(param1, param2) | |
assertTrue(result, "for p1=#{param1} and p2=#{param2}") | |
end | |
end | |
module Testing | |
def run_tests | |
begin | |
count = 0 | |
self.class.instance_methods(false).grep(/^test_/).each do |meth| | |
puts "TEST: #{meth}" | |
self.send(meth) | |
count += 1 | |
end | |
if count.zero? | |
puts "\n ❓ hmm 🎉 #{count} tests found" | |
else | |
puts "\n ✅ YAY 🎉 #{count} tests passed" | |
end | |
rescue => ex | |
puts "\n 😔 BOO: #{ex}" | |
ex.backtrace.each do |line| | |
next unless line =~ /solution\.rb/ | |
puts line | |
end | |
end | |
end | |
# ---- ASSERTIONS --------------------------------------------------------- | |
def assertEqual(expected, actual) | |
raise "Not equal: [#{expected}] is not [#{actual}]" unless expected == actual | |
end | |
def assertTrue(something, message = 'no other message') | |
raise "<#{something}> is not true: #{message}" unless something == true | |
end | |
def assertFalse(something, message = 'no other message') | |
raise "<#{something}> is not false: #{message}" unless something == false | |
end | |
end | |
code = Code.new | |
class << code | |
include Testing | |
end | |
code.run_tests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment