提交 952cc074 编写于 作者: T Takeshi Hagikura 提交者: GitHub

Refactor the tests that allow some round errors to use custom Matcher (#134)

to simplify the test code.
上级 7c3dc1a3
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* 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 com.google.android.flexbox.test;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import java.util.Locale;
/**
* Custom {@link BaseMatcher} that expects {@link Number} value allowing some errors to allow
* such as rounding errors.
*/
class IsEqualAllowingError<T extends Number> extends BaseMatcher<T> {
private Number expected;
private Integer errorAllowed;
private IsEqualAllowingError(Number expected) {
this(expected, 1);
}
private IsEqualAllowingError(Number expected, int errorAllowed) {
this.expected = expected;
this.errorAllowed = errorAllowed;
}
@Override
public boolean matches(Object item) {
if (!(item instanceof Number)) {
return false;
}
Number other = (Number) item;
return expected.intValue() - errorAllowed <= other.intValue() &&
other.intValue() <= expected.intValue() + errorAllowed;
}
@Override
public void describeTo(Description description) {
description.appendText(
String.format(Locale.US, "expected value is <%s> allowing error of <%s>.", expected,
errorAllowed));
}
@Factory
static <T extends Number> Matcher<T> isEqualAllowingError(T expected) {
return new IsEqualAllowingError<>(expected);
}
@Factory
static <T extends Number> Matcher<T> isEqualAllowingError(T expected, int errorAllowed) {
return new IsEqualAllowingError<>(expected, errorAllowed);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册