Skip to content

Commit

Permalink
chore: add Java test on autoprops from private constructor (#262)
Browse files Browse the repository at this point in the history
Add a test for Java, and update the .NET test to use the static constructor as well.
  • Loading branch information
rix0rrr authored Oct 16, 2018
1 parent bf1f586 commit a7e0566
Show file tree
Hide file tree
Showing 10 changed files with 805 additions and 738 deletions.
4 changes: 4 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,10 @@ export class DoNotOverridePrivates {
* Class that implements interface properties automatically, but using a private constructor
*/
export class ClassWithPrivateConstructorAndAutomaticProperties implements IInterfaceWithProperties {
public static create(readOnlyString: string, readWriteString: string) {
return new ClassWithPrivateConstructorAndAutomaticProperties(readOnlyString, readWriteString);
}

private constructor(public readonly readOnlyString: string, public readWriteString: string) {
}
}
25 changes: 24 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,29 @@
}
],
"kind": "class",
"methods": [
{
"name": "create",
"parameters": [
{
"name": "readOnlyString",
"type": {
"primitive": "string"
}
},
{
"name": "readWriteString",
"type": {
"primitive": "string"
}
}
],
"returns": {
"fqn": "jsii-calc.ClassWithPrivateConstructorAndAutomaticProperties"
},
"static": true
}
],
"name": "ClassWithPrivateConstructorAndAutomaticProperties",
"properties": [
{
Expand Down Expand Up @@ -3332,5 +3355,5 @@
}
},
"version": "0.7.7",
"fingerprint": "SMX3FQtN8x0lUjxLt3hL50eC+4BhUchNcGPOAxkO9Xw="
"fingerprint": "16f4wL/B1M7rOOzyAzBEtqlOi2GYhDAU5rctonoha5Y="
}
Original file line number Diff line number Diff line change
Expand Up @@ -811,22 +811,11 @@ public void ReturnAbstract()
[Fact(DisplayName = Prefix + nameof(TestClassWithPrivateConstructorAndAutomaticProperties))]
public void TestClassWithPrivateConstructorAndAutomaticProperties()
{
var testType = typeof(ClassWithPrivateConstructorAndAutomaticProperties);
var obj = ClassWithPrivateConstructorAndAutomaticProperties.Create("Hello", "Bye");
Assert.Equal("Bye", obj.ReadWriteString);
obj.ReadWriteString = "Hello";

// No public constructors
Assert.Equal(0, testType.GetConstructors().Select(c => c.IsPublic).Count());

var readOnlyProperty =
testType.GetProperty(nameof(ClassWithPrivateConstructorAndAutomaticProperties.ReadOnlyString));

Assert.True(readOnlyProperty.CanRead);
Assert.False(readOnlyProperty.CanWrite);

var readWriteProperty =
testType.GetProperty(nameof(ClassWithPrivateConstructorAndAutomaticProperties.ReadWriteString));

Assert.True(readWriteProperty.CanRead);
Assert.True(readWriteProperty.CanWrite);
Assert.Equal("Hello", obj.ReadOnlyString);
}

class MulTen : Multiply
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import software.amazon.jsii.tests.calculator.lib.StructWithOnlyOptionals;
import software.amazon.jsii.tests.calculator.lib.Value;
import software.amazon.jsii.tests.calculator.JavaReservedWords;
import software.amazon.jsii.tests.calculator.ClassWithPrivateConstructorAndAutomaticProperties;
import org.junit.Test;

import java.io.IOException;
Expand Down Expand Up @@ -918,6 +919,14 @@ public void setPrivateProperty(String value) {
assertEquals("MyNewValue", obj.privatePropertyValue());
}

@Test
public void classWithPrivateConstructorAndAutomaticProperties() {
ClassWithPrivateConstructorAndAutomaticProperties obj = ClassWithPrivateConstructorAndAutomaticProperties.create("Hello", "Bye");
assertEquals("Bye", obj.getReadWriteString());
obj.setReadWriteString("Hello");
assertEquals("Hello", obj.getReadOnlyString());
}

static class MulTen extends Multiply {
public MulTen(final int value) {
super(new Number(value), new Number(10));
Expand Down
Loading

0 comments on commit a7e0566

Please sign in to comment.