Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
apesic committed Sep 17, 2020
1 parent 282e5d1 commit 92372e2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
14 changes: 11 additions & 3 deletions lib/rpn_stack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ class RpnStack {
stack.add(EditableItem.blank());
}
RpnStack.clone(RpnStack source) {
// XXX need to deep copy items
stack.addAll(source.stack);
for (final o in source.stack) {
StackItem clone;
if (o is EditableItem) {
clone = EditableItem(o);
} else if (o is RealizedItem) {
clone = RealizedItem(o.value);
}
stack.add(clone);
}
appendNew = source.appendNew;
}
final List<StackItem> stack = [];
Expand Down Expand Up @@ -45,8 +52,9 @@ class RpnStack {
}

void advance() {
final current = stack.first;
_realizeStack();
push(EditableItem(stack[0]));
push(EditableItem(current));
}

void swap() {
Expand Down
25 changes: 15 additions & 10 deletions lib/stack_item_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@ class StackItemWidget extends StatelessWidget {
switch (value) {
case 'copy':
Clipboard.setData(ClipboardData(text: item.toString())).then(
// XXX get snackbar to match theme colors
(_) => Scaffold.of(context).showSnackBar(const SnackBar(
content: Text('Copied to clipboard'),
duration: Duration(seconds: 1),
)));
(_) => Scaffold.of(context).showSnackBar(
const SnackBar(
content: Text('Copied to clipboard'),
duration: Duration(seconds: 2),
),
),
);
break;
case 'paste':
num newVal;
Clipboard.getData('text/plain').then((value) {
num newVal;
final s = value.text;
try {
if (s.contains('.')) {
Expand All @@ -67,10 +69,13 @@ class StackItemWidget extends StatelessWidget {
newVal = int.parse(s);
}
} on FormatException catch (_) {
Scaffold.of(context).showSnackBar(const SnackBar(
content: Text('Clipboard is not a valid number'),
duration: Duration(seconds: 1),
));
Scaffold.of(context).showSnackBar(
SnackBar(
content: const Text('Clipboard is not a valid number'),
backgroundColor: Theme.of(context).errorColor,
duration: const Duration(seconds: 3),
),
);
return 0;
}
onPaste(newVal);
Expand Down
5 changes: 5 additions & 0 deletions test/rpn_stack_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ void main() {
expect(s.isEmpty, true);
});

test('advancing when empty has no result', () {
final s = RpnStack()..advance();
expect(s.isEmpty, true);
});

test('characters are appended', () {
final s = RpnStack()
..appendCurrent('2')
Expand Down

0 comments on commit 92372e2

Please sign in to comment.