Skip to content

Instantly share code, notes, and snippets.

@localhostdotdev
Last active April 23, 2019 02:58
Show Gist options
  • Select an option

  • Save localhostdotdev/a70ef43cc49879c743d8d8bfc91d6f2c to your computer and use it in GitHub Desktop.

Select an option

Save localhostdotdev/a70ef43cc49879c743d8d8bfc91d6f2c to your computer and use it in GitHub Desktop.
adds let and it to ActiveSupport::TestCase
class ActiveSupport::TestCase
parallelize(workers: :number_of_processors)
def self.let(name, &block)
@@lets ||= SimpleHash.new
@@lets[name] = block
end
def self.it(&block)
name = "test_"
name += self.class.name.underscore
name += "_#{block.source.underscore}"
define_method(name, &block)
end
def method_missing(name, *args, &block)
super if args.any? || block
super unless @@lets.has_key?(name)
if @@lets.fetch(name).is_a?(Proc)
@@lets[name] = @@lets.fetch(name).call
end
@@lets.fetch(name)
end
def respond_to_missing(name, include_private = false)
@@lets.has_key?(name) || super
end
def methods
super + @@lets.keys.map(&:to_sym)
end
end
require 'test_helper'
class SimpleHashTest < ActiveSupport::TestCase
let(:data) { { a: 1, b: [{ c: { d: 2 } }, { e: 3 }], f: 4 } }
it { assert_nothing_raised { SimpleHash.new(data) } }
it { assert_nothing_raised { SimpleHash[data] } }
it { assert_equal(SimpleHash[data].a, 1) }
it { assert_equal(SimpleHash[data].b[0].c.d, 2) }
it { assert_equal(SimpleHash[data].b[1].e, 3) }
it { assert_equal(SimpleHash[data].f, 4) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment