Beispiel #1
0
        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);
        }
Beispiel #2
0
        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);
        }
        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);
        }
Beispiel #4
0
        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 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 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);
		}
        static CSAttribute ExportAttribute(string objcSelector)
        {
            var paramList = new CSArgumentList();

            paramList.Add(CSConstant.Val(objcSelector));
            return(new CSAttribute(new CSIdentifier("Export"), paramList, true));
        }
        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");
        }
Beispiel #9
0
        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 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");
        }
        static CSProperty MakeGetOnlyStringProp(string name, string val)
        {
            CSCodeBlock body = new CSCodeBlock();

            body.Add(CSReturn.ReturnLine(CSConstant.Val(val)));

            return(new CSProperty(CSSimpleType.String, CSMethodKind.None, new CSIdentifier(name), CSVisibility.Public,
                                  body, CSVisibility.Public, null));
        }
 void DeclInitExposure(CSVisibility vis)
 {
     using (Stream stm = Utils.BasicClass("None", "AClass", null, cl => {
         cl.Fields.Add(CSFieldDeclaration.FieldLine(CSSimpleType.Byte, "b", CSConstant.Val((byte)0), CSVisibility.Public));
         return(cl);
     })) {
         Utils.CompileAStream(stm);
     }
 }
Beispiel #13
0
        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}");
        }
 public void StaticMethodParam()
 {
     using (Stream stm = Utils.BasicClass("None", "AClass", null, cl => {
         CSParameterList pl = new CSParameterList().And(new CSParameter(CSSimpleType.Int, "x"));
         CSCodeBlock b = new CSCodeBlock().And(CSReturn.ReturnLine(CSConstant.Val(0)));
         cl.Methods.Add(CSMethod.PublicMethod(CSMethodKind.Virtual, CSSimpleType.Int, "Foo", pl, b));
         return(cl);
     })) {
         Utils.CompileAStream(stm);
     }
 }
		public void SmokeProtocolAssocGetSetProp ()
		{
			var swiftCode = @"
public protocol Iterator2 {
	associatedtype Elem
	var item: Elem { get set }
}
";
			var printer = CSFunctionCall.ConsoleWriteLine (CSConstant.Val ("OK"));
			var callingCode = CSCodeBlock.Create (printer);
			TestRunning.TestAndExecute (swiftCode, callingCode, "OK\n", platform: PlatformName.macOS);
		}
        static CodeElementCollection <ICodeElement> CaptureSwiftOutputPostlude(string fileName)
        {
            //#if _MAC_TS_TEST_
            //            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName);
            //#else
            //NSUrl[] urls = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User);
            //string path = Path.Combine(urls[0].Path, fileName);
            //#endif

            //NSUrl[] urls = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User);
            //string path = Path.Combine(urls[0].Path, fileName);
            //if (File.Exists(path))
            //{
            //    Console.Write(File.ReadAllText(path));
            //}

            CSCodeBlock  block  = new CSCodeBlock();
            CSIdentifier pathID = new CSIdentifier("path");

            block.Add(new CSIdentifier("\n#if _MAC_TS_TEST_\n"));
            block.Add(CSVariableDeclaration.VarLine(CSSimpleType.String, pathID,
                                                    new CSFunctionCall("Path.Combine", false,
                                                                       new CSIdentifier("Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)") +
                                                                       CSConstant.Val("/Documents/"),
                                                                       CSConstant.Val(fileName))));
            block.Add(new CSIdentifier("\n#else\n"));
            CSIdentifier urlID    = new CSIdentifier("urls");
            CSLine       urlsLine = CSVariableDeclaration.VarLine(new CSSimpleType("NSUrl", true), urlID,
                                                                  new CSFunctionCall("NSFileManager.DefaultManager.GetUrls", false,
                                                                                     new CSIdentifier("NSSearchPathDirectory.DocumentDirectory"),
                                                                                     new CSIdentifier("NSSearchPathDomain.User")));

            block.Add(urlsLine);


            CSLine pathLine = CSVariableDeclaration.VarLine(CSSimpleType.String, pathID,
                                                            new CSFunctionCall("Path.Combine", false,
                                                                               new CSArray1D(urlID.Name, CSConstant.Val(0)).Dot(new CSIdentifier("Path")),
                                                                               CSConstant.Val(fileName)));

            block.Add(pathLine);
            block.Add(new CSIdentifier("\n#endif\n"));

            CSCodeBlock ifBlock = new CSCodeBlock();
            CSLine      writer  = CSFunctionCall.FunctionCallLine("Console.Write", false, new CSFunctionCall("File.ReadAllText", false, pathID));

            ifBlock.Add(writer);
            ifBlock.Add(CSFunctionCall.FunctionCallLine("File.Delete", false, pathID));
            CSIfElse iftest = new CSIfElse(new CSFunctionCall("File.Exists", false, pathID), ifBlock);

            block.Add(iftest);
            return(block);
        }
		public void SmokeProtocolAssocFuncArg ()
		{
			var swiftCode = @"
public protocol Iterator3 {
	associatedtype Elem
	func ident (a:Elem)
}
";
			var printer = CSFunctionCall.ConsoleWriteLine (CSConstant.Val ("OK"));
			var callingCode = CSCodeBlock.Create (printer);
			TestRunning.TestAndExecute (swiftCode, callingCode, "OK\n", platform: PlatformName.macOS);
		}
        public void SmokeTestSimplest()
        {
            var swiftCode   = @"
public protocol Identity0 {
	func whoAmI () -> Self
}
";
            var printer     = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("Got here."));
            var callingCode = CSCodeBlock.Create(printer);

            TestRunning.TestAndExecute(swiftCode, callingCode, "Got here.\n", platform: PlatformName.macOS);
        }
