Created
December 8, 2008 18:30
-
-
Save mkhl/33554 to your computer and use it in GitHub Desktop.
Ruby
This file contains hidden or 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
# prevent the use of `` in tests | |
Spec::Runner.configure do |configuration| | |
backtick = nil # establish the variable in this scope | |
saved_system = nil | |
configuration.prepend_before(:all) do | |
# raise an exception if Kernel#` or Kernel#system is used | |
# in our tests, we want to ensure we're fully self-contained | |
Kernel.instance_eval do | |
backtick = instance_method(:'`') | |
saved_system = instance_method(:system) | |
define_method :'`' do |str| | |
raise "Cannot use backticks in tests" | |
end | |
define_method :system do |*args| | |
raise "Cannot use Kernel#system in tests" | |
end | |
end | |
end | |
configuration.prepend_after(:all) do | |
# and now restore Kernel#` and Kernel#system | |
Kernel.instance_eval do | |
define_method :'`', backtick | |
define_method :system, saved_system | |
end | |
end | |
end | |
This file contains hidden or 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
def no_stdout | |
old_stdout = $stdout | |
$stdout.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w')) | |
yield | |
$stdout = old_stdout | |
end | |
def test_whatever | |
no_stdout do | |
puts 'no one will ever see this' | |
end | |
end | |
This file contains hidden or 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
config = YAML.load(ERB.new(IO.read(config_file)).result) |
This file contains hidden or 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
require 'erb' | |
name = 'bob' | |
template = 'whatever, <%= name %>' | |
puts ERB.new(template).result(binding) | |
This file contains hidden or 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
class Test::Unit::TestCase | |
alias :original_puts :puts | |
def puts(string ="") | |
super string.to_s + "\s(#{caller.first.match(/(\w+\.\w+:\d+)|Rakefile:\d+/)[0]})" | |
end | |
alias :original_p :p | |
def p(string="") | |
original_puts "\s(#{caller.first.match(/(\w+\.\w+:\d+)|Rakefile:\d+/)[0]})" | |
super(string) | |
end | |
alias :original_print :print | |
def print(string="") | |
super string + "\s(#{caller.first.match(/(\w+\.\w+:\d+)|Rakefile:\d+/)[0]})" | |
end | |
end | |
This file contains hidden or 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
## | |
# test/spec/mini | |
# [email protected] | |
# | |
def context(*args, &block) | |
return super unless (name = args.first) && block | |
require 'test/unit' | |
klass = Class.new(Test::Unit::TestCase) do | |
def self.specify(name, &block) define_method("test_#{name.gsub(/\W/,'_')}", &block) end | |
def self.xspecify(*args) end | |
def self.setup(&block) define_method(:setup, &block) end | |
def self.teardown(&block) define_method(:teardown, &block) end | |
end | |
klass.class_eval &block | |
end | |
# ensure setup / spec / teardown order | |
# also ensure we can define methods inline so `include Helpers` works | |
context "test/spec/mini" do | |
def put(message) | |
@messages << message | |
end | |
setup do | |
@messages = [] | |
put :setup | |
assert true | |
end | |
specify "a specification!" do | |
put :specify1 | |
assert true | |
end | |
specify "another specification?!" do | |
put :specify2 | |
assert true | |
end | |
specify "failed spec" do | |
put :failed | |
flunk | |
end | |
specify "error spec" do | |
put :error | |
raise "donut" | |
end | |
xspecify "disabled spec" do | |
put :disabled | |
assert true | |
end | |
teardown do | |
put :teardown | |
assert true | |
Kernel.puts @messages.join(':') | |
end | |
end | |
This file contains hidden or 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
class Object | |
define_method :not do | |
Not.new(self) | |
end | |
class Not | |
private *instance_methods.select { |m| m !~ /(^__|^\W|^binding$)/ } | |
def initialize(subject) | |
@subject = subject | |
end | |
def method_missing(sym, *args, &blk) | |
[email protected](sym,*args,&blk) | |
end | |
end | |
end | |
This file contains hidden or 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
require "yaml" | |
require "fileutils" | |
PWD = File.dirname(__FILE__) | |
Dir["defaults/*.yml"].each do |defaults_file| | |
FileUtils.chdir(PWD) | |
defaults = YAML.load(File.open(defaults_file).read) | |
domain = defaults.delete("domain") | |
print "#{domain}: " | |
defaults.each do |key, value| | |
output = 'defaults write #{domain} #{key.inspect} #{value.inspect}' | |
if $? != 0 | |
puts output | |
exit 1 | |
end | |
putc "." | |
end | |
print "\n" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment