有没有办法将以下字符串与任何 hamcrest 匹配器匹配。
"{\"messageType\":\"identify\",\"_id\":\"7de9a446-2ced-4bda-af35-81e95ad2dc32\",\"address\":\"192.168.0.0\",\"port\":7070}"
Run Code Online (Sandbox Code Playgroud)
这个字符串被传递给一个方法。我使用 JMock 期望来匹配它。
问题:“72e3a446-2fed-4bda-ac35-34e95ab3dc32”部分是随机生成的UUID,是在测试方法内部生成的。是否有一个 Hamcrest 字符串匹配器可以匹配类似的东西
new StringCompositeMatcher("{\"messageType\":\"identify\",\"_id\":\"", with(any(String.class)), "\"address\":\"192.168.0.0\",\"port\":7070}" )
Run Code Online (Sandbox Code Playgroud)
它必须匹配预期的字符串以"{\"messageType\":\"identify\",\"_id\":\"之后的任何字符串开始,并以",\"address\":\"192.168.0.0\",\"port\":7070}"
编辑:解决方案
with(allOf(new StringStartsWith("{\"messageType\":\"identify\",\"_id\":\""), new StringEndsWith("\",\"address\":\"192.168.0.0\",\"port\":7070}")))
Run Code Online (Sandbox Code Playgroud) 我正在使用这个FLANN匹配算法来匹配下面显示代码的2张图片中的兴趣点.
有一段时间代码找到匹配点列表:
std::vector<DMatch> good_matches;
Run Code Online (Sandbox Code Playgroud)
我想在两张图片中得到点定位(x,y).创建置换贴图.我怎样才能访问这些点本地化?
干杯,
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
void readme();
/** @function main */
int main(int argc, char** argv) {
if (argc != 3) {
readme();
return -1;
}
// Transform in GrayScale
Mat img_1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
Mat img_2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
// Checks if the image could be loaded
if (!img_1.data || !img_2.data) {
std::cout << " --(!) Error reading images " << std::endl;
return -1; …Run Code Online (Sandbox Code Playgroud) 假设我有几个字段成员的案例类:
case class User(name: String, ..., createdAt: LocalDateTime)
Run Code Online (Sandbox Code Playgroud)
如何在不考虑createdAt字段的情况下检查相等性?
有没有比以下更好的方法:
val expected = User("username", stubDate)
actual shouldBe expected.copy(createdAt = actual.createdAt)
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 spacy 模式匹配器创建名词块。例如,如果我有一句话“冰球混战花了几个小时。” 我想返回“冰球混战”和“小时”。我目前有这个:
from spacy.matcher import Matcher
nlp = spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocab)
matcher.add("NounChunks", None, [{"POS": "NOUN"}, {"POS": "NOUN", "OP": "*"}, {"POS": "NOUN", "OP": "*"}] )
doc = nlp("The ice hockey scrimmage took hours.")
matches = matcher(doc)
for match_id, start, end in matches:
string_id = nlp.vocab.strings[match_id]
span = doc[start:end]
print(match_id, string_id, start, end, span.text)
Run Code Online (Sandbox Code Playgroud)
但它返回的是“冰球混战”的所有版本,而不仅仅是最长的版本。
12482938965902279598 NounChunks 1 2 ice
12482938965902279598 NounChunks 1 3 ice hockey
12482938965902279598 NounChunks 2 3 hockey
12482938965902279598 NounChunks 1 4 ice hockey scrimmage
12482938965902279598 …Run Code Online (Sandbox Code Playgroud) 如果我有一个Matcher [A]如何创建一个Matcher [Iterable [A]],只有当Iterable的每个元素都满足原始Matcher时才会满足.
class ExampleSpec extends Specification {
def allSatisfy[A](m: => Matcher[A]): Matcher[Iterable[A]] = error("TODO")
def notAllSatisfy[A](m: => Matcher[A]): Matcher[Iterable[A]] = allSatisfy(m).not
"allSatisfy" should {
"Pass if all elements satisfy the expectation" in {
List(1, 2, 3, 4) must allSatisfy(beLessThan(5))
}
"Fail if any elements do not satisfy the expectation" in {
List(1, 2, 3, 5) must notAllSatisfy(beLessThan(5))
}
}
}
Run Code Online (Sandbox Code Playgroud) 我在Java中有一些文本,我想使用模式和匹配器从中提取一些东西.这是我的计划:
public String getItemsByType(String text, String start, String end) {
String patternHolder;
StringBuffer itemLines = new StringBuffer();
patternHolder = start + ".*" + end;
Pattern pattern = Pattern.compile(patternHolder);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
itemLines.append(text.substring(matcher.start(), matcher.end())
+ "\n");
}
return itemLines.toString();
}
Run Code Online (Sandbox Code Playgroud)
当搜索到的文本在同一行时,此代码完全有效,例如:
String text = "My name is John and I am 18 years Old";
getItemsByType(text, "My", "John");
Run Code Online (Sandbox Code Playgroud)
立即从文本中抓取"我的名字是约翰"的文字.但是,当我的文字看起来像这样:
String text = "My name\nis John\nand I'm\n18 years\nold";
getItemsByType(text, "My", "John");
Run Code Online (Sandbox Code Playgroud)
它没有抓住任何东西,因为"我的"和"约翰"在不同的行上.我该如何解决这个问题?
我有一个输入字段,只能接受字母(无论是低位还是大写).我可以简单地使用co ca(仅包含/任何)运算符来编写它.但这很费时间.
if p_input3 ca '*/ + - ? ! % ( ) = 0123456789'.
MESSAGE e000 WITH 'Only letters are allowed. No numbers or special characters'.
ENDIF.
Run Code Online (Sandbox Code Playgroud)
有用.但我想用正则表达式来检查它.我试过这段代码.但它不能很好地工作.
DATA: text TYPE string,
matcher type REF TO cl_abap_matcher.
PARAMETERS: p_input3 TYPE string DEFAULT 'abCD*()fhi' LOWER CASE.
matcher = cl_abap_matcher=>create(
pattern = `([a-zA-Z])`
text = p_input3
).
if matcher = abap_true.
MESSAGE e000 with 'Only letters are allowed. No numbers or special characters'.
ENDIF.
Run Code Online (Sandbox Code Playgroud)
有人能想出来吗?
我正在尝试对此方法进行单元测试:
/**
* finds all widget descriptions containing specified text
* @param searchText
* @return
*/
@Transactional
public List<Integer> returnWidgetIdsFromSearchWord(String searchText){
List<Integer> widgetIds = new ArrayList<Integer>();
MapSqlParameterSource args = new MapSqlParameterSource();
try{
widgetIds = (List<Integer>) jdbt.queryForList("SELECT idwidgets FROM descriptions "
+ "WHERE descriptiontext LIKE '%"+ searchText + "%'", args, Integer.class);
}catch(Exception e){
}
return widgetIds;
}
Run Code Online (Sandbox Code Playgroud)
使用此JUnit测试:
@Test
public void testReturnWidgetIdsFromSearchWord(){
List<Integer> widgetIds = null;
when(jdbt.queryForList(Matchers.anyString(),
Matchers.any(MapSqlParameterSource.class),
Matchers.any(Integer.class))).thenReturn(idList);
widgetIds = (List<Integer>) dDao.returnWidgetIdsFromSearchWord("someText");
assertEquals(widgetIds, idList);
}
Run Code Online (Sandbox Code Playgroud)
我试过在没有Matcher的情况下使用Integer.class - 没有运气,因为它抱怨需要3个匹配器.有什么建议?谢谢
有没有办法在rspec中匹配参数的属性值?检查的文件,它看起来像有其他可用的匹配,比如anything,an_instance_of和hash_including-但没有检查对象的属性值.
示例 - 假设我有此实例方法:
class DateParser
def parse_year(a_date)
puts a_date.year
end
end
Run Code Online (Sandbox Code Playgroud)
然后我可以这样写一个期望:
dp = DateParser.new
expect(dp).to receive(:parse_year).with(some_matcher)
Run Code Online (Sandbox Code Playgroud)
我希望some_matcher检查parse_year是否使用year具有值为2014 的属性的任何对象调用.这是否可以使用rspec中的开箱即用参数匹配,或者是否必须编写自定义参数?
似乎最新版本的ScalaTest我们有两个匹配器Matchers(之前是ShouldMatchers)和MustMatchers.
这两者之间真正的区别是什么?团队可以选择那种风格还是?
另外,shouldBe和之间的任何区别should be