Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions lib/src/specs/expression.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions test/specs/code/expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
}