Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dotnet abstract classes should have proxy implementations #241

Merged
merged 4 commits into from
Sep 22, 2018
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,6 @@ jspm_packages/
# that NuGet uses for external packages. We don't want to ignore the
# lerna packages.
!/packages/*

#MacOS metadata
.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
using Amazon.JSII.Generator.Class;
using Amazon.JSII.JsonModel.Spec;
using Xunit;

namespace Amazon.JSII.Generator.UnitTests.Class
{
public class AbstractClassProxyGeneratorTests : GeneratorTestBase
{
private const string Prefix = nameof(Generator) + "." + nameof(AbstractClassProxyGenerator) + ".";

private string Render(ClassType classType)
{
Symbols.MapTypeToPackage("myFqn", classType.Assembly);
Symbols.MapNamespace(classType.QualifiedNamespace, "MyNamespace");
Symbols.MapTypeName("myFqn", "MyClass", TypeKind.Class);

var generator = new AbstractClassProxyGenerator(classType.Assembly, classType, Symbols, Namespaces);

var syntaxTree = generator.CreateSyntaxTree();
return syntaxTree.ToString();
}

[Fact(DisplayName = Prefix + nameof(IncludesAttribute))]
public void IncludesAttribute()
{
var classType = new ClassType
(
"myFqn",
costleya marked this conversation as resolved.
Show resolved Hide resolved
"myPackage",
"myClass",
true,
initializer: new Method(true, false, false)
);

var actual = Render(classType);
var expected =
@"namespace MyNamespace
{
[JsiiTypeProxy(typeof(MyClass), ""myFqn"")]
internal sealed class MyClassProxy : MyClass
{
private MyClassProxy(ByRefValue reference): base(reference)
{
}
}
}";
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}

[Fact(DisplayName = Prefix + nameof(IncludesDocs))]
public void IncludesDocs()
{
// Docs are not currently generated as part of the C# code.
ClassType classType = new ClassType
(
fullyQualifiedName: "myFqn",
assembly: "myPackage",
name: "myClass",
isAbstract: true,
initializer: new Method(true, false, false),
docs: new Docs {{"foo", "bar"}}
);

string actual = Render(classType);
string expected =
@"namespace MyNamespace
{
/// <remarks>foo: bar</remarks>
[JsiiTypeProxy(typeof(MyClass), ""myFqn"")]
internal sealed class MyClassProxy : MyClass
{
private MyClassProxy(ByRefValue reference): base(reference)
{
}
}
}";
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}

[Fact(DisplayName = Prefix + nameof(IncludesProperties))]
public void IncludesProperties()
{
ClassType classType = new ClassType
(
fullyQualifiedName: "myFqn",
assembly: "myPackage",
name: "myClass",
isAbstract: true,
initializer: new Method(true, false, false),
properties: new[]
{
new Property
(
name: "myProp",
type: new TypeReference("myPropTypeFqn"),
isImmutable: false,
isAbstract: true,
isProtected: false
),
new Property
(
name: "notMyProp",
type: new TypeReference("myPropTypeFqn"),
isImmutable: false,
isAbstract: false,
isProtected: false
)
}
);

Symbols.MapTypeName("myPropTypeFqn", "MyPropType", TypeKind.Class);
Symbols.MapPropertyName("myFqn", "myProp", "MyProp");

string actual = Render(classType);
string expected =
@"namespace MyNamespace
{
[JsiiTypeProxy(typeof(MyClass), ""myFqn"")]
internal sealed class MyClassProxy : MyClass
{
private MyClassProxy(ByRefValue reference): base(reference)
{
}

[JsiiProperty(""myProp"", ""{\""fqn\"":\""myPropTypeFqn\""}"")]
public override MyPropType MyProp
{
get => GetInstanceProperty<MyPropType>();
set => SetInstanceProperty(value);
}
}
}";
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}

[Fact(DisplayName = Prefix + nameof(IncludesMethods))]
public void IncludesMethods()
{
ClassType classType = new ClassType
(
fullyQualifiedName: "myFqn",
assembly: "myPackage",
name: "myClass",
isAbstract: true,
initializer: new Method(true, false, false),
methods: new[]
{
new Method
(
false,
false,
true,
name: "myMethod"
),
new Method
(
false,
false,
false,
name: "notMyMethod"
)
}
);

Symbols.MapMethodName("myFqn", "myMethod", "MyMethod");

string actual = Render(classType);
string expected =
@"namespace MyNamespace
{
[JsiiTypeProxy(typeof(MyClass), ""myFqn"")]
internal sealed class MyClassProxy : MyClass
{
private MyClassProxy(ByRefValue reference): base(reference)
{
}

[JsiiMethod(""myMethod"", null, ""[]"")]
public override void MyMethod()
{
InvokeInstanceVoidMethod(new object[]{});
}
}
}";
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using Amazon.JSII.Generator.Class;
using Amazon.JSII.Generator.Interface;
using Amazon.JSII.JsonModel.Spec;
using Microsoft.CodeAnalysis;
using Xunit;
using TypeKind = Amazon.JSII.JsonModel.Spec.TypeKind;

namespace Amazon.JSII.Generator.UnitTests.Class
{
public class ClassGeneratorTests : GeneratorTestBase
{
const string Prefix = nameof(Generator) + "." + nameof(InterfaceGenerator) + ".";
const string Prefix = nameof(Generator) + "." + nameof(ClassGenerator) + ".";

string Render(ClassType classType)
private string Render(ClassType classType)
{
Symbols.MapTypeToPackage("myFqn", classType.Assembly);
Symbols.MapNamespace(classType.QualifiedNamespace, "MyNamespace");
Symbols.MapTypeName("myFqn", "MyClass", JsonModel.Spec.TypeKind.Class);
Symbols.MapTypeName("myFqn", "MyClass", TypeKind.Class);

ClassGenerator generator = new ClassGenerator(classType.Assembly, classType, Symbols, Namespaces);

Expand All @@ -36,7 +36,7 @@ public void IncludesAttribute()

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public class MyClass : DeputyBase
Expand Down Expand Up @@ -71,7 +71,7 @@ public void IncludesAbstractKeyword()

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public abstract class MyClass : DeputyBase
Expand Down Expand Up @@ -103,12 +103,12 @@ public void IncludesDocs()
name: "myClass",
isAbstract: false,
initializer: new Method(true, false, false),
docs: new Docs { { "foo", "bar" } }
docs: new Docs {{"foo", "bar"}}
);

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
/// <remarks>foo: bar</remarks>
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
Expand Down Expand Up @@ -144,7 +144,7 @@ public void IncludesProperties()
{
new Property
(
name: "myProp",
name: "myProp",
type: new TypeReference("myPropTypeFqn"),
isImmutable: false,
isAbstract: false,
Expand All @@ -153,12 +153,12 @@ public void IncludesProperties()
}
);

Symbols.MapTypeName("myPropTypeFqn", "MyPropType", JsonModel.Spec.TypeKind.Class);
Symbols.MapTypeName("myPropTypeFqn", "MyPropType", TypeKind.Class);
Symbols.MapPropertyName("myFqn", "myProp", "MyProp");

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public class MyClass : DeputyBase
Expand Down Expand Up @@ -212,7 +212,7 @@ public void IncludesMethods()

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public class MyClass : DeputyBase
Expand Down Expand Up @@ -252,11 +252,11 @@ public void IncludesBase()
@base: new TypeReference("myBaseTypeFqn")
);

Symbols.MapTypeName("myBaseTypeFqn", "MyBaseType", JsonModel.Spec.TypeKind.Class);
Symbols.MapTypeName("myBaseTypeFqn", "MyBaseType", TypeKind.Class);

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public class MyClass : MyBaseType
Expand Down Expand Up @@ -294,12 +294,12 @@ public void IncludesInterfaces()
}
);

Symbols.MapTypeName("myInterfaceFqn1", "MyInterface1", JsonModel.Spec.TypeKind.Interface);
Symbols.MapTypeName("myInterfaceFqn2", "MyInterface2", JsonModel.Spec.TypeKind.Interface);
Symbols.MapTypeName("myInterfaceFqn1", "MyInterface1", TypeKind.Interface);
Symbols.MapTypeName("myInterfaceFqn2", "MyInterface2", TypeKind.Interface);

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public class MyClass : DeputyBase, IMyInterface1, IMyInterface2
Expand All @@ -320,4 +320,4 @@ protected MyClass(DeputyProps props): base(props)
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}
}
}
}
Loading