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}");
        }
Exemplo n.º 2
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}");
        }
Exemplo n.º 3
0
        public void TestSetRemove(string swiftType, string cstype, string val)
        {
            var variant   = swiftType;
            var swiftCode =
                $"public func makeSetTR{variant}() -> Set<{swiftType}> {{\n" +
                "    return Set()\n" +
                "}\n";


            var callingCode = new CodeElementCollection <ICodeElement> ();

            var setID   = new CSIdentifier("theSet");
            var setDecl = CSVariableDeclaration.VarLine(new CSSimpleType("SwiftSet", false, new CSSimpleType(cstype)), setID,
                                                        new CSFunctionCall($"TopLevelEntities.MakeSetTR{variant}", false));
            var valID        = new CSIdentifier("theVal");
            var valDecl      = CSVariableDeclaration.VarLine(new CSSimpleType(cstype), valID, new CSIdentifier(val));
            var containsLine = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("theSet.Contains", (CSIdentifier)val));

            var addLine    = CSFunctionCall.FunctionCallLine("theSet.Insert", false, valID);
            var removeLine = CSFunctionCall.FunctionCallLine("theSet.Remove", false, valID);

            callingCode.Add(setDecl);
            callingCode.Add(valDecl);
            callingCode.Add(containsLine);
            callingCode.Add(addLine);
            callingCode.Add(containsLine);
            callingCode.Add(removeLine);
            callingCode.Add(containsLine);

            TestRunning.TestAndExecute(swiftCode, callingCode, "False\nTrue\nFalse\n", testName: $"TestSetRemove{variant}");
        }
        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 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 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 static void TestAndExecuteNoDevice(string swiftCode, CodeElementCollection <ICodeElement> callingCode,
                                                  string expectedOutput, string testName = null,
                                                  PlatformName platform       = PlatformName.None,
                                                  UnicodeMapper unicodeMapper = null)
        {
            SetInvokingTestNameIfUnset(ref testName, out string nameSpace);

            using (var provider = new DisposableTempDirectory()) {
                var compiler = Utils.CompileSwift(swiftCode, provider, nameSpace);

                var libName           = $"lib{nameSpace}.dylib";
                var tempDirectoryPath = Path.Combine(provider.DirectoryPath, "BuildDir");
                Directory.CreateDirectory(tempDirectoryPath);
                File.Copy(Path.Combine(compiler.DirectoryPath, libName), Path.Combine(tempDirectoryPath, libName));

                Utils.CompileToCSharp(provider, tempDirectoryPath, nameSpace, unicodeMapper: unicodeMapper);

                CSFile csFile = TestRunningCodeGenerator.GenerateTestEntry(callingCode, testName, nameSpace, platform);
                csFile.Namespaces.Add(CreateManagedConsoleRedirect());
                CodeWriter.WriteToFile(Path.Combine(tempDirectoryPath, "NameNotImportant.cs"), csFile);

                var sourceFiles = Directory.GetFiles(tempDirectoryPath, "*.cs");
                Compiler.CSCompile(tempDirectoryPath, sourceFiles, "NameNotImportant.exe", platform: platform);

                CopyTestReferencesTo(tempDirectoryPath, platform);

                var output = Execute(tempDirectoryPath, "NameNotImportant.exe", platform);
                Assert.AreEqual(expectedOutput, output);
            }
        }
        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 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 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");
        }
Exemplo n.º 11
0
        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 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 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 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 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");
        }
Exemplo n.º 16
0
        public CSTryCatch(CSCodeBlock tryBlock, params CSCatch [] catchBlocks)
        {
            TryBlock    = tryBlock ?? new CSCodeBlock();
            CatchBlocks = new CodeElementCollection <CSCatch> ();
            CatchBlocks.AddRange(catchBlocks);

            Add(new SimpleElememt("try ", true));
            Add(TryBlock);
            Add(CatchBlocks);
        }
Exemplo n.º 17
0
        public SLDo(SLCodeBlock doBlock, params SLCatch [] catchBlocks)
        {
            DoBlock     = doBlock ?? new SLCodeBlock(null);
            CatchBlocks = new CodeElementCollection <SLCatch> ();
            CatchBlocks.AddRange(catchBlocks);

            Add(new SimpleElememt("do ", true));
            Add(DoBlock);
            Add(CatchBlocks);
        }
Exemplo n.º 18
0
        public void TheEpsilonIssue()
        {
            string swiftCode =
                "public let 𝑒 = 2.718\n";

            var callingCode = new CodeElementCollection <ICodeElement> ();

            callingCode.Add(CSFunctionCall.ConsoleWriteLine((CSIdentifier)"TopLevelEntities.LittleEpsilon"));

            TestRunning.TestAndExecute(swiftCode, callingCode, "2.718\n");
        }
Exemplo n.º 19
0
        public void TopLevelPropertyAllUnicode1()
        {
            string swiftCode =
                "public var v\x3004:Int = 3\n";


            var callingCode = new CodeElementCollection <ICodeElement> ();

            callingCode.Add(CSFunctionCall.ConsoleWriteLine((CSIdentifier)"TopLevelEntities.VU3004"));
            TestRunning.TestAndExecute(swiftCode, callingCode, "3\n");
        }
Exemplo n.º 20
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}");
        }
