Skip to content

Commit c2da606

Browse files
committed
#48 with parser
1 parent 5c7509a commit c2da606

File tree

8 files changed

+100
-208
lines changed

8 files changed

+100
-208
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@ and `@ClasspathSource` from [Jucs](https://github.com/objectionary/jucs)):
6060

6161
```java
6262
import org.eolang.jucs.ClasspathSource;
63-
import org.eolang.xax.XaxStory;
6463
import org.hamcrest.MatcherAssert;
6564
import org.hamcrest.Matchers;
6665
import org.junit.jupiter.params.ParameterizedTest;
67-
import org.junit.jupiter.api.Test;
6866
6967
final class MyTest {
7068
@ParameterizedTest

pom.xml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ SOFTWARE.
9797
<artifactId>xsline</artifactId>
9898
<version>0.22.1</version>
9999
</dependency>
100-
<dependency>
101-
<groupId>org.cactoos</groupId>
102-
<artifactId>cactoos</artifactId>
103-
<version>0.56.1</version>
104-
</dependency>
105100
<dependency>
106101
<groupId>org.junit.jupiter</groupId>
107102
<artifactId>junit-jupiter-api</artifactId>
@@ -120,12 +115,23 @@ SOFTWARE.
120115
<version>12.5</version>
121116
<scope>runtime</scope>
122117
</dependency>
118+
<dependency>
119+
<groupId>org.cactoos</groupId>
120+
<artifactId>cactoos</artifactId>
121+
<version>0.56.1</version>
122+
<scope>test</scope>
123+
</dependency>
123124
<dependency>
124125
<groupId>org.eolang</groupId>
125126
<artifactId>jucs</artifactId>
126127
<version>0.2.0</version>
127128
<scope>test</scope>
128129
</dependency>
130+
<dependency>
131+
<groupId>org.eolang</groupId>
132+
<artifactId>eo-parser</artifactId>
133+
<version>0.46.0</version>
134+
</dependency>
129135
</dependencies>
130136
<profiles>
131137
<profile>

src/main/java/org/eolang/xax/StoryMatcher.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.Collection;
4040
import java.util.LinkedList;
4141
import java.util.Map;
42+
import java.util.function.Function;
4243
import org.hamcrest.BaseMatcher;
4344
import org.hamcrest.Description;
4445
import 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

src/main/java/org/eolang/xax/XaxStory.java

Lines changed: 0 additions & 194 deletions
This file was deleted.

src/test/java/org/eolang/xax/StoryMatcherTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
*/
2424
package org.eolang.xax;
2525

26+
import java.io.IOException;
27+
import org.cactoos.io.InputOf;
2628
import org.cactoos.io.ResourceOf;
2729
import org.cactoos.text.TextOf;
2830
import org.cactoos.text.UncheckedText;
2931
import org.eolang.jucs.ClasspathSource;
32+
import org.eolang.parser.EoSyntax;
3033
import org.hamcrest.MatcherAssert;
3134
import org.hamcrest.Matchers;
3235
import org.junit.jupiter.api.Test;
@@ -60,7 +63,15 @@ void validatesSimpleScenario(final String yaml) {
6063
MatcherAssert.assertThat(
6164
"passes with no exceptions",
6265
yaml,
63-
new StoryMatcher()
66+
new StoryMatcher(
67+
eo -> {
68+
try {
69+
return new EoSyntax(new InputOf(eo)).parsed();
70+
} catch (final IOException ex) {
71+
throw new IllegalArgumentException(ex);
72+
}
73+
}
74+
)
6475
);
6576
}
6677

src/test/resources/org/eolang/xax/broken/bad-simple.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
---
2323
sheets:
2424
- /org/eolang/xax/simple.xsl
25-
document:
25+
document: |
2626
<doc>
2727
<a>
2828
<foo>hello</foo>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2022-2024 Yegor Bugayenko
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included
13+
# in all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
---
23+
input: |
24+
# No comments.
25+
[] > foo
26+
QQ.io.stdout > @
27+
"Hello, world!"
28+
asserts:
29+
- /program/objects/o

0 commit comments

Comments
 (0)