void TLExceptionSimple(string toAdd, string output) { string swiftCode = $"public enum MyErrorTLES{toAdd} : Error {{\ncase itFailed\n}}\n" + $"public func throwItTLES{toAdd}(doThrow: Bool) throws -> Int\n {{\n if doThrow {{\n throw MyErrorTLES{toAdd}.itFailed\n}}\nelse {{\n return 5\n}}\n}}\n"; CodeElementCollection <ICodeElement> callingCode = new CodeElementCollection <ICodeElement> (); CSCodeBlock tryBlock = new CSCodeBlock(); CSLine call = CSFunctionCall.FunctionCallLine("Console.Write", false, new CSFunctionCall($"TopLevelEntities.ThrowItTLES{toAdd}", false, new CSIdentifier(toAdd))); tryBlock.Add(call); CSCodeBlock catchBlock = new CSCodeBlock(); catchBlock.Add(CSFunctionCall.FunctionCallLine("Console.Write", false, CSConstant.Val("exception thrown"))); CSTryCatch catcher = new CSTryCatch(tryBlock, typeof(SwiftException), null, catchBlock); callingCode.Add(catcher); TestRunning.TestAndExecute(swiftCode, callingCode, output, testName: $"TLExceptionSimple{toAdd}"); }
void VirtualMethodException(string toAdd, string output) { string swiftCode = $"public enum MyErrorVME{toAdd} : Error {{\ncase itFailed\n}}\n" + $"open class FooVME{toAdd} {{\npublic init() {{\n}}\n public final func doIt(doThrow:Bool) throws {{\n let _ = try throwIt(doThrow:doThrow)\n}}\n open func throwIt(doThrow: Bool) throws -> Int\n {{\n if doThrow {{\n throw MyErrorVME{toAdd}.itFailed\n}}\nelse {{\n return 5\n}}\n}}\n}}\n"; CodeElementCollection <ICodeElement> callingCode = new CodeElementCollection <ICodeElement> (); CSClass fooOver = new CSClass(CSVisibility.Public, $"SubFooVME{toAdd}"); fooOver.Inheritance.Add(new CSIdentifier($"FooVME{toAdd}")); CSCodeBlock overBody = new CSCodeBlock(); CSCodeBlock ifblock = new CSCodeBlock(); CSCodeBlock elseblock = new CSCodeBlock(); ifblock.Add(new CSLine(new CSThrow(new CSFunctionCall("ArgumentException", true, CSConstant.Val("gotcha."))))); elseblock.Add(CSReturn.ReturnLine(CSConstant.Val(6))); CSIfElse ifElse = new CSIfElse(new CSIdentifier("doThrow"), ifblock, elseblock); overBody.Add(ifElse); CSMethod myThrowIt = new CSMethod(CSVisibility.Public, CSMethodKind.Override, new CSSimpleType("nint"), new CSIdentifier("ThrowIt"), new CSParameterList(new CSParameter(CSSimpleType.Bool, new CSIdentifier("doThrow"))), overBody); fooOver.Methods.Add(myThrowIt); CSCodeBlock tryBlock = new CSCodeBlock(); CSLine varLine = CSVariableDeclaration.VarLine(new CSSimpleType($"SubFooVME{toAdd}"), "foo", new CSFunctionCall($"SubFooVME{toAdd}", true)); tryBlock.Add(varLine); CSLine call = CSFunctionCall.FunctionCallLine("foo.DoIt", false, new CSIdentifier(toAdd)); tryBlock.Add(call); CSCodeBlock catchBlock = new CSCodeBlock(); catchBlock.Add(CSFunctionCall.FunctionCallLine("Console.Write", false, new CSIdentifier("e").Dot(new CSIdentifier("Message")))); CSTryCatch catcher = new CSTryCatch(tryBlock, typeof(SwiftException), "e", catchBlock); callingCode.Add(catcher); TestRunning.TestAndExecute(swiftCode, callingCode, output, testName: $"VirtualMethodException{toAdd}", otherClass: fooOver, platform: PlatformName.macOS); }
void VirtualMethodExceptionSimple(string toAdd, string output) { string swiftCode = $"public enum MyErrorVMES{toAdd} : Error {{\ncase itFailed\n}}\n" + $"open class FooVMES{toAdd} {{\npublic init() {{\n}}\n open func throwIt(doThrow: Bool) throws -> Int\n {{\n if doThrow {{\n throw MyErrorVMES{toAdd}.itFailed\n}}\nelse {{\n return 5\n}}\n}}\n}}\n"; CodeElementCollection <ICodeElement> callingCode = new CodeElementCollection <ICodeElement> (); CSCodeBlock tryBlock = new CSCodeBlock(); CSLine varLine = CSVariableDeclaration.VarLine(new CSSimpleType($"FooVMES{toAdd}"), "foo", new CSFunctionCall($"FooVMES{toAdd}", true)); tryBlock.Add(varLine); CSLine call = CSFunctionCall.FunctionCallLine("Console.Write", false, new CSFunctionCall("foo.ThrowIt", false, new CSIdentifier(toAdd))); tryBlock.Add(call); CSCodeBlock catchBlock = new CSCodeBlock(); catchBlock.Add(CSFunctionCall.FunctionCallLine("Console.Write", false, new CSIdentifier("e").Dot(new CSIdentifier("Message")))); CSTryCatch catcher = new CSTryCatch(tryBlock, typeof(SwiftException), "e", catchBlock); callingCode.Add(catcher); TestRunning.TestAndExecute(swiftCode, callingCode, output, testName: $"VirtualMethodExceptionSimple{toAdd}", platform: PlatformName.macOS); }