public void WrapVirtClassNonVirtProp() { string swiftCode = "open class VirtClassNVP {\n" + " public init() { }\n" + " public var x:Int = 17" + "}\n"; var decl = CSVariableDeclaration.VarLine(new CSSimpleType("VirtClassNVP"), "cl", new CSFunctionCall("VirtClassNVP", true)); var printer = CSFunctionCall.ConsoleWriteLine((CSIdentifier)"cl.X"); var callingCode = CSCodeBlock.Create(decl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "17\n"); }
public void BadArgumentName() { string swiftCode = "open class AEXMLElement {\n" + " open func allDescendants (where predicate: @escaping (AEXMLElement) -> Bool) -> [AEXMLElement] {\n" + " return []\n" + " }\n" + "}\n"; var printer = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("ok")); var callingCode = CSCodeBlock.Create(printer); TestRunning.TestAndExecute(swiftCode, callingCode, "ok\n", platform: PlatformName.macOS); }
public void BasicTest(PlatformName platform) { var swiftCode = @" public func dateIsNotUsed () { } "; var clID = new CSIdentifier("date"); var clDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, clID, new CSFunctionCall("SwiftDate.SwiftDate_TimeIntervalSince1970", false, CSConstant.Val(0.0))); var printer = CSFunctionCall.ConsoleWriteLine(new CSFunctionCall($"{clID.Name}.ToNSDate", false)); var callingCode = CSCodeBlock.Create(clDecl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "1970-01-01 00:00:00 +0000\n", testName: $"BasicTest{platform}", platform: platform); }
void TupleProp(string type1, string type2, string cstype1, string cstype2, string val1, string val2, string expected) { string appendage = type1 + type2.Replace('(', '_').Replace(')', '_').Replace(',', '_').Replace(' ', '_'); string swiftCode = $"public final class MontyTupleProp{appendage} {{ public init() {{ }}\n public var prop:({type1}, {type2}) {{\n return ({val1}, {val2});\n }}\n}}\n"; CSLine cstupe = CSVariableDeclaration.VarLine(new CSSimpleType("Tuple", false, new CSSimpleType(cstype1), new CSSimpleType(cstype2)), "tupe", new CSFunctionCall($"MontyTupleProp{appendage}", true).Dot(new CSIdentifier("Prop"))); CSLine printer = CSFunctionCall.ConsoleWriteLine((CSIdentifier)"tupe"); CSCodeBlock callingCode = CSCodeBlock.Create(cstupe, printer); TestRunning.TestAndExecute(swiftCode, callingCode, expected, testName: $"TupleProp{appendage}"); }
public void ObjCFunctionSmokeTest() { var swiftCode = "import Foundation\n" + "@objc\n" + "public protocol SmokeFunc0 {\n" + " func intOnBool (a: Int) -> Bool\n" + "}\n"; var caller = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("did it")); var callingCode = CSCodeBlock.Create(caller); TestRunning.TestAndExecute(swiftCode, callingCode, "did it\n", platform: PlatformName.macOS); }
public void ObjCSubscriptSmokeTest() { var swiftCode = "import Foundation\n" + "@objc\n" + "public protocol SmokeSub0 {\n" + " @objc subscript (index:Int) -> Bool { get set }\n" + "}\n"; var caller = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("did it")); var callingCode = CSCodeBlock.Create(caller); TestRunning.TestAndExecute(swiftCode, callingCode, "did it\n", platform: PlatformName.macOS); }
public void SomeProtocolAssocSubscriptGet () { var swiftCode = @" public protocol Iterator4 { associatedtype Elem subscript (index: Int) -> Elem { get } } "; var printer = CSFunctionCall.ConsoleWriteLine (CSConstant.Val ("OK")); var callingCode = CSCodeBlock.Create (printer); TestRunning.TestAndExecute (swiftCode, callingCode, "OK\n", platform: PlatformName.macOS); }
public void HasCGFloat(PlatformName platform) { var swiftCode = @" import Foundation import CoreGraphics public func returnsFloat() -> CGFloat { return 42.5 } "; var printer = CSFunctionCall.ConsoleWriteLine(new CSFunctionCall("TopLevelEntities.ReturnsFloat", false)); var callingCode = CSCodeBlock.Create(printer); TestRunning.TestAndExecute(swiftCode, callingCode, "42.5\n", platform: platform); }
public void TopLevelFunctionHomonym() { var swiftCode = "public func same(a:Int) -> Int { return a }\n" + "public func same(b:Int) -> Int { return b }\n" + "public func same(b:Int) -> Bool { return true }\n"; var call = new CSFunctionCall("TopLevelEntities.Same_a_nint", false, CSConstant.Val(14)); var call1 = new CSFunctionCall("TopLevelEntities.Same_b_bool", false, CSConstant.Val(14)); CSLine printer = CSFunctionCall.ConsoleWriteLine(call); CSLine printer1 = CSFunctionCall.ConsoleWriteLine(call1); CSCodeBlock callingCode = CSCodeBlock.Create(printer, printer1); TestRunning.TestAndExecute(swiftCode, callingCode, "14\nTrue\n"); }
public void OptionalInitFailTest() { string swiftCode = "open class OptionalInitFail {\n" + " public init? (b: Bool) {\n" + " if !b { return nil; }\n" + " }\n" + "}\n"; var decl = CSVariableDeclaration.VarLine(new CSSimpleType("SwiftOptional<OptionalInitFail>"), "opt", new CSFunctionCall("OptionalInitFail.OptionalInitFailOptional", false, CSConstant.Val(false))); var printer = CSFunctionCall.ConsoleWriteLine(new CSIdentifier("opt.HasValue")); var callingCode = CSCodeBlock.Create(decl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "False\n"); }
void Wrap2Tuple(string type1, string type2, string cstype1, string cstype2, string val1, string val2, string expected) { string typetype = type1 + type2; string swiftCode = $"public final class MontyW2T{typetype} {{ public init() {{ }}\n public static func tupe(a:{type1}, b: {type2}) -> ({type1}, {type2})\n {{\n return (a, b);\n }}\n}}\n"; CSLine cstupe = CSVariableDeclaration.VarLine(new CSSimpleType("Tuple", false, new CSSimpleType(cstype1), new CSSimpleType(cstype2)), "tupe", new CSFunctionCall($"MontyW2T{typetype}.Tupe", false, new CSIdentifier(val1), new CSIdentifier(val2))); CSLine printer = CSFunctionCall.ConsoleWriteLine((CSIdentifier)"tupe"); CSCodeBlock callingCode = CSCodeBlock.Create(cstupe, printer); TestRunning.TestAndExecute(swiftCode, callingCode, expected, testName: $"Wrap2Tuple{typetype}"); }
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 OperatorSmokeTest2() { var swiftCode = "postfix operator -*\n" + "public postfix func -* (arg: Int) -> Int {\n" + " return -1 * arg\n" + "}\n"; var printIt = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("TopLevelEntities.PostfixOperatorMinusStar", CSConstant.Val(88))); var callingCode = new CodeElementCollection <ICodeElement> { printIt }; TestRunning.TestAndExecute(swiftCode, callingCode, "-88\n"); }
public void TestAllocCount(string tag) { var swiftCode = $"public func neverUsed{tag}() {{\n}}\n"; var ptrID = new CSIdentifier("ptr"); var ptrDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, ptrID, new CSFunctionCall("System.Runtime.InteropServices.Marshal.AllocHGlobal", false, CSConstant.Val(128))); var clID = new CSIdentifier("ubp"); var clDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, clID, new CSFunctionCall(tag, true, ptrID, CSConstant.Val(128))); var printer = CSFunctionCall.ConsoleWriteLine(clID.Dot(new CSIdentifier("Count"))); var cleanUp = CSFunctionCall.FunctionCallLine("System.Runtime.InteropServices.Marshal.FreeHGlobal", false, ptrID); var callingCode = CSCodeBlock.Create(ptrDecl, clDecl, printer, cleanUp); TestRunning.TestAndExecute(swiftCode, callingCode, "128\n", testName: "TestAllocCount" + tag); }
public void SubscriptMarshaling() { string swiftCode = "open class AEXMLElement2 {\n" + " public init (name:String) {\n" + " }\n" + " open subscript (key: String) -> AEXMLElement2 {\n" + " return AEXMLElement2 (name: \"\")\n" + " }\n" + "}\n"; var printer = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("ok")); var callingCode = CSCodeBlock.Create(printer); TestRunning.TestAndExecute(swiftCode, callingCode, "ok\n", platform: PlatformName.macOS); }
public void OperatorSmokeTest1() { var swiftCode = "prefix operator *-\n" + "public prefix func *- (arg: Int) -> Int {\n" + " return -1 * arg\n" + "}\n"; var printIt = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("TopLevelEntities.PrefixOperatorStarMinus", CSConstant.Val(47))); var callingCode = new CodeElementCollection <ICodeElement> { printIt }; TestRunning.TestAndExecute(swiftCode, callingCode, "-47\n"); }
public void RequiredInitTest() { string swiftCode = "open class BaseWithReq {\n" + " public var x: String\n" + " public required init (s: String) {\n" + " x = s\n" + " }\n" + "}\n"; var decl = CSVariableDeclaration.VarLine(new CSSimpleType("BaseWithReq"), "bs", new CSFunctionCall("BaseWithReq", true, new CSFunctionCall("SwiftString.FromString", false, CSConstant.Val("got it")))); var printer = CSFunctionCall.ConsoleWriteLine(new CSIdentifier("bs").Dot(new CSIdentifier("X"))); var callingCode = CSCodeBlock.Create(decl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "got it\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 TestConstructorClassHomonym() { var swiftCode = "public class FooClassCtor {\n" + " public var x:Int\n" + " public init(a:Int) { x = a\n }\n" + " public init(b:Int) { x = b + b\n }\n" + "}\n"; var call = new CSFunctionCall("FooClassCtor.FooClassCtor_a", false, CSConstant.Val(14)); var call1 = new CSFunctionCall("FooClassCtor.FooClassCtor_b", false, CSConstant.Val(14)); CSLine printer = CSFunctionCall.ConsoleWriteLine(call.Dot(new CSIdentifier("X"))); CSLine printer1 = CSFunctionCall.ConsoleWriteLine(call1.Dot(new CSIdentifier("X"))); CSCodeBlock callingCode = CSCodeBlock.Create(printer, printer1); TestRunning.TestAndExecute(swiftCode, callingCode, "14\n28\n"); }
public void OperatorSmokeTest3() { var swiftCode = "postfix operator ^^\n" + "public postfix func ^^ (arg: String) -> String {\n" + " return \"//\" + arg + \"//\"\n" + "}\n"; var printIt = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("TopLevelEntities.PostfixOperatorHatHat", CSFunctionCall.Function("SwiftString.FromString", CSConstant.Val("nothing")))); var callingCode = new CodeElementCollection <ICodeElement> { printIt }; TestRunning.TestAndExecute(swiftCode, callingCode, "//nothing//\n"); }
public void OperatorSmokeTest0() { var swiftCode = "import Darwin\n" + "infix operator ** : MultiplicationPrecedence\n" + "public func ** (left: Double, right: Double) -> Double {\n" + " return pow(left, right)\n" + "}\n"; var printIt = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("TopLevelEntities.InfixOperatorStarStar", CSConstant.Val(2.0), CSConstant.Val(8.0))); var callingCode = new CodeElementCollection <ICodeElement> { printIt }; TestRunning.TestAndExecute(swiftCode, callingCode, "256\n"); }
public void TestReturnsAny() { var swiftCode = @" public func returnsAny () -> Any { return 7; } "; var any = new CSIdentifier("any"); var anyDecl = CSVariableDeclaration.VarLine(any, new CSFunctionCall("TopLevelEntities.ReturnsAny", false)); var unbox = new CSFunctionCall("SwiftExistentialContainer0.Unbox<nint>", false, any); var printer = CSFunctionCall.ConsoleWriteLine(unbox); var callingCode = CSCodeBlock.Create(anyDecl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "7\n"); }
public void TestProtocolTypeAttribute() { var swiftCode = @" public protocol Useless { func doNothing () } "; // this will throw on fail // SwiftProtocolTypeAttribute.DescriptorForType (typeof (Useless)); var getter = CSFunctionCall.FunctionCallLine("SwiftProtocolTypeAttribute.DescriptorForType", false, new CSSimpleType("IUseless").Typeof()); var printer = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("OK")); var callingCode = CSCodeBlock.Create(getter, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "OK\n", platform: PlatformName.macOS); }
public void CGFloatIdentity(PlatformName platform) { var swiftCode = @" import Foundation import CoreGraphics public func identityCrisis (f: CGFloat) -> CGFloat { return f } "; var printer = CSFunctionCall.ConsoleWriteLine(new CSFunctionCall("TopLevelEntities.IdentityCrisis", false, new CSCastExpression(new CSSimpleType("nfloat"), CSConstant.Val(42.5)))); var callingCode = CSCodeBlock.Create(printer); TestRunning.TestAndExecute(swiftCode, callingCode, "42.5\n", platform: platform); }
public void WrapVirtClassNonVirtMethod() { string swiftCode = "open class VirtClassNVM {\n" + " public init() { }\n" + " public func returns17() -> Int {\n" + " return 17" + " }\n" + "}\n"; var decl = CSVariableDeclaration.VarLine(new CSSimpleType("VirtClassNVM"), "cl", new CSFunctionCall("VirtClassNVM", true)); var printer = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("cl.Returns17")); var callingCode = CSCodeBlock.Create(decl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "17\n"); }
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 HandlesSimpleClosureWithNoArgs() { var swiftCode = @" public func simpleReturnClosure () -> (Int32)->Bool { return { (a:Int32) in return a % 2 == 0 } } "; var closID = new CSIdentifier("cl"); var closDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, closID, new CSFunctionCall("TopLevelEntities.SimpleReturnClosure", false)); var printer = CSFunctionCall.ConsoleWriteLine(new CSFunctionCall(closID.Name, false, CSConstant.Val(42))); var callingCode = CSCodeBlock.Create(closDecl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "True\n"); }
public void ClosureSmokeTest() { string swiftCode = "public func callClosureCST(f:@escaping()->()) {\n" + " f()\n" + "}"; CodeElementCollection <ICodeElement> callingCode = new CodeElementCollection <ICodeElement> (); CSCodeBlock body = new CSCodeBlock(); body.Add(CSFunctionCall.ConsoleWriteLine(CSConstant.Val("C# output"))); CSLine invoker = CSFunctionCall.FunctionCallLine("TopLevelEntities.CallClosureCST", false, new CSLambda(new CSParameterList(), body)); callingCode.Add(invoker); TestRunning.TestAndExecute(swiftCode, callingCode, "C# output\n"); }
public void TestGenericConstructorClassHomonym() { var swiftCode = "public class FooGeneric<T> {\n" + " public var x:Int\n" + " public var t:T\n" + " public init(a:Int, c:T) { x = a\n t = c\n}\n" + " public init(b:Int, c:T) { x = b + b\n t = c\n}\n" + "}\n"; var call = new CSFunctionCall("FooGeneric<bool>.FooGeneric_a_c", false, CSConstant.Val(14), CSConstant.Val(true)); var call1 = new CSFunctionCall("FooGeneric<bool>.FooGeneric_b_c", false, CSConstant.Val(14), CSConstant.Val(true)); CSLine printer = CSFunctionCall.ConsoleWriteLine(call.Dot(new CSIdentifier("X"))); CSLine printer1 = CSFunctionCall.ConsoleWriteLine(call1.Dot(new CSIdentifier("X"))); CSCodeBlock callingCode = CSCodeBlock.Create(printer, printer1); TestRunning.TestAndExecute(swiftCode, callingCode, "14\n28\n"); }