admin管理员组文章数量:1026989
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
1 Answer
Reset to default 2From 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.
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
1 Answer
Reset to default 2From 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.
本文标签: How to test dart record patternsStack Overflow
版权声明:本文标题:How to test dart record patterns? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745660367a2161852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论