Пример #1
0
    def testPackageClassHasScope(self):
        package = Package(name=Name(["foo"]))
        clas = package.addClass(Name(["C"]), sourceName="C", typeParameters=[],
                                supertypes=[getRootClassType()], flags=frozenset([PUBLIC]))
        classType = ClassType(clas)
        publicCtor = package.addFunction(Name(["C", CONSTRUCTOR_SUFFIX]),
                                         sourceName=CONSTRUCTOR_SUFFIX,
                                         returnType=UnitType,
                                         typeParameters=[], parameterTypes=[classType],
                                         flags=frozenset([PUBLIC, METHOD, EXTERN]))
        privateCtor = package.addFunction(Name(["C", CONSTRUCTOR_SUFFIX]),
                                          sourceName=CONSTRUCTOR_SUFFIX,
                                          returnType=UnitType,
                                          typeParameters=[], parameterTypes=[classType],
                                          flags=frozenset([PRIVATE, METHOD, EXTERN]))
        clas.constructors = [publicCtor, privateCtor]
        publicMethod = package.addFunction(Name(["C", "m1"]), sourceName="m1",
                                           returnType=UnitType,
                                           typeParameters=[], parameterTypes=[classType],
                                           flags=frozenset([PUBLIC, METHOD, EXTERN]))
        privateMethod = package.addFunction(Name(["C", "m2"]), sourceName="m2",
                                            returnType=UnitType,
                                            typeParameters=[], parameterTypes=[classType],
                                            flags=frozenset([PRIVATE, METHOD, EXTERN]))
        clas.methods = [publicMethod, privateMethod]
        publicField = package.newField(Name(["C", "x"]), sourceName="x", type=UnitType,
                                       flags=frozenset([PUBLIC, EXTERN]))
        privateField = package.newField(Name(["C", "y"]), sourceName="y", type=UnitType,
                                        flags=frozenset([PRIVATE, EXTERN]))
        clas.fields = [publicField, privateField]

        packageLoader = FakePackageLoader([package])
        info = CompileInfo(None, Package(id=TARGET_PACKAGE_ID), packageLoader)
        topPackageScope = PackageScope(PACKAGE_SCOPE_ID, None, info,
                                       packageLoader.getPackageNames(), [], None)
        builtinScope = BuiltinGlobalScope(topPackageScope)
        fooPackageScope = topPackageScope.scopeForPrefix("foo", NoLoc)

        defnInfo = fooPackageScope.lookupFromSelf("C", NoLoc).getDefnInfo()
        self.assertIs(clas, defnInfo.irDefn)
        self.assertIs(fooPackageScope.scopeId, defnInfo.scopeId)
        self.assertFalse(info.hasScope(clas.id))
        classScope = NonLocalObjectTypeDefnScope.ensureForDefn(clas, info)
        self.assertIs(classScope, info.getScope(clas.id))
        defnInfo = classScope.lookupFromSelf(CONSTRUCTOR_SUFFIX, NoLoc).getDefnInfo()
        self.assertIs(publicCtor, defnInfo.irDefn)
        self.assertIs(classScope.scopeId, defnInfo.scopeId)
        defnInfo = classScope.lookupFromSelf("m1", NoLoc).getDefnInfo()
        self.assertIs(publicMethod, defnInfo.irDefn)
        self.assertRaises(ScopeException, classScope.lookupFromSelf, "m2", NoLoc)
        defnInfo = classScope.lookupFromSelf("x", NoLoc).getDefnInfo()
        self.assertIs(publicField, defnInfo.irDefn)
        self.assertRaises(ScopeException, classScope.lookupFromSelf, "y", NoLoc)
Пример #2
0
 def testImportRedeclare(self):
     foo = Package(name=Name(["foo"]))
     foo.addGlobal(Name(["bar"]), None, None, frozenset([PUBLIC, LET]))
     source = "let bar = 12\n" + \
              "import foo.bar"
     self.assertRaises(ScopeException, self.analyzeFromSource, source,
                       packageLoader=FakePackageLoader([foo]))
