gpt4 book ai didi

dart - 为单元测试定义 “accept”和 “notAccept”匹配器的更好方法

转载 作者:行者123 更新时间:2023-12-03 02:58:28 24 4
gpt4 key购买 nike

我想定义匹配器以检查解析器是否可以接受字符串。我做到了,但感觉不好。

Dart单元测试代码:

library test_parser;

import 'package:unittest/unittest.dart';
import '../lib/shark_parser.dart';

main() {
SharkParser parser;
setUp(() {
parser = new SharkParser();
});
tearDown(() {
parser = null;
});

group("paramType parser", () {
test("should accept valid types", () {
expect(parser.paramType(), accept("String"));
expect(parser.paramType(), accept("int"));
expect(parser.paramType(), accept("List"));
expect(parser.paramType(), accept("List<User>"));
});
test("should not accept invalid types", () {
expect(parser.paramType(), notAccept("#"));
expect(parser.paramType(), notAccept("0"));
expect(parser.paramType(), notAccept("String()"));
expect(parser.paramType(), notAccept("List< User >"));
});
});
}

自定义匹配器:

Matcher accept(String matchingString) => new AcceptMatcher(matchingString);

Matcher notAccept(String matchingString) => new NotAcceptMatcher(matchingString);

class NotAcceptMatcher extends Matcher {
String matchingString;

NotAcceptMatcher(this.matchingString);

bool matches(item, Map matchState) {
return !item.end().accept(matchingString);
}

Description describe(Description description) {
return description.add("parser not accept string: $matchingString");
}

Description describeMismatch(item, Description mismatchDescription,
Map matchState, bool verbose) {
mismatchDescription.add("accepts it");
return mismatchDescription;
}
}

class AcceptMatcher extends Matcher {

String matchingString;

AcceptMatcher(this.matchingString);

bool matches(item, Map matchState) {
return item.end().accept(matchingString);
}

Description describe(Description description) {
return description.add("parser accept string: $matchingString");
}

Description describeMismatch(item, Description mismatchDescription,
Map matchState, bool verbose) {
mismatchDescription.add("doesn't accept");
return mismatchDescription;
}

}

您可以看到我必须定义两个匹配器 NotAcceptMatcherAcceptMatcher。逻辑非常相似,但我不知道如何使其简单。

还有其他更简单的解决方案吗?

最佳答案

您可以使用isNot匹配器反转accept匹配器:

expect(parser.paramType(), isNot(accept("#")));

它看起来有点有趣,所以您可以创建一个函数来做到这一点:

Matcher notAccept(String s) => isNot(accept(s));

expect(parser.paramType(), notAccept("#"));

该说明可能无法完美阅读,但可以节省您一些工作。

匹配器看起来似乎很冗长,但是如果您愿意,可以通过省略一些类型注释并使用简短的变量名来使匹配器定义更加简洁。当我编写框架要调用的类或函数时,我发现这是一个不错的选择,因为我不担心为其他用户编写文档。

class AcceptMatcher extends Matcher {
final acceptString;

AcceptMatcher(this.acceptString);

matches(item, _) => item.end().accept(acceptString);

describe(d) => d.add("parser accept string: $acceptString");

describeMismatch(item, d, _, __) => d..add("doesn't accept");
}

另一个选择是只使用 predicate匹配器:

Matcher accept(String s) => 
predicate((i) => i.end().accept(s), "parser accepts $s");

关于dart - 为单元测试定义 “accept”和 “notAccept”匹配器的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21806832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com