-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_helper.rb
77 lines (64 loc) · 1.75 KB
/
test_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require "rbconfig"
require "stringio"
require "test/unit"
require "rubygems" # for FFI
require "oklahoma_mixer"
module TestHelper
def run_ruby(code)
# find Ruby and OklahomaMixer
exe = File::join( *Config::CONFIG.values_at( *%w[ bindir
ruby_install_name ] ) ) <<
Config::CONFIG["EXEEXT"]
lib = File.join(File.dirname(__FILE__), *%w[.. lib])
# escape for the shell
[exe, lib].each do |path|
path.gsub!(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/n, '\\')
path.gsub!(/\n/, "'\n'")
path.sub!(/\A\z/, "''")
end
# run the program and read the results
open("| #{exe} -I #{lib} -rubygems -r oklahoma_mixer", "r+") do |program|
program << code
program.close_write
@output = program.read.to_s
end
end
def capture_stderr
$stderr = StringIO.new
yield
$stderr.string
ensure
$stderr = STDERR
end
def capture_args(receiver, method)
called_with = nil
singleton = class << receiver; self end
singleton.send(:define_method, method) do |*args|
called_with = args
end
yield
called_with
end
def db_path(ext)
File.join( File.dirname(__FILE__),
"#{self.class.to_s.sub(/\ATest/, '').downcase}.#{ext}" )
end
def hdb(*args, &block)
OKMixer.open(db_path("tch"), *args, &block)
end
def bdb(*args, &block)
OKMixer.open(db_path("tcb"), *args, &block)
end
def fdb(*args, &block)
OKMixer.open(db_path("tcf"), *args, &block)
end
def tdb(*args, &block)
OKMixer.open(db_path("tct"), *args, &block)
end
def remove_db_files
Dir.glob(db_path("*")) do |file|
File.unlink(file)
end
end
end
Test::Unit::TestCase.send(:include, TestHelper)