Пример #3
0
 def infoAndScopeWithPackageNames(self, args):
     packageNameFromString = lambda s: Name.fromString(s, isPackageName=True)
     names = map(packageNameFromString, args)
     package = Package(id=TARGET_PACKAGE_ID)
     packageLoader = FakePackageLoader(names)
     info = CompileInfo(None, package, packageLoader)
     scope = PackageScope(PACKAGE_SCOPE_ID, None, info, names, [], None)
     return info, scope
Пример #4
0
 def analyzeFromSource(self, source, packageLoader=None):
     ast = self.parseFromSource(source)
     package = Package(id=TARGET_PACKAGE_ID)
     if packageLoader is None:
         packageLoader = FakePackageLoader([])
     info = CompileInfo(ast, package, packageLoader, isUsingStd=False)
     analyzeDeclarations(info)
     return info
Пример #5
0
 def testTypeParameterCycleForeign(self):
     package = Package(name=Name(["foo"]))
     barClass = package.addClass(Name(["Bar"]), sourceName="Bar", typeParameters=[],
                                 supertypes=[getRootClassType()],
                                 constructors=[], fields=[],
                                 methods=[], flags=frozenset([PUBLIC]))
     loader = FakePackageLoader([package])
     source = "class Baz <: foo.Bar\n" + \
              "def f[static T <: Baz >: foo.Bar] = ()"
     self.assertRaises(InheritanceException, self.analyzeFromSource, source, packageLoader=loader)
Пример #6
0
    def testInheritFromImportedClass(self):
        foo = Package(name=Name(["foo"]))
        Bar = foo.addClass(Name(["Bar"]), sourceName="Bar", typeParameters=[],
                           supertypes=[getRootClassType()],
                           constructors=[], fields=[],
                           methods=[], flags=frozenset([PUBLIC]))
        x = foo.newField(Name(["Bar", "x"]), sourceName="x", flags=frozenset([PUBLIC, LET]))
        Bar.fields.append(x)

        source = "import foo.Bar\n" + \
                 "class Baz <: Bar"
        info = self.analyzeFromSource(source, packageLoader=FakePackageLoader([foo]))
        bazScope = info.getScope(info.ast.modules[0].definitions[1])
        self.assertTrue(bazScope.isBound("x"))
Пример #7
0
 def testImportGlobalFromPackage(self):
     foo = Package(name=Name(["foo"]))
     foo.addGlobal(Name(["bar"]), sourceName="bar", flags=frozenset([PUBLIC, LET]))
     foo.addGlobal(Name(["baz"]), sourceName="baz", flags=frozenset([LET]))
     source = "import foo._"
     info = self.analyzeFromSource(source, packageLoader=FakePackageLoader([foo]))
     scope = info.getScope(info.ast.modules[0])
     self.assertFalse(scope.isBound("baz"))
     scope.define("bar")
     nameInfo = scope.lookupFromSelf("bar", NoLoc)
     importedDefnInfo = nameInfo.getDefnInfo()
     self.assertEquals(scope.scopeId, importedDefnInfo.scopeId)
     self.assertFalse(importedDefnInfo.isVisible)
     self.assertIsNone(importedDefnInfo.importedTypeArguments)
Пример #8
0
 def analyzeFromSource(self, source, name=None):
     filename = "(test)"
     tokens = lex(filename, source)
     ast = parse(filename, tokens)
     if name is None:
         name = Name(["test"])
     package = Package(TARGET_PACKAGE_ID, name=name)
     packageLoader = FakePackageLoader([])
     info = CompileInfo(ast, package, packageLoader, isUsingStd=False)
     analyzeDeclarations(info)
     analyzeTypeDeclarations(info)
     analyzeInheritance(info)
     analyzeTypes(info)
     convertClosures(info)
     return info
