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() {
  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());
  40. }
  41. }
  42.  
  43. class B extends A {
  44.  
  45. String description() {
  46. return "B";
  47. }
  48. }
  49.  
  50. class Ideone {
  51.  
  52. public static void main(String[] args) {
  53. new ASpec().runAll(List.of(A::new, B::new));
  54. }
  55. }
Success #stdin #stdout 0.07s 33796KB
stdin
Standard input is empty
stdout
Standard output is empty