Sha256: 44512947327ca86db0658eb9745e3e74a24bb0149aae2461d943ef60df7e1d28

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

require 'spec_helper'

describe 'ClassReflection' do
  before :each do
    class MyClass
      def foo
        :hello
      end
    end
    @object = MyClass.new
    @object2 = MyClass.new

    @reflection = VirtualKeywords::ClassReflection.new
  end

  it 'retrieves the instance methods of a class' do
    method_names = @reflection.instance_methods_of(Fizzbuzzer).keys
    method_names.should include :fizzbuzz
  end

  it 'finds the subclasses of classes and flattens the result' do
    rails_classes = [ActiveRecord::Base, ApplicationController]
    subclasses = @reflection.subclasses_of_classes rails_classes

    subclasses.should include Fizzbuzzer
    subclasses.should include Greeter
  end

  it 'installs methods on classes' do
    @reflection.install_method_on_class(MyClass, 'def foo; :goodbye; end')
    @object.foo.should eql :goodbye
  end

  it 'installs methods that change instance fields' do
    class MyClass
      def foo
        :hello
      end
    end
    @reflection.install_method_on_class(
        MyClass, 'def foo; @bar = :bar; :goodbye; end')
    @reflection.install_method_on_class(
        MyClass, 'def bar; @bar; end')

    @object.foo.should eql :goodbye
    @object.bar.should eql :bar
  end

  it 'installs methods that mutate globals' do
    $thing = :old
    @reflection.install_method_on_class(
        MyClass, 'def foo; $thing = :new; end')
    
    @object.foo
    $thing.should eql :new
  end

  it 'installs methods on specific instances' do
    @reflection.install_method_on_instance(
        @object, 'def foo; :goodbye; end')
    @object.foo.should eql :goodbye
    @object2.foo.should eql :hello
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
virtual_keywords-0.3.1 lib/spec/class_reflection_spec.rb
virtual_keywords-0.3.0 lib/spec/class_reflection_spec.rb