Beispiel #19
0
        public void ExtensionSmokeTest()
        {
            var swiftCode =
                "public extension Double {\n" +
                "    public func DoubleIt() -> Double  { return self * 2; }\n" +
                "}\n";

            ;
            var         printer     = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("success"));
            CSCodeBlock callingCode = CSCodeBlock.Create(printer);

            TestRunning.TestAndExecute(swiftCode, callingCode, "success\n");
        }
Beispiel #20
0
        public void NSObjectSubclassableMethodTest3()
        {
            string swiftCode =
                "import Foundation\n" +
                "@objc\n" +
                "open class Subclassable3 : NSObject {\n" +
                "   public override init () { }\n" +
                "   open var returnsTrue:Bool {\n" +
                "       get { return true\n } " +
                "   }\n" +
                "}\n" +
                "public func callIt (a: Subclassable3) -> Bool {\n" +
                "    return a.returnsTrue\n" +
                "}\n";


            var theSub = new CSClass(CSVisibility.Public, "TheSub3");
            var ctor   = new CSMethod(CSVisibility.Public, CSMethodKind.None, null, theSub.Name,
                                      new CSParameterList(), new CSBaseExpression [0], true, new CSCodeBlock());

            theSub.Constructors.Add(ctor);
            theSub.Inheritance.Add(new CSIdentifier("Subclassable3"));

            var theBody = new CSCodeBlock();

            theBody.Add(CSReturn.ReturnLine(CSConstant.Val(false)));

            LineCodeElementCollection <ICodeElement> getCode =
                new LineCodeElementCollection <ICodeElement> (
                    new ICodeElement [] {
                CSReturn.ReturnLine(CSConstant.Val(false))
            }, false, true);
            CSProperty returnsFalse = new CSProperty(CSSimpleType.Bool, CSMethodKind.Override, new CSIdentifier("ReturnsTrue"),
                                                     CSVisibility.Public, new CSCodeBlock(getCode),
                                                     CSVisibility.Public, null);

            theSub.Properties.Add(returnsFalse);


            var callingCode = new CodeElementCollection <ICodeElement> ();
            var objID       = new CSIdentifier("subTest");
            var objDecl     = CSVariableDeclaration.VarLine(CSSimpleType.Var, objID, new CSFunctionCall("TheSub3", true));
            var call        = CSFunctionCall.ConsoleWriteLine(objID.Dot((CSIdentifier)"ReturnsTrue"));
            var call2       = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("TopLevelEntities.CallIt", objID));

            callingCode.Add(objDecl);
            callingCode.Add(call);
            callingCode.Add(call2);

            TestRunning.TestAndExecute(swiftCode, callingCode, "False\nFalse\n", otherClass: theSub, platform: PlatformName.macOS);
        }
