Skip to content

Commit

Permalink
objectionary#3267: Raw implementation of Xmir2Xmir
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky committed Aug 6, 2024
1 parent e5d92cd commit dcbc51c
Show file tree
Hide file tree
Showing 4 changed files with 409 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ void checksPacks(final String pack) throws IOException {
);
}

@ParameterizedTest
@ClasspathSource(value = "org/eolang/maven/xmir2xmir/", glob = "**.yaml")
void checksXmirToXmir(final String pack, @TempDir final Path dir) throws Exception {
MatcherAssert.assertThat(
"Xmir2xmir tests passes",
new Xmir2Xmir(pack, dir),
Matchers.equalTo(true)
);
}

@Test
void skipsAlreadyOptimized(@TempDir final Path temp) throws IOException {
final FakeMaven maven = new FakeMaven(temp)
Expand Down
167 changes: 167 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/Xmir2Xmir.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;

import com.jcabi.xml.XMLDocument;
import com.yegor256.Jaxec;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Map;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.TrClasspath;
import com.yegor256.xsline.Xsline;
import org.cactoos.Scalar;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.Unchecked;
import org.cactoos.text.TextOf;
import org.eolang.maven.util.HmBase;
import org.eolang.maven.util.Home;
import org.yaml.snakeyaml.Yaml;

/**
* Test scenario in YAML.
*
* @since 0.1
*/
@SuppressWarnings({"JTCOP.RuleAllTestsHaveProductionClass", "JTCOP.RuleCorrectTestName"})
public final class Xmir2Xmir {

/**
* Expected content of xml.
*/
private final Unchecked<String> expected;

/**
* Content of xml after transformations.
*/
private final Unchecked<String> processed;

/**
* Ctor.
* @param pack Content of .yaml test.
* @param home Where to save files.
*/
public Xmir2Xmir(final String pack, final Path home) {
this(
new Sticky<>(() -> new Yaml().load(pack)),
new HmBase(home)
);
}

/**
* Ctor.
* @param yaml Parsed yaml.
* @param home Home to save.
*/
private Xmir2Xmir(final Scalar<Map<String, Object>> yaml, final Home home) {
this(
() -> Xmir2Xmir.xcop(
(String) yaml.value().get("expected"),
home,
"expected.xml"
),
() -> Xmir2Xmir.xcop(
Xmir2Xmir.process(
(String) yaml.value().get("before"),
(Iterable<String>) yaml.value().get("xsls")
),
home,
"processed.xml"
)
);
}

/**
* Ctor.
* @param expected Expected normalized xml content.
* @param processed Processed by transformations xml content.
*/
private Xmir2Xmir(final Scalar<String> expected, final Scalar<String> processed) {
this.expected = new Unchecked<>(new Sticky<>(expected));
this.processed = new Unchecked<>(new Sticky<>(processed));
}

@Override
public String toString() {
return new StringBuilder().append("Expected and processed xmls sre different.\n")
.append("Expected:\n")
.append(this.expected.value())
.append("\nProcessed:\n")
.append(this.processed.value()).toString();
}

@Override
public boolean equals(final Object obj) {
if (!(obj instanceof Boolean)) {
throw new IllegalArgumentException(
String.format(
"Can't compare with anything except Boolean: %s",
obj.getClass()
)
);
}
return new Unchecked<>(
() -> this.expected.value().equals(this.processed.value())
).value();
}

@Override
public int hashCode() {
throw new UnsupportedOperationException("#hashCode()");
}

/**
* Convert xmir to a determined format using the command
* [xcop](https://github.com/yegor256/xcop).
* @param raw Raw xmir content.
* @param home Home.
* @param dst Filename where to save.
* @return Xcop'ed xmir content.
*/
private static String
xcop(final String raw, final Home home, final String dst) throws Exception {
final Path relative = Paths.get(dst);
home.save(raw, relative);
new Jaxec("xcop", "--fix")
.with(dst)
.withHome(home.absolute(Paths.get(".")))
.execUnsafe();
return new TextOf(home.absolute(relative)).asString();
}

/**
* Pass xml through xsls.
* @param before XML content before.
* @param xsls xsls.
* @return Content of output xml.
*/
private static String process(final String before, final Iterable<String> xsls) {
TrClasspath<Shift> train = new TrClasspath<>();
for (final String sheet : xsls) {
train = train.with(sheet);
}
return new Xsline(train.back()).pass(new XMLDocument(before)).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# The MIT License (MIT)
#
# Copyright (c) 2016-2024 Objectionary.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
---
xsls:
- /org/eolang/maven/pre/align-test-classes.xsl
before:
<?xml version="1.0" encoding="UTF-8"?>
<program>
<metas>
<meta line="25">
<head>tests</head>
<tail/>
</meta>
</metas>
<objects>
<class abstract=""
line="30"
name="nesting-testTest"
original-name="nesting-test"
pos="0">
<xmir>&lt;o abstract="" line="30" name="nesting-test" original-name="nesting-test" pos="0"&gt;
&lt;o base="nesting-test$qwerty-1" cut="0" line="31" name="qwerty-1" pos="2" ref="31"/&gt;
&lt;o base="nesting-test$qwerty-2" cut="2" line="33" name="qwerty-2" pos="2" ref="33"/&gt;
&lt;o base="org.eolang.true" line="34" name="@" pos="2"/&gt;
&lt;/o&gt;</xmir>
<class abstract=""
line="31"
name="nesting-testTest$qwerty-1"
original-name="qwerty-1"
parent="nesting-testTest"
pos="2">
<xmir>&lt;o abstract="" line="31" name="nesting-test$qwerty-1" original-name="qwerty-1" parent="nesting-test" pos="2"&gt;
&lt;o base="nesting-test$qwerty-1$qwerty-2" cut="0" line="32" name="qwerty-2" pos="4" ref="32"/&gt;
&lt;/o&gt;</xmir>
<class abstract=""
line="32"
name="nesting-testTest$qwerty-1$qwerty-2"
original-name="qwerty-2"
parent="nesting-testTest$qwerty-1"
pos="4">
<xmir>&lt;o abstract="" line="32" name="nesting-test$qwerty-1$qwerty-2" original-name="qwerty-2" parent="nesting-test$qwerty-1" pos="4"/&gt;</xmir>
</class>
</class>
<class abstract=""
line="33"
name="nesting-testTest$qwerty-2"
original-name="qwerty-2"
parent="nesting-testTest"
pos="2">
<xmir>&lt;o abstract="" line="33" name="nesting-test$qwerty-2" original-name="qwerty-2" parent="nesting-test" pos="2"/&gt;</xmir>
</class>
</class>
</objects>
</program>
expected:
<?xml version="1.0" encoding="UTF-8"?>
<program>
<metas>
<meta line="25">
<head>tests</head>
<tail/>
</meta>
</metas>
<objects>
<class abstract=""
line="30"
name="nesting-testTest"
original-name="nesting-test"
pos="0">
<xmir>&lt;o abstract="" line="30" name="nesting-test" original-name="nesting-test" pos="0"&gt; &lt;o base="nesting-test$qwerty-1" cut="0" line="31" name="qwerty-1" pos="2" ref="31"/&gt; &lt;o base="nesting-test$qwerty-2" cut="2" line="33" name="qwerty-2" pos="2" ref="33"/&gt; &lt;o base="org.eolang.true" line="34" name="@" pos="2"/&gt; &lt;/o&gt;</xmir>
<class abstract=""
line="31"
name="nesting-testTest$qwerty-1"
original-name="qwerty-1"
parent="nesting-testTest"
pos="2">
<xmir>&lt;o abstract="" line="31" name="nesting-test$qwerty-1" original-name="qwerty-1" parent="nesting-test" pos="2"&gt; &lt;o base="nesting-test$qwerty-1$qwerty-2" cut="0" line="32" name="qwerty-2" pos="4" ref="32"/&gt; &lt;/o&gt;</xmir>
<class abstract=""
line="32"
name="nesting-testTest$qwerty-1$qwerty-2"
original-name="qwerty-2"
parent="nesting-testTest$qwerty-1"
pos="4">
<xmir>&lt;o abstract="" line="32" name="nesting-test$qwerty-1$qwerty-2" original-name="qwerty-2" parent="nesting-test$qwerty-1" pos="4"/&gt;</xmir>
</class>
</class>
<class abstract=""
line="33"
name="nesting-testTest$qwerty-2"
original-name="qwerty-2"
parent="nesting-testTest"
pos="2">
<xmir>&lt;o abstract="" line="33" name="nesting-test$qwerty-2" original-name="qwerty-2" parent="nesting-test" pos="2"/&gt;</xmir>
</class>
<class abstract=""
line="32"
name="nesting-testTest$qwerty-1$qwerty-2"
original-name="qwerty-2"
parent="nesting-testTest$qwerty-1"
pos="4">
<xmir>&lt;o abstract="" line="32" name="nesting-test$qwerty-1$qwerty-2" original-name="qwerty-2" parent="nesting-test$qwerty-1" pos="4"/&gt;</xmir>
</class>
</class>
</objects>
</program>
Loading

0 comments on commit dcbc51c

Please sign in to comment.