public void TestCharacterConstructors() { var swiftCode = @"public func Echo (c: Character) -> Character { return c; }"; var callingCode = new CodeElementCollection <ICodeElement> (); StringBuilder expected = new StringBuilder(); foreach (string c in TestCases) { CSIdentifier testIdentifier = (CSIdentifier)$@"""{c}"""; var ctorCall = CSFunctionCall.Ctor("SwiftCharacter", testIdentifier); var fromCall = CSFunctionCall.Function("SwiftCharacter.FromCharacter", testIdentifier); var implicitCall = new CSCastExpression((CSSimpleType)"SwiftCharacter", testIdentifier); foreach (var call in new CSBaseExpression [] { ctorCall, fromCall, implicitCall }) { CSLine print = CSFunctionCall.FunctionLine("Console.Write", CSFunctionCall.Function("TopLevelEntities.Echo", call)); callingCode.Add(print); expected.Append(c); } } TestRunning.TestAndExecute(swiftCode, callingCode, expected.ToString()); }
void WrapMultipleMethod(string type, string return1, string return2, string expected) { string swiftCode = String.Format("public final class MontyWMM{3} {{ public init() {{ }}\n public func val() -> {0} {{ return {1}; }}; public func val1() -> {0} {{ return {2}; }}}}", type, return1, return2, type); CodeElementCollection <ICodeElement> callingCode = new CodeElementCollection <ICodeElement> (); callingCode.Add(CSFunctionCall.ConsoleWriteLine(CSConstant.Val("{0} {1}"), CSFunctionCall.Ctor($"MontyWMM{type}").Dot(CSFunctionCall.Function("Val")), CSFunctionCall.Ctor($"MontyWMM{type}").Dot(CSFunctionCall.Function("Val1")))); TestRunning.TestAndExecute(swiftCode, callingCode, expected, testName: $"WrapMultiMethod{type}"); }
void WrapSingleMethod(string type, string returnVal, string expected) { string swiftCode = String.Format("public final class MontyWSM{2} {{ class InnerMonty {{\n}}\npublic init() {{}}\n public func val() -> {0} {{ return {1}; }} }}", type, returnVal, type); CodeElementCollection <ICodeElement> callingCode = new CodeElementCollection <ICodeElement> (); callingCode.Add(CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Ctor($"MontyWSM{type}").Dot(CSFunctionCall.Function("Val")))); TestRunning.TestAndExecute(swiftCode, callingCode, expected, testName: $"WrapSingleMethod{type}"); }
void WrapSingleProperty(string type, string returnVal, string reassignVal, string expected) { string swiftCode = String.Format("public final class MontyWSP{2} {{ public init() {{ }}\npublic var val:{0} = {1}; }}", type, returnVal, type); CodeElementCollection <ICodeElement> callingCode = new CodeElementCollection <ICodeElement> { CSAssignment.Assign("var nothing", CSFunctionCall.Ctor($"MontyWSP{type}")), CSFunctionCall.ConsoleWriteLine(CSIdentifier.Create("nothing").Dot((CSIdentifier)"Val")), CSAssignment.Assign("nothing.Val", new CSConstant(reassignVal)), CSFunctionCall.ConsoleWriteLine(CSIdentifier.Create("nothing").Dot((CSIdentifier)"Val")) }; TestRunning.TestAndExecute(swiftCode, callingCode, expected, testName: $"WrapSingleProperty{type}"); }
public void UnicodeInClassNameAndProperty() { string swiftCode = "public class cd1\x1800 {\n" + " public init() { }\n" + " public var v\x3004:Int = 3\n" + "}\n"; var callingCode = new CodeElementCollection <ICodeElement> { CSVariableDeclaration.VarLine((CSSimpleType)"Cd1U1800", "cl", CSFunctionCall.Ctor("Cd1U1800")), CSFunctionCall.ConsoleWriteLine((CSIdentifier)"cl.VU3004") }; TestRunning.TestAndExecute(swiftCode, callingCode, "3\n"); }
public void TestMissingNSObject(PlatformName platform) { string swiftCode = "import Foundation\n" + "public final class MissingNSObject {\n" + " public init() { }\n" + " public func val() -> Int { return 5; }\n" + " public func skip(a:NSObject) { }\n" + "}"; CodeElementCollection <ICodeElement> callingCode = new CodeElementCollection <ICodeElement> (); callingCode.Add(CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Ctor("MissingNSObject").Dot(CSFunctionCall.Function("Val")))); TestRunning.TestAndExecute(swiftCode, callingCode, "5\n", platform: platform); }
public void UnicodeInClassNameAndMethod() { string swiftCode = "public class cc1\x1800 {\n" + " public init() { }\n" + " public func doIt\x3004() -> Int {\n" + " return 3\n" + " }\n" + "}\n"; var callingCode = new CodeElementCollection <ICodeElement> { CSVariableDeclaration.VarLine((CSSimpleType)"Cc1U1800", "cl", CSFunctionCall.Ctor("Cc1U1800")), CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("cl.DoItU3004")) }; TestRunning.TestAndExecute(swiftCode, callingCode, "3\n"); }
public void UnicodeInStructName() { string swiftCode = "public struct s\x1800 {\n" + " public init() { }\n" + " public func doIt() -> Int {\n" + " return 3\n" + " }\n" + "}\n"; var callingCode = new CodeElementCollection <ICodeElement> { CSVariableDeclaration.VarLine((CSSimpleType)"SU1800", "st", CSFunctionCall.Ctor("SU1800")), CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("st.DoIt")) }; TestRunning.TestAndExecute(swiftCode, callingCode, "3\n"); }
public void NSObjectObjCMethodTest1() { string swiftCode = "import Foundation\n" + "@objc\n" + "open class ItsLessSimple : NSObject {\n" + " public override init () { }\n" + " @objc open func returns15() -> Int {\n" + " return 15\n" + " }\n" + "}\n"; var objID = new CSIdentifier("obj"); var objDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, objID, CSFunctionCall.Ctor("ItsLessSimple")); var printer = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function(objID.Name + ".Returns15")); var callingCode = CSCodeBlock.Create(objDecl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "15\n", platform: PlatformName.macOS); }
public void ExtensionOnClassTest() { var swiftCode = "public class Hamburger {\n" + " public init () { }\n" + " public func AddOnions () {}\n" + "}\n" + "public extension Hamburger {" + " public func Price () -> Double {\n" + " return 2.99\n" + " }\n" + "}\n"; var myDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, "burg", CSFunctionCall.Ctor("Hamburger")); var printer = CSFunctionCall.ConsoleWriteLine(new CSFunctionCall("burg.Price", false)); var callingCode = CSCodeBlock.Create(myDecl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "2.99\n"); }
public void ExtensionOnUserTypeTest() { var swiftCode = @"public class HotDogOnUserType { public init () { } } public extension HotDogOnUserType { public func Price () -> Double { return 2.99 } }"; var myDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, "dawg", CSFunctionCall.Ctor("HotDogOnUserType")); var printer = CSFunctionCall.ConsoleWriteLine(new CSFunctionCall("dawg.Price", false)); var callingCode = CSCodeBlock.Create(myDecl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "2.99\n"); }
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 void TestCharacterCreation() { var swiftCode = @" public class CharacterHolder { public var C: Character; public init (c: Character) {C = c } }"; var callingCode = new CodeElementCollection <ICodeElement> (); StringBuilder expected = new StringBuilder(); foreach (string c in TestCases) { CSIdentifier testIdentifier = (CSIdentifier)$@"""{c}"""; Action <ICSExpression> testCharacter = creation => { CSCodeBlock block = new CSCodeBlock(); block.Add(CSVariableDeclaration.VarLine((CSSimpleType)"SwiftCharacter", (CSIdentifier)"Char", creation)); block.Add(CSVariableDeclaration.VarLine((CSSimpleType)"CharacterHolder", (CSIdentifier)"Foo", CSFunctionCall.Ctor("CharacterHolder", (CSIdentifier)"Char"))); block.Add(CSFunctionCall.FunctionLine("Console.Write", (CSIdentifier)"(string)Foo.C")); expected.Append(c); callingCode.Add(block); }; testCharacter(CSFunctionCall.Function("SwiftCharacter.FromCharacter", (testIdentifier))); testCharacter(new CSCastExpression((CSSimpleType)"SwiftCharacter", testIdentifier)); } TestRunning.TestAndExecute(swiftCode, callingCode, expected.ToString()); }
public void TestCharacterReturns() { var swiftCode = @" public class CharacterEcho { var C : Character; public init (c : Character) { C = c } public func GetValue () -> Character { return C } public var Value : Character { get { return C; } } }"; var callingCode = new CodeElementCollection <ICodeElement> (); StringBuilder expected = new StringBuilder(); foreach (string c in TestCases) { CSIdentifier testIdentifier = (CSIdentifier)$@"""{c}"""; CSCodeBlock block = new CSCodeBlock(); var ctorParam = new CSCastExpression((CSSimpleType)"SwiftCharacter", testIdentifier); CSLine instance = CSVariableDeclaration.VarLine((CSSimpleType)"CharacterEcho", (CSIdentifier)"Foo", CSFunctionCall.Ctor("CharacterEcho", ctorParam)); // First the properties var explicitCastProp = (CSIdentifier)"(string)Foo.Value"; var toStringProp = (CSIdentifier)"Foo.Value.ToString ()"; // Then the function returns as well var explicitCastFun = (CSIdentifier)"(string)Foo.GetValue ()"; var toStringFun = (CSIdentifier)"Foo.GetValue ().ToString ()"; block.Add(instance); foreach (var call in new CSBaseExpression [] { explicitCastProp, toStringProp, explicitCastFun, toStringFun }) { CSLine print = CSFunctionCall.FunctionLine("Console.Write", call); block.Add(print); expected.Append(c); } callingCode.Add(block); } TestRunning.TestAndExecute(swiftCode, callingCode, expected.ToString()); }
public void PropertyWithPrivateSet() { string swiftCode = $@"public class Node {{ public init() {{ }} public init(parent: Node) {{ Parent = parent }} open private(set) weak var Parent: Node? }}"; var parentDecl = CSVariableDeclaration.VarLine((CSSimpleType)"Node", "parent", CSFunctionCall.Ctor("Node")); var childDecl = CSVariableDeclaration.VarLine((CSSimpleType)"Node", "child", CSFunctionCall.Ctor("Node", (CSIdentifier)"parent")); var callingCode = CSCodeBlock.Create(parentDecl, childDecl); TestRunning.TestAndExecute(swiftCode, callingCode, ""); }
public void IgnoresPrivateClassProperty() { var swiftCode = @"public class Pluralize { public init() { } class var hidden: Int { return 3; } public func nothing() { } } "; var pluralDecl = CSVariableDeclaration.VarLine((CSSimpleType)"Pluralize", "plural", CSFunctionCall.Ctor("Pluralize")); var noop = CSFunctionCall.FunctionLine("plural.Nothing"); var output = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("Success")); var callingCode = CSCodeBlock.Create(pluralDecl, noop, output); TestRunning.TestAndExecute(swiftCode, callingCode, "Success\n"); }