示例#1
0
        public void SmokeTestOverride0()
        {
            string code = "open class Foo { public init() { }\nopen func doSomething() { }\n}\n";
            List <ClassDeclaration> classes = ReflectClassDeclarations(code);

            Assert.AreEqual(1, classes.Count);
            ClassDeclaration theClass = classes [0].MakeUnrooted() as ClassDeclaration;

            TypeMapper typeMapper = new TypeMapper(Compiler.kTypeDatabases, UnicodeMapper.Default);

            typeMapper.RegisterClass(theClass);

            OverrideBuilder overrider = new OverrideBuilder(typeMapper, theClass, null, new ModuleDeclaration("OverrideModule"));

            Assert.IsNotNull(overrider.OverriddenClass);
            Assert.AreEqual(1, overrider.ClassImplementations.Count);
            Assert.IsNotNull(overrider.OverriddenVirtualMethods);
            Assert.AreEqual(1, overrider.OverriddenVirtualMethods.Count);

            using (TempDirectoryFilenameProvider temp = new TempDirectoryFilenameProvider(null, false)) {
                string file      = temp.ProvideFileFor("output.swift");
                SLFile swiftFile = new SLFile(overrider.Imports);
                swiftFile.Classes.AddRange(overrider.ClassImplementations);
                CodeWriter.WriteToFile(file, swiftFile);
            }
        }
示例#2
0
        public void ArcClassStruct()
        {
            string swiftCode =
                "public final class Foo {\npublic var _nm:String\npublic init(name:String) {\n_nm = name }\n" +
                "deinit {\nprint(_nm)\n}\n}\n" +
                "public struct Bar {\n public var a:Foo\n public init(f:Foo) {\n a = f\n}\n }\n"
            ;

            swiftCode += TestRunning.CreateSwiftConsoleRedirect();

            using (TempDirectoryFilenameProvider provider = new TempDirectoryFilenameProvider(null, true)) {
                Utils.CompileSwift(swiftCode, provider);

                string libFileName = Path.Combine(provider.DirectoryPath, "libXython.dylib");
                var    errors      = new ErrorHandling();

                ModuleInventory.FromFile(libFileName, errors);
                Utils.CheckErrors(errors);

                Utils.CompileToCSharp(provider);

                CSUsingPackages use = new CSUsingPackages("System", "System.Runtime.InteropServices", "SwiftRuntimeLibrary");

                CSNamespace ns     = new CSNamespace("Xython");
                CSFile      csfile = CSFile.Create(use, ns);

                CSIdentifier inst  = new CSIdentifier("inst");
                CSLine       newer = CSVariableDeclaration.VarLine((CSSimpleType)"Foo", inst, CSFunctionCall.Ctor("Foo",
                                                                                                                  CSFunctionCall.Function("SwiftString.FromString", CSConstant.Val("nothing"))));

                CSIdentifier inst1  = new CSIdentifier("bar");
                CSLine       newer1 = CSVariableDeclaration.VarLine((CSSimpleType)"Bar", inst1, CSFunctionCall.Ctor("Bar", inst));

                CSLine disposer  = CSFunctionCall.FunctionCallLine(inst.Name + ".Dispose", false);
                CSLine disposer1 = CSFunctionCall.FunctionCallLine(inst1.Name + ".Dispose", false);

                CSCodeBlock mainBody = CSCodeBlock.Create(newer, newer1, disposer, disposer1);

                CSMethod main = new CSMethod(CSVisibility.Public, CSMethodKind.Static, CSSimpleType.Void,
                                             (CSIdentifier)"Main", new CSParameterList(new CSParameter(CSSimpleType.CreateArray("string"), "args")),
                                             mainBody);
                CSClass mainClass = new CSClass(CSVisibility.Public, "NameNotImportant", new CSMethod [] { main });
                ns.Block.Add(mainClass);

                string csOutFilename  = provider.ProvideFileFor(provider.UniqueName(null, "CSWrap", "cs"));
                var    exeOutFilename = provider.UniquePath(null, "CSWrap", "exe");

                CodeWriter.WriteToFile(csOutFilename, csfile);

                Compiler.CSCompile(provider.DirectoryPath, Directory.GetFiles(provider.DirectoryPath, "*.cs"), exeOutFilename);

                TestRunning.CopyTestReferencesTo(provider.DirectoryPath);

                string output = Compiler.RunWithMono(exeOutFilename, provider.DirectoryPath);
                Assert.AreEqual("nothing\n", output);
            }
        }
        static void PropertyTestCore(string simpleClass)
        {
            using (TempDirectoryFilenameProvider provider = new TempDirectoryFilenameProvider()) {
                Utils.CompileSwift(simpleClass, provider);

                string          libFileName = Path.Combine(provider.DirectoryPath, "libXython.dylib");
                var             errors      = new ErrorHandling();
                ModuleInventory inventory   = ModuleInventory.FromFile(libFileName, errors);
                Utils.CheckErrors(errors);

                Utils.CompileToCSharp(provider);
            }
        }
示例#4
0
        List <ClassDeclaration> ReflectClassDeclarations(string code)
        {
            using (TempDirectoryFilenameProvider fileProvider = new TempDirectoryFilenameProvider(null, false)) {
                CustomSwiftCompiler  compiler = Utils.DefaultSwiftCompiler(fileProvider);
                SwiftCompilerOptions options  = new SwiftCompilerOptions("NameNotImportant", null, null, null);
                compiler.CompileString(options, code);

                List <ModuleDeclaration> modules = compiler.ReflectToModules(new string [] { compiler.DirectoryPath },
                                                                             new string [] { compiler.DirectoryPath }, "", "NameNotImportant");
                Assert.AreEqual(1, modules.Count);
                return(modules [0].AllClasses);
            }
        }
        string RunBindingToolsForSwift(string swiftCode, string testName = null)
        {
            var nameSpace = "OutputTests";

            using (TempDirectoryFilenameProvider provider = new TempDirectoryFilenameProvider()) {
                var compiler = Utils.CompileSwift(swiftCode, provider, nameSpace);

                var libName = $"lib{nameSpace}.dylib";

                using (DisposableTempDirectory temp = new DisposableTempDirectory()) {
                    File.Copy(Path.Combine(compiler.DirectoryPath, libName), Path.Combine(temp.DirectoryPath, libName));

                    return(Utils.CompileToCSharp(provider, temp.DirectoryPath, nameSpace, separateProcess: true));
                }
            }
        }
        public ObjCProtocolCompiler(PlatformName targetPlatform, IEnumerable <ProtocolDeclaration> protocols, TempDirectoryFilenameProvider provider,
                                    ModuleDeclaration module, string swiftLibPath, string outputDirectory, TypeMapper typeMapper,
                                    WrappingResult wrapper, ErrorHandling errors, bool verbose)
        {
            this.targetPlatform  = targetPlatform;
            this.protocols       = protocols.Where(p => p.IsObjC && p.IsPublicOrOpen && !p.IsUnavailable && !p.IsDeprecated).ToList();
            this.provider        = provider;
            this.module          = module;
            this.swiftLibPath    = swiftLibPath;
            this.outputDirectory = outputDirectory;
            this.typeMapper      = typeMapper;
            this.wrapper         = wrapper;
            this.errors          = errors;
            this.verbose         = verbose;

            topLevelFunctionCompiler = new TopLevelFunctionCompiler(this.typeMapper);
        }