File: object_spec.rb

package info (click to toggle)
ruby-extlib 0.9.16-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 576 kB
  • sloc: ruby: 7,014; makefile: 5
file content (114 lines) | stat: -rw-r--r-- 3,090 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require 'spec_helper'
require 'extlib/object'

module HactiveSupport
  class MemoizeConsideredUseless
  end
end

class SymbolicDuck
  def quack
  end
end

class ClassyDuck
end

module Foo
  class Bar
  end
end

class Oi
  attr_accessor :foo
end

describe Object do

  describe "#full_const_get" do
    it 'returns constant by FQ name in receiver namespace' do
      Object.full_const_get("Oi").should == Oi
      Object.full_const_get("Foo::Bar").should == Foo::Bar
    end
  end

  describe "#full_const_set" do
    it 'sets constant value by FQ name in receiver namespace' do
      Object.full_const_set("HactiveSupport::MCU", HactiveSupport::MemoizeConsideredUseless)

      Object.full_const_get("HactiveSupport::MCU").should == HactiveSupport::MemoizeConsideredUseless
      HactiveSupport.full_const_get("MCU").should == HactiveSupport::MemoizeConsideredUseless
    end
  end

  describe "#make_module" do
    it 'creates a module from string FQ name' do
      Object.make_module("Milano")
      Object.make_module("Norway::Oslo")

      Object.const_defined?("Milano").should == true
      Norway.const_defined?("Oslo").should == true
    end

    it "handles the case where we already have a class in the heirarchy" do
      Object.make_module("Foo::Bar::Baz")
      Object.const_defined?("Foo").should == true
      Foo.const_defined?("Bar").should == true
      Foo::Bar.const_defined?("Baz").should == true
      Foo::Bar::Baz.should be_kind_of(Module)
    end
  end


  describe "#quacks_like?" do
    it 'returns true if duck is a Symbol and receiver responds to it' do
      SymbolicDuck.new.quacks_like?(:quack).should be(true)
    end

    it 'returns false if duck is a Symbol and receiver DOES NOT respond to it' do
      SymbolicDuck.new.quacks_like?(:wtf).should be(false)
    end

    it 'returns true if duck is a class and receiver is its instance' do
      receiver = ClassyDuck.new
      receiver.quacks_like?(ClassyDuck).should be(true)
    end

    it 'returns false if duck is a class and receiver IS NOT its instance' do
      receiver = ClassyDuck.new
      receiver.quacks_like?(SymbolicDuck).should be(false)
    end

    it 'returns true if duck is an array and at least one of its members quacks like this duck' do
      receiver = ClassyDuck.new
      ary      = [ClassyDuck, SymbolicDuck]

      receiver.quacks_like?(ary).should be(true)
    end

    it 'returns false if duck is an array and none of its members quacks like this duck' do
      receiver = ClassyDuck.new
      ary      = [SymbolicDuck.new, SymbolicDuck]

      receiver.quacks_like?(ary).should be(false)
    end
  end

  describe "#in?" do
    it 'returns true if object is included in collection' do
      @ary = [1, 2, 3]
      @set = Set.new([2, 3, 5])

      1.in?(@ary).should be(true)
      2.in?(@ary).should be(true)
      3.in?(@ary).should be(true)
      4.in?(@ary).should be(false)

      1.in?(@set).should be(false)
      2.in?(@set).should be(true)
      3.in?(@set).should be(true)
      4.in?(@set).should be(false)
      5.in?(@set).should be(true)
    end
  end
end