fork download
  1. import java.util.Collection;
  2. import java.util.List;
  3. import java.util.function.Supplier;
  4.  
  5. class Should {
  6.  
  7. static void beEqual(Object expected, Object given) {
  8. if (!expected.equals(given)) {
  9. throw new RuntimeException("equality failed. expected " + expected + ", but given " + given);
  10. }
  11. }
  12. }
  13.  
  14. // ----------------------------------------------------------------
  15.  
  16. class A {
  17.  
  18. String description(int x) {
  19. return "A";
  20. }
  21. }
  22.  
  23. class ASpec {
  24.  
  25. void runAll(Collection<Supplier<? extends A>> implementations) {
  26. implementations.forEach(this::run);
  27. }
  28.  
  29. void run(Supplier<? extends A> impl) {
  30. should_be_A_object_description(impl.get());
  31. }
  32.  
  33. private String descriptionOf(A a) {
  34. return a.getClass().getSimpleName();
  35. }
  36.  
  37. void should_be_A_object_description(A a) {
  38. final var expected = descriptionOf(a);
  39. Should.beEqual(expected, a.description(42));
  40. }
  41. }
  42.  
  43. class B extends A {
  44.  
  45. String description() {
  46. return "B";
  47. }
  48. }
  49.  
  50. final class Ideone {
  51.  
  52. public static void main(String[] args) {
  53. new ASpec().runAll(List.of(A::new, B::new));
  54. }
  55. }
  56.  
Runtime error #stdin #stdout #stderr 0.11s 36520KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.RuntimeException: equality failed. expected B, but given A
	at Should.beEqual(Main.java:9)
	at ASpec.should_be_A_object_description(Main.java:39)
	at ASpec.run(Main.java:30)
	at java.base/java.lang.Iterable.forEach(Iterable.java:75)
	at ASpec.runAll(Main.java:26)
	at Ideone.main(Main.java:53)