public
Description: Ruby 2.0 functionality implemented in Ruby 1.8.
Homepage: http://devblog.michaelgalero.com
Clone URL: git://github.com/mikong/point2.git
commit  813f6e8b13fb9fd9532295a2bbe9101a8da943f8
tree    b5a0faaeb792eca46efa75e20b45a40a6bb4b9d8
parent  c7a1c0d50f4e868b3fe7919dea19b78a4ffd8480
point2 / spec / point2_spec.rb
100644 127 lines (94 sloc) 3.319 kb
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
115
116
117
118
119
120
121
122
123
124
125
126
127
require 'rubygems'
require 'spec'
require File.expand_path(File.dirname(__FILE__) + "/../point2.rb")
 
class Book
  attr_accessor :title, :no_of_pages
  
  def initialize(title, no_of_pages = 0)
    @title = title
    @no_of_pages = no_of_pages
  end
  
  def to_ary
    [[:title, title], [:no_of_pages, no_of_pages]]
  end
  
  def to_hash
    { :title => title, :no_of_pages => no_of_pages }
  end
end
 
describe "Point2 Project" do
  
  describe Object do
 
    it "can try a method" do
      @book.try(:title).should be_nil
      @book = Book.new('Harry Potter')
      @book.try(:title).should eql('Harry Potter')
    end
    
    it "can be tapped" do
      @reversed_and_capitalized = "dog".reverse.tap {|o| @reversed = o}.capitalize
      @reversed.should == 'god'
      @reversed_and_capitalized.should == 'God'
    end
    
    it "can return a public method" do
      @object = Object.new
      
      # calling a non-existent method
      lambda do
        @object.public_method(:tatata)
      end.should raise_error(NameError)
      
      # calling a private method
      lambda do
        @object.public_method(:format)
      end.should raise_error(NameError)
      
      # calling a public method
      @method = @object.public_method(:class)
      @method.call.should == @object.class
    end
    
    it "can __send__ *args to a public method" do
      @object = Object.new
      
      # send to a private method
      lambda do
        @object.public_send(:format)
      end.should raise_error(NoMethodError)
      
      @object.public_send(:class).should == @object.class
    end
  end
  
  describe Array do
    
    before :each do
      @book = Book.new('My Book', 25)
    end
    
    it "should be able to try to convert an object to array" do
      Array.try_convert(@book).should == [[:title, 'My Book'], [:no_of_pages, 25]]
    end
    
    it "should be able to flatten an array with a level parameter" do
      @array = [1, [2, 3], [4, [5, 6]], 7, 8]
      @array.flatten.should == [1, 2, 3, 4, 5, 6, 7, 8]
      @array.flatten(-1).should == [1, 2, 3, 4, 5, 6, 7, 8]
      @array.flatten(0).should == [1, [2, 3], [4, [5, 6]], 7, 8]
      @array.flatten(1).should == [1, 2, 3, 4, [5, 6], 7, 8]
      @array.flatten(2).should == [1, 2, 3, 4, 5, 6, 7, 8]
      
      # bang! method
      @array.flatten!(1)
      @array.should == [1, 2, 3, 4, [5, 6], 7, 8]
    end
    
  end
  
  describe Hash do
    
    # NOTE: Ruby 1.8 Hash doesn't remember the order of its elements
    
    before :each do
      @hash = { :books => ['Book 1', 'Book 2', 'Book 3'], :reading_glasses => 1 }
      @book = Book.new('My Book', 25)
    end
    
    it "should be able to try to convert an object to hash" do
      Hash.try_convert(@book).should == { :title => 'My Book', :no_of_pages => 25 }
    end
    
    it "should be able to produce a flattened array" do
      @hash.flatten.should == [ :books, 'Book 1', 'Book 2', 'Book 3', :reading_glasses, 1 ]
      @hash.flatten(1).should == [ :books, ['Book 1', 'Book 2', 'Book 3'], :reading_glasses, 1 ]
    end
    
  end
  
  describe Time do
    
    before :each do
      @time = Time.utc(2008, "apr", 1)
    end
    
    it "should confirm it's weekday" do
      @time.tuesday?.should be_true
      @time.sunday?.should be_false
    end
    
  end
  
end