public
Description: A lean-and-mean Ruby/ObjC bridge
Homepage: http://rubyobjc.com
Clone URL: git://github.com/timburks/rubyobjc.git
rubyobjc / test / test_objc.rb
100644 212 lines (193 sloc) 6.755 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#
# test_objc.rb
#
# Tests RubyObjC core functions.
#
# Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
# For more information about this file, visit http://www.rubyobjc.com.
#
 
require 'test/unit'
require 'lib/objc'
 
class ObjC::NSObject
  use_cmethod("passInt:", "i@:i")
  def self.passInt_(x)
    (x * 1) + 0
  end
end
 
class TestObjC < Test::Unit::TestCase
 
  def test_class_method
    #ObjC::Class.find("NSObject").add_class_method_handler("passInt:", "i@:i")
    require 'test/testobject'
    x = ObjC::TestObject.alloc.init
    errors = x.testClassMethod
    assert_equal(0, errors)
  end
 
  def test_ivars
    require 'test/testobject'
    ivars = ObjC::TestObject.ivars
    assert_equal("_boolValue", ivars[0].name)
    assert_equal("c", ivars[0].type_encoding)
    assert_equal(4, ivars[0].offset)
    ivars = ObjC::NSObject.ivars
    assert_equal(1, ivars.length)
    assert_equal("isa", ivars[0].name)
    assert_equal("#", ivars[0].type_encoding)
    assert_equal(0, ivars[0].offset)
  end
 
  def test_classes
    classes = ObjC::Class.to_a
    assert(classes.length != 0, "there must be some classes found")
    # classes should be sortable
    classes.sort
    # lookup some classes by name
    nsobject = ObjC::Class.find("NSObject")
    assert(nsobject != nil, "if a class exists, a wrapper must be created")
    nsstring = ObjC::Class.find("NSString")
    assert(nsstring != nil, "if a class exists, a wrapper must be created")
    # lookup a class that doesn't exist
    nothing = ObjC::Class.find("ThisIsNotAClassName")
    assert(nothing == nil, "a wrapper for a nonexistent class must be nil")
    # equality check
    nsobject2 = ObjC::Class.find("NSObject")
    assert(nsobject == nsobject2, "wrappers for the same class must be equal")
    assert(nsobject != nsstring, "wrappers for different classes must not be equal")
    # method access
    class_methods = nsobject.cmethods
    assert(class_methods.length > 0, "a class' methods could not be found")
    instance_methods = nsobject.imethods
    assert(instance_methods.length > 0, "instance methods could not be found")
    # lookup a few instance methods
    instance_methods[0..10].each {|im| assert_not_equal(nil, nsobject.get_imethod(im.to_s))}
    class_methods = nsobject.cmethods
    assert(class_methods.length > 0, "class methods could not be found")
    # lookup a few class methods
    class_methods[0..10].each {|cm| assert_not_equal(nil, nsobject.get_cmethod(cm.name))}
    # class name
    assert_equal("NSObject", nsobject.name)
    assert_equal("NSString", nsstring.name)
    assert_equal("NSObject", nsobject.to_s)
    assert_equal("NSString", nsstring.to_s)
    # superclasses
    assert_equal(nil, nsobject.super)
    assert_equal(nsobject, nsstring.super)
    assert_equal(nsstring, ObjC::Class.find("NSMutableString").super)
    # protocols, noncritical to rubyobjc operation
    assert_equal(["NSObject"], nsobject.protocols)
    assert_equal(["NSCopying", "NSMutableCopying", "NSCoding"], nsstring.protocols)
  end
 
  def test_methods
    nsmutabledictionary = ObjC::Class.find("NSMutableDictionary")
    assert nsmutabledictionary.imethods.length > 20
    method = nsmutabledictionary.get_imethod "setObject:forKey:"
    assert_equal 4, method.argument_count
    assert_equal "v16@0:4@8@12", method.type_encoding
    assert nsmutabledictionary.cmethods.length > 0
    method = nsmutabledictionary.get_cmethod "dictionaryWithCapacity:"
    assert_equal 3, method.argument_count
    assert_equal "@12@0:4I8", method.type_encoding
  end
 
  def test_hash
    c = ObjC::Class.find "NSObject"
    d = ObjC::Class.find "NSObject"
    assert_equal true, (c == d)
    assert_equal true, c.eql?(d)
    assert_equal true, (d == c)
    assert_equal true, d.eql?(c)
    assert_equal c.hash, d.hash
    h = {}
    h[c] = 1
    h[d] = 2
    assert_equal 1, h.keys.length
  end
 
  def test_object_creation
    object1 = ObjC::NSObject.alloc
    object2 = object1.init
    object3 = ObjC::NSString.alloc.init
    assert_not_equal(nil, object1)
    assert_not_equal(nil, object2)
    assert_not_equal(nil, object3)
    view1 = ObjC::NSView.alloc
    view2 = view1.init
    view3 = ObjC::NSView.alloc.init
    assert_not_equal(nil, view1)
    assert_not_equal(nil, view2)
    assert_not_equal(nil, view3)
    assert_raise(NoMethodError) {ObjC::NSView.new}
  end
 
  def test_message
    hello = "hello, tester"
    s = ObjC::NSString.stringWithString_ hello
    l = s.length
    assert hello.length, l
  end
 
  def test_array
    a = ObjC::NSMutableArray.alloc.init
    a.addObject_("one")
    a.addObject_(22)
    assert_equal "one", a.objectAtIndex_(0).to_s
    assert_equal 22, a.objectAtIndex_(1).intValue
    assert_equal 22, a.objectAtIndex_(1).charValue
    assert_equal 2, a.count
    a = nil
  end
 
  def test_dictionary
    d = ObjC::NSMutableDictionary.alloc.init
    d.setObject_forKey_("one", "two")
    assert_equal "one", d.objectForKey_("two").to_s
    d.setObject_forKey_(13, 22)
    assert_equal 13, d.objectForKey_(22).intValue
    d.setObject_forKey_(1.3, 2.2)
    assert_in_delta 1.3, d.objectForKey_(2.2).floatValue, 1e-6
    assert_equal 1.3, d.objectForKey_(2.2).doubleValue
    d = nil
  end
 
  def test_conversions
    s = ObjC::NSString.stringWithCString_ "123.45"
    assert_equal 123, s.to_i
    assert_equal 123.45, s.to_f
  end
 
  def test_properties
    require 'test/testobject'
    x = ObjC::TestObject.alloc.init
    v = true
    x.setBoolValue_ v
    assert_equal 1, x.boolValue
    v = false
    x.setBoolValue_ v
    assert_equal 0, x.boolValue
    v = 123
    x.setIntValue_ v
    assert_equal v, x.intValue
    v = 123456
    x.setLongValue_ v
    assert_equal v, x.longValue
    v = 123.456
    x.setFloatValue_ v
    assert_in_delta v, x.floatValue, 0.00001
    v = 123.456
    x.setDoubleValue_ v
    assert_equal v, x.doubleValue
  end
 
  def test_signatures
    assert_equal "i@:@", ObjC.get_signature_for_selector("numberOfRowsInTableView:")
    assert_equal "@@:@@i", ObjC.get_signature_for_selector("tableView:objectValueForTableColumn:row:")
    assert_equal "c@:@@i", ObjC.get_signature_for_selector("tableView:shouldEditTableColumn:row:")
    1.times {|i| ObjC.get_signatures} # this mustn't crash
  end
 
  def test_use
    ObjC::NSObject.use "foo:", "i@:i"
    ObjC::NSObject.use "foo:", "d@:d"
    ObjC::NSObject.use "foo:", "f@:f"
    # we should use the first signature given
    assert_equal "i@:i", ObjC::Class.find("NSObject").get_imethod("foo:").signature
  end
 
  def test_data
    x = [3,2,1,0,5,4,3].pack("c*")
    assert_equal(7, x.length)
    d = ObjC::NSData.dataWithBytes_length_(x, x.length)
    assert_equal(x, d.to_s)
  end
 
  def test_ready
    assert_equal(true, ObjC::Ready)
  end
end