public void ObjCInvokePropInSwift() { var swiftCode = "import Foundation\n" + "@objc\n" + "public protocol HandProp1 {\n" + " @objc var whichHand:Int { get }\n" + "}\n" + "@objc\n" + "internal class HandPropImpl : NSObject, HandProp1 {\n" + " @objc public var whichHand:Int {\n" + " get {\n" + " return 42\n" + " }\n" + " }\n" + "}\n" + "public func makeHandProp1 () -> HandProp1 {\n" + " return HandPropImpl ()\n" + "}\n"; var inst = CSVariableDeclaration.VarLine(CSSimpleType.Var, "lefty", new CSFunctionCall("TopLevelEntities.MakeHandProp1", false)); var caller = CSFunctionCall.ConsoleWriteLine(new CSIdentifier("lefty.WhichHand")); var callingCode = CSCodeBlock.Create(inst, caller); TestRunning.TestAndExecute(swiftCode, callingCode, "42\n", platform: PlatformName.macOS); }
public void ObjCRefProtocolArg() { var swiftCode = "import Foundation\n" + "@objc\n" + "public protocol LifeTheUniverseAnd {\n" + " @objc func Everything () -> Int\n" + "}\n" + "@objc\n" + "internal class Liff : NSObject, LifeTheUniverseAnd {\n" + " private var x: Int\n" + " public init (z: Int) {\n" + " x = z\n" + " }\n" + " @objc func Everything () -> Int {\n" + " return x\n" + " }\n" + "}\n" + "public func makeIt (a: Int) -> LifeTheUniverseAnd {\n" + " return Liff(z: a)\n" + "}\n" + "public func setIt (a:inout LifeTheUniverseAnd) {\n" + " a = Liff(z: 42)\n" + "}\n"; var inst = CSVariableDeclaration.VarLine(CSSimpleType.Var, "liff", new CSFunctionCall("TopLevelEntities.MakeIt", false, CSConstant.Val(17))); var morphIt = CSFunctionCall.FunctionCallLine("TopLevelEntities.SetIt", false, new CSIdentifier("ref liff")); var printIt = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("liff.Everything")); var callingCode = CSCodeBlock.Create(inst, morphIt, printIt); TestRunning.TestAndExecute(swiftCode, callingCode, "42\n", platform: PlatformName.macOS); }
public void ObjCInvokeProtoFunc() { var swiftCode = "import Foundation\n" + "@objc\n" + "public protocol Hand {\n" + " @objc func whichHand()\n" + "}\n" + "public func doWhichHand(a:Hand) {\n" + " a.whichHand()\n" + "}\n"; var altClass = new CSClass(CSVisibility.Public, "MyHandsProtoFunc"); altClass.Inheritance.Add(new CSIdentifier("NSObject")); altClass.Inheritance.Add(new CSIdentifier("IHand")); var printIt = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("lefty")); var whichHandMethod = new CSMethod(CSVisibility.Public, CSMethodKind.None, CSSimpleType.Void, new CSIdentifier("WhichHand"), new CSParameterList(), CSCodeBlock.Create(printIt)); altClass.Methods.Add(whichHandMethod); var altCtor = new CSMethod(CSVisibility.Public, CSMethodKind.None, null, altClass.Name, new CSParameterList(), new CSBaseExpression [0], true, new CSCodeBlock()); altClass.Constructors.Add(altCtor); var caller = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("did it")); var callingCode = CSCodeBlock.Create(caller); TestRunning.TestAndExecute(swiftCode, callingCode, "did it\n", otherClass: altClass, platform: PlatformName.macOS); }
public void ObjCInvokeProp() { var swiftCode = "import Foundation\n" + "@objc\n" + "public protocol HandProp {\n" + " @objc var whichHand:Int { get }\n" + "}\n" + "public func doWhichHand(a:HandProp) -> Int {\n" + " return a.whichHand\n" + "}\n"; var altClass = new CSClass(CSVisibility.Public, "MyHandsProp"); altClass.Inheritance.Add(new CSIdentifier("NSObject")); altClass.Inheritance.Add(new CSIdentifier("IHandProp")); var whichHandProp = CSProperty.PublicGetBacking(new CSSimpleType("nint"), new CSIdentifier("WhichHand"), new CSIdentifier("42")); altClass.Properties.Add(whichHandProp); var altCtor = new CSMethod(CSVisibility.Public, CSMethodKind.None, null, altClass.Name, new CSParameterList(), new CSBaseExpression [0], true, new CSCodeBlock()); altClass.Constructors.Add(altCtor); var altInst = CSVariableDeclaration.VarLine(CSSimpleType.Var, "lefty", new CSFunctionCall("MyHandsProp", true)); var caller = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("TopLevelEntities.DoWhichHand", new CSIdentifier("lefty"))); var callingCode = CSCodeBlock.Create(altInst, caller); TestRunning.TestAndExecute(swiftCode, callingCode, "42\n", otherClass: altClass, platform: PlatformName.macOS); }
public void CallAVirtualInACtor(PlatformName platform) { string swiftCode = @"import Foundation open class VirtInInit : NSObject { private var theValue:Int = 0; public init (value:Int) { super.init() setValue (value: value) } open func setValue (value: Int) { theValue = value } open func getValue () -> Int { return theValue; } } "; var clDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, "cl", new CSFunctionCall("VirtInInit", true, CSConstant.Val(5))); var printer = CSFunctionCall.ConsoleWriteLine(new CSFunctionCall("cl.GetValue", false)); var setter = CSFunctionCall.FunctionCallLine("cl.SetValue", CSConstant.Val(7)); var callingCode = CSCodeBlock.Create(clDecl, printer, setter, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "5\n7\n", platform: platform); }
public void VirtualOpenPropStruct(PlatformName platform) { string swiftCode = $"import Foundation\nopen class AnotherOpenVirtualClass{platform} {{\n\tpublic init () {{ }}\n\topen var OSVersion = OperatingSystemVersion (majorVersion: 1, minorVersion:2, patchVersion: 3)\n}}\n"; // var cl = new AnotherVirtualClass (); // var vers = cl.OSVersion; // Console.WriteLine(vers); // vers.Major = 5; // cl.OSVersion = vers; // vers = cl.OSVersion; // Console.WriteLine(vers); var versID = new CSIdentifier("vers"); var clID = new CSIdentifier("cl"); var osverExpr = clID.Dot(new CSIdentifier("OSVersion")); var clDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, clID, new CSFunctionCall($"AnotherOpenVirtualClass{platform}", true)); var versDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, versID, osverExpr); var printer = CSFunctionCall.ConsoleWriteLine(versID); var setMajor = CSAssignment.Assign(versID.Dot(new CSIdentifier("Major")), CSConstant.Val(5)); var setOSVer = CSAssignment.Assign(osverExpr, versID); var resetVer = CSAssignment.Assign(versID, osverExpr); var callingCode = CSCodeBlock.Create(clDecl, versDecl, printer, setMajor, setOSVer, resetVer, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "1.2.3\n5.2.3\n", platform: platform); }
public void NSImageViewSmokeTest2() { string swiftCode = "import Foundation\n" + "import Cocoa\n" + "@IBDesignable\n" + "open class NSImageX : NSImageView {\n" + " open override func draw (_ dirtyRect: NSRect) {\n" + " super.draw(dirtyRect)\n" + " let path = NSBezierPath ()\n" + " path.lineWidth = 8.0\n" + " path.move(to: NSPoint (x:frame.minX, y:frame.minY))\n" + " path.line(to: NSPoint(x:frame.maxX, y:frame.maxY))\n" + " path.move(to: NSPoint (x:frame.maxX, y:frame.minY))\n" + " path.line(to: NSPoint (x:frame.minX, y: frame.maxY))\n" + " NSColor.red.set()\n" + " path.stroke()\n" + " }\n" + "}\n"; var printer = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("ok")); var callingCode = CSCodeBlock.Create(printer); TestRunning.TestAndExecute(swiftCode, callingCode, "ok\n", platform: PlatformName.macOS); }
void WrapSingleSubscriptGetOnly(string type, string csType, string csReplacement, string csAlt, string expected) { string swiftCode = TestRunningCodeGenerator.kSwiftFileWriter + $"public protocol MontyWSGO{type} {{ subscript(i:Int32) -> {type} {{ get }} \n }}\n" + $"public class TestMontyWSGO{type} {{\npublic init() {{ }}\npublic func doIt(m:MontyWSGO{type}) {{\nvar s = \"\", t=\"\"\nprint(m[0], to:&s)\nprint(m[1], to:&t)\nwriteToFile(s+t, \"WrapSingleSubscriptGetOnly{type}\")\n}}\n}}\n"; CSClass overCS = new CSClass(CSVisibility.Public, $"OverWSGO{type}"); overCS.Inheritance.Add(new CSIdentifier($"IMontyWSGO{type}")); CSParameterList overParams = new CSParameterList(); overParams.Add(new CSParameter(CSSimpleType.Int, "i")); CSCodeBlock overBody = CSCodeBlock.Create(CSReturn.ReturnLine(new CSTernary(new CSIdentifier("i") == CSConstant.Val(0), new CSIdentifier(csReplacement), new CSIdentifier(csAlt), false))); CSProperty overProp = new CSProperty(new CSSimpleType(csType), CSMethodKind.None, CSVisibility.Public, overBody, CSVisibility.Public, null, overParams); overCS.Properties.Add(overProp); CSLine decl = CSVariableDeclaration.VarLine(new CSSimpleType($"OverWSGO{type}"), "myOver", new CSFunctionCall($"OverWSGO{type}", true)); CSLine decl1 = CSVariableDeclaration.VarLine(new CSSimpleType($"TestMontyWSGO{type}"), "tester", new CSFunctionCall($"TestMontyWSGO{type}", true)); CSLine invoker = CSFunctionCall.FunctionCallLine("tester.DoIt", false, new CSIdentifier("myOver")); CSCodeBlock callingCode = CSCodeBlock.Create(decl, decl1, invoker); TestRunning.TestAndExecute(swiftCode, callingCode, expected, testName: $"WrapSingleSubscriptGetOnly{type}", otherClass: overCS); }
public void CGFloatVirtual(PlatformName platform) { var swiftCode = @" import Foundation import CoreGraphics open class ItsACGFloat { open var value:CGFloat = 0 public init (with: CGFloat) { value = with } open func getValue () -> CGFloat { return value } } "; var cgfID = new CSIdentifier("cgf"); var cgfDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, cgfID, new CSFunctionCall("ItsACGFloat", true, new CSCastExpression(new CSSimpleType("nfloat"), CSConstant.Val(42.5)))); var printer = CSFunctionCall.ConsoleWriteLine(new CSFunctionCall("cgf.GetValue", false)); var callingCode = CSCodeBlock.Create(cgfDecl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "42.5\n", platform: platform); }
void TLArrayMethodRemove(string swiftType, string csType, string csValue, string csNewValue, string output) { string swiftCode = $"public class FooTLAMR{swiftType} {{\n" + $"public init() {{ }}\n" + $"public func makeArray(a:{swiftType}) -> [{swiftType}]\n {{\n return [{swiftType}](repeating:a, count:2) \n}}\n" + $"}}"; CodeElementCollection <ICodeElement> callingCode = new CodeElementCollection <ICodeElement> (); CSLine decl = CSVariableDeclaration.VarLine(new CSSimpleType("SwiftArray", false, new CSSimpleType(csType)), new CSIdentifier("arr"), new CSFunctionCall($"new FooTLAMR{swiftType}().MakeArray", false, new CSIdentifier(csValue))); CSLine addLine = CSFunctionCall.FunctionCallLine("arr.Insert", false, CSConstant.Val(1), new CSIdentifier(csNewValue)); CSLine remLine = CSFunctionCall.FunctionCallLine("arr.RemoveAt", false, CSConstant.Val(1)); CSLine call = CSFunctionCall.FunctionCallLine("Console.Write", false, new CSIdentifier("arr.Count")); CSForEach feach = new CSForEach(new CSSimpleType(csType), "x", new CSIdentifier("arr"), null); feach.Body.Add(CSFunctionCall.FunctionCallLine("Console.Write", false, CSConstant.Val(" {0}"), feach.Ident)); callingCode.Add(decl); callingCode.Add(addLine); callingCode.Add(remLine); callingCode.Add(call); callingCode.Add(feach); TestRunning.TestAndExecute(swiftCode, callingCode, output, testName: $"TLArrayMethodRemove{swiftType}"); }
public void TestMultiOverride(PlatformName platform) { var swiftCode = @" open class FirstClass { public init () { } open func firstFunc () -> Int { return 42 } } open class SecondClass : FirstClass { public override init () { } open func secondFunc () -> Int { return 17 } } "; var declID = new CSIdentifier("cl"); var decl = CSVariableDeclaration.VarLine(CSSimpleType.Var, declID, new CSFunctionCall("SecondClass", true)); var printer = CSFunctionCall.ConsoleWriteLine(new CSFunctionCall($"{declID.Name}.FirstFunc", false)); var callingCode = CSCodeBlock.Create(decl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "42\n", "TestMultiOverride", platform: platform); }
void WrapOptionalArg(string type1, string cstype1, string val1, string expected, bool isStatic = true, string distinguisher = "") { var typeString = type1 + distinguisher; string finalDecl = (isStatic ? "final" : ""); string statDecl = (isStatic ? "static" : ""); string swiftCode = TestRunningCodeGenerator.kSwiftFileWriter + $"public {finalDecl} class MontyWOA{typeString} {{ public init() {{ }}\n public {statDecl} func printOpt(a:{type1}?)\n {{\n var s = \"\"\nif a != nil {{\n print(\"Optional(\\(a!))\", to:&s)\n }}\n else {{ print(\"nil\", to:&s)\n }}\nwriteToFile(s, \"WrapOptionalArg{typeString}\")\n }}\n}}\n"; CSBaseExpression optValue = null; if (val1 != null) { optValue = new CSFunctionCall(String.Format("SwiftOptional<{0}>", cstype1), true, new CSIdentifier(val1)); } else { optValue = new CSFunctionCall(String.Format("SwiftOptional<{0}>.None", cstype1), false); } CSLine csopt = CSVariableDeclaration.VarLine(new CSSimpleType("SwiftOptional", false, new CSSimpleType(cstype1)), "opt", optValue); CSLine printer = CSFunctionCall.FunctionCallLine((isStatic ? $"MontyWOA{typeString}.PrintOpt" : $"new MontyWOA{typeString}().PrintOpt"), false, new CSIdentifier("opt")); CSCodeBlock callingCode = CSCodeBlock.Create(csopt, printer); TestRunning.TestAndExecute(swiftCode, callingCode, expected, testName: $"WrapOptionalArg{typeString}"); }
public void TestSetRemove(string swiftType, string cstype, string val) { var variant = swiftType; var swiftCode = $"public func makeSetTR{variant}() -> Set<{swiftType}> {{\n" + " return Set()\n" + "}\n"; var callingCode = new CodeElementCollection <ICodeElement> (); var setID = new CSIdentifier("theSet"); var setDecl = CSVariableDeclaration.VarLine(new CSSimpleType("SwiftSet", false, new CSSimpleType(cstype)), setID, new CSFunctionCall($"TopLevelEntities.MakeSetTR{variant}", false)); var valID = new CSIdentifier("theVal"); var valDecl = CSVariableDeclaration.VarLine(new CSSimpleType(cstype), valID, new CSIdentifier(val)); var containsLine = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("theSet.Contains", (CSIdentifier)val)); var addLine = CSFunctionCall.FunctionCallLine("theSet.Insert", false, valID); var removeLine = CSFunctionCall.FunctionCallLine("theSet.Remove", false, valID); callingCode.Add(setDecl); callingCode.Add(valDecl); callingCode.Add(containsLine); callingCode.Add(addLine); callingCode.Add(containsLine); callingCode.Add(removeLine); callingCode.Add(containsLine); TestRunning.TestAndExecute(swiftCode, callingCode, "False\nTrue\nFalse\n", testName: $"TestSetRemove{variant}"); }
void WrapSingleProperty(string type, string returnVal, string csType, string csReplacement, string expected) { string appendage = type.Replace('.', '_'); string swiftCode = $"open class MontyWSP{appendage} {{ public init() {{}}\n open var val: {type} {{\nget {{ return {returnVal}\n}} }} }}"; CSClass overCS = new CSClass(CSVisibility.Public, $"OverWSP{appendage}"); overCS.Inheritance.Add(new CSIdentifier($"MontyWSP{appendage}")); CSCodeBlock getterBody = CSCodeBlock.Create(CSReturn.ReturnLine(new CSIdentifier(csReplacement))); CSProperty overProp = new CSProperty(new CSSimpleType(csType), CSMethodKind.Override, new CSIdentifier("Val"), CSVisibility.Public, getterBody, CSVisibility.Public, null); overCS.Properties.Add(overProp); CSCodeBlock printBody = CSCodeBlock.Create(CSFunctionCall.ConsoleWriteLine(CSConstant.Val("{0}, {1}"), (CSIdentifier)"base.Val", (CSIdentifier)"Val")); CSMethod printIt = new CSMethod(CSVisibility.Public, CSMethodKind.None, CSSimpleType.Void, new CSIdentifier("PrintIt"), new CSParameterList(), printBody); overCS.Methods.Add(printIt); CSLine decl = CSVariableDeclaration.VarLine(new CSSimpleType($"OverWSP{appendage}"), "printer", new CSFunctionCall($"OverWSP{appendage}", true)); CSLine invoker = CSFunctionCall.FunctionCallLine("printer.PrintIt", false); CSCodeBlock callingCode = CSCodeBlock.Create(decl, invoker); TestRunning.TestAndExecute(swiftCode, callingCode, expected, testName: $"WrapSingleProperty{appendage}", otherClass: overCS, platform: PlatformName.macOS); }
public void HasTuple() { var swiftCode = @" public func tupleTestDoesNothing () { }"; // var ocsty = typeof (Tuple<bool, float>) // var mt = StructMarshal.Marshaler.Metatypeof (ocsty); // Type csty; // SwiftTypeRegistry.Registry.TryGetValue(mt, out csty); // Console.WriteLine (csty == ocsty); var ocsTypeID = new CSIdentifier("ocsty"); var csTypeID = new CSIdentifier("csty"); var mtID = new CSIdentifier("mt"); var ocstyDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, ocsTypeID, new CSSimpleType("Tuple", false, CSSimpleType.Bool, CSSimpleType.Float).Typeof()); var mtDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, mtID, new CSFunctionCall("StructMarshal.Marshaler.Metatypeof", false, ocsTypeID)); var cstyDecl = CSVariableDeclaration.VarLine(CSSimpleType.Type, csTypeID); var tryGetLine = CSFunctionCall.FunctionCallLine("SwiftTypeRegistry.Registry.TryGetValue", false, mtID, new CSUnaryExpression(CSUnaryOperator.Out, csTypeID)); var printer = CSFunctionCall.ConsoleWriteLine(csTypeID == ocsTypeID); var callingCode = CSCodeBlock.Create(ocstyDecl, mtDecl, cstyDecl, tryGetLine, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "True\n"); }
public void TestExistentialContainer() { var swiftCode = @" public protocol SomeProtocol { func foo () } "; // var ocsty = typeof (ISomeProtocol); // var mt = StructMarshal.Marshaler.ExistentialMetatypeof (ocsty); // Type csty; // SwiftTypeRegistry.Registry.TryGetValue (mt, out csty); // Console.WriteLine (csty.Name); // Console.WriteLine (csty == ocsty); var ocsTypeID = new CSIdentifier("ocsty"); var csTypeID = new CSIdentifier("csty"); var mtID = new CSIdentifier("mt"); var ocstyDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, ocsTypeID, new CSSimpleType("ISomeProtocol").Typeof()); var mtDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, mtID, new CSFunctionCall("StructMarshal.Marshaler.ExistentialMetatypeof", false, ocsTypeID)); var cstyDecl = CSVariableDeclaration.VarLine(CSSimpleType.Type, csTypeID); var tryGetLine = CSFunctionCall.FunctionCallLine("SwiftTypeRegistry.Registry.TryGetValue", false, mtID, new CSUnaryExpression(CSUnaryOperator.Out, csTypeID)); var print1 = CSFunctionCall.ConsoleWriteLine(ocsTypeID.Dot(new CSIdentifier("Name"))); var print2 = CSFunctionCall.ConsoleWriteLine(csTypeID == ocsTypeID); var callingCode = CSCodeBlock.Create(ocstyDecl, mtDecl, cstyDecl, tryGetLine, print1, print2); TestRunning.TestAndExecute(swiftCode, callingCode, "ISomeProtocol\nTrue\n", platform: PlatformName.macOS); }
public void HasBuiltInTypes(string csType) { var swiftCode = $"public func doesNothing{csType}() {{\n}}\n"; // var ocsty = typeof (csType); // var mt = StructMarshal.Marshaler.Metatypeof (ocsty); // Type csty; // SwiftTypeRegistry.Registry.TryGetValue(mt, out csty); // Console.WriteLine (csty == ocsty); var ocsTypeID = new CSIdentifier("ocsty"); var csTypeID = new CSIdentifier("csty"); var mtID = new CSIdentifier("mt"); var ocstyDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, ocsTypeID, new CSSimpleType(csType).Typeof()); var mtDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, mtID, new CSFunctionCall("StructMarshal.Marshaler.Metatypeof", false, ocsTypeID)); var cstyDecl = CSVariableDeclaration.VarLine(CSSimpleType.Type, csTypeID); var tryGetLine = CSFunctionCall.FunctionCallLine("SwiftTypeRegistry.Registry.TryGetValue", false, mtID, new CSUnaryExpression(CSUnaryOperator.Out, csTypeID)); var printer = CSFunctionCall.ConsoleWriteLine(csTypeID == ocsTypeID); var callingCode = CSCodeBlock.Create(ocstyDecl, mtDecl, cstyDecl, tryGetLine, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "True\n", testName: "HashBuiltInTypes" + csType); }
public void TestDictionary() { var swiftCode = @" public func dictionaryTestDoesNothing () { }"; // var ocsty = typeof (SwiftOptional<bool>); // var mt = StructMarshal.Marshaler.Metatypeof (ocsty); // Type csty; // SwiftTypeRegistry.Registry.TryGetValue (mt, out csty); // Console.WriteLine (csty.Name); // Console.WriteLine (genargs.Length); // Console.WriteLine (genargs[1].Name); // Console.WriteLine (csty == ocsty); var ocsTypeID = new CSIdentifier("ocsty"); var csTypeID = new CSIdentifier("csty"); var mtID = new CSIdentifier("mt"); var ocstyDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, ocsTypeID, new CSSimpleType("SwiftDictionary", false, CSSimpleType.Bool, CSSimpleType.Int).Typeof()); var mtDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, mtID, new CSFunctionCall("StructMarshal.Marshaler.Metatypeof", false, ocsTypeID)); var cstyDecl = CSVariableDeclaration.VarLine(CSSimpleType.Type, csTypeID); var tryGetLine = CSFunctionCall.FunctionCallLine("SwiftTypeRegistry.Registry.TryGetValue", false, mtID, new CSUnaryExpression(CSUnaryOperator.Out, csTypeID)); var print1 = CSFunctionCall.ConsoleWriteLine(ocsTypeID.Dot(new CSIdentifier("Name"))); var genargsID = new CSIdentifier("genargs"); var genArgsDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, genargsID, new CSFunctionCall($"{csTypeID.Name}.GetGenericArguments", false)); var print2 = CSFunctionCall.ConsoleWriteLine(genargsID.Dot(new CSIdentifier("Length"))); var print3 = CSFunctionCall.ConsoleWriteLine(new CSIdentifier($"{genargsID.Name} [0].Name")); var print4 = CSFunctionCall.ConsoleWriteLine(csTypeID == ocsTypeID); var callingCode = CSCodeBlock.Create(ocstyDecl, mtDecl, cstyDecl, tryGetLine, print1, genArgsDecl, print2, print3, print4); TestRunning.TestAndExecute(swiftCode, callingCode, "SwiftDictionary`2\n2\nBoolean\nTrue\n"); }
public void WrapVirtualSubscriptProtocol() { string swiftCode = "public protocol Thingy {\n" + " func whoAmI () -> String\n" + "}\n" + "public class Popeye : Thingy {\n" + " public init() { }\n" + " public func whoAmI () -> String\n {" + " return \"who I yam\"\n" + " }\n" + "}\n" + "open class Scripto {\n" + " private var x:Thingy = Popeye()\n" + " public init() { }\n" + " open subscript (index: Int) -> Thingy {\n" + " get {\n" + " return x\n" + " }\n" + " set(newValue) {\n" + " x = newValue\n" + " }\n" + " }\n" + "}\n"; var decl = CSVariableDeclaration.VarLine(new CSSimpleType("Scripto"), "sub", new CSFunctionCall("Scripto", true)); var decl2 = CSVariableDeclaration.VarLine(CSSimpleType.Var, "pop", new CSIndexExpression("sub", false, CSConstant.Val(0))); var printer = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("pop.WhoAmI")); var callingCode = CSCodeBlock.Create(decl, decl2, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "who I yam\n", platform: PlatformName.macOS); }
void WrapSingleGetSetSubscript0(string type, string returnVal, string csType, string csReplacement, string expected) { string swiftCode = $"public class MontyWSGSSub{type} {{ public init() {{}}\n private var _x:{type} = {returnVal}\npublic subscript(i:Int32) -> {type} {{\nget {{ return _x\n}}\nset {{ _x = newValue\n}} }} }}"; CSLine decl = CSVariableDeclaration.VarLine(new CSSimpleType($"MontyWSGSSub{type}"), "monty", new CSFunctionCall($"MontyWSGSSub{type}", true)); CSLine invoker = CSFunctionCall.ConsoleWriteLine((CSIdentifier)"monty[0]"); CSCodeBlock callingCode = CSCodeBlock.Create(decl, invoker); TestRunning.TestAndExecute(swiftCode, callingCode, expected, $"WrapSingleGetSetSubscript0{type}"); }
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); } }
void CheckName(string typeName, string expected) { using (DisposableTempDirectory temp = new DisposableTempDirectory(null, true)) { string csFile = Path.Combine(temp.DirectoryPath, temp.UniqueName("CS", "", "cs")); string source = $@"using System; using SwiftRuntimeLibrary; using SwiftRuntimeLibrary.SwiftMarshal; namespace dlopentest {{ class MainClass {{ public static void Main (string[] args) {{ SwiftNominalTypeDescriptor nt = StructMarshal.Marshaler.Metatypeof(typeof({typeName})).GetNominalTypeDescriptor(); Console.WriteLine(nt.GetFullName()); }} }} }}"; source += TestRunning.GetManagedConsoleRedirectCode(); File.WriteAllText(csFile, source); Compiler.CSCompile(temp.DirectoryPath, new string [] { csFile }, "TestIt.exe", $"-lib:{Compiler.CompilerLocation.SwiftCompilerLib}", PlatformName.macOS); TestRunning.CopyTestReferencesTo(temp.DirectoryPath); string output = Compiler.RunWithMono(Path.Combine(temp.DirectoryPath, "TestIt.exe"), temp.DirectoryPath, platform: PlatformName.macOS); Assert.AreEqual(expected, output); string tsource = $@"using System; using NewClassCompilerTests; using SwiftRuntimeLibrary; using TomTest; using SwiftRuntimeLibrary.SwiftMarshal; namespace MetatypeTests {{ public class CheckName{typeName} : ITomTest {{ public void Run() {{ SwiftNominalTypeDescriptor nt = StructMarshal.Marshaler.Metatypeof(typeof({typeName})).GetNominalTypeDescriptor(); Console.WriteLine(nt.GetMangledName()); }} public string TestName {{ get {{ return ""CheckName{typeName}""; }} }} public string ExpectedOutput {{ get {{ return {ToStringLiteral (expected)}; }} }} }} }}"; string thisTestPath = Path.Combine(Compiler.kSwiftDeviceTestRoot, "MetatypeTests"); Directory.CreateDirectory(thisTestPath); string tpath = Path.Combine(thisTestPath, $"CheckName{typeName}.cs"); File.WriteAllText(tpath, tsource); } }
public void WrapSingleGetSetSubscript3() { string swiftCode = $"public struct FooWSGSub3 {{ public let X:Int\npublic init(i:Int) {{ X = i\n }} }}\n public class MontyWSGSub3 {{ public init() {{}}\n private var _x:FooWSGSub3 = FooWSGSub3(i:42);\npublic subscript(i:Int32) -> FooWSGSub3 {{\nget {{ return _x\n}}\nset {{ _x = newValue\n}} }} }}"; CSLine decl = CSVariableDeclaration.VarLine(new CSSimpleType("MontyWSGSub3"), "monty", new CSFunctionCall("MontyWSGSub3", true)); CSLine invoker = CSFunctionCall.ConsoleWriteLine((CSIdentifier)"monty[0].X"); CSLine setter = CSAssignment.Assign("monty[0]", new CSFunctionCall("FooWSGSub3", true, CSConstant.Val(37L))); CSCodeBlock callingCode = CSCodeBlock.Create(decl, invoker, setter, invoker); TestRunning.TestAndExecute(swiftCode, callingCode, "42\n37\n"); }
public void AttributeProtocol() { var swiftCode = @" public protocol BoringProtocol { func Add (a: Int, b: Int) -> Int } "; var callingCode = PrintTypeName("IBoringProtocol"); TestRunning.TestAndExecute(swiftCode, callingCode, ".BoringProtocol\n", platform: PlatformName.macOS); }
public void TopLevelPropertyAllUnicode1() { string swiftCode = "public var v\x3004:Int = 3\n"; var callingCode = new CodeElementCollection <ICodeElement> (); callingCode.Add(CSFunctionCall.ConsoleWriteLine((CSIdentifier)"TopLevelEntities.VU3004")); TestRunning.TestAndExecute(swiftCode, callingCode, "3\n"); }
public void TheEpsilonIssue() { string swiftCode = "public let 𝑒 = 2.718\n"; var callingCode = new CodeElementCollection <ICodeElement> (); callingCode.Add(CSFunctionCall.ConsoleWriteLine((CSIdentifier)"TopLevelEntities.LittleEpsilon")); TestRunning.TestAndExecute(swiftCode, callingCode, "2.718\n"); }
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 WrapSingleGetSetProperty(string type, string returnVal, string csType, string csReplacement, string expectedOutput) { string appendage = type.Replace('.', '_'); var swiftCode = String.Format("public class Monty {{ public init() {{}}\n private var _x:{0} = {1}\npublic var val: {0} {{\nget {{ return _x\n}}\nset {{ _x = newValue\n}} }} }}", type, returnVal); var decl = CSVariableDeclaration.VarLine(new CSSimpleType("Monty"), "monty", new CSFunctionCall("Monty", true)); var invoker = CSFunctionCall.ConsoleWriteLine(new CSIdentifier("monty.Val")); var setter = CSAssignment.Assign("monty.Val", new CSIdentifier(csReplacement)); var callingCode = CSCodeBlock.Create(decl, invoker, setter, invoker); TestRunning.TestAndExecute(swiftCode, callingCode, expectedOutput, testName: $"WrapSingleProperty{appendage}"); }
public void StaticExtensionPropOnBool() { var swiftCode = "public extension Bool {\n" + " public static var truthy: Int { return 4; }\n" + "}\n"; var extendIt = new CSFunctionCall("ExtensionsForSystemDotbool0.GetTruthy", false); var printer = CSFunctionCall.ConsoleWriteLine(extendIt); var callingCode = CSCodeBlock.Create(printer); TestRunning.TestAndExecuteNoDevice(swiftCode, callingCode, "4\n"); }
public void ExtensionPropOnInt32() { var swiftCode = "public extension Int32 {\n" + " public var times3 : Int32 { return self * 3; }\n" + "}\n"; var extendIt = new CSFunctionCall("3.GetTimes3", false); var printer = CSFunctionCall.ConsoleWriteLine(extendIt); var callingCode = CSCodeBlock.Create(printer); TestRunning.TestAndExecuteNoDevice(swiftCode, callingCode, "9\n"); }