From bc5d83fbdb37ff348c8bb737ec09cf7e9a5c3b4e Mon Sep 17 00:00:00 2001 From: Eric Schneller Date: Tue, 31 Jul 2018 19:48:29 +0200 Subject: [PATCH] Add Expression.operators (add/substract/divide/multiply/euclidean modulo) Closes #216 --- lib/src/specs/expression.dart | 45 ++++++++++++++++++++++++++++ test/specs/code/expression_test.dart | 24 +++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/lib/src/specs/expression.dart b/lib/src/specs/expression.dart index 00f5e33..e05f91b 100644 --- a/lib/src/specs/expression.dart +++ b/lib/src/specs/expression.dart @@ -142,6 +142,51 @@ abstract class Expression implements Spec { ); } + /// Returns the result of `this` `+` [other]. + Expression operatorAdd(Expression other) { + return new BinaryExpression._( + expression, + other, + '+', + ); + } + + /// Returns the result of `this` `-` [other]. + Expression operatorSubstract(Expression other) { + return new BinaryExpression._( + expression, + other, + '-', + ); + } + + /// Returns the result of `this` `/` [other]. + Expression operatorDivide(Expression other) { + return new BinaryExpression._( + expression, + other, + '/', + ); + } + + /// Returns the result of `this` `*` [other]. + Expression operatorMultiply(Expression other) { + return new BinaryExpression._( + expression, + other, + '*', + ); + } + + /// Returns the result of `this` `%` [other]. + Expression operatorEuclideanModulo(Expression other) { + return new BinaryExpression._( + expression, + other, + '%', + ); + } + Expression conditional(Expression whenTrue, Expression whenFalse) { return new BinaryExpression._( expression, diff --git a/test/specs/code/expression_test.dart b/test/specs/code/expression_test.dart index df1888d..e6f2e65 100644 --- a/test/specs/code/expression_test.dart +++ b/test/specs/code/expression_test.dart @@ -412,4 +412,28 @@ void main() { equalsDart('foo ? 1 : 2'), ); }); + + test('should emit an operator add call', () { + expect(refer('foo').operatorAdd(refer('foo2')), equalsDart('foo + foo2')); + }); + + test('should emit an operator substract call', () { + expect(refer('foo').operatorSubstract(refer('foo2')), + equalsDart('foo - foo2')); + }); + + test('should emit an operator divide call', () { + expect( + refer('foo').operatorDivide(refer('foo2')), equalsDart('foo / foo2')); + }); + + test('should emit an operator multiply call', () { + expect( + refer('foo').operatorMultiply(refer('foo2')), equalsDart('foo * foo2')); + }); + + test('should emit an euclidean modulo operator call', () { + expect(refer('foo').operatorEuclideanModulo(refer('foo2')), + equalsDart('foo % foo2')); + }); }