Skip to content

Instantly share code, notes, and snippets.

View JonathonMA's full-sized avatar

Jonathon M. Abbott JonathonMA

  • Brisbane, Australia
View GitHub Profile
@JonathonMA
JonathonMA / private_def.rb
Created October 24, 2013 03:09
Why wait for ruby 2.1?
gem 'minitest', '>= 5'
require 'minitest/autorun'
module Kernel
def private_def method_name, &block
define_method method_name, &block
private method_name
end
end
@JonathonMA
JonathonMA / custom_plan.rb
Created October 22, 2013 23:32
Zeus fix for rails 4 minitest double test run issue
require 'zeus/rails'
class CustomPlan < Zeus::Rails
def test_environment
require 'minitest/unit'
MiniTest::Unit.class_variable_set('@@installed_at_exit', true)
super
end
end
@JonathonMA
JonathonMA / sanity_check.rb
Last active December 25, 2015 20:19
Check for assumed infrastructure
#!/usr/bin/env ruby
# Encoding: UTF-8
require 'socket'
def listening? host, port
TCPSocket.open(host, port)#.close
true
rescue
false
end
class MiniStub
def initialize *args
unless args.first.is_a? Hash
@name = args.shift
end
@stubbed = args.first || {}
end
def inspect
"#<MiniStub(#{@name})>"
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define C99
struct timeval tv;
time_t curtime;
@JonathonMA
JonathonMA / wat_errors.rb
Created September 18, 2013 06:41
Errors, how do they work?
begin
fail NoMethodError, "foo"
rescue StandardError => e
puts "NoMethodError is rescued by StandardError: #{e.inspect}"
rescue NoMethodError => e
puts "NoMethodError is not rescued by StandardError: #{e.inspect}"
p e
end
puts "NoMethodError is not a StandardError" unless NoMethodError.is_a? StandardError
@JonathonMA
JonathonMA / mocks.rb
Created September 17, 2013 04:30
F@cking Mocks? How do they work?
# Note minitest 2 doesn't run out of assertions so that test case would fail
gem 'minitest', '~> 4'
require 'minitest/unit'
require 'minitest/mock'
require 'minitest/autorun'
def do_nothing_with object
end
@JonathonMA
JonathonMA / testfor.rb
Created September 13, 2013 06:00
Sick of boilerplate.
#!/usr/bin/env ruby
# testfor - gen test for class
#
# USAGE
#
# Generate a test skeleton for the Person class
# $ testfor Person
require 'active_support'
require 'active_support/core_ext/string/inflections'
@JonathonMA
JonathonMA / post-checkout
Created September 11, 2013 04:20
Git post-checkout hook to enable sensible foreman restart upon branch change with bonus bundle check
#!/usr/bin/env ruby
# post-checkout - git post checkout hook script
#
# This script will update the contents of .git/CURRENT_BRANCH with the name of
# the current branch after a checkout.
#
# Use it with rerun to restart foreman on branch changes.
# Bonus feature: sanity checks your Gemfile using $ bundle check
#
# Start foreman like this to watch CURRENT_BRANCH:
@JonathonMA
JonathonMA / cross.rb
Last active December 22, 2015 01:39
Cross product of an array against itself n times
def cross values, n = 3
Array.new(n, values).inject do |acc, i|
acc.product(i).map(&:flatten)
end
end
require 'set'
describe do
it "should cross itself" do