Sha256: 446c80607c1414eb0095860d20c1a070d59f6fe6d76ae358a9e0d9c31d157fae

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

require "helper"
require 'dr/ruby_ext/meta_ext'
#Module.send :include, DR::Meta

class TestMetaExt < Minitest::Test
	def setup
		@foo=Module.new do
			extend DR::MetaModule
			def foo
				"foo"
			end
		end
		@bar=Module.new do
			def bar
				"bar"
			end
		end
		@baz=Module.new do
			def baz
				"baz"
			end
		end
		@foo.includes_extends_host_with(@bar,@baz)
	end
	def test_includes
		klass=@foo
		test1=Class.new do
			include klass
		end
		test1.new.foo
		test1.new.bar
		test1.baz
	end
	def test_extends
		klass=@foo
		test1=Class.new do
			extend klass
		end
		test1.foo
		test1.new.bar
		test1.baz
	end
end

describe DR::Meta do
	## Does not work anymore in recent rubies (ruby 2.4+)
	# it "Can convert a class to module" do
	# 	(Class.new { include DR::Meta.refined_module(String) { def length; super+5; end } }).new("foo").length.must_equal(8)
	# end

	it "Can show all ancestors" do
		DR::Meta.all_ancestors("foo").include?(String.singleton_class).must_equal(true)
	end

	it "Can generate bound methods" do
		m=DR::Meta.get_bound_method("foo", :bar) do |x|
			self+x
		end
		m.call("bar").must_equal("foobar")
	end

	it "Can apply unbound methods" do
		DR::Meta.apply(method: String.instance_method(:length), to: "foo").must_equal(3)
	end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
drain-0.5.1 test/test_meta.rb
drain-0.5 test/test_meta.rb
drain-0.4 test/test_meta.rb
drain-0.3.0 test/test_meta.rb
drain-0.2.0 test/test_meta.rb