jqwik-任意地图-在地图内生成随机数量的条目 [英] jqwik - Arbitrary Map - Generate a random number of entries within a Map
问题描述
此代码用于为元素生成一个单个映射"条目.但是我想使用generateInputMapElements从地图中生成随机数量的条目,并且 传递到statusReturnedFromApplyingRule()
This code works to generate a Single Map entry for elements. But I want to generate a random number of entries from within the Map using generateInputMapElements and pass to the statusReturnedFromApplyingRule()
@Property
//@Report(Reporting.GENERATED)
boolean statusReturnedFromApplyingRule(@ForAll("generateRule") Rule rule,
@ForAll("generateInputMapElements") Iterable<Map<String, Object>> elements) {
RangeMatchRule rangeMatchRule = new RangeMatchRule();
final RuleIF.Status status = rangeMatchRule.applyRule(rule, elements);
return RuleIF.getEnums().contains(status.toString());
}
@Provide
Arbitrary<Iterable<Map<String, Object>>> generateInputMapElements() {
Arbitrary<Double> metricValueArb = Arbitraries.doubles()
.between(0, 50.0);
Arbitrary<Map<String, Object>> inputMapArb =
metricValueArb.map(metricsValue -> {
Map<String, Object> inputMap = new HashMap<>();
inputMap.put(Utils.METRIC_VALUE, metricsValue);
return inputMap;
});
return inputMapArb.map(inputMap -> {
List<Map<String, Object>> inputMapLst = new ArrayList<>();
inputMapLst.add(inputMap);
return inputMapLst;
});
}
推荐答案
假设您想要一个具有单个条目的地图列表(可迭代),我看到两个基本选项.
Assuming that you want a list (iterable) of maps with a single entry, I see two basic options.
选项1-使用Arbitrary.list()
生成列表并直接在生成器代码中指定最小和最大大小:
Option 1 - Use Arbitrary.list()
to generate a list and specify min and max size directly in the generator code:
@Provide
Arbitrary<List<Map<String, Object>>> generateInputMapElements() {
Arbitrary<Double> metricValueArb = Arbitraries.doubles()
.between(0, 50.0);
return metricValueArb
.map(metricsValue -> {
Map<String, Object> inputMap = new HashMap<>();
inputMap.put(Utils.METRIC_VALUE, metricsValue);
return inputMap;
})
.list().ofMinSize(1).ofMaxSize(10);
}
选项2-仅生成单个地图,并对可迭代对象使用标准注释:
Option 2 - Generate only the individual maps and use standard annotations for the iterable:
@Property
@Report(Reporting.GENERATED)
boolean statusReturnedFromApplyingRule2(
@ForAll("generateRule") Rule rule,
@ForAll @Size(min = 1, max = 10) Iterable<@From("generateInputMap") Map<String, Object>> elements
) {
...
}
@Provide
Arbitrary<Map<String, Object>> generateInputMap() {
Arbitrary<Double> metricValueArb = Arbitraries.doubles()
.between(0, 50.0);
return metricValueArb
.map(metricsValue -> {
Map<String, Object> inputMap = new HashMap<>();
inputMap.put(Utils.METRIC_VALUE, metricsValue);
return inputMap;
});
}
我个人选择选项2,因为它需要较少的代码.不过是YMMV.
I'd personally go with option 2 because it requires less code. YMMV though.
这篇关于jqwik-任意地图-在地图内生成随机数量的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!