public void TestForFinality() { string simpleClass = "public class Garble { public final func finalmethod() { }; public func virtmethod() { } }"; using (DisposableTempFile montyLib = new DisposableTempFile("libXython.dylib", false)) { Compiler.CompileStringToFileUsing(null, XCodeCompiler.SwiftcCustom, simpleClass, " -emit-library -module-name Xython", montyLib); var errors = new ErrorHandling(); ModuleInventory inventory = ModuleInventory.FromFile(montyLib.Filename, errors); Utils.CheckErrors(errors); ClassContents cl = inventory.ClassesForName(new SwiftName("Xython", false)).FirstOrDefault(); Assert.IsNotNull(cl); if (cl.WitnessTable == null || cl.WitnessTable.MangledNames == null || cl.WitnessTable.MangledNames.Count() == 0) { return; } foreach (var oi in cl.Methods.Values) { foreach (TLFunction f in oi.Functions) { if (f.MangledName.Contains("finalmethod")) { Assert.IsTrue(cl.IsFinal(f)); } else if (f.MangledName.Contains("virtmethod")) { Assert.IsFalse(cl.IsFinal(f)); } } } } }
void FindsProperty(string code, Func <ClassDeclaration, bool> classFinder, Func <PropertyDeclaration, bool> propFinder) { CustomSwiftCompiler compiler = Utils.CompileSwift(code, moduleName: "CanFind"); var errors = new ErrorHandling(); ModuleInventory mi = ModuleInventory.FromFile(Path.Combine(compiler.DirectoryPath, "libCanFind.dylib"), errors); ModuleDeclaration mod = compiler.ReflectToModules(new string [] { compiler.DirectoryPath }, new string [] { compiler.DirectoryPath }, null, "CanFind") [0]; ClassDeclaration classDecl = mod.AllClasses.FirstOrDefault(classFinder); Assert.IsNotNull(classDecl, "null class"); PropertyDeclaration propDecl = classDecl.Members.OfType <PropertyDeclaration> ().FirstOrDefault(propFinder); Assert.IsNotNull(propDecl, "null property"); FunctionDeclaration getter = propDecl.GetGetter(); Assert.IsNotNull(getter, "null getter"); FunctionDeclaration setter = propDecl.GetSetter(); Assert.IsNotNull(setter, "null setter"); TLFunction tlgetter = XmlToTLFunctionMapper.ToTLFunction(getter, mi); Assert.IsNotNull(tlgetter, "null tlgetter"); TLFunction tlsetter = XmlToTLFunctionMapper.ToTLFunction(setter, mi); Assert.IsNotNull(tlsetter, "null tlsetter"); }
void CanFindThing(string code, Func <ClassDeclaration, bool> classFinder, Func <FunctionDeclaration, bool> funcFinder, Func <TLFunction, bool> tlVerifier) { CustomSwiftCompiler compiler = Utils.CompileSwift(code, moduleName: "CanFind"); var errors = new ErrorHandling(); ModuleInventory mi = ModuleInventory.FromFile(Path.Combine(compiler.DirectoryPath, "libCanFind.dylib"), errors); ModuleDeclaration mod = compiler.ReflectToModules(new string [] { compiler.DirectoryPath }, new string [] { compiler.DirectoryPath }, null, "CanFind") [0]; ClassDeclaration classDecl = mod.AllClasses.FirstOrDefault(classFinder); Assert.IsNotNull(classDecl, "nominal type not found"); FunctionDeclaration funcDecl = classDecl.AllMethodsNoCDTor().FirstOrDefault(funcFinder); Assert.IsNotNull(funcDecl, "func decl not found"); // see the note in the implementation of CanFindThing above TLFunction func = XmlToTLFunctionMapper.ToTLFunction(funcDecl, mi, null); Assert.IsNotNull(func, "TLFunction not found"); Assert.IsTrue(tlVerifier(func), "verifier failed"); }
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); } }
void CanFindThing(string code, Func <FunctionDeclaration, bool> funcFinder, Func <TLFunction, bool> tlVerifier) { CustomSwiftCompiler compiler = Utils.CompileSwift(code, moduleName: "CanFind"); var errors = new ErrorHandling(); ModuleInventory mi = ModuleInventory.FromFile(Path.Combine(compiler.DirectoryPath, "libCanFind.dylib"), errors); ModuleDeclaration mod = compiler.ReflectToModules(new string [] { compiler.DirectoryPath }, new string [] { compiler.DirectoryPath }, null, "CanFind") [0]; FunctionDeclaration funcDecl = mod.Functions.FirstOrDefault(funcFinder); Assert.IsNotNull(funcDecl, "no function found"); TLFunction func = XmlToTLFunctionMapper.ToTLFunction(funcDecl, mi); Assert.IsNotNull(func, $"failed to find TLFunction for {funcDecl.Name}"); Assert.IsTrue(tlVerifier(func), "verifier failed"); }
void CanFindThing(string code, Func <FunctionDeclaration, bool> funcFinder, Func <TLFunction, bool> tlVerifier) { CustomSwiftCompiler compiler = Utils.CompileSwift(code, moduleName: "CanFind"); var errors = new ErrorHandling(); ModuleInventory mi = ModuleInventory.FromFile(Path.Combine(compiler.DirectoryPath, "libCanFind.dylib"), errors); ModuleDeclaration mod = compiler.ReflectToModules(new string [] { compiler.DirectoryPath }, new string [] { compiler.DirectoryPath }, null, "CanFind") [0]; FunctionDeclaration funcDecl = mod.Functions.FirstOrDefault(funcFinder); Assert.IsNotNull(funcDecl, "no function found"); // note: if you get an NRE from here then you are testing an argument that includes // an associated type path from a generic. Don't do that. You need much more infrastructure to // do that than you really want here. TLFunction func = XmlToTLFunctionMapper.ToTLFunction(funcDecl, mi, null); Assert.IsNotNull(func, $"failed to find TLFunction for {funcDecl.Name}"); Assert.IsTrue(tlVerifier(func), "verifier failed"); }