最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to test dart record patterns? - Stack Overflow

matteradmin5PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far