Exemplo n.º 21
0
        void WrapSingleMethod(string type, string returnVal, string expected)
        {
            string swiftCode = String.Format("public final class MontyWSM{2} {{ class InnerMonty {{\n}}\npublic init() {{}}\n public func val() -> {0} {{ return {1}; }} }}",
                                             type, returnVal, type);


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

            callingCode.Add(CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Ctor($"MontyWSM{type}").Dot(CSFunctionCall.Function("Val"))));

            TestRunning.TestAndExecute(swiftCode, callingCode, expected, testName: $"WrapSingleMethod{type}");
        }
Exemplo n.º 22
0
        public void TopLevelFunctionAllUnicode1()
        {
            string swiftCode =
                "public func f\x3004() -> Int {\n" +
                "    return 3\n" +
                "}\n";


            var callingCode = new CodeElementCollection <ICodeElement> ();

            callingCode.Add(CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("TopLevelEntities.FU3004")));
            TestRunning.TestAndExecute(swiftCode, callingCode, "3\n");
        }
Exemplo n.º 23
0
        /// <summary>コードリストを生成します</summary>
        private void Generate(CodeElementCollection codeList, StringWriter writer)
        {
            string indent = codeList.Indent;

            foreach (ICodeElement element in codeList)
            {
                if (element.PhraseType == PhraseTypes.改行字句)
                {
                    continue;
                }
                Generate(element, writer, indent);
            }
        }
Exemplo n.º 24
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);
        }
Exemplo n.º 25
0
        public void UnicodeInOperatorName()
        {
            string swiftCode =
                "infix operator \x2295\x2295\n" +
                "public func \x2295\x2295 (left:Int, right: Int) -> Int {\n" +
                "    return left + right\n" +
                "}\n";
            var callingCode = new CodeElementCollection <ICodeElement> {
                CSVariableDeclaration.VarLine((CSSimpleType)"nint", "x", CSFunctionCall.Function("TopLevelEntities.InfixOperatorCirclePlusCirclePlus", CSConstant.Val(3), CSConstant.Val(4))),
                CSFunctionCall.ConsoleWriteLine((CSIdentifier)"x")
            };

            TestRunning.TestAndExecute(swiftCode, callingCode, "7\n");
        }
Exemplo n.º 26
0
        void WrapSingleProperty(string type, string returnVal, string reassignVal, string expected)
        {
            string swiftCode = String.Format("public final class MontyWSP{2} {{ public init() {{ }}\npublic var val:{0} = {1}; }}",
                                             type, returnVal, type);

            CodeElementCollection <ICodeElement> callingCode = new CodeElementCollection <ICodeElement> {
                CSAssignment.Assign("var nothing", CSFunctionCall.Ctor($"MontyWSP{type}")),
                CSFunctionCall.ConsoleWriteLine(CSIdentifier.Create("nothing").Dot((CSIdentifier)"Val")),
                CSAssignment.Assign("nothing.Val", new CSConstant(reassignVal)),
                CSFunctionCall.ConsoleWriteLine(CSIdentifier.Create("nothing").Dot((CSIdentifier)"Val"))
            };

            TestRunning.TestAndExecute(swiftCode, callingCode, expected, testName: $"WrapSingleProperty{type}");
        }
Exemplo n.º 27
0
        public void WrapClassReturningClass()
        {
            string classOne  = "public final class GarbleWCRC { public init() { }\npublic func success() { var s = \"\"\n print(\"success\", to:&s)\nwriteToFile(s, \"WrapClassReturningClass\")\n  } }\n";
            string classTwo  = "public final class MontyWCRC { public init() { }\npublic func doIt() -> GarbleWCRC { return GarbleWCRC(); } }";
            string swiftCode = TestRunningCodeGenerator.kSwiftFileWriter + classOne + classTwo;

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

            callingCode.Add(CSAssignment.Assign("var monty", new CSFunctionCall("MontyWCRC", true)));
            callingCode.Add(CSAssignment.Assign("var garb", new CSFunctionCall("monty.DoIt", false)));
            callingCode.Add(CSFunctionCall.FunctionCallLine("garb.Success", false));

            TestRunning.TestAndExecute(swiftCode, callingCode, "success\n");
        }
        public void OperatorSmokeTest2()
        {
            var swiftCode =
                "postfix operator -*\n" +
                "public postfix func -* (arg: Int) -> Int {\n" +
                "    return -1 * arg\n" +
                "}\n";

            var printIt     = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("TopLevelEntities.PostfixOperatorMinusStar", CSConstant.Val(88)));
            var callingCode = new CodeElementCollection <ICodeElement> {
                printIt
            };

            TestRunning.TestAndExecute(swiftCode, callingCode, "-88\n");
        }
        public void OperatorSmokeTest1()
        {
            var swiftCode =
                "prefix operator *-\n" +
                "public prefix func *- (arg: Int) -> Int {\n" +
                "    return -1 * arg\n" +
                "}\n";

            var printIt     = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("TopLevelEntities.PrefixOperatorStarMinus", CSConstant.Val(47)));
            var callingCode = new CodeElementCollection <ICodeElement> {
                printIt
            };

            TestRunning.TestAndExecute(swiftCode, callingCode, "-47\n");
        }
Exemplo n.º 30
0
        public void UnicodeInClassNameAndProperty()
        {
            string swiftCode =
                "public class cd1\x1800 {\n" +
                "   public init() { }\n" +
                "   public var v\x3004:Int = 3\n" +
                "}\n";

            var callingCode = new CodeElementCollection <ICodeElement> {
                CSVariableDeclaration.VarLine((CSSimpleType)"Cd1U1800", "cl", CSFunctionCall.Ctor("Cd1U1800")),
                CSFunctionCall.ConsoleWriteLine((CSIdentifier)"cl.VU3004")
            };

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