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 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 TLDictionaryAddGet(string sKeyType, string sValType, string csKeyType, string csValType, string csKey, string csValue, string output) { string swiftCode = $"public func makeDictTLDAG{sKeyType}{sValType}() -> [{sKeyType}:{sValType}]\n {{\n return [{sKeyType}:{sValType}]() \n}}\n"; CodeElementCollection <ICodeElement> callingCode = new CodeElementCollection <ICodeElement> (); CSLine decl = CSVariableDeclaration.VarLine(new CSSimpleType("SwiftDictionary", false, new CSSimpleType(csKeyType), new CSSimpleType(csValType)), new CSIdentifier("dict"), new CSFunctionCall($"TopLevelEntities.MakeDictTLDAG{sKeyType}{sValType}", false)); CSLine call = CSFunctionCall.FunctionCallLine("Console.Write", false, new CSIdentifier("dict.Count")); CSLine addcall = CSFunctionCall.FunctionCallLine("dict.Add", false, new CSIdentifier(csKey), new CSIdentifier(csValue)); CSLine writeCall = CSFunctionCall.FunctionCallLine("Console.Write", false, CSConstant.Val(' ')); CSLine getLine = CSVariableDeclaration.VarLine(new CSSimpleType(csValType), "val", new CSArray1D("dict", new CSIdentifier(csKey))); CSLine valueWrite = CSFunctionCall.FunctionCallLine("Console.Write", false, new CSIdentifier("val")); callingCode.Add(decl); callingCode.Add(call); callingCode.Add(addcall); callingCode.Add(writeCall); callingCode.Add(call); callingCode.Add(writeCall); callingCode.Add(getLine); callingCode.Add(valueWrite); TestRunning.TestAndExecute(swiftCode, callingCode, output, testName: $"TLDictionaryAddGet{sKeyType}{sValType}"); }
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 EnumInlineInfixOperator() { var swiftCode = @" infix operator ^*=*^ public enum CompassPoints3 { case North, East, South, West public static func ^*=*^(left: CompassPoints3, right: CompassPoints3) -> Bool { return left == right } } "; var leftID = new CSIdentifier("left"); var leftDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, leftID, new CSIdentifier("CompassPoints3.North")); var rightID = new CSIdentifier("right"); var rightDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, rightID, new CSIdentifier("CompassPoints3.East")); var resultID = new CSIdentifier("result"); var resultDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, resultID, new CSFunctionCall("CompassPoints3Extensions.InfixOperatorHatStarEqualsStarHat", false, leftID, rightID)); var printer = CSFunctionCall.ConsoleWriteLine(resultID); var callingCode = new CodeElementCollection <ICodeElement> { leftDecl, rightDecl, resultDecl, printer }; TestRunning.TestAndExecute(swiftCode, callingCode, "False\n"); }
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"); }
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 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); }
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 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"); }
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 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 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 SuperSimpleEnumTest() { var swiftCode = @" import Foundation @objc public enum CompassPoints2 : Int { case North = 0, East, South, West } prefix operator ^*^ public prefix func ^*^(val: CompassPoints2) -> CompassPoints2 { switch val { case .North: return .South case .East: return .West case .South: return .North case .West: return .East } }"; var leftID = new CSIdentifier("left"); var leftDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, leftID, new CSIdentifier("CompassPoints2.North")); var resultID = new CSIdentifier("result"); var resultDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, resultID, new CSFunctionCall("TopLevelEntities.PrefixOperatorHatStarHat", false, leftID)); var printer = CSFunctionCall.ConsoleWriteLine(resultID); var callingCode = new CodeElementCollection <ICodeElement> { leftDecl, resultDecl, printer }; TestRunning.TestAndExecute(swiftCode, callingCode, "South\n"); }
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 ClassInlinePostfixOperator() { var swiftCode = @" postfix operator ^*--*^ public class NumRep1 { public init (a: Int) { val = a } public var val: Int public static postfix func ^*--*^(val: NumRep1) -> NumRep1 { return NumRep1 (a: -val.val) } }"; var leftID = new CSIdentifier("left"); var leftDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, leftID, new CSFunctionCall("NumRep1", true, CSConstant.Val(4))); var resultID = new CSIdentifier("result"); var resultDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, resultID, new CSFunctionCall("NumRep1.PostfixOperatorHatStarMinusMinusStarHat", false, leftID)); var printer = CSFunctionCall.ConsoleWriteLine(resultID.Dot(new CSIdentifier("Val"))); var callingCode = new CodeElementCollection <ICodeElement> { leftDecl, resultDecl, printer }; TestRunning.TestAndExecute(swiftCode, callingCode, "-4\n"); }
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 EnumInlinePrefixOperator() { var swiftCode = @" prefix operator ^+-+^ public enum CompassPoints4 { case North, East, South, West public static prefix func ^+-+^ (item: CompassPoints4) -> CompassPoints4 { switch item { case .North: return .West case .East: return .North case .South: return .East case .West: return .South } } } "; var itemID = new CSIdentifier("item"); var itemDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, itemID, new CSIdentifier("CompassPoints4.North")); var resultID = new CSIdentifier("result"); var resultDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, resultID, new CSFunctionCall("CompassPoints4Extensions.PrefixOperatorHatPlusMinusPlusHat", false, itemID)); var printer = CSFunctionCall.ConsoleWriteLine(resultID); var callingCode = new CodeElementCollection <ICodeElement> { itemDecl, resultDecl, printer }; TestRunning.TestAndExecute(swiftCode, callingCode, "West\n"); }
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 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 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 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 StructInfixOpTest() { var swiftCode = @" public struct IntRep { public init (with: Int32) { val = with } public var val:Int32 = 0 } infix operator %^^% : AdditionPrecedence public func %^^% (left:IntRep, right: IntRep) -> IntRep { return IntRep (with: left.val + right.val) } "; var leftID = new CSIdentifier("left"); var leftDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, leftID, new CSFunctionCall("IntRep", true, CSConstant.Val(3))); var rightID = new CSIdentifier("right"); var rightDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, rightID, new CSFunctionCall("IntRep", true, CSConstant.Val(4))); var resultID = new CSIdentifier("result"); var resultDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, resultID, new CSFunctionCall("TopLevelEntities.InfixOperatorPercentHatHatPercent", false, leftID, rightID)); var printer = CSFunctionCall.ConsoleWriteLine(resultID.Dot(new CSIdentifier("Val"))); var callingCode = new CodeElementCollection <ICodeElement> { leftDecl, rightDecl, resultDecl, printer }; TestRunning.TestAndExecute(swiftCode, callingCode, "7\n"); }
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()); }
public void StructPostfixOpTest() { var swiftCode = @" public struct IntRep2 { public init (with: Int32) { val = with } public var val:Int32 = 0 } prefix operator %--% public prefix func %--% (left:IntRep2) -> IntRep2 { return IntRep2 (with: left.val - 1) } "; var leftID = new CSIdentifier("left"); var leftDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, leftID, new CSFunctionCall("IntRep2", true, CSConstant.Val(3))); var resultID = new CSIdentifier("result"); var resultDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, resultID, new CSFunctionCall("TopLevelEntities.PrefixOperatorPercentMinusMinusPercent", false, leftID)); var printer = CSFunctionCall.ConsoleWriteLine(resultID.Dot(new CSIdentifier("Val"))); var callingCode = new CodeElementCollection <ICodeElement> { leftDecl, resultDecl, printer }; TestRunning.TestAndExecute(swiftCode, callingCode, "2\n"); }
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 SimpleEnumPostfixOpTest() { var swiftCode = @" public enum CompassPoints1 { case North, East, South, West } postfix operator ^+^ public postfix func ^+^(val: CompassPoints1) -> CompassPoints1 { switch val { case .North: return .East case .East: return .South case .South: return .West case .West: return .North } } "; var leftID = new CSIdentifier("left"); var leftDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, leftID, new CSIdentifier("CompassPoints1.North")); var resultID = new CSIdentifier("result"); var resultDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, resultID, new CSFunctionCall("TopLevelEntities.PostfixOperatorHatPlusHat", false, leftID)); var printer = CSFunctionCall.ConsoleWriteLine(resultID); var callingCode = new CodeElementCollection <ICodeElement> { leftDecl, resultDecl, printer }; TestRunning.TestAndExecute(swiftCode, callingCode, "East\n"); }
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 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"); }