Search in sources :

Example 86 with Description

use of org.hamcrest.Description in project sonarqube by SonarSource.

the class CoveragePerTestMediumTest method invalidCoverage.

@Test
public // SONAR-6183
void invalidCoverage() throws IOException {
    File baseDir = createTestFiles();
    File srcDir = new File(baseDir, "src");
    File coverageFile = new File(srcDir, "sample.xoo.coverage");
    FileUtils.write(coverageFile, "0:2\n");
    exception.expect(IllegalStateException.class);
    exception.expectMessage("Error processing line 1 of file");
    exception.expectCause(new TypeSafeMatcher<Throwable>() {

        @Override
        public void describeTo(Description description) {
        // nothing to do
        }

        @Override
        protected boolean matchesSafely(Throwable item) {
            return item.getMessage().contains("Line number must be strictly positive");
        }
    });
    runTask(baseDir);
}
Also used : Description(org.hamcrest.Description) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) Test(org.junit.Test)

Example 87 with Description

use of org.hamcrest.Description in project freud by LMAX-Exchange.

the class ClassByteCodeMethodMatchers method hasMethodInvocation.

public static FreudExtendedMatcher<ClassByteCodeMethod> hasMethodInvocation(final Class expectedOwner, final String expectedMethodName, final Class... expectedParamsDeclared) {
    return new FreudExtendedMatcher<ClassByteCodeMethod>() {

        private String expectedOwnerName;

        private String[] expectedParamNames;

        {
            expectedOwnerName = typeEncoding(expectedOwner);
            expectedParamNames = (expectedParamsDeclared.length == 0) ? EMPTY_ARGS : new String[expectedParamsDeclared.length];
            for (int i = 0, size = expectedParamsDeclared.length; i < size; i++) {
                expectedParamNames[i] = typeEncoding(expectedParamsDeclared[i]);
            }
        }

        @Override
        protected boolean matchesSafely(final ClassByteCodeMethod item) {
            final boolean[] found = new boolean[] { false };
            found[0] = false;
            item.findInstruction(new AbstractInstructionVisitor() {

                @Override
                public void methodInvocation(final Instruction instruction, final String owner, final String methodName, final String... args) {
                    if (!found[0] && expectedOwnerName.equals(owner) && expectedMethodName.equals(methodName) && Arrays.equals(expectedParamNames, args)) {
                        found[0] = true;
                    }
                }
            });
            return found[0];
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("hasMethodInvocation(" + expectedOwner.getName() + ", " + expectedMethodName + ", " + Arrays.toString(expectedParamNames) + ")");
        }
    };
}
Also used : Description(org.hamcrest.Description) ClassByteCodeMethod(org.freud.analysed.classbytecode.method.ClassByteCodeMethod) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) FreudExtendedMatcher(org.freud.java.matcher.FreudExtendedMatcher) AbstractInstructionVisitor(org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)

Example 88 with Description

use of org.hamcrest.Description in project freud by LMAX-Exchange.

the class CodeBlockMatchers method hasMethodCall.

public static FreudExtendedMatcher<CodeBlock> hasMethodCall(final String methodCall) {
    return new FreudExtendedMatcher<CodeBlock>() {

        private final String methodName;

        private final String[] instanceReferences;

        {
            String[] values = methodCall.split("\\.");
            instanceReferences = new String[values.length - 1];
            System.arraycopy(values, 0, instanceReferences, 0, instanceReferences.length);
            methodName = values[values.length - 1];
        }

        @Override
        protected boolean matchesSafely(final CodeBlock toBeAnalysed) {
            List<MethodCall> methodCallList = toBeAnalysed.getMethodCallListByMethodName(methodName);
            if (methodCallList != null) {
                for (MethodCall methodCall : methodCallList) {
                    if (Arrays.equals(instanceReferences, methodCall.getInstanceReferences())) {
                        return true;
                    }
                }
            }
            return false;
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("HasMethodCall(" + methodCall + ")");
        }
    };
}
Also used : Description(org.hamcrest.Description) CodeBlock(org.freud.analysed.javasource.CodeBlock) FreudExtendedMatcher(org.freud.java.matcher.FreudExtendedMatcher) MethodCall(org.freud.analysed.javasource.MethodCall)

Example 89 with Description

use of org.hamcrest.Description in project platform_frameworks_base by android.

the class MockUtils method checkUserRestrictions.

public static Bundle checkUserRestrictions(String... keys) {
    final Bundle expected = DpmTestUtils.newRestrictions(Preconditions.checkNotNull(keys));
    final Matcher<Bundle> m = new BaseMatcher<Bundle>() {

        @Override
        public boolean matches(Object item) {
            if (item == null)
                return false;
            return UserRestrictionsUtils.areEqual((Bundle) item, expected);
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("User restrictions=" + getRestrictionsAsString(expected));
        }
    };
    return MockitoHamcrest.argThat(m);
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) Bundle(android.os.Bundle)

Example 90 with Description

use of org.hamcrest.Description in project neo4j by neo4j.

the class OnlineBackupCommandTest method exitCode.

private Matcher<CommandFailed> exitCode(final int expectedCode) {
    return new BaseMatcher<CommandFailed>() {

        @Override
        public void describeTo(Description description) {
            description.appendText("expected exit code ").appendValue(expectedCode);
        }

        @Override
        public void describeMismatch(Object o, Description description) {
            if (o instanceof CommandFailed) {
                CommandFailed e = (CommandFailed) o;
                description.appendText("but it was ").appendValue(e.code());
            } else {
                description.appendText("but exception is not a CommandFailed exception");
            }
        }

        @Override
        public boolean matches(Object o) {
            if (o instanceof CommandFailed) {
                CommandFailed e = (CommandFailed) o;
                return expectedCode == e.code();
            }
            return false;
        }
    };
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) CommandFailed(org.neo4j.commandline.admin.CommandFailed)

Aggregations

Description (org.hamcrest.Description)122 Test (org.junit.Test)38 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)35 StringDescription (org.hamcrest.StringDescription)29 BaseMatcher (org.hamcrest.BaseMatcher)27 View (android.view.View)22 ViewParent (android.view.ViewParent)11 TextView (android.widget.TextView)11 ViewGroup (android.view.ViewGroup)8 Expectations (org.jmock.Expectations)8 URL (java.net.URL)7 Matcher (org.hamcrest.Matcher)7 Invocation (org.jmock.api.Invocation)7 BoundedMatcher (android.support.test.espresso.matcher.BoundedMatcher)6 ImageView (android.widget.ImageView)6 File (java.io.File)6 IOException (java.io.IOException)6 URI (java.net.URI)6 List (java.util.List)6 JsonNode (org.codehaus.jackson.JsonNode)6