All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eolang.parser.CheckPack Maven / Gradle / Ivy

There is a newer version: 0.44.0
Show newest version
/*
 * 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.parser;

import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StClasspath;
import com.yegor256.xsline.Train;
import com.yegor256.xsline.Xsline;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import org.cactoos.io.InputOf;
import org.yaml.snakeyaml.Yaml;

/**
 * One test scenario of parsing and post-processing.
 *
 * @since 1.0
 */
public final class CheckPack {

    /**
     * The scenario in YAML.
     */
    private final String script;

    /**
     * Ctor.
     * @param scrpt The script
     */
    public CheckPack(final String scrpt) {
        this.script = scrpt;
    }

    /**
     * Make a run and return the list of XPath expressions
     * that don't pass after the run.
     *
     * @return List of XPath expressions that failed
     * @throws IOException If fails
     */
    @SuppressWarnings("unchecked")
    public Collection failures() throws IOException {
        final Yaml yaml = new Yaml();
        final Map map = yaml.load(this.script);
        final String src = map.get("eo").toString();
        final Iterable xsls = (Iterable) map.get("xsls");
        Train train = new ParsingTrain();
        if (xsls != null) {
            if (!map.containsKey("defaults")) {
                train = train.empty();
            }
            for (final String xsl : xsls) {
                train = train.with(new StClasspath(xsl));
            }
        }
        final XML out = new Xsline(train).pass(
            new EoSyntax(
                "scenario",
                new InputOf(String.format("%s\n", src))
            ).parsed()
        );
        Logger.debug(this, "Output XML:\n%s", out);
        final Collection failures = new LinkedList<>();
        for (final String xpath : (Iterable) map.get("tests")) {
            if (out.nodes(xpath).isEmpty()) {
                failures.add(xpath);
            }
        }
        if (!failures.isEmpty()) {
            Logger.info(this, "Broken XML:\n%s", out);
            Logger.info(this, "Broken EO:\n%s", src);
        }
        return failures;
    }

    /**
     * Is this check disabled?
     * @return True if disabled.
     */
    public boolean skip() {
        return new Yaml()
            .>load(this.script)
            .getOrDefault("skip", Boolean.FALSE)
            .equals(Boolean.TRUE);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy