Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 2c0ebca

Browse files
eredomatanlurey
authored andcommitted
Add Expression.operators (add/substract/divide/multiply/euclidean modulo) (#218)
Closes #216
1 parent 61d4158 commit 2c0ebca

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

lib/src/specs/expression.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,51 @@ abstract class Expression implements Spec {
142142
);
143143
}
144144

145+
/// Returns the result of `this` `+` [other].
146+
Expression operatorAdd(Expression other) {
147+
return new BinaryExpression._(
148+
expression,
149+
other,
150+
'+',
151+
);
152+
}
153+
154+
/// Returns the result of `this` `-` [other].
155+
Expression operatorSubstract(Expression other) {
156+
return new BinaryExpression._(
157+
expression,
158+
other,
159+
'-',
160+
);
161+
}
162+
163+
/// Returns the result of `this` `/` [other].
164+
Expression operatorDivide(Expression other) {
165+
return new BinaryExpression._(
166+
expression,
167+
other,
168+
'/',
169+
);
170+
}
171+
172+
/// Returns the result of `this` `*` [other].
173+
Expression operatorMultiply(Expression other) {
174+
return new BinaryExpression._(
175+
expression,
176+
other,
177+
'*',
178+
);
179+
}
180+
181+
/// Returns the result of `this` `%` [other].
182+
Expression operatorEuclideanModulo(Expression other) {
183+
return new BinaryExpression._(
184+
expression,
185+
other,
186+
'%',
187+
);
188+
}
189+
145190
Expression conditional(Expression whenTrue, Expression whenFalse) {
146191
return new BinaryExpression._(
147192
expression,

test/specs/code/expression_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,28 @@ void main() {
412412
equalsDart('foo ? 1 : 2'),
413413
);
414414
});
415+
416+
test('should emit an operator add call', () {
417+
expect(refer('foo').operatorAdd(refer('foo2')), equalsDart('foo + foo2'));
418+
});
419+
420+
test('should emit an operator substract call', () {
421+
expect(refer('foo').operatorSubstract(refer('foo2')),
422+
equalsDart('foo - foo2'));
423+
});
424+
425+
test('should emit an operator divide call', () {
426+
expect(
427+
refer('foo').operatorDivide(refer('foo2')), equalsDart('foo / foo2'));
428+
});
429+
430+
test('should emit an operator multiply call', () {
431+
expect(
432+
refer('foo').operatorMultiply(refer('foo2')), equalsDart('foo * foo2'));
433+
});
434+
435+
test('should emit an euclidean modulo operator call', () {
436+
expect(refer('foo').operatorEuclideanModulo(refer('foo2')),
437+
equalsDart('foo % foo2'));
438+
});
415439
}

0 commit comments

Comments
 (0)