|
2 | 2 |
|
3 | 3 | import java.lang.reflect.*; |
4 | 4 | import java.util.*; |
| 5 | +import java.util.function.*; |
5 | 6 |
|
6 | 7 | import org.apiguardian.api.*; |
7 | 8 |
|
|
10 | 11 | import net.jqwik.api.lifecycle.*; |
11 | 12 |
|
12 | 13 | @API(status = API.Status.INTERNAL) |
13 | | -class FootnotesHook implements ResolveParameterHook, AroundTryHook { |
| 14 | +class FootnotesHook implements RegistrarHook { |
14 | 15 |
|
15 | | - @Override |
16 | | - public PropagationMode propagateTo() { |
17 | | - return PropagationMode.ALL_DESCENDANTS; |
| 16 | + private static Store<FootnotesCollector> getFootnotesCollectorStore() { |
| 17 | + return Store.getOrCreate( |
| 18 | + Tuple.of(FootnotesHook.class, "footnotes"), |
| 19 | + Lifespan.TRY, FootnotesCollector::new |
| 20 | + ); |
18 | 21 | } |
19 | 22 |
|
20 | 23 | @Override |
21 | | - public Optional<ParameterSupplier> resolve( |
22 | | - ParameterResolutionContext parameterContext, |
23 | | - LifecycleContext lifecycleContext |
24 | | - ) { |
25 | | - if (parameterContext.typeUsage().isOfType(Footnotes.class)) { |
26 | | - ParameterSupplier footnotesSupplier = optionalTry -> { |
27 | | - Tuple2<String, Store<List<String>>> labelAndStore = optionalTry |
28 | | - .map(tryLifecycleContext -> Tuple.of(tryLifecycleContext.label(), getFootnotesStore())) |
29 | | - .orElseThrow(() -> { |
30 | | - String message = String.format( |
31 | | - "Illegal argument [%s] in method [%s].%n" + |
32 | | - "Objects of type %s can only be injected directly " + |
33 | | - "in property methods or in @BeforeTry and @AfterTry " + |
34 | | - "lifecycle methods.", |
35 | | - parameterContext.parameter(), |
36 | | - parameterContext.optionalMethod().map(Method::toString).orElse("unknown"), |
37 | | - Footnotes.class |
38 | | - ); |
39 | | - return new IllegalArgumentException(message); |
40 | | - }); |
41 | | - |
42 | | - return new StoreBasedFootnotes(labelAndStore); |
43 | | - }; |
44 | | - return Optional.of(footnotesSupplier); |
45 | | - } |
46 | | - return Optional.empty(); |
| 24 | + public void registerHooks(Registrar registrar) { |
| 25 | + registrar.register(FootnotesResolveParameter.class, PropagationMode.ALL_DESCENDANTS); |
| 26 | + registrar.register(FootnotesInnermost.class, PropagationMode.ALL_DESCENDANTS); |
| 27 | + registrar.register(FootnotesOutermost.class, PropagationMode.ALL_DESCENDANTS); |
47 | 28 | } |
48 | 29 |
|
49 | | - private Store<List<String>> getFootnotesStore() { |
50 | | - return Store.getOrCreate( |
51 | | - Tuple.of(FootnotesHook.class, "footnotes"), |
52 | | - Lifespan.TRY, ArrayList::new |
53 | | - ); |
| 30 | + static class FootnotesResolveParameter implements ResolveParameterHook { |
| 31 | + |
| 32 | + @Override |
| 33 | + public Optional<ParameterSupplier> resolve( |
| 34 | + ParameterResolutionContext parameterContext, |
| 35 | + LifecycleContext lifecycleContext |
| 36 | + ) { |
| 37 | + if (parameterContext.typeUsage().isOfType(Footnotes.class)) { |
| 38 | + ParameterSupplier footnotesSupplier = optionalTry -> { |
| 39 | + Tuple2<String, Store<FootnotesCollector>> labelAndStore = |
| 40 | + optionalTry |
| 41 | + .map(tryLifecycleContext -> Tuple.of(tryLifecycleContext.label(), getFootnotesCollectorStore())) |
| 42 | + .orElseThrow(() -> { |
| 43 | + String message = String.format( |
| 44 | + "Illegal argument [%s] in method [%s].%n" + |
| 45 | + "Objects of type %s can only be injected directly " + |
| 46 | + "in property methods or in @BeforeTry and @AfterTry " + |
| 47 | + "lifecycle methods.", |
| 48 | + parameterContext.parameter(), |
| 49 | + parameterContext.optionalMethod() |
| 50 | + .map(Method::toString) |
| 51 | + .orElse("unknown"), |
| 52 | + Footnotes.class |
| 53 | + ); |
| 54 | + return new IllegalArgumentException(message); |
| 55 | + }); |
| 56 | + |
| 57 | + return new StoreBasedFootnotes(labelAndStore); |
| 58 | + }; |
| 59 | + return Optional.of(footnotesSupplier); |
| 60 | + } |
| 61 | + return Optional.empty(); |
| 62 | + } |
54 | 63 | } |
55 | 64 |
|
56 | | - @Override |
57 | | - public TryExecutionResult aroundTry(TryLifecycleContext context, TryExecutor aTry, List<Object> parameters) { |
58 | | - TryExecutionResult executionResult = aTry.execute(parameters); |
59 | | - if (executionResult.isFalsified()) { |
60 | | - List<String> footnotes = getFootnotes(); |
61 | | - return executionResult.withFootnotes(footnotes); |
| 65 | + static class FootnotesOutermost implements AroundTryHook { |
| 66 | + |
| 67 | + @Override |
| 68 | + public TryExecutionResult aroundTry(TryLifecycleContext context, TryExecutor aTry, List<Object> parameters) { |
| 69 | + TryExecutionResult executionResult = aTry.execute(parameters); |
| 70 | + if (executionResult.isFalsified()) { |
| 71 | + getFootnotesCollectorStore().get().evaluateOutermost(); |
| 72 | + return executionResult.withFootnotes(getFootnotes()); |
| 73 | + } |
| 74 | + return executionResult; |
| 75 | + } |
| 76 | + |
| 77 | + private List<String> getFootnotes() { |
| 78 | + return getFootnotesCollectorStore().get().getFootnotes(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public int aroundTryProximity() { |
| 83 | + // Outside lifecycle methods |
| 84 | + return -20; |
62 | 85 | } |
63 | | - return executionResult; |
64 | 86 | } |
65 | 87 |
|
66 | | - @Override |
67 | | - public int aroundTryProximity() { |
68 | | - // Outside lifecycle methods |
69 | | - return -20; |
| 88 | + static class FootnotesInnermost implements AroundTryHook { |
| 89 | + |
| 90 | + @Override |
| 91 | + public TryExecutionResult aroundTry(TryLifecycleContext context, TryExecutor aTry, List<Object> parameters) { |
| 92 | + TryExecutionResult executionResult = aTry.execute(parameters); |
| 93 | + if (executionResult.isFalsified()) { |
| 94 | + getFootnotesCollectorStore().get().evaluateInnermost(); |
| 95 | + } |
| 96 | + return executionResult; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public int aroundTryProximity() { |
| 101 | + // Mostly first thing after property method execution |
| 102 | + return 99; |
| 103 | + } |
70 | 104 | } |
71 | 105 |
|
72 | | - private List<String> getFootnotes() { |
73 | | - return getFootnotesStore().get(); |
| 106 | + private static class FootnotesCollector { |
| 107 | + |
| 108 | + private final List<String> footnotes = new ArrayList<>(); |
| 109 | + private final List<String> collectedFootnotes = new ArrayList<>(); |
| 110 | + private final List<Supplier<String>> collectedSuppliers = new ArrayList<>(); |
| 111 | + |
| 112 | + private List<String> getFootnotes() { |
| 113 | + return footnotes; |
| 114 | + } |
| 115 | + |
| 116 | + private void addFootnote(String footnote) { |
| 117 | + collectedFootnotes.add(footnote); |
| 118 | + } |
| 119 | + |
| 120 | + private void addAfterFailure(Supplier<String> footnoteSupplier) { |
| 121 | + collectedSuppliers.add(footnoteSupplier); |
| 122 | + } |
| 123 | + |
| 124 | + private void evaluateInnermost() { |
| 125 | + for (Supplier<String> footnoteSupplier : collectedSuppliers) { |
| 126 | + footnotes.add(footnoteSupplier.get()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private void evaluateOutermost() { |
| 131 | + for (String footnote : collectedFootnotes) { |
| 132 | + footnotes.add(footnote); |
| 133 | + } |
| 134 | + } |
74 | 135 | } |
75 | 136 |
|
76 | 137 | private static class StoreBasedFootnotes implements Footnotes { |
77 | 138 |
|
78 | 139 | private final String label; |
79 | | - private final Store<List<String>> store; |
| 140 | + private final Store<FootnotesCollector> store; |
80 | 141 |
|
81 | | - public StoreBasedFootnotes(Tuple2<String, Store<List<String>>> labelAndStore) { |
| 142 | + private StoreBasedFootnotes(Tuple2<String, Store<FootnotesCollector>> labelAndStore) { |
82 | 143 | this.label = labelAndStore.get1(); |
83 | 144 | this.store = labelAndStore.get2(); |
84 | 145 | } |
85 | 146 |
|
86 | 147 | @Override |
87 | 148 | public void addFootnote(String footnote) { |
88 | | - store.get().add(footnote); |
| 149 | + store.get().addFootnote(footnote); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void addAfterFailure(Supplier<String> footnoteSupplier) { |
| 154 | + store.get().addAfterFailure(footnoteSupplier); |
89 | 155 | } |
90 | 156 |
|
91 | 157 | @Override |
|
0 commit comments