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

org.gradle.nativeplatform.fixtures.app.XCTestSourceFileElement Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.nativeplatform.fixtures.app;

import com.google.common.base.Joiner;
import com.google.common.collect.Sets;
import org.gradle.api.Transformer;
import org.gradle.integtests.fixtures.SourceFile;
import org.gradle.integtests.fixtures.TestClassExecutionResult;
import org.gradle.util.CollectionUtils;
import org.hamcrest.Matchers;

import java.util.List;
import java.util.Set;

public abstract class XCTestSourceFileElement extends SourceFileElement implements XCTestElement {
    private final String testSuiteName;
    private final Set imports = Sets.newLinkedHashSet();
    private final Set testableImports = Sets.newLinkedHashSet();

    public XCTestSourceFileElement(String testSuiteName) {
        this.testSuiteName = testSuiteName;
        withImport("XCTest");
    }

    @Override
    public int getFailureCount() {
        int result = 0;
        for (XCTestCaseElement element : getTestCases()) {
            if (element.isExpectFailure()) {
                result++;
            }
        }
        return result;
    }

    @Override
    public int getPassCount() {
        int result = 0;
        for (XCTestCaseElement element : getTestCases()) {
            if (!element.isExpectFailure()) {
                result++;
            }
        }
        return result;
    }

    @Override
    public int getTestCount() {
        return getTestCases().size();
    }

    public final String getTestSuiteName() {
        return testSuiteName;
    }

    @Override
    public String getSourceSetName() {
        return "test";
    }

    public SourceFile getSourceFile() {
        return sourceFile("swift", getTestSuiteName() + ".swift",
                renderImports()
                + "\n"
                + "class " + getTestSuiteName() + ": XCTestCase {\n"
                + "    " + renderTestCases() + "\n"
                + "}");
    }

    private String renderImports() {
        StringBuilder sb = new StringBuilder();
        for (String importModule : getImports()) {
            sb.append("import ").append(importModule).append('\n');
        }
        for (String importModule : getTestableImports()) {
            sb.append("@testable import ").append(importModule).append('\n');
        }
        return sb.toString();
    }

    private String renderTestCases() {
        return Joiner.on("\n").join(
            CollectionUtils.collect(getTestCases(), new Transformer() {
                @Override
                public String transform(XCTestCaseElement testCase) {
                    return testCase.getContent() + "\n";
                }
            })
        );
    }

    protected XCTestCaseElement passingTestCase(String methodName) {
        return testCase(methodName, "XCTAssert(true)", false);
    }

    protected XCTestCaseElement failingTestCase(String methodName) {
        return testCase(methodName, "XCTAssert(false)", true);
    }

    protected XCTestCaseElement testCase(String methodName, String assertion) {
        return testCase(methodName, assertion, false);
    }

    protected XCTestCaseElement testCase(final String methodName, final String assertion, final boolean isExpectFailure) {
        return new XCTestCaseElement() {
            @Override
            public String getContent() {
                return "func " + methodName + "() {\n"
                    + "    " + assertion + "\n"
                    + "}";
            }

            @Override
            public String getName() {
                return methodName;
            }

            @Override
            public boolean isExpectFailure() {
                return isExpectFailure;
            }
        };
    }

    public abstract List getTestCases();

    @SuppressWarnings("unchecked")
    public void assertTestCasesRan(TestClassExecutionResult testExecutionResult) {
        testExecutionResult.assertTestCount(getTestCount(), getFailureCount(), 0);

        for (XCTestCaseElement testCase : getTestCases()) {
            if (testCase.isExpectFailure()) {
                testExecutionResult.assertTestFailed(testCase.getName(), Matchers.anything());
            } else {
                testExecutionResult.assertTestPassed(testCase.getName());
            }
        }
    }

    public Set getImports() {
        return imports;
    }

    public XCTestSourceFileElement withImport(String importName) {
        imports.add(importName);
        return this;
    }

    public Set getTestableImports() {
        return testableImports;
    }

    public XCTestSourceFileElement withTestableImport(String importName) {
        testableImports.add(importName);
        return this;
    }

    public SourceFile emptyInfoPlist() {
        return sourceFile("resources", "Info.plist",
            "\n"
                + "\n"
                + "\n"
                + "\n"
                + "\n"
                + "");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy