Skip to content

Commit

Permalink
Merge pull request #999 from jqno/rewrites-instantiation
Browse files Browse the repository at this point in the history
Rewrites instantiation
  • Loading branch information
jqno committed Sep 24, 2024
2 parents eba0c16 + 2efc52a commit 7f227d0
Show file tree
Hide file tree
Showing 144 changed files with 4,458 additions and 4,409 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- #withPrefabValuesForField method, where you can assign prefab values to a field instead of to a class. The values will then be used for that field only. ([Issue 747](https://github.com/jqno/equalsverifier/issues/747))

### Changed

- The internal instantiation logic has been heavily refactored.

### Deprecated

- `Warning.ZERO_FIELDS`: the use case for this Warning is better handled by the new `#withPrefabValuesForField` method.

## [3.16.2] - 2024-08-23

### Changed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package nl.jqno.equalsverifier.integration.extended_contract;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException;
import org.junit.jupiter.api.Test;

public class PrefabValuesForFieldInRecordTest {

@Test
public void fail_whenRecordHasSinglePrecondition() {
ExpectedException
.when(() -> EqualsVerifier.forClass(SinglePrecondition.class).verify())
.assertFailure()
.assertMessageContains("Record:", "failed to run constructor", "[1]");
}

@Test
public void succeed_whenRecordHasSinglePrecondition_givenPrefabValuesForField() {
EqualsVerifier
.forClass(SinglePrecondition.class)
.withPrefabValuesForField("i", 111, 142)
.verify();
}

@Test
public void fail_whenRecordHasDualPrecondition() {
ExpectedException
.when(() -> EqualsVerifier.forClass(DualPrecondition.class).verify())
.assertFailure()
.assertMessageContains("Record:", "failed to run constructor", "[1, 1]");
}

@Test
public void fail_whenRecordHasDualPrecondition_givenPrefabValuesForOnlyOneField() {
ExpectedException
.when(() ->
EqualsVerifier
.forClass(DualPrecondition.class)
.withPrefabValuesForField("x", 111, 142)
.verify()
)
.assertFailure()
.assertMessageContains("Record:", "failed to run constructor", "[111, 1]");
}

@Test
public void succeed_whenRecordHasDualPrecondition_givenPrefabValueForBothFields() {
EqualsVerifier
.forClass(DualPrecondition.class)
.withPrefabValuesForField("x", 111, 142)
.withPrefabValuesForField("y", 505, 555)
.verify();
}

record SinglePrecondition(int i) {
public SinglePrecondition {
if (i < 100 || i > 200) {
throw new IllegalArgumentException("i must be between 100 and 200! But was " + i);
}
}
}

record DualPrecondition(int x, int y) {
public DualPrecondition {
if (x < 100 || x > 200) {
throw new IllegalArgumentException("x must be between 100 and 200! But was " + x);
}
if (y < 500 || y > 600) {
throw new IllegalArgumentException("y must be between 500 and 600! But was " + y);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ public void fail_whenConstructorChecksValue() {
.assertMessageContains("Record:", "failed to run constructor", "prefab values");
}

@Test
public void succeed_whenConstructorChecksValue_givenPrefabValues() {
EqualsVerifier
.forClass(ValueCheckingRecord.class)
.withPrefabValues(int.class, 10, 11)
.suppress(Warning.ZERO_FIELDS)
.verify();
}

@Test
public void fail_whenRecordInvariantIsViolated_givenIntFieldIsModifiedInConstructor() {
ExpectedException
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nl.jqno.equalsverifier.internal.reflection;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class ClassProbeRecordTest {

@Test
public void isRecord() {
var probe = new ClassProbe<>(SimpleRecord.class);
assertTrue(probe.isRecord());
}

record SimpleRecord(int i) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.objenesis.ObjenesisStd;

public class RecordInstantiatorTest {

@Test
public void instantiateRecord() {
Instantiator<?> instantiator = Instantiator.of(SimpleRecord.class);
Instantiator<?> instantiator = Instantiator.of(SimpleRecord.class, new ObjenesisStd());
Object simpleRecord = instantiator.instantiate();
assertEquals(SimpleRecord.class, simpleRecord.getClass());
}
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7f227d0

Please sign in to comment.