public static void TestAndExecuteNoDevice(string swiftCode, CodeElementCollection <ICodeElement> callingCode, string expectedOutput, string testName = null, PlatformName platform = PlatformName.None, UnicodeMapper unicodeMapper = null) { SetInvokingTestNameIfUnset(ref testName, out string nameSpace); 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); CSFile csFile = TestRunningCodeGenerator.GenerateTestEntry(callingCode, testName, nameSpace, platform); csFile.Namespaces.Add(CreateManagedConsoleRedirect()); CodeWriter.WriteToFile(Path.Combine(tempDirectoryPath, "NameNotImportant.cs"), csFile); var sourceFiles = Directory.GetFiles(tempDirectoryPath, "*.cs"); Compiler.CSCompile(tempDirectoryPath, sourceFiles, "NameNotImportant.exe", platform: platform); CopyTestReferencesTo(tempDirectoryPath, platform); var output = Execute(tempDirectoryPath, "NameNotImportant.exe", platform); Assert.AreEqual(expectedOutput, output); } }
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}"); } } } }