diff --git a/CHANGELOG.md b/CHANGELOG.md index f06f8c1..20eb42c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 2.4.0 + +* Add `equalTo`, `notEqualTo`, `greaterThan`, `lessThan`, `greateOrEqualTo`, and + `lessOrEqualTo` to `Expression`. + ## 2.3.0 * Using `equalsDart` and expecting `dartfmt` by default is *deprecated*. This diff --git a/lib/src/specs/expression.dart b/lib/src/specs/expression.dart index 8566ea5..4b82fd6 100644 --- a/lib/src/specs/expression.dart +++ b/lib/src/specs/expression.dart @@ -76,6 +76,60 @@ abstract class Expression implements Spec { ); } + /// Returns the result of `this` `==` [other]. + Expression equalTo(Expression other) { + return new BinaryExpression._( + expression, + other, + '==', + ); + } + + /// Returns the result of `this` `!=` [other]. + Expression notEqualTo(Expression other) { + return new BinaryExpression._( + expression, + other, + '!=', + ); + } + + /// Returns the result of `this` `>` [other]. + Expression greaterThan(Expression other) { + return new BinaryExpression._( + expression, + other, + '>', + ); + } + + /// Returns the result of `this` `<` [other]. + Expression lessThan(Expression other) { + return new BinaryExpression._( + expression, + other, + '<', + ); + } + + /// Returns the result of `this` `>=` [other]. + Expression greaterOrEqualTo(Expression other) { + return new BinaryExpression._( + expression, + other, + '>=', + ); + } + + /// Returns the result of `this` `<=` [other]. + Expression lessOrEqualTo(Expression other) { + return new BinaryExpression._( + expression, + other, + '<=', + ); + } + Expression conditional(Expression whenTrue, Expression whenFalse) { return new BinaryExpression._( expression, diff --git a/pubspec.yaml b/pubspec.yaml index 5b84a70..4c0a6ae 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: code_builder -version: 2.3.0 +version: 2.4.0-dev description: A fluent API for generating Dart code author: Dart Team homepage: https://github.com/dart-lang/code_builder diff --git a/test/specs/code/expression_test.dart b/test/specs/code/expression_test.dart index 7f04c73..920f1a6 100644 --- a/test/specs/code/expression_test.dart +++ b/test/specs/code/expression_test.dart @@ -359,6 +359,48 @@ void main() { ); }); + test('should emit an equality check', () { + expect( + refer('foo').equalTo(literalString('bar')), + equalsDart("foo == 'bar'"), + ); + }); + + test('should emit an inequality check', () { + expect( + refer('foo').notEqualTo(literalString('bar')), + equalsDart("foo != 'bar'"), + ); + }); + + test('should emit an greater than check', () { + expect( + refer('foo').greaterThan(literalString('bar')), + equalsDart("foo > 'bar'"), + ); + }); + + test('should emit an less than check', () { + expect( + refer('foo').lessThan(literalString('bar')), + equalsDart("foo < 'bar'"), + ); + }); + + test('should emit an greater or equals check', () { + expect( + refer('foo').greaterOrEqualTo(literalString('bar')), + equalsDart("foo >= 'bar'"), + ); + }); + + test('should emit an less or equals check', () { + expect( + refer('foo').lessOrEqualTo(literalString('bar')), + equalsDart("foo <= 'bar'"), + ); + }); + test('should emit a conditional', () { expect( refer('foo').conditional(literal(1), literal(2)),