Beispiel #21
0
        public void BadArgumentName()
        {
            string swiftCode =
                "open class AEXMLElement {\n" +
                "    open func allDescendants (where predicate: @escaping (AEXMLElement) -> Bool) -> [AEXMLElement] {\n" +
                "        return []\n" +
                "    }\n" +
                "}\n";

            var printer     = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("ok"));
            var callingCode = CSCodeBlock.Create(printer);

            TestRunning.TestAndExecute(swiftCode, callingCode, "ok\n", platform: PlatformName.macOS);
        }
        public void ObjCFunctionSmokeTest()
        {
            var swiftCode =
                "import Foundation\n" +
                "@objc\n" +
                "public protocol SmokeFunc0 {\n" +
                "    func intOnBool (a: Int) -> Bool\n" +
                "}\n";

            var caller      = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("did it"));
            var callingCode = CSCodeBlock.Create(caller);

            TestRunning.TestAndExecute(swiftCode, callingCode, "did it\n", platform: PlatformName.macOS);
        }
        public void ObjCSubscriptSmokeTest()
        {
            var swiftCode =
                "import Foundation\n" +
                "@objc\n" +
                "public protocol SmokeSub0 {\n" +
                "    @objc subscript (index:Int) -> Bool { get set }\n" +
                "}\n";

            var caller      = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("did it"));
            var callingCode = CSCodeBlock.Create(caller);

            TestRunning.TestAndExecute(swiftCode, callingCode, "did it\n", platform: PlatformName.macOS);
        }
		public void SomeProtocolAssocSubscriptGetSetParams ()
		{
			var swiftCode = @"
public protocol Iterator6 {
	associatedtype Elem
	subscript (index: Elem) -> Elem {
		get set
	}
}
";
			var printer = CSFunctionCall.ConsoleWriteLine (CSConstant.Val ("OK"));
			var callingCode = CSCodeBlock.Create (printer);
			TestRunning.TestAndExecute (swiftCode, callingCode, "OK\n", platform: PlatformName.macOS);
		}
