Skip to content
Permalink
Browse files

Merge pull request #544 from cptobvious/module-names

Module names
  • Loading branch information
alex committed Mar 23, 2013
2 parents d20736a + b05c7ad commit e4ddaf7da756eb3d7ab048aedb600c03761a9c22
@@ -26,7 +26,7 @@ def test_acosh(self, space):
w_res = space.execute("return [Math.acosh(1), Math.acosh(2)]")
assert self.unwrap(space, w_res) == [0, math.acosh(2)]

with self.raises(space, "DomainError", 'Numerical argument is out of domain - "acosh"'):
with self.raises(space, "Math::DomainError", 'Numerical argument is out of domain - "acosh"'):
space.execute("Math.acosh(0)")

def test_asin(self, space):
@@ -49,7 +49,7 @@ def test_atanh(self, space):
w_res = space.execute("return [Math.atanh(1), Math.atanh(-1), Math.atanh(0), Math.atanh(0.5)]")
assert self.unwrap(space, w_res) == [float("inf"), float("-inf"), 0, math.atanh(0.5)]

with self.raises(space, "DomainError", 'Numerical argument is out of domain - "atanh"'):
with self.raises(space, "Math::DomainError", 'Numerical argument is out of domain - "atanh"'):
space.execute("Math.atanh(2)")

def test_cbrt(self, space):
@@ -100,9 +100,9 @@ def test_gamma(self, space):
w_res = space.execute("return Math.gamma(Float::INFINITY)")
assert space.float_w(w_res) == float("inf")

with self.raises(space, "DomainError", 'Numerical argument is out of domain - "gamma"'):
with self.raises(space, "Math::DomainError", 'Numerical argument is out of domain - "gamma"'):
space.execute("""Math.gamma(-1)""")
with self.raises(space, "DomainError", 'Numerical argument is out of domain - "gamma"'):
with self.raises(space, "Math::DomainError", 'Numerical argument is out of domain - "gamma"'):
space.execute("""Math.gamma(-Float::INFINITY)""")

w_res = space.execute("return Math.gamma(Float::NAN)")
@@ -117,7 +117,7 @@ def test_ldexp(self, space):
assert self.unwrap(space, w_res) == 1234

def test_log(self, space):
with self.raises(space, "DomainError", 'Numerical argument is out of domain - "log"'):
with self.raises(space, "Math::DomainError", 'Numerical argument is out of domain - "log"'):
space.execute("Math.log(-1)")

w_res = space.execute("return Math.log(0)")
@@ -133,7 +133,7 @@ def test_log(self, space):
self.assert_float_equal(space.float_w(w_res), math.log(3, 4))

def test_log10(self, space):
with self.raises(space, "DomainError", 'Numerical argument is out of domain - "log10"'):
with self.raises(space, "Math::DomainError", 'Numerical argument is out of domain - "log10"'):
space.execute("Math.log10(-1)")

w_res = space.execute("return Math.log10(0)")
@@ -146,7 +146,7 @@ def test_log10(self, space):
assert space.float_w(w_res) == 1.0

def test_log2(self, space):
with self.raises(space, "DomainError", 'Numerical argument is out of domain - "log2"'):
with self.raises(space, "Math::DomainError", 'Numerical argument is out of domain - "log2"'):
space.execute("Math.log2(-1)")

w_res = space.execute("return Math.log2(0)")
@@ -6,6 +6,18 @@ class TestClassObject(BaseTopazTest):
def test_name(self, space):
space.execute("Class")

def test_generated_subclass(self, space):
w_res = space.execute("""
class Foo
class Bar
class Baz
end
end
end
return Foo::Bar::Baz.name
""")
assert space.str_w(w_res) == "Foo::Bar::Baz"

