public void Should_Throw_If_Method_Is_Null() { // Given, When var result = Record.Exception(() => PropertyAliasGenerator.Generate(null)); // Then Assert.IsArgumentNullException(result, "method"); }
public void Should_Throw_If_Method_Is_Null() { // Given, When var result = Record.Exception(() => PropertyAliasGenerator.Generate(null)); // Then Assert.IsType <ArgumentNullException>(result); Assert.Equal("method", ((ArgumentNullException)result).ParamName); }
public void Should_Throw_If_Declaring_Type_Is_Not_Static() { // Given var method = GetType().GetMethod("Should_Throw_If_Declaring_Type_Is_Not_Static"); // When var result = Record.Exception(() => PropertyAliasGenerator.Generate(method)); // Then Assert.IsType <CakeException>(result); Assert.Equal("The type 'Cake.Core.Tests.Unit.Scripting.CodeGen.PropertyAliasGeneratorTests+TheGenerateMethod' is not static.", result.Message); }
private static string GetAliasCode(Script context) { var result = new List <string>(); foreach (var alias in context.Aliases) { result.Add(alias.Type == ScriptAliasType.Method ? MethodAliasGenerator.Generate(alias.Method) : PropertyAliasGenerator.Generate(alias.Method)); } return(string.Join("\r\n", result)); }
public void Should_Throw_If_Method_Is_Generic() { // Given var method = typeof(PropertyAliasGeneratorFixture).GetMethod("GenericScriptMethod"); // When var result = Record.Exception(() => PropertyAliasGenerator.Generate(method)); // Then Assert.IsType <CakeException>(result); Assert.Equal("The property alias 'GenericScriptMethod' cannot be generic.", result.Message); }
public void Should_Throw_If_Property_Alias_Do_Not_Have_A_Cake_Context_As_First_Parameter() { // Given var method = typeof(PropertyAliasGeneratorFixture).GetMethod("PropertyAliasWithoutContext"); // When var result = Record.Exception(() => PropertyAliasGenerator.Generate(method)); // Then Assert.IsType <CakeException>(result); Assert.Equal("The property alias 'PropertyAliasWithoutContext' has an invalid signature.", result.Message); }
public void Should_Throw_If_Property_Alias_Have_More_Than_One_Argument() { // Given var method = typeof(PropertyAliasGeneratorFixture).GetMethod("PropertyAliasWithMoreThanOneMethod"); // When var result = Record.Exception(() => PropertyAliasGenerator.Generate(method)); // Then Assert.IsType <CakeException>(result); Assert.Equal("The property alias 'PropertyAliasWithMoreThanOneMethod' has an invalid signature.", result.Message); }
public void Should_Throw_If_Method_Is_Not_An_Cake_Property_Alias() { // Given var method = typeof(PropertyAliasGeneratorFixture).GetMethod("NotAScriptMethod"); // When var result = Record.Exception(() => PropertyAliasGenerator.Generate(method)); // Then Assert.IsType <CakeException>(result); Assert.Equal("The method 'NotAScriptMethod' is not a property alias.", result.Message); }
public void Should_Throw_If_Property_Alias_Returns_Void() { // Given var method = typeof(PropertyAliasGeneratorFixture).GetMethod("PropertyAliasReturningVoid"); // When var result = Record.Exception(() => PropertyAliasGenerator.Generate(method)); // Then Assert.IsType <CakeException>(result); Assert.Equal("The property alias 'PropertyAliasReturningVoid' cannot return void.", result.Message); }
public void Should_Generate_Correct_Code_For_Valid_Property_Alias() { // Given const string expected = "public System.Int32 PropertyAliasReturningInteger{get{return " + "Cake.Core.Tests.Fixtures.PropertyAliasGeneratorFixture.PropertyAliasReturningInteger(Context);}}"; var method = typeof(PropertyAliasGeneratorFixture).GetMethod("PropertyAliasReturningInteger"); // When var result = PropertyAliasGenerator.Generate(method); // Then Assert.Equal(expected, result); }
private static string GenerateCode(MethodInfo method) { string code; if (method.IsDefined(typeof(CakeMethodAliasAttribute))) { code = MethodAliasGenerator.Generate(method); } else if (method.IsDefined(typeof(CakePropertyAliasAttribute))) { code = PropertyAliasGenerator.Generate(method); } else { throw new InvalidOperationException("Unknown alias type."); } return(code); }
public void Should_Generate_Cached_Code_For_Cached_Property_Alias_Returning_Value_Type() { // Given var expected = new StringBuilder(); expected.Append("private System.Boolean? _PropertyAliasReturningCachedBoolean;\n"); expected.Append("public System.Boolean PropertyAliasReturningCachedBoolean{get{"); expected.Append("if(_PropertyAliasReturningCachedBoolean==null){_PropertyAliasReturningCachedBoolean="); expected.Append("Cake.Core.Tests.Fixtures.PropertyAliasGeneratorFixture.PropertyAliasReturningCachedBoolean"); expected.Append("(Context);}return _PropertyAliasReturningCachedBoolean.Value;}}"); var method = typeof(PropertyAliasGeneratorFixture).GetMethod("PropertyAliasReturningCachedBoolean"); // When var result = PropertyAliasGenerator.Generate(method); // Then Assert.Equal(expected.ToString(), result); }
private static string GetAliasCode(Script context) { var result = new Dictionary <string, string>(); foreach (var alias in context.Aliases) { string hash, code = alias.Type == ScriptAliasType.Method ? MethodAliasGenerator.Generate(alias.Method, out hash) : PropertyAliasGenerator.Generate(alias.Method, out hash); string @namespace = alias.Method.DeclaringType.Namespace ?? "@null"; if (result.ContainsKey(hash)) { var message = $"{alias.Type.ToString().ToLowerInvariant()} \"{alias.Name}\" excluded from code generation and will need to be fully qualified on ICakeContext."; if (context.ExcludedNamespaces.ContainsKey(@namespace)) { context.ExcludedNamespaces[@namespace].Add(message); } else { context.ExcludedNamespaces.Add(@namespace, new List <string>() { message }); } continue; } else if (context.ExcludedNamespaces.ContainsKey(@namespace)) { var message = $"{alias.Type.ToString().ToLowerInvariant()} \"{alias.Name}\" was included in code generation, but will need to be fully qualified on ICakeContext."; context.ExcludedNamespaces[@namespace].Add(message); } result.Add(hash, code); } return(string.Join("\r\n", result.Values)); }
public string Generate(string name) { var method = _methods.SingleOrDefault(x => x.Name == name); return(PropertyAliasGenerator.Generate(method).NormalizeGeneratedCode()); }