3939import java .util .Collection ;
4040import java .util .LinkedList ;
4141import java .util .Map ;
42+ import java .util .function .Function ;
4243import org .hamcrest .BaseMatcher ;
4344import org .hamcrest .Description ;
4445import org .junit .jupiter .api .Assumptions ;
@@ -54,7 +55,12 @@ public final class StoryMatcher extends BaseMatcher<String> {
5455 /**
5556 * The train to start with.
5657 */
57- private final Train <Shift > train = new TrDefault <>();
58+ private final Train <Shift > train ;
59+
60+ /**
61+ * The parser to use, when {@code input} is provided in the YAML.
62+ */
63+ private final Function <String , XML > parser ;
5864
5965 /**
6066 * The header of the match.
@@ -66,16 +72,36 @@ public final class StoryMatcher extends BaseMatcher<String> {
6672 */
6773 private String summary ;
6874
75+ /**
76+ * Ctor.
77+ */
78+ public StoryMatcher () {
79+ this (
80+ input -> {
81+ throw new UnsupportedOperationException (
82+ "Parser is not provided, while YAML doesn't have the 'document' property"
83+ );
84+ }
85+ );
86+ }
87+
88+ /**
89+ * Ctor.
90+ * @param prsr The parser to use
91+ */
92+ public StoryMatcher (final Function <String , XML > prsr ) {
93+ this .parser = prsr ;
94+ this .train = new TrDefault <>();
95+ }
96+
6997 @ Override
7098 @ SuppressWarnings ("unchecked" )
7199 public boolean matches (final Object story ) {
72100 final Map <String , Object > yaml = new Yaml ().load (
73101 String .class .cast (story )
74102 );
75103 Assumptions .assumeTrue (yaml .get ("skip" ) == null );
76- final XML before = new XMLDocument (
77- yaml .get ("document" ).toString ()
78- );
104+ final XML before = this .before (yaml );
79105 final XML after = this .xsline (yaml ).pass (before );
80106 Object asserts = yaml .get ("asserts" );
81107 if (asserts == null ) {
@@ -143,6 +169,22 @@ public void describeMismatch(final Object log, final Description desc) {
143169 desc .appendText ("\n " ).appendText (this .summary );
144170 }
145171
172+ /**
173+ * Build input XML document.
174+ * @param yaml The YAML
175+ * @return The XML
176+ */
177+ private XML before (final Map <String , Object > yaml ) {
178+ final Object doc = yaml .get ("document" );
179+ final XML xml ;
180+ if (doc == null ) {
181+ xml = this .parser .apply (yaml .get ("input" ).toString ());
182+ } else {
183+ xml = new XMLDocument (doc .toString ());
184+ }
185+ return xml ;
186+ }
187+
146188 /**
147189 * Build a transformation line.
148190 * @param yaml The YAML
0 commit comments