最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to test dart record patterns? - Stack Overflow

programmeradmin1浏览0评论

I have a function: (int,double?) eva(String);

I can test it with:

expect(eva("something"), (4,10));
// or
expect(eva("something").$1, isNonZero);
expect(eva("something").$2, 10);

but not as

expect(eva("something"), (isNonZero,10) );

Any tips are welcome.

EDIT:
I have a workaround: the function eva() should return a list, and so we can write:

expect(eva("something"), [isNonZero,10]);

I have a function: (int,double?) eva(String);

I can test it with:

expect(eva("something"), (4,10));
// or
expect(eva("something").$1, isNonZero);
expect(eva("something").$2, 10);

but not as

expect(eva("something"), (isNonZero,10) );

Any tips are welcome.

EDIT:
I have a workaround: the function eva() should return a list, and so we can write:

expect(eva("something"), [isNonZero,10]);
Share Improve this question edited Nov 16, 2024 at 16:40 kantal asked Nov 16, 2024 at 10:53 kantalkantal 2,4072 gold badges9 silver badges16 bronze badges 2
  • your workaround loses typesafety though. – Randal Schwartz Commented Nov 16, 2024 at 17:35
  • @RandalSchwartz Yes, but as my eva() function is already for test purposes only, I can embed type safety in it. – kantal Commented Nov 17, 2024 at 14:44
Add a comment  | 

1 Answer 1

Reset to default 2

From the expect documentation, the second argument should either be a matcher or a value. If it is a value, "it will be wrapped in an equals matcher", so passing a record as the second argument will only ever test for equality.

Edit:
As pointed out by @jamesdin and confirmed by the matcher library equals function documentation, "For Iterables and Maps, [an equals matcher] will recursively match the elements."
End edit

I would recommend creating a CustomMatcher that can analyze nested matchers within a record. This would let you do something like
expect(eva("something"), MyRecordMatcher( (isNonZero, 10) ));.

Edit:
Another way to do this without changing eva to return a list is to convert the record to a list at the time of the test. There is no built-in way to do this, so you would have to make that conversion function yourself, but it would let you do something like
expect(recordToList(eva("something")), [isNonZero,10]);.

In my opinion this is less elegant and it adds a point of failure to getting the "actual" value, but it should work.

发布评论

评论列表(0)

  1. 暂无评论