Пример #9
0
 def testInheritForeignTypeInForeignTypeInSamePackage(self):
     package = Package(name=Name(["foo"]))
     barClass = package.addClass(Name(["Bar"]), sourceName="Bar", typeParameters=[],
                                 supertypes=[getRootClassType()],
                                 constructors=[], fields=[],
                                 methods=[], flags=frozenset([PUBLIC]))
     bazClass = package.addClass(Name(["Baz"]), sourceName="Baz", typeParameters=[],
                                 supertypes=[ClassType(barClass), getRootClassType()],
                                 constructors=[], fields=[],
                                 methods=[], flags=frozenset([PUBLIC]))
     loader = FakePackageLoader([package])
     info = self.analyzeFromSource("class Quux <: foo.Baz", packageLoader=loader)
     quuxClass = info.package.findClass(name="Quux")
     self.assertEquals([ClassType(bazClass), ClassType(barClass), getRootClassType()],
                       quuxClass.supertypes)
Пример #10
0
 def testInheritFromForeignType(self):
     package = Package(name=Name(["foo"]))
     foreignClass = package.addClass(Name(["Bar"]), sourceName="Bar", typeParameters=[],
                                     supertypes=[getRootClassType()],
                                     constructors=[], fields=[],
                                     methods=[], flags=frozenset([PUBLIC]))
     field = package.newField(Name(["Bar", "x"]), sourceName="x", flags=frozenset([PUBLIC]))
     foreignClass.fields = [field]
     loader = FakePackageLoader([package])
     source = "class Baz <: foo.Bar"
     info = self.analyzeFromSource(source, packageLoader=loader)
     bazClass = info.package.findClass(name="Baz")
     self.assertEquals([ClassType(foreignClass), getRootClassType()], bazClass.supertypes)
     bazScope = info.getScope(bazClass)
     self.assertTrue(bazScope.isBound("x"))
Пример #11
0
 def testInheritForeignTypeInForeignTypeInDifferentPackage(self):
     fooPackage = Package(name=Name(["foo"]))
     barClass = fooPackage.addClass(Name(["Bar"]), sourceName="Bar", typeParameters=[],
                                    supertypes=[getRootClassType()],
                                    constructors=[], fields=[],
                                    methods=[], flags=frozenset([PUBLIC]))
     bazPackage = Package(name=Name(["baz"]))
     loader = FakePackageLoader([fooPackage, bazPackage])
     bazPackage.dependencies.append(PackageDependency.fromPackage(fooPackage))
     quuxClass = bazPackage.addClass(Name(["Quux"]), sourceName="Quux", typeParameters=[],
                                     supertypes=[ClassType(barClass), getRootClassType()],
                                     constructors=[], fields=[],
                                     methods=[], flags=frozenset([PUBLIC]))
     info = self.analyzeFromSource("class Zzyzx <: baz.Quux", packageLoader=loader)
     zzyzxClass = info.package.findClass(name="Zzyzx")
     self.assertEquals([ClassType(quuxClass), ClassType(barClass), getRootClassType()],
                       zzyzxClass.supertypes)
Пример #12
0
    def testImportGlobalFromPackageMultipleModules(self):
        foo = Package(name=Name(["foo"]))
        foo.addGlobal(Name(["bar"]), sourceName="bar", flags=frozenset([PUBLIC, LET]))
        loader = FakePackageLoader([foo])

        source1 = "import foo.bar"
        mod1 = self.parseFromSource(source1)
        source2 = "import foo.bar\n" + \
                  "let baz = 12"
        mod2 = self.parseFromSource(source2)
        astt = ast.Package([mod1, mod2], NoLoc)
        astt.id = AstId(-1)
        package = Package(id=TARGET_PACKAGE_ID)
        info = CompileInfo(astt, package, loader, isUsingStd=False)
        analyzeDeclarations(info)

        baz = info.package.findGlobal(name="baz")
        bazScope = info.getScope(baz.astDefn)
        self.assertIs(info.getScope(mod2), bazScope)
Пример #13
0
 def testImportMissingFromPackage(self):
     foo = Package(name=Name(["foo"]))
     source = "import foo.baz"
     self.assertRaises(ScopeException, self.analyzeFromSource, source,
                       packageLoader=FakePackageLoader([foo]))
Пример #14
0
 def testImportNonPublicFromPackage(self):
     foo = Package(name=Name(["foo"]))
     foo.addGlobal(Name(["baz"]), None, None, frozenset([LET]))
     source = "import foo.baz"
     self.assertRaises(ScopeException, self.analyzeFromSource, source,
                       packageLoader=FakePackageLoader([foo]))