Sha256: 2c402c123022bc4b2ffc1f96be387373b99c4a34a7c2c87f791cf35a93234581

Contents?: true

Size: 1.72 KB

Versions: 14

Compression:

Stored size: 1.72 KB

Contents

#! /usr/bin/ruby
#encoding: utf-8

require 'test/unit'
require 'shoulda'
require 'minitest/spec'
require 'minitest/autorun'
require 'y_support/name_magic'

describe NameMagic do
  before do
    mod = Module.new do include NameMagic end
    @ç = Class.new do include mod end
    @reporter = Object.new
    puts "..."
    @reporter.singleton_class.class_exec { attr_reader :report, :naming }
    @ç.new_instance_closure do |instance|
      @reporter.define_singleton_method :report do
        "New instance reported"
      end
    end
    @ç.name_set_closure do |name, instance, old_name|
      @reporter.define_singleton_method :name_set do
        "Name of the new instance was #{name}"
      end
      name
    end
    @ç.name_get_closure do |name_object|
      @reporter.define_singleton_method :name_get do
        "Name get closure called on #{name_object}"
      end
      name_object
    end
  end
  
  it "should work" do
    @ç.must_respond_to :const_magic
    @ç.instances.must_be_empty
    @ç.nameless_instances.must_be_empty
    @reporter.report.must_equal nil
    x = @ç.new( name: "Boris" )
    @reporter.report.must_equal "New instance reported"
    @reporter.name_set.must_equal "Name of the new instance was Boris"
    x.name.must_equal :Boris
    @reporter.name_get.must_equal "Name get closure called on Boris"
    ufo = @ç.new
    @ç.nameless_instances.must_equal [ufo]
    UFO = @ç.new
    @reporter.report.must_equal "New instance reported"
    @reporter.name_set.must_equal "Name of the new instance was Boris"
    UFO.name
    @reporter.name_set.must_equal "Name of the new instance was UFO"
    @reporter.name_get.must_equal "Name get closure called on UFO"
    Elaine = @ç.new
    Elaine.name.must_equal :Elaine
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
y_support-2.0.13 test/name_magic_test.rb
y_support-2.0.11 test/name_magic_test.rb
y_support-2.0.10 test/name_magic_test.rb
y_support-2.0.9 test/name_magic_test.rb
y_support-2.0.8 test/name_magic_test.rb
y_support-2.0.7 test/name_magic_test.rb
y_support-2.0.6 test/name_magic_test.rb
y_support-2.0.5 test/name_magic_test.rb
y_support-2.0.3 test/name_magic_test.rb
y_support-2.0.2 test/name_magic_test.rb
y_support-2.0.1 test/name_magic_test.rb
y_support-2.0.0 test/name_magic_test.rb
y_support-1.1.0 test/name_magic_test.rb
y_support-1.0.0 test/name_magic_test.rb