Beispiel #25
0
        public void SubscriptMarshaling()
        {
            string swiftCode =
                "open class AEXMLElement2 {\n" +
                "    public init (name:String) {\n" +
                "    }\n" +
                "    open subscript (key: String) -> AEXMLElement2 {\n" +
                "        return AEXMLElement2 (name: \"\")\n" +
                "    }\n" +
                "}\n";
            var printer     = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("ok"));
            var callingCode = CSCodeBlock.Create(printer);

            TestRunning.TestAndExecute(swiftCode, callingCode, "ok\n", platform: PlatformName.macOS);
        }
        public void TestProtocolTypeAttribute()
        {
            var swiftCode = @"
public protocol Useless {
	func doNothing ()
}
";
            // this will throw on fail
            // SwiftProtocolTypeAttribute.DescriptorForType (typeof (Useless));
            var getter = CSFunctionCall.FunctionCallLine("SwiftProtocolTypeAttribute.DescriptorForType", false,
                                                         new CSSimpleType("IUseless").Typeof());
            var printer     = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("OK"));
            var callingCode = CSCodeBlock.Create(getter, printer);

            TestRunning.TestAndExecute(swiftCode, callingCode, "OK\n", platform: PlatformName.macOS);
        }
        public void ClosureSmokeTest()
        {
            string swiftCode =
                "public func callClosureCST(f:@escaping()->()) {\n" +
                "    f()\n" +
                "}";

            CodeElementCollection <ICodeElement> callingCode = new CodeElementCollection <ICodeElement> ();
            CSCodeBlock body = new CSCodeBlock();

            body.Add(CSFunctionCall.ConsoleWriteLine(CSConstant.Val("C# output")));
            CSLine invoker = CSFunctionCall.FunctionCallLine("TopLevelEntities.CallClosureCST", false,
                                                             new CSLambda(new CSParameterList(), body));

            callingCode.Add(invoker);
            TestRunning.TestAndExecute(swiftCode, callingCode, "C# output\n");
        }
        public static Tuple <CSNamespace, CSUsingPackages> CreateTestClass(CodeElementCollection <ICodeElement> callingCode, string testName,
                                                                           string expectedOutput, string nameSpace, string testClassName, CSClass otherClass, string skipReason, PlatformName platform)
        {
            var use = GetTestClassUsings(nameSpace);

            // [TomSkip(skipReason)]
            // public class TomTesttestName : ITomTest
            // {
            //    public testClassName() { }
            //    public string TestName { get { return testName; } }
            //    public string ExpectedOutput { get { return expectedOuput; } }
            //    public void Run() {
            //       callingCode;
            //    }
            // }
            // otherClass

            CSNamespace ns = new CSNamespace(nameSpace);

            if (otherClass != null)
            {
                ns.Block.Add(otherClass);
            }

            CSCodeBlock body = new CSCodeBlock(callingCode);

            body.Add(CaptureSwiftOutputPostlude(testName));

            CSMethod run = new CSMethod(CSVisibility.Public, CSMethodKind.None, CSSimpleType.Void, new CSIdentifier("Run"),
                                        new CSParameterList(), body);
            CSClass testClass = new CSClass(CSVisibility.Public, new CSIdentifier($"TomTest{testName}"), new CSMethod [] { run });

            testClass.Inheritance.Add(new CSIdentifier("ITomTest"));
            testClass.Properties.Add(MakeGetOnlyStringProp("TestName", testName));
            testClass.Properties.Add(MakeGetOnlyStringProp("ExpectedOutput", expectedOutput));
            ns.Block.Add(testClass);
            if (skipReason != null)
            {
                CSArgumentList al = new CSArgumentList();
                al.Add(CSConstant.Val(skipReason));
                CSAttribute attr = new CSAttribute("TomSkip", al);
                attr.AttachBefore(testClass);
            }
            return(new Tuple <CSNamespace, CSUsingPackages> (ns, use));
        }
        public void IgnoresPrivateClassProperty()
        {
            var swiftCode = @"public class Pluralize {
				public init() { }
					class var hidden: Int { return 3; }
					public func nothing() { }
				}
";

            var pluralDecl = CSVariableDeclaration.VarLine((CSSimpleType)"Pluralize", "plural", CSFunctionCall.Ctor("Pluralize"));
            var noop       = CSFunctionCall.FunctionLine("plural.Nothing");

            var output = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("Success"));

            var callingCode = CSCodeBlock.Create(pluralDecl, noop, output);

            TestRunning.TestAndExecute(swiftCode, callingCode, "Success\n");
        }
        public void WrapOptionalClassReturn()
        {
            string swiftCode =
                TestRunningCodeGenerator.kSwiftFileWriter +
                $"public class FooWOCR {{\n public init() {{\n}}\n public func doIt() {{\nvar s = \"\"\n print(\"hi mom\", to:&s);\nwriteToFile(s, \"WrapOptionalClassReturn\")\n }} }}\n" +
                "public final class MontyWOCR { public init() { }\n public static func getFoo(a:Bool)\n->FooWOCR? {\n if a {\n return FooWOCR();\n }\n else { return nil;\n }\n }\n}\n";

            CSLine csopt1 = CSVariableDeclaration.VarLine(new CSSimpleType("SwiftOptional<FooWOCR>"), "foo1",
                                                          new CSFunctionCall("MontyWOCR.GetFoo", false, CSConstant.Val(true)));
            CSLine csopt2 = CSVariableDeclaration.VarLine(new CSSimpleType("SwiftOptional<FooWOCR>"), "foo2",
                                                          new CSFunctionCall("MontyWOCR.GetFoo", false, CSConstant.Val(false)));

            CSLine printer = CSFunctionCall.ConsoleWriteLine(CSConstant.Val("{0}, {1}"), (CSIdentifier)"foo1", (CSIdentifier)"foo2");

            CSCodeBlock callingCode = CSCodeBlock.Create(csopt1, csopt2, printer);

            TestRunning.TestAndExecute(swiftCode, callingCode, "SwiftOptionalTypeTests.FooWOCR, \n");
        }