为jUnit创建匹配器<stream>

时间:2016-04-06 06:06:40

标签: java junit hamcrest

以下代码旨在允许有关流大小的更自然的断言。

Matcher<Stream> hasCount(int count) {
    return new TypeSafeDiagnosingMatcher<Stream>() {
        protected boolean matchesSafely(Stream stream, Description desc) {
            long streamCount = stream.count();
            if (streamCount == count) {
                return true;
            } else {
                desc.appendText("count ").appendValue(streamCount);
                return false;
            }
        }

        public void describeTo(Description desc) {
            desc.appendText("count ").appendValue(count);
        }
    };
}

这允许断言,例如:

assertThat(getWidgetStream(), hasCount(52));

当断言通过时它工作正常但是当它失败时(例如assertThat(Stream.empty(), hasCount(1));)它返回错误 &#34; stream已经被操作或关闭了#34;而不是预期的描述&#34;期望:计数&lt; 1&gt;有:计数&lt; 0&gt;&#34;。

当我检查TypeSafeDiagnosingMatcher的来源时,我发现matchesSafelymatches都调用了describeMismatch。所以Hamcrest假设matchesSafely是幂等的,我的不是。

有解决这个问题的方法吗?

1 个答案:

答案 0 :(得分:0)

我发现的一个解决方案是在调用之间存储结果。

     //getting the currency values from session.
         session_start();
       //old currency
         $from=(!empty($_SESSION['old_currency']))?$_SESSION['old_currency']:'INR';
         echo 'From:&nbsp;'.$from.'<br>';
  //new currency  
  $to=(!empty($_SESSION['new_currency']))?$_SESSION['new_currency']:'INR';
         echo 'To:&nbsp;'.$to.'<br>';
    //amount to be converted.

    $prize=array('2000','600','7000','2500','100000');
 $prize_old =(!empty($_SESSION['old_prize']))?$_SESSION['old_prize']:$prize; 
$prize_new =(!empty($_SESSION['new_prize']))?$_SESSION['new_prize']:$prize;  
$prize_converted = array();
    echo '<br><br>';
    for($i=0;$i<5;$i++){
        //call the currency convert function 
        if($from!=$to)
        {
            $response=currencyConvert($from,$to, $prize_new[$i]);
            $converted_amount=filter_var($response,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
            array_push($prize_converted,round($converted_amount));
        }
        else
        {
            $converted_amount=$prize[$i];
             array_push($prize_converted,$converted_amount);

        }
        echo '<span>'.$converted_amount.' '.$to.'</span><hr/>';
    }
$_SESSION['old_priz'] = $prize;  
$_SESSION['new_prize'] = $prize_converted;


//make the api request for convert currency.
function currencyConvert($from,$to,$amount){
  // echo '<br>convert'.$f.'to'.$t;
     $url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to"; 

    $request = curl_init(); 
    $timeOut = 0;
    curl_setopt ($request, CURLOPT_URL, $url); 
    curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
    curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut); 
    $response = curl_exec($request);
    curl_close($request);
    $regex  = '#\<span class=bld\>(.+?)\<\/span\>#s';
    preg_match($regex, $response, $converted);
    if(!empty($converted))
    return  ($converted[0])?$converted[0]:0;

}

这有效,但不是特别优雅。