Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions tests/generator-Tests/Unit-Tests/CodeGeneratorContextTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Linq;
using MonoDroid.Generation;
using NUnit.Framework;

namespace generatortests
{
[TestFixture]
public class CodeGeneratorContextTests
{
[Test]
public void GetContextTypeMember ()
{
var klass = new TestClass ("Object", "java.code.MyClass");

klass.AddMethod (SupportTypeBuilder.CreateMethod (klass, "Echo", new CodeGenerationOptions (), "uint", false, false, new Parameter ("value", "uint", "uint", false)));
klass.AddField (new TestField ("string", "Foo"));

var context = new CodeGeneratorContext ();
context.ContextTypes.Push (klass);

Assert.AreEqual ("java.code.MyClass", context.GetContextTypeMember ());

context.ContextMethod = klass.Methods.Single ();

Assert.AreEqual ("java.code.MyClass.Echo (uint)", context.GetContextTypeMember ());

context.ContextMethod = null;
context.ContextField = klass.Fields.Single ();

Assert.AreEqual ("java.code.MyClass.Foo", context.GetContextTypeMember ());

context.ContextMethod = klass.Methods.Single ();
context.ContextField = null;
context.ContextTypes.Clear ();

Assert.AreEqual ("Echo (uint)", context.GetContextTypeMember ());
}
}
}
17 changes: 11 additions & 6 deletions tools/generator/CodeGeneratorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ public class CodeGeneratorContext

public string GetContextTypeMember ()
{
var output = ContextType?.FullName ?? string.Empty;
var parts = new List<string> ();

if (ContextMethod != null) {
output += $"{ContextMethod.Name} ({string.Join (", ", ContextMethod?.Parameters.Select (p => p.InternalType).ToArray ())})";
return output;
}
// Type name
if (!string.IsNullOrEmpty (ContextType?.FullName))
parts.Add (ContextType?.FullName);

return output + ContextField?.Name;
// Member name
if (ContextMethod != null)
parts.Add ($"{ContextMethod.Name} ({string.Join (", ", ContextMethod?.Parameters.Select (p => p.InternalType).ToArray ())})");
else if (ContextField != null)
parts.Add (ContextField.Name);

return string.Join (".", parts);
}
}
}