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 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 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 SimpleProtocolProGetSetAssocTestAltSyntax () { var swiftCode = @" public protocol Simplest3 { associatedtype Item var thing: Item { get set } } public func doSetProp<T> (a: inout T, b:T.Item) where T:Simplest3 { a.thing = b } "; var altClass = new CSClass (CSVisibility.Public, "Simple3Impl"); altClass.Inheritance.Add (new CSIdentifier ("ISimplest3<SwiftString>")); var thingProp = CSProperty.PublicGetSet (new CSSimpleType ("SwiftString"), "Thing"); altClass.Properties.Add (thingProp); var ctor = new CSMethod (CSVisibility.Public, CSMethodKind.None, null, altClass.Name, new CSParameterList (), CSCodeBlock.Create ()); altClass.Methods.Add (ctor); var instID = new CSIdentifier ("inst"); var instDecl = CSVariableDeclaration.VarLine (instID, new CSFunctionCall ("Simple3Impl", true)); var doSetProp = CSFunctionCall.FunctionCallLine ("TopLevelEntities.DoSetProp<Simple3Impl, SwiftString>", false, instID, new CSFunctionCall ("SwiftString.FromString", false, CSConstant.Val ("Got here!"))); var printer = CSFunctionCall.ConsoleWriteLine (new CSIdentifier ($"{instID.Name}.Thing")); var callingCode = CSCodeBlock.Create (instDecl, doSetProp, printer); TestRunning.TestAndExecute (swiftCode, callingCode, "Got here!\n", otherClass: altClass, platform: PlatformName.macOS); }
public void SimpleProtocolProGetSetIndexer () { var swiftCode = @" public protocol Simplest5 { associatedtype Item subscript (index: Int) -> Item { get set } } public func doSetIt<T:Simplest5> (a: inout T, i: Int, v: T.Item) { a[i] = v } "; var altClass = new CSClass (CSVisibility.Public, "Simple5Impl"); altClass.Inheritance.Add (new CSIdentifier ("ISimplest5<SwiftString>")); var fieldName = new CSIdentifier ("v"); altClass.Fields.Add (CSFieldDeclaration.FieldLine (new CSSimpleType ("SwiftString"), fieldName)); var getBlock = CSCodeBlock.Create (CSReturn.ReturnLine (fieldName)); var setBlock = CSCodeBlock.Create (CSAssignment.Assign (fieldName, new CSIdentifier ("value"))); var parameters = new CSParameterList (new CSParameter (new CSSimpleType ("nint"), new CSIdentifier ("index"))); var thingIndex = new CSProperty (new CSSimpleType ("SwiftString"), CSMethodKind.None, CSVisibility.Public, getBlock, CSVisibility.Public, setBlock, parameters); altClass.Properties.Add (thingIndex); var ctor = new CSMethod (CSVisibility.Public, CSMethodKind.None, null, altClass.Name, new CSParameterList (), CSCodeBlock.Create ()); altClass.Methods.Add (ctor); var instID = new CSIdentifier ("inst"); var instDecl = CSVariableDeclaration.VarLine (instID, new CSFunctionCall ("Simple5Impl", true)); var callSetter = CSFunctionCall.FunctionCallLine ("TopLevelEntities.DoSetIt", false, instID, CSConstant.Val (3), new CSFunctionCall ("SwiftString.FromString", false, CSConstant.Val ("Got here!"))); var printer = CSFunctionCall.ConsoleWriteLine (new CSIdentifier ($"{instID.Name}[3]")); var callingCode = CSCodeBlock.Create (instDecl, callSetter, printer); TestRunning.TestAndExecute (swiftCode, callingCode, "Got here!\n", otherClass: altClass, 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 OperatorCompositionNoInvoke() { var swiftCode = "infix operator ∘\n" + " public func ∘<T>(left: @escaping (T) -> (T), right: @escaping (T) -> (T)) -> (T) -> (T) {\n" + " return { (x) in\n" + " left (right(x))\n" + " }\n" + "}\n"; var lbody1 = new CSCodeBlock(); var lid = new CSIdentifier("d"); lbody1.Add(CSReturn.ReturnLine(lid * CSConstant.Val(2.0))); var pl = new CSParameterList(); pl.Add(new CSParameter(CSSimpleType.Double, lid)); var lam1 = new CSLambda(pl, lbody1); var lbody2 = new CSCodeBlock(); lbody2.Add(CSReturn.ReturnLine(lid * CSConstant.Val(3.0))); var lam2 = new CSLambda(pl, lbody2); var compFunc = CSFunctionCall.FunctionCallLine("TopLevelEntities.InfixOperatorRing", false, lam1, lam2); var printIt = CSFunctionCall.ConsoleWriteLine(CSConstant.Val(12.0)); var callingCode = new CodeElementCollection <ICodeElement> { compFunc, printIt }; TestRunning.TestAndExecute(swiftCode, callingCode, "12\n"); }
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 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); }
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 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 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"); }
CSCodeBlock PrintTypeName(string csTypeName, bool hexEncode = false) { // string name; // SwiftTypeNameAttribute.TryGetSwiftName (typeof (csTypeName), out name); // Console.WriteLine (name.Substring (name.IndexOf ('.'))); var nameID = "name"; var nameDecl = CSVariableDeclaration.VarLine(new CSSimpleType(typeof(string)), nameID); var tryGet = CSFunctionCall.FunctionCallLine("SwiftTypeNameAttribute.TryGetSwiftName", false, new CSFunctionCall("typeof", false, new CSIdentifier(csTypeName)), new CSIdentifier($"out {nameID}")); CSBaseExpression nameExpr = new CSFunctionCall($"{nameID}.Substring", false, new CSFunctionCall($"{nameID}.IndexOf", false, CSConstant.Val('.'))); if (hexEncode) { nameExpr = new CSFunctionCall("BitConverter.ToString", false, new CSFunctionCall("System.Text.Encoding.Default.GetBytes", false, nameExpr)); } var printer = CSFunctionCall.ConsoleWriteLine(nameExpr); return(CSCodeBlock.Create(nameDecl, tryGet, printer)); }
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 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); }
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 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 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 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 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); }
public void SimplestProtocolPropGetAssocTest () { var swiftCode = @" public protocol Simplest1 { associatedtype Item var printThing: Item { get } } public func doPrint<T>(a:T) where T:Simplest1 { let _ = a.printThing } "; var altClass = new CSClass (CSVisibility.Public, "Simple1Impl"); altClass.Inheritance.Add (new CSIdentifier ("ISimplest1<SwiftString>")); var strID = new CSIdentifier ("theStr"); var strDecl = CSVariableDeclaration.VarLine (strID, CSConstant.Val ("Got here!")); var printPart = CSFunctionCall.ConsoleWriteLine (strID); var returnPart = CSReturn.ReturnLine (new CSFunctionCall ("SwiftString.FromString", false, strID)); var printBody = CSCodeBlock.Create (strDecl, printPart, returnPart); var speak = new CSProperty (new CSSimpleType ("SwiftString"), CSMethodKind.None, new CSIdentifier ("PrintThing"), CSVisibility.Public, printBody, CSVisibility.Public, null); altClass.Properties.Add (speak); var ctor = new CSMethod (CSVisibility.Public, CSMethodKind.None, null, altClass.Name, new CSParameterList (), CSCodeBlock.Create ()); altClass.Methods.Add (ctor); var instID = new CSIdentifier ("inst"); var instDecl = CSVariableDeclaration.VarLine (instID, new CSFunctionCall ("Simple1Impl", true)); var doPrint = CSFunctionCall.FunctionCallLine ("TopLevelEntities.DoPrint<Simple1Impl, SwiftString>", false, instID); var callingCode = CSCodeBlock.Create (instDecl, doPrint); TestRunning.TestAndExecute (swiftCode, callingCode, "Got here!\n", otherClass: altClass, platform: PlatformName.macOS); }
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 SimpleProtocolProGetIndexer () { var swiftCode = @" public protocol Simplest4 { associatedtype Item subscript (index: Int) -> Item { get } } public func doGetIt<T:Simplest4> (a: T, i: Int) -> T.Item { return a[i] } "; var altClass = new CSClass (CSVisibility.Public, "Simple4Impl"); altClass.Inheritance.Add (new CSIdentifier ("ISimplest4<SwiftString>")); var getBlock = CSCodeBlock.Create (CSReturn.ReturnLine (new CSFunctionCall ("SwiftString.FromString", false, CSConstant.Val ("Got here!")))); var parameters = new CSParameterList (new CSParameter (new CSSimpleType ("nint"), new CSIdentifier ("index"))); var thingIndex = new CSProperty (new CSSimpleType ("SwiftString"), CSMethodKind.None, CSVisibility.Public, getBlock, CSVisibility.Public, null, parameters); altClass.Properties.Add (thingIndex); var ctor = new CSMethod (CSVisibility.Public, CSMethodKind.None, null, altClass.Name, new CSParameterList (), CSCodeBlock.Create ()); altClass.Methods.Add (ctor); var instID = new CSIdentifier ("inst"); var instDecl = CSVariableDeclaration.VarLine (instID, new CSFunctionCall ("Simple4Impl", true)); var resultID = new CSIdentifier ("result"); var resultDecl = CSVariableDeclaration.VarLine (resultID, new CSFunctionCall ("TopLevelEntities.DoGetIt<Simple4Impl, SwiftString>", false, instID, CSConstant.Val (3))); var printer = CSFunctionCall.ConsoleWriteLine (resultID); var callingCode = CSCodeBlock.Create (instDecl, resultDecl, printer); TestRunning.TestAndExecute (swiftCode, callingCode, "Got here!\n", otherClass: altClass, platform: PlatformName.macOS); }
public void TestUnwrappedOptional() { string swiftCode = "public class FooAny {\n" + " public init() { }\n" + "}\n" + "public class AnyBang {\n" + " public var x: AnyObject!\n" + " public init (ix: AnyObject!) {\n" + " x = ix\n" + " }\n" + "}\n"; var fooAnyID = new CSIdentifier("fooAny"); var fooAnyDecl = CSVariableDeclaration.VarLine(new CSSimpleType("FooAny"), fooAnyID, new CSFunctionCall("FooAny", true)); var anyObjID = new CSIdentifier("anyObj"); var anyObjDecl = CSVariableDeclaration.VarLine(new CSSimpleType("SwiftAnyObject"), anyObjID, new CSFunctionCall("SwiftAnyObject.FromISwiftObject", false, fooAnyID)); var anyBangID = new CSIdentifier("anyBang"); var anyBangDecl = CSVariableDeclaration.VarLine(new CSSimpleType("AnyBang"), anyBangID, new CSFunctionCall("AnyBang", true, new CSFunctionCall("SwiftOptional<SwiftAnyObject>", true, anyObjID))); var printer = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("success.")); var callingCode = CSCodeBlock.Create(fooAnyDecl, anyObjDecl, anyBangDecl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "success.\n"); }
string CompileWithInvokingCode(CSFile cs, CSLine invoker, string nameSpace) { CSNamespace ns = new CSNamespace(nameSpace); CSCodeBlock mainBody = CSCodeBlock.Create(invoker); CSMethod main = new CSMethod(CSVisibility.Public, CSMethodKind.Static, CSSimpleType.Void, new CSIdentifier("Main"), new CSParameterList(new CSParameter(new CSSimpleType("string", true), "args")), mainBody); CSClass cl = new CSClass(CSVisibility.Public, "NameNotImportant", new CSMethod [] { main }); ns.Block.Add(cl); cs.Namespaces.Add(ns); using (DisposableTempFile csOut = new DisposableTempFile(null, null, "cs", true)) { CodeWriter.WriteToFile(csOut.Filename, cs); using (Stream csExeStm = Compiler.CompileUsing(null, XCodeCompiler.CSharpExe, csOut.Filename, $"-lib:{kSwiftRuntimeMacOutputDirectory} -r:{kSwiftRuntimeLibraryMac}")) { using (DisposableTempFile csExe = new DisposableTempFile(null, null, "exe", true)) { csExeStm.CopyTo(csExe.Stream); csExe.Stream.Close(); string output = Compiler.RunWithMono(csExe.Filename); return(output); } } } }
void WrapMultiTuple(string type, int count, string appendage, string expected, int nothing, params string [] values) { StringBuilder sbargs = new StringBuilder(); StringBuilder sbtype = new StringBuilder(); StringBuilder sbret = new StringBuilder(); for (int i = 0; i < count; i++) { if (i > 0) { sbargs.Append(", "); sbtype.Append(", "); sbret.Append(", "); } sbargs.Append(String.Format("a{0}: {1}", i, type)); sbtype.Append(type); sbret.Append(String.Format("a{0}", i)); } string swiftCode = $"public final class MontyWMT{appendage} {{ public init() {{ }}\n public static func tupe({sbargs.ToString ()}) -> ({sbtype.ToString ()})\n {{\n return ({sbret.ToString ()});\n }}\n}}\n"; CSBaseExpression [] args = values.Select(str => (CSBaseExpression) new CSIdentifier(str)).ToArray(); CSLine cstupe = CSVariableDeclaration.VarLine(new CSSimpleType("var"), "tupe", new CSFunctionCall($"MontyWMT{appendage}.Tupe", false, args)); CSLine printer = CSFunctionCall.ConsoleWriteLine((CSIdentifier)"tupe"); CSCodeBlock callingCode = CSCodeBlock.Create(cstupe, printer); TestRunning.TestAndExecute(swiftCode, callingCode, expected, testName: $"WrapMultiTuple{appendage}"); }
public void TestExtenstionGetSetSubscriptOnUInt16() { var swiftCode = "public extension UInt16 {\n" + " subscript (index:Int) -> UInt16 {\n" + " get { return self & UInt16(1 << index); }\n" + " set {\n" + " if newValue != 0 {\n" + " self = self | (1 << index)\n" + " }\n" + " else {\n" + " self = self & ~UInt16(1 << index)\n" + " }\n" + " }\n" + " }\n" + "}\n"; var extendIt = new CSFunctionCall("((ushort)321).GetSubscript", false, CSConstant.Val(1)); var printer = CSFunctionCall.ConsoleWriteLine(extendIt); var decl = CSVariableDeclaration.VarLine(CSSimpleType.UShort, "ashort", CSConstant.Val((ushort)0)); var changeIt = CSFunctionCall.FunctionCallLine("ExtensionsForSystemDotushort0.SetSubscript", false, new CSIdentifier("ref ashort"), CSConstant.Val(1), CSConstant.Val(3)); var printAgain = CSFunctionCall.ConsoleWriteLine((CSIdentifier)"ashort"); var callingCode = CSCodeBlock.Create(printer, decl, changeIt, printAgain); TestRunning.TestAndExecuteNoDevice(swiftCode, callingCode, "0\n8\n"); }
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 CanGetProtocolConformanceDesc() { // var nomDesc = SwiftProtocolTypeAttribute.DescriptorForType (typeof (IIteratorProtocol<>)); // var witTable = SwiftCore.ConformsToSwiftProtocol (StructMarshal.Marshaler.Metatypeof (typeof (SwiftIteratorProtocolProxy<nint>)), // nomDesc); // Console.WriteLine (confDesc != IntPtr.Zero); var swiftCode = @" public func canGetProtocolConfDesc () { } "; var nomDescID = new CSIdentifier("nomDesc"); var witTableID = new CSIdentifier("witTable"); var nomDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, nomDescID, new CSFunctionCall("SwiftProtocolTypeAttribute.DescriptorForType", false, new CSSimpleType("IIteratorProtocol<>").Typeof())); var metaTypeCall = new CSFunctionCall("StructMarshal.Marshaler.Metatypeof", false, new CSSimpleType("SwiftIteratorProtocolProxy<nint>").Typeof()); var confDescDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, witTableID, new CSFunctionCall("SwiftCore.ConformsToSwiftProtocol", false, metaTypeCall, nomDescID)); var printer = CSFunctionCall.ConsoleWriteLine(witTableID.Dot(new CSIdentifier("Handle")) != new CSIdentifier("IntPtr.Zero")); var callingCode = CSCodeBlock.Create(nomDecl, confDescDecl, printer); TestRunning.TestAndExecute(swiftCode, callingCode, "True\n"); }
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); }