mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Implemented ObjectSpace.count_objects to count the number of allocated objects for each type
This commit is contained in:
@@ -86,6 +86,19 @@ def assert_true(ret, msg = nil, diff = nil)
|
||||
ret
|
||||
end
|
||||
|
||||
def assert_false(ret, msg = nil, diff = nil)
|
||||
if $mrbtest_assert
|
||||
$mrbtest_assert_idx += 1
|
||||
if ret
|
||||
msg = "Expected #{ret.inspect} to be false" unless msg
|
||||
diff = assertion_diff(false, ret) unless diff
|
||||
|
||||
$mrbtest_assert.push([$mrbtest_assert_idx, msg, diff])
|
||||
end
|
||||
end
|
||||
!ret
|
||||
end
|
||||
|
||||
def assert_equal(exp, act, msg = nil)
|
||||
msg = "Expected to be equal" unless msg
|
||||
diff = assertion_diff(exp, act)
|
||||
@@ -135,6 +148,14 @@ def assert_raise(*exp)
|
||||
ret
|
||||
end
|
||||
|
||||
##
|
||||
# Fails unless +obj+ is a kind of +cls+.
|
||||
def assert_kind_of(cls, obj, msg = nil)
|
||||
msg = "Expected #{obj.inspect} to be a kind of #{cls}, not #{obj.class}" unless msg
|
||||
diff = assertion_diff(cls, obj.class)
|
||||
assert_true(obj.kind_of?(cls), msg, diff)
|
||||
end
|
||||
|
||||
##
|
||||
# Report the test result and print all assertions
|
||||
# which were reported broken.
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
|
||||
assert('ObjectSpace.count_objects') do
|
||||
h = {}
|
||||
ObjectSpace.count_objects(h)
|
||||
assert_kind_of(Hash, h)
|
||||
assert_true(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) })
|
||||
assert_true(h.values.all? {|x| x.is_a?(Integer) })
|
||||
|
||||
h = ObjectSpace.count_objects
|
||||
assert_kind_of(Hash, h)
|
||||
assert_true(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) })
|
||||
assert_true(h.values.all? {|x| x.is_a?(Integer) })
|
||||
|
||||
assert_raise(TypeError) { ObjectSpace.count_objects(1) }
|
||||
|
||||
h0 = {:MRB_TT_FOO=>1000}
|
||||
h = ObjectSpace.count_objects(h0)
|
||||
assert_false(h0.has_key?(:MRB_TT_FOO))
|
||||
end
|
||||
Reference in New Issue
Block a user