Skip to content

Instantly share code, notes, and snippets.

@th1agoalmeida
Created October 12, 2018 21:33
Show Gist options
  • Save th1agoalmeida/76e7c47294c2639a9f2b614ff78ebbf3 to your computer and use it in GitHub Desktop.
Save th1agoalmeida/76e7c47294c2639a9f2b614ff78ebbf3 to your computer and use it in GitHub Desktop.
Faxtures (pronounced Facts-tures) are special fixture files that read, write, and COMPARE large outputs
module Faxtures
module_function
class Error < StandardError; end
class ReadFileNotFound < Error; end
# def read(*names, name, &block)
# TODO: read file line by line for RAM optimization
# end
# def write(*names, name, array, &block)
# TODO: yield blocks and write file line by line for RAM optimization
# end
def base_path
TEST_ROOT
end
def read_lines(*names, name)
dir = base_path.join('faxtures').join(*names)
f = dir.join("#{name}.txt")
unescape_array(f.readlines)
rescue Errno::ENOENT => e
raise ReadFileNotFound, e.message
end
def write_lines(*names, name, array)
dir = base_path.join('faxtures').join(*names)
f = dir.join("#{name}.txt")
escaped_array = escape_array(array).join("\n")
f.write(escaped_array)
true
rescue Errno::ENOENT => e
OutputAdapter.block([e.class, e.message], :red)
FileUtils.mkdir_p(dir)
retry
end
def unescape_array(array)
array.map { |s| s.strip.gsub('\n', "\n") }
end
def escape_array(array)
array.map { |s| s.gsub("\n", '\n') }
end
module Syntax
def assert_faxture(*names, name, lines)
expected = read_faxture(*names, name)
assert_equal expected, lines
rescue Faxtures::ReadFileNotFound
write_faxture(*names, name, lines)
retry
end
def write_faxture(*args)
Faxtures.write_lines(*args)
end
def read_faxture(*args)
Faxtures.read_lines(*args)
end
end
end
ActiveSupport::TestCase.include Faxtures::Syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment