Skip to content

Commit 01f3ba9

Browse files
authored
bugfix Tracker.record to not discard default values (#27)
1 parent d10156b commit 01f3ba9

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

lib/src/tracker.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class Tracker<TrackableType extends Trackable> {
137137
void record(TrackableType trackable,
138138
{Map<String, String?> defaults = const {}}) {
139139
for (final dumper in _dumpers) {
140-
dumper.record(trackable);
140+
dumper.record(trackable, defaults: defaults);
141141
}
142142
}
143143

test/tracker_test.dart

+27-17
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ class FruitEvent implements Trackable {
2121
final LogicValue apple;
2222
final String banana;
2323
final int carrot;
24+
final String? pear;
2425

25-
FruitEvent(this.apple, this.banana, this.carrot);
26+
FruitEvent(this.apple, this.banana, this.carrot, this.pear);
2627

2728
@override
2829
String? trackerString(TrackerField field) {
@@ -35,6 +36,8 @@ class FruitEvent implements Trackable {
3536
return carrot.toString();
3637
case 'Durian':
3738
return (carrot * 2).toRadixString(16);
39+
case 'Pear':
40+
return pear;
3841
}
3942
return null;
4043
}
@@ -48,17 +51,22 @@ void main() {
4851
const TrackerField('Apple', columnWidth: 10),
4952
const TrackerField('Banana', columnWidth: 5),
5053
const TrackerField('Carrot', columnWidth: 12, justify: Justify.center),
51-
const TrackerField('Durian', columnWidth: 12, mapOnly: true)
54+
const TrackerField('Durian', columnWidth: 12, mapOnly: true),
55+
const TrackerField('Pear', columnWidth: 12)
5256
],
5357
)
54-
..record(FruitEvent(LogicValue.ofString('1x0'), 'banana', 25))
5558
..record(
56-
FruitEvent(LogicValue.ofString('1x01111000011010101'), 'aaa', 5));
59+
FruitEvent(LogicValue.ofString('1x0'), 'banana', 25, 'green pear'),
60+
defaults: {'Pear': 'red pear'})
61+
..record(
62+
FruitEvent(
63+
LogicValue.ofString('1x01111000011010101'), 'aaa', 5, null),
64+
defaults: {'Pear': 'red pear'});
5765

5866
// Expect JSON log to look like:
5967
// {"records":[
60-
// {"Apple": "3'b1x0", "Banana": "banana", "Carrot": "25", "Durian": "32"}
61-
// , {"Apple": "19'b1x01111000011010101", "Banana": "aaa", "Carrot": "4", "Durian": "8"}
68+
// {"Apple": "3'b1x0", "Banana": "banana", "Carrot": "25", "Durian": "32", "Pear": "green pear"}
69+
// , {"Apple": "19'b1x01111000011010101", "Banana": "aaa", "Carrot": "4", "Durian": "8", "Pear": "red pear"}
6270
// ]}
6371

6472
await tracker.terminate();
@@ -68,22 +76,24 @@ void main() {
6876
expect(jsonOutput['records'].length, equals(2));
6977
expect(jsonOutput['records'][0]['Banana'], equals('banana'));
7078
expect(jsonOutput['records'][1]['Durian'], equals('a'));
79+
expect(jsonOutput['records'][0]['Pear'], equals('green pear'));
80+
expect(jsonOutput['records'][1]['Pear'], equals('red pear'));
7181

7282
// Expect table log to look like:
73-
// ---------------------------------------
74-
// | A | B | C |
75-
// | P | A | A |
76-
// | P | N | R |
77-
// | L | A | R |
78-
// | E | N | O |
79-
// | | A | T |
80-
// ---------------------------------------
81-
// | 3'b1x0 | bana* | 25 | {Apple: 3'b1x0, Banana: banana, Carrot: 25, Durian: 32}
82-
// | 19'b1x011* | aaa | 5 | {Apple: 19'b1x01111000011010101, Banana: aaa, Carrot: 5, Durian: a}
83+
// ------------------------------------------------------
84+
// | A | B | C | P |
85+
// | P | A | A | E |
86+
// | P | N | R | A |
87+
// | L | A | R | R |
88+
// | E | N | O | |
89+
// | | A | T | |
90+
// ------------------------------------------------------
91+
// | 3'b1x0 | bana* | 25 | green pear | {Apple: 3'b1x0, Banana: banana, Carrot: 25, Durian: 32, Pear: green pear}
92+
// | 19'b1x011* | aaa | 5 | red pear | {Apple: 19'b1x01111000011010101, Banana: aaa, Carrot: 5, Durian: a, Pear: red pear}
8393

8494
final logOutput = File(tracker.tableFileName).readAsStringSync();
8595
expect(logOutput.contains('bana*'), equals(true));
86-
expect(logOutput.split('\n')[1].split('|').length, equals(5));
96+
expect(logOutput.split('\n')[1].split('|').length, equals(6));
8797

8898
File(tracker.jsonFileName).deleteSync();
8999
File(tracker.tableFileName).deleteSync();

0 commit comments

Comments
 (0)