-
Notifications
You must be signed in to change notification settings - Fork 9
test: use rational-aware deep comparison #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
takenagain
merged 2 commits into
feat/orderbook-rpc-namespace
from
codex/refactor-test-for-robust-comparison
Aug 4, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ import 'dart:convert'; | |||||||||||
| import 'dart:io'; | ||||||||||||
|
|
||||||||||||
| import 'package:decimal/decimal.dart'; | ||||||||||||
| import 'package:rational/rational.dart'; | ||||||||||||
| import 'package:komodo_defi_rpc_methods/komodo_defi_rpc_methods.dart'; | ||||||||||||
| import 'package:test/test.dart'; | ||||||||||||
|
|
||||||||||||
|
|
@@ -85,24 +86,87 @@ void main() { | |||||||||||
|
|
||||||||||||
| group('TradePreimageResponse', () { | ||||||||||||
| test('parses fixture and round trips', () { | ||||||||||||
| final fixture = File('test/fixtures/swaps/trade_preimage.json').readAsStringSync(); | ||||||||||||
| final fixture = | ||||||||||||
| File('test/fixtures/swaps/trade_preimage.json').readAsStringSync(); | ||||||||||||
| final json = jsonDecode(fixture) as Map<String, dynamic>; | ||||||||||||
|
|
||||||||||||
| final response = TradePreimageResponse.fromJson(json); | ||||||||||||
|
|
||||||||||||
| expect(response.mmrpc, '2.0'); | ||||||||||||
| expect(response.result.totalFees.length, 2); | ||||||||||||
|
|
||||||||||||
| // Adjust expected JSON to match serialization format | ||||||||||||
| // Use a robust comparison for rational/decimal values | ||||||||||||
| final expected = Map<String, dynamic>.from(json); | ||||||||||||
| final fees = List<Map<String, dynamic>>.from((json['result'] as Map<String, dynamic>)['total_fees'] as List); | ||||||||||||
| fees[1]['required_balance_rat'][0] = [ | ||||||||||||
| 1, | ||||||||||||
| [0], | ||||||||||||
| ]; | ||||||||||||
| expected['result'] = Map<String, dynamic>.from(json['result'] as Map)..['total_fees'] = fees; | ||||||||||||
|
|
||||||||||||
| expect(response.toJson(), expected); | ||||||||||||
| expect( | ||||||||||||
| deepEqualsWithRationalTolerance(response.toJson(), expected), | ||||||||||||
| isTrue, | ||||||||||||
| reason: | ||||||||||||
| 'Serialized response does not match expected fixture (with rational/decimal tolerance)', | ||||||||||||
| ); | ||||||||||||
| }); | ||||||||||||
| }); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Recursively compares two objects, treating rational/decimal representations as equal if numerically equivalent. | ||||||||||||
| /// | ||||||||||||
| /// Rational representations are expected to be lists of the form: | ||||||||||||
| /// `[numerator, [denominator1, denominator2, ...]]` | ||||||||||||
| /// This format represents chained division, i.e.: | ||||||||||||
| /// `numerator / denominator1 / denominator2 / ...` | ||||||||||||
| /// | ||||||||||||
| /// Examples of rational lists: | ||||||||||||
| /// `[1, [2]]` // represents 1/2 | ||||||||||||
| /// `[3, [4, 5]]` // represents 3/4/5 = 3/20 | ||||||||||||
| /// `[7, [1]]` // represents 7/1 = 7 | ||||||||||||
| /// | ||||||||||||
| /// Both compared objects are considered equal if their rational/decimal values are | ||||||||||||
| /// numerically equivalent. | ||||||||||||
| bool deepEqualsWithRationalTolerance(dynamic a, dynamic b) { | ||||||||||||
| if (a is Map && b is Map) { | ||||||||||||
| if (a.length != b.length) return false; | ||||||||||||
| for (final key in a.keys) { | ||||||||||||
| if (!b.containsKey(key)) return false; | ||||||||||||
| if (!deepEqualsWithRationalTolerance(a[key], b[key])) return false; | ||||||||||||
| } | ||||||||||||
| return true; | ||||||||||||
| } | ||||||||||||
| // Handle rational/decimal representations before generic list comparison | ||||||||||||
| if (_isRationalList(a) && _isRationalList(b)) { | ||||||||||||
| final ra = _rationalListToRational(a as List<dynamic>); | ||||||||||||
| final rb = _rationalListToRational(b as List<dynamic>); | ||||||||||||
| return ra == rb; | ||||||||||||
| } | ||||||||||||
| if (a is List && b is List) { | ||||||||||||
| if (a.length != b.length) return false; | ||||||||||||
| for (int i = 0; i < a.length; i++) { | ||||||||||||
| if (!deepEqualsWithRationalTolerance(a[i], b[i])) return false; | ||||||||||||
| } | ||||||||||||
| return true; | ||||||||||||
| } | ||||||||||||
| return a == b; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| bool _isRationalList(dynamic x) { | ||||||||||||
| return x is List && | ||||||||||||
| x.length == 2 && | ||||||||||||
| x[0] is int && | ||||||||||||
| x[1] is List && | ||||||||||||
| (x[1] as List).every((e) => e is int); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| Rational _rationalListToRational(List<dynamic> rat) { | ||||||||||||
| final int numerator = rat[0] as int; | ||||||||||||
| final List<int> denominators = List<int>.from(rat[1] as List); | ||||||||||||
| if (denominators.any((d) => d == 0)) { | ||||||||||||
| if (numerator == 0 || numerator == 1) { | ||||||||||||
| return Rational.zero; | ||||||||||||
| } | ||||||||||||
| throw ArgumentError('Denominator cannot be zero in rational list: $rat'); | ||||||||||||
| } | ||||||||||||
| Rational value = Rational.fromInt(numerator); | ||||||||||||
| for (final d in denominators) { | ||||||||||||
|
||||||||||||
| for (final d in denominators) { | |
| for (final d in denominators) { | |
| if (d == 0) { | |
| throw ArgumentError('Denominator cannot be zero in rational list: $rat'); | |
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation should clarify the expected format of rational representations (e.g., [numerator, [denominator1, denominator2, ...]] for chained division) and provide examples of what constitutes a rational list.