def test_to_s(self, space):
w_res = space.execute("return 1.class.to_s")
assert space.str_w(w_res) == "Fixnum"
@@ -17,6 +17,24 @@ def test_new(self, space):
""")
assert space.int_w(w_res) == 4

def test_submodule(self, space):
w_res = space.execute("""
return Math::DomainError.name
""")
assert space.str_w(w_res) == "Math::DomainError"

def test_generated_submodule(self, space):
w_res = space.execute("""
module Foo
module Bar
module Baz
end
end
end
return Foo::Bar::Baz.name
""")
assert space.str_w(w_res) == "Foo::Bar::Baz"

def test_module_function(self, space):
w_res = space.execute("""
module Mod
@@ -43,8 +43,7 @@ class T
t = T.new
return t.instance_eval(&M::C).name
""")
# TODO: this shoudl really be M::X
assert space.str_w(w_res) == "X"
assert space.str_w(w_res) == "M::X"

def test___id__(self, space):
w_res = space.execute("return BasicObject.new.__id__")
@@ -376,7 +376,7 @@ def BUILD_CLASS(self, space, bytecode, frame, pc):
"wrong argument type %s (expected Class)" % cls_name
)
assert isinstance(superclass, W_ClassObject)
w_cls = space.newclass(name, superclass)
w_cls = space.newclass(name, superclass, is_singleton=False, w_scope=w_scope)
space.set_const(w_scope, name, w_cls)
elif not space.is_kind_of(w_cls, space.w_class):
raise space.error(space.w_TypeError, "%s is not a class" % name)
@@ -390,8 +390,9 @@ def BUILD_MODULE(self, space, bytecode, frame, pc):

name = space.symbol_w(w_name)
w_mod = w_scope.find_const(space, name)

if w_mod is None:
w_mod = space.newmodule(name)
w_mod = space.newmodule(name, w_scope=w_scope)
space.set_const(w_scope, name, w_mod)
elif not space.is_kind_of(w_mod, space.w_module) or space.is_kind_of(w_mod, space.w_class):
raise space.error(space.w_TypeError, "%s is not a module" % name)
@@ -180,5 +180,5 @@ def method_tanh(self, space, value):


class W_DomainError(W_StandardError):
classdef = ClassDef("DomainError", W_StandardError.classdef, filepath=__file__)
classdef = ClassDef("Math::DomainError", W_StandardError.classdef, filepath=__file__)
method_allocate = new_exception_allocate(classdef)
@@ -358,16 +358,15 @@ def method_included(self, space, w_mod):
pass

@classdef.method("extended")
def method_included(self, space, w_mod):
def method_extended(self, space, w_mod):
# TODO: should be private
pass

@classdef.method("name")
def method_name(self, space):
name = self.name
if name is None:
if self.name is None:
return space.w_nil
return space.newstr_fromstr(name)
return space.newstr_fromstr(self.name)

@classdef.method("private")
def method_private(self, space, args_w):
@@ -377,11 +377,13 @@ def newrange(self, w_start, w_end, exclusive):
def newregexp(self, regexp, flags):
return W_RegexpObject(self, regexp, flags)

def newmodule(self, name):
return W_ModuleObject(self, name)
def newmodule(self, name, w_scope=None):
complete_name = self.buildname(name, w_scope)
return W_ModuleObject(self, complete_name)

def newclass(self, name, superclass, is_singleton=False):
return W_ClassObject(self, name, superclass, is_singleton=is_singleton)
def newclass(self, name, superclass, is_singleton=False, w_scope=None):
complete_name = self.buildname(name, w_scope)
return W_ClassObject(self, complete_name, superclass, is_singleton=is_singleton)

def newfunction(self, w_name, w_code, lexical_scope):
name = self.symbol_w(w_name)
@@ -416,6 +418,14 @@ def newbinding_fromblock(self, block):
cells = block.cells[:]
return W_BindingObject(self, names, cells, block.w_self, block.lexical_scope)

def buildname(self, name, w_scope):
complete_name = name
if w_scope is not None:
assert isinstance(w_scope, W_ModuleObject)
if w_scope.name != "Object":
complete_name = "%s::%s" % (w_scope.name, name)
return complete_name

def int_w(self, w_obj):
return w_obj.int_w(self)

0 comments on commit e4ddaf7

Please sign in to comment.
You can’t perform that action at this time.