Sha256: 4a664cf3acfc786a3b974513b04ac2a95e0ffadcd1f5eb03f317a71320bf48c3

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

require_relative "../lib/object_shadow"
require "minitest/autorun"

describe "Object#shadow" do
  let :c do
    Class.new do
      def initialize
        @ivar = 42
        @another_variable = 43
      end
    end
  end

  let :o do
    c.new
  end

  describe "#variables" do
    it "shows list of instance variable names" do
      assert_equal \
        [:ivar, :another_variable],
        o.shadow.variables
    end
  end

  describe "#variable?" do
    it "returns true if variable is defined" do
      assert \
        o.shadow.variable?(:ivar)
    end

    it "returns false if variable is not defined" do
      refute \
        o.shadow.variable?(:ovar)
    end
  end

  describe "#[]" do
    it "returns value of given instance variable name" do
      assert_equal \
        42,
        o.shadow[:ivar]
    end
  end

  describe "#[]=" do
    it "sets value of given instance variable name" do
      o.shadow[:ivar] = 1
      assert_equal \
        1,
        o.instance_variable_get(:@ivar)
    end
  end

  describe "to_h" do
    it "returns hash of all instance variables" do
      assert_equal(
        { ivar: 42, another_variable: 43 },
        o.shadow.to_h
      )
    end
  end

  describe "to_a" do
    it "returns array of all instance variable values" do
      assert_equal \
        [42, 43],
        o.shadow.to_a
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
object_shadow-1.1.1 spec/object_shadow_instance_variables_spec.rb
object_shadow-1.1.0 spec/object_shadow_instance_variables_spec.rb
object_shadow-1.0.0 spec/object_shadow_instance_variables_spec.rb