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 getClass().getSimpleName();
  20. }
  21. }
  22.  
  23. class ASpec<T extends A> {
  24.  
  25. final void runAll(Collection<Supplier<? extends T>> implementations) {
  26. implementations.forEach(this::run);
  27. }
  28.  
  29. void run(Supplier<? extends T> a) {
  30. should_be_A_object_description_when_x_is_42(a.get());
  31. }
  32.  
  33. String descriptionOf(A a) {
  34. return a.getClass().getSimpleName();
  35. }
  36.  
  37. void should_be_A_object_description_when_x_is_42(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. class BSpec<T extends B> extends ASpec<B> {
  51.  
  52. void run(Supplier<? extends B> b) {
  53. super.run(b);
  54. should_be_B_object_description(b.get());
  55. }
  56.  
  57. String bDescriptionOf(B b) {
  58. return "B";
  59. }
  60.  
  61. void should_be_B_object_description(B b) {
  62. final var expected = bDescriptionOf(b);
  63. Should.beEqual(expected, b.description());
  64. }
  65. }
  66.  
  67. final class Ideone {
  68.  
  69. public static void main(String[] args) {
  70. new ASpec<>().runAll(List.of(A::new, B::new));
  71. new BSpec<>().runAll(List.of(B::new));
  72. }
  73. }
Success #stdin #stdout 0.07s 33812KB
stdin
Standard input is empty
stdout
Standard output is empty