Skip to content

Commit

Permalink
fix test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sjhorn committed Apr 13, 2024
1 parent dcfe3bc commit 382154e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
18 changes: 5 additions & 13 deletions lib/src/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ typedef CellChangedCallback = void Function(

class Engine with Iterable<A1> {
static final formatExpression = FormatExpression().build();
static final evaluator = Evaluator().build();
static final validator = ValidateExpression().build();
// static final evaluator = Evaluator().build();
// static final validator = ValidateExpression().build();

final HashSet<CellChangedCallback> _listeners =
HashSet<CellChangedCallback>();
Expand Down Expand Up @@ -127,13 +127,11 @@ class Engine with Iterable<A1> {

Iterable<A1> get keys => _cellMap.keys;

static bool _all(cell) => true;

(List<int>, List<int>) columnsAndRows({
bool Function(A1 cell) criteria = _all,
bool Function(A1 cell)? criteria,
Iterable<A1>? a1Iterable,
}) {
final cellsInRange = (a1Iterable ?? keys).where(criteria);
final cellsInRange = (a1Iterable ?? keys).where(criteria ?? (_) => true);

if (cellsInRange.isEmpty) {
return ([], []);
Expand Down Expand Up @@ -399,13 +397,7 @@ class Engine with Iterable<A1> {
final a1 = A1.fromVector(column, row);
final cell = _cellMap[a1];
buffer.write(cell?.contentString.padRight(20) ?? ''.padRight(20));
buffer.write(' | ');
if (cell?.content is ExpressionContent) {
buffer.write(cell?.formattedString(20) ?? ''.padRight(20));
} else {
buffer.write(''.padRight(20));
}
buffer.write(' | ');
buffer.write(' | ${cell?.formattedString(20) ?? "".padRight(20)} | ');
}
buffer.write('\n');
}
Expand Down
1 change: 0 additions & 1 deletion lib/src/model/cell_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ enum CellFormat {
CellFormat.integer => 'I',
CellFormat.left => 'L',
CellFormat.right => 'R',
_ => null,
};
}
55 changes: 52 additions & 3 deletions test/visicalc_engine_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ void main() {
setUp(() {
engine = Engine(sheet);
});

test(' - errors', () async {
final engine = Engine({'A1'.a1: '@'});
expect(engine['a1'.a1]?.formulaType, isA<ErrorType>());
expect(engine['a1'.a1]?.resultType, isA<ErrorResult>());

expect(() => Engine({'A1'.a1: '@'}, parseErrorThrows: true),
throwsA(isA<FormatException>()));
});
test(' - a negative number', () async {
expect(engine['A1'.a1]!.formulaType, equals(NegativeOp(NumType(12.2))));
expect(engine['A1'.a1]!.resultType, equals(NumberResult(-12.2)));
Expand Down Expand Up @@ -256,12 +265,19 @@ void main() {
Engine engine = Engine({});

List<(CellChangeType, Set<A1>)> changes = [];
void listener(a1Set, changeType) {
changes.add((changeType, a1Set));
}

setUp(() {
changes.clear();
engine = Engine(sheet);
engine.addListener((a1Set, changeType) {
changes.add((changeType, a1Set));
});
engine.addListener(listener);
});
test(' - remove listener', () async {
engine.removeListener(listener);
engine['b6'.a1] = '-12.2';
expect(changes.isEmpty, isTrue);
});
test(' - add single cell without references triggers listeners', () async {
engine['b6'.a1] = '-12.2';
Expand Down Expand Up @@ -499,4 +515,37 @@ void main() {
expect(engine.referencesTo('B2'.a1), containsAll({'B10'}.a1));
});
});
group('misc methods', () {
final engine = Engine({
'A1'.a1: 'A2',
'A2'.a1: '12',
'A3'.a1: '/-=',
'A3'.a1: '/F',
});
test(' - columnsAndRows', () async {
expect(
engine.columnsAndRows(criteria: (a1) => false).$1, equals(<int>[]));
expect(
engine.columnsAndRows(criteria: (a1) => false).$2, equals(<int>[]));
});
test(' - iterator', () async {
expect(engine.iterator.moveNext(), isTrue);
});
test(' - toString', () async {
expect(
engine.toString(),
equals(' A fx | A | \n'
'------------------------------------------------\n'
' 1 A2 | 12 | \n'
' 2 12 | 12 | \n'
' 3 = | ==================== | \n'
''));
});
test(' - referenceToString', () async {
expect(
engine.referencesToString(),
equals('A2: {A1} \n'),
);
});
});
}

0 comments on commit 382154e

Please sign in to comment.