public static CSFile GenerateTestEntry(CodeElementCollection <ICodeElement> callingCode, string testName, string nameSpace, PlatformName platform, CSClass otherClass = null) { var use = GetTestEntryPointUsings(nameSpace, platform); var ns = new CSNamespace(nameSpace); if (otherClass != null) { ns.Block.Add(otherClass); } var mainBody = new CSCodeBlock(callingCode); mainBody.Add(CaptureSwiftOutputPostlude(testName)); var main = new CSMethod(CSVisibility.Public, CSMethodKind.Static, CSSimpleType.Void, (CSIdentifier)"Main", new CSParameterList(new CSParameter(CSSimpleType.CreateArray("string"), "args")), mainBody); var mainClass = new CSClass(CSVisibility.Public, "NameNotImportant", new CSMethod [] { main }); AddSupportingCode(mainClass, platform); ns.Block.Add(mainClass); return(CSFile.Create(use, ns)); }
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); } }
public static Stream BasicClass(string nameSpace, string className, CSMethod m, ClassMutator mutator, UsingMutator useMutator = null) { CSUsingPackages use = new CSUsingPackages("System"); if (useMutator != null) { use = useMutator(use); } CSClass cl = new CSClass(CSVisibility.Public, className, m != null ? new CSMethod [] { m } : null); if (mutator != null) { cl = mutator(cl); } CSNamespace ns = new CSNamespace(nameSpace); ns.Block.Add(cl); CSFile file = CSFile.Create(use, ns); return(CodeWriter.WriteToStream(file)); }
public static void TestAndExecute(string swiftCode, CodeElementCollection <ICodeElement> callingCode, string expectedOutput, string testName = null, CSClass otherClass = null, string skipReason = null, string iosExpectedOutput = null, PlatformName platform = PlatformName.None, UnicodeMapper unicodeMapper = null, int expectedErrorCount = 0, Action <string> postCompileCheck = null, string[] expectedOutputContains = null) { SetInvokingTestNameIfUnset(ref testName, out string nameSpace); string testClassName = "TomTest" + testName; using (var provider = new DisposableTempDirectory()) { var compiler = Utils.CompileSwift(swiftCode, provider, nameSpace); var libName = $"lib{nameSpace}.dylib"; var tempDirectoryPath = Path.Combine(provider.DirectoryPath, "BuildDir"); Directory.CreateDirectory(tempDirectoryPath); File.Copy(Path.Combine(compiler.DirectoryPath, libName), Path.Combine(tempDirectoryPath, libName)); Utils.CompileToCSharp(provider, tempDirectoryPath, nameSpace, unicodeMapper: unicodeMapper, expectedErrorCount: expectedErrorCount); if (postCompileCheck != null) { postCompileCheck(tempDirectoryPath); } Tuple <CSNamespace, CSUsingPackages> testClassParts = TestRunningCodeGenerator.CreateTestClass(callingCode, testName, iosExpectedOutput ?? expectedOutput, nameSpace, testClassName, otherClass, skipReason, platform); var thisTestPath = Path.Combine(Compiler.kSwiftDeviceTestRoot, nameSpace); Directory.CreateDirectory(thisTestPath); var thisTestPathSwift = Path.Combine(thisTestPath, "swiftsrc"); Directory.CreateDirectory(thisTestPathSwift); var swiftPrefix = string.Empty; var swiftSuffix = string.Empty; var csPrefix = string.Empty; var csSuffix = string.Empty; var nameSuffix = string.Empty; switch (platform) { case PlatformName.macOS: swiftPrefix = "#if os(OSX)\n"; swiftSuffix = "#endif\n"; csPrefix = "#if __MACOS__\n"; csSuffix = "\n#endif\n"; nameSuffix = "_macOS"; break; case PlatformName.iOS: swiftPrefix = "#if os(iOS)\n"; swiftSuffix = "#endif\n"; csPrefix = "#if __IOS__\n"; csSuffix = "\n#endif\n"; nameSuffix = "_iOS"; break; case PlatformName.tvOS: swiftPrefix = "#if os(tvOS)\n"; swiftSuffix = "#endif\n"; csPrefix = "#if __TVOS__\n"; csSuffix = "\n#endif\n"; nameSuffix = "_tvOS"; break; case PlatformName.watchOS: swiftPrefix = "#if os(watchOS)\n"; swiftSuffix = "#endif\n"; csPrefix = "#if __WATCHOS__\n"; csSuffix = "\n#endif\n"; nameSuffix = "_watchOS"; break; case PlatformName.None: break; default: throw new NotImplementedException(platform.ToString()); } File.WriteAllText(Path.Combine(thisTestPathSwift, $"{testClassName}{testName}{nameSuffix}.swift"), swiftPrefix + swiftCode + swiftSuffix); CSFile csTestFile = CSFile.Create(testClassParts.Item2, testClassParts.Item1); var csTestFilePath = Path.Combine(thisTestPath, $"{testClassName}{testName}{nameSuffix}.cs"); // Write out the file without csPrefix/csSuffix CodeWriter.WriteToFile(csTestFilePath, csTestFile); if (!string.IsNullOrEmpty(csPrefix) || !string.IsNullOrEmpty(csSuffix)) { // Read the C# code, and prepend/append the csPrefix/csSuffix blobs, then save the modified contents again. File.WriteAllText(csTestFilePath, csPrefix + File.ReadAllText(csTestFilePath) + csSuffix); } var csFile = TestRunningCodeGenerator.GenerateTestEntry(callingCode, testName, nameSpace, platform, otherClass); csFile.Namespaces.Add(CreateManagedConsoleRedirect()); CodeWriter.WriteToFile(Path.Combine(tempDirectoryPath, "NameNotImportant.cs"), csFile); var sourceFiles = Directory.GetFiles(tempDirectoryPath, "*.cs"); var objcRuntimePath = Path.Combine(tempDirectoryPath, "ObjCRuntime"); if (Directory.Exists(objcRuntimePath)) { sourceFiles = sourceFiles.And(Directory.GetFiles(objcRuntimePath, "*.cs")); } var compilerWarnings = Compiler.CSCompile(tempDirectoryPath, sourceFiles, "NameNotImportant.exe", platform: platform); if (compilerWarnings.Contains("warning")) { FailOnBadWarnings(compilerWarnings); } CopyTestReferencesTo(tempDirectoryPath, platform); var output = Execute(tempDirectoryPath, "NameNotImportant.exe", platform); if (expectedOutput != null) { Assert.AreEqual(expectedOutput, output); } else { foreach (var s in expectedOutputContains) { Assert.IsTrue(output.Contains(s), $"Expected to find string {s} in {output}"); } } } }