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 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 TestCharacterReturns() { var swiftCode = @" public class CharacterEcho { var C : Character; public init (c : Character) { C = c } public func GetValue () -> Character { return C } public var Value : Character { get { return C; } } }"; var callingCode = new CodeElementCollection <ICodeElement> (); StringBuilder expected = new StringBuilder(); foreach (string c in TestCases) { CSIdentifier testIdentifier = (CSIdentifier)$@"""{c}"""; CSCodeBlock block = new CSCodeBlock(); var ctorParam = new CSCastExpression((CSSimpleType)"SwiftCharacter", testIdentifier); CSLine instance = CSVariableDeclaration.VarLine((CSSimpleType)"CharacterEcho", (CSIdentifier)"Foo", CSFunctionCall.Ctor("CharacterEcho", ctorParam)); // First the properties var explicitCastProp = (CSIdentifier)"(string)Foo.Value"; var toStringProp = (CSIdentifier)"Foo.Value.ToString ()"; // Then the function returns as well var explicitCastFun = (CSIdentifier)"(string)Foo.GetValue ()"; var toStringFun = (CSIdentifier)"Foo.GetValue ().ToString ()"; block.Add(instance); foreach (var call in new CSBaseExpression [] { explicitCastProp, toStringProp, explicitCastFun, toStringFun }) { CSLine print = CSFunctionCall.FunctionLine("Console.Write", call); block.Add(print); expected.Append(c); } callingCode.Add(block); } TestRunning.TestAndExecute(swiftCode, callingCode, expected.ToString()); }