Example #1
0
        public void TestSizeof(string typeName, string expected)
        {
            using (DisposableTempDirectory temp = new DisposableTempDirectory(null, true)) {
                string csFile = Path.Combine(temp.DirectoryPath, temp.UniqueName("CS", "", "cs"));
                string source = $@"using System;
using SwiftRuntimeLibrary;

using SwiftRuntimeLibrary.SwiftMarshal;
namespace dlopentest
{{
	class MainClass
	{{
		public static void Main (string[] args)
		{{
			var size = StructMarshal.Marshaler.Sizeof(typeof({typeName}));
			Console.WriteLine(size);
		}}
	}}
}}";
                source += TestRunning.GetManagedConsoleRedirectCode();
                File.WriteAllText(csFile, source);

                Compiler.CSCompile(temp.DirectoryPath, new string [] { csFile }, "TestIt.exe", $"-lib:{Compiler.CompilerLocation.SwiftCompilerLib}");
                TestRunning.CopyTestReferencesTo(temp.DirectoryPath);

                string output = Compiler.RunWithMono(Path.Combine(temp.DirectoryPath, "TestIt.exe"), temp.DirectoryPath);
                Assert.AreEqual(expected, output);
            }
        }
Example #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);
            }
        }
Example #3
0
        void CheckName(string typeName, string expected)
        {
            using (DisposableTempDirectory temp = new DisposableTempDirectory(null, true)) {
                string csFile = Path.Combine(temp.DirectoryPath, temp.UniqueName("CS", "", "cs"));
                string source = $@"using System;
using SwiftRuntimeLibrary;

using SwiftRuntimeLibrary.SwiftMarshal;

namespace dlopentest
{{
	class MainClass
	{{
		public static void Main (string[] args)
		{{
			SwiftNominalTypeDescriptor nt = StructMarshal.Marshaler.Metatypeof(typeof({typeName})).GetNominalTypeDescriptor();
			Console.WriteLine(nt.GetFullName());
		}}
	}}
}}";
                source += TestRunning.GetManagedConsoleRedirectCode();
                File.WriteAllText(csFile, source);
                Compiler.CSCompile(temp.DirectoryPath, new string [] { csFile }, "TestIt.exe", $"-lib:{Compiler.CompilerLocation.SwiftCompilerLib}", PlatformName.macOS);
                TestRunning.CopyTestReferencesTo(temp.DirectoryPath);

                string output = Compiler.RunWithMono(Path.Combine(temp.DirectoryPath, "TestIt.exe"), temp.DirectoryPath, platform: PlatformName.macOS);
                Assert.AreEqual(expected, output);

                string tsource = $@"using System;
using NewClassCompilerTests;
using SwiftRuntimeLibrary;
using TomTest;
using SwiftRuntimeLibrary.SwiftMarshal;

namespace MetatypeTests
{{
	public class CheckName{typeName} : ITomTest
	{{
		public void Run()
		{{
			SwiftNominalTypeDescriptor nt = StructMarshal.Marshaler.Metatypeof(typeof({typeName})).GetNominalTypeDescriptor();
			Console.WriteLine(nt.GetMangledName());
		}}

		public string TestName {{ get {{ return ""CheckName{typeName}""; }} }}
		public string ExpectedOutput {{ get {{ return {ToStringLiteral (expected)}; }} }}
	}}
}}";

                string thisTestPath = Path.Combine(Compiler.kSwiftDeviceTestRoot, "MetatypeTests");

                Directory.CreateDirectory(thisTestPath);
                string tpath = Path.Combine(thisTestPath, $"CheckName{typeName}.cs");
                File.WriteAllText(tpath, tsource);
            }
        }