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

Commit 10b9b9e

Browse files
nateboschmatanlurey
authored andcommitted
Add comparison operators (#177)
1 parent 6b253e7 commit 10b9b9e

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.4.0
2+
3+
* Add `equalTo`, `notEqualTo`, `greaterThan`, `lessThan`, `greateOrEqualTo`, and
4+
`lessOrEqualTo` to `Expression`.
5+
16
## 2.3.0
27

38
* Using `equalsDart` and expecting `dartfmt` by default is *deprecated*. This

lib/src/specs/expression.dart

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,60 @@ abstract class Expression implements Spec {
7676
);
7777
}
7878

79+
/// Returns the result of `this` `==` [other].
80+
Expression equalTo(Expression other) {
81+
return new BinaryExpression._(
82+
expression,
83+
other,
84+
'==',
85+
);
86+
}
87+
88+
/// Returns the result of `this` `!=` [other].
89+
Expression notEqualTo(Expression other) {
90+
return new BinaryExpression._(
91+
expression,
92+
other,
93+
'!=',
94+
);
95+
}
96+
97+
/// Returns the result of `this` `>` [other].
98+
Expression greaterThan(Expression other) {
99+
return new BinaryExpression._(
100+
expression,
101+
other,
102+
'>',
103+
);
104+
}
105+
106+
/// Returns the result of `this` `<` [other].
107+
Expression lessThan(Expression other) {
108+
return new BinaryExpression._(
109+
expression,
110+
other,
111+
'<',
112+
);
113+
}
114+
115+
/// Returns the result of `this` `>=` [other].
116+
Expression greaterOrEqualTo(Expression other) {
117+
return new BinaryExpression._(
118+
expression,
119+
other,
120+
'>=',
121+
);
122+
}
123+
124+
/// Returns the result of `this` `<=` [other].
125+
Expression lessOrEqualTo(Expression other) {
126+
return new BinaryExpression._(
127+
expression,
128+
other,
129+
'<=',
130+
);
131+
}
132+
79133
Expression conditional(Expression whenTrue, Expression whenFalse) {
80134
return new BinaryExpression._(
81135
expression,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: code_builder
2-
version: 2.3.0
2+
version: 2.4.0-dev
33
description: A fluent API for generating Dart code
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/dart-lang/code_builder

test/specs/code/expression_test.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,48 @@ void main() {
359359
);
360360
});
361361

362+
test('should emit an equality check', () {
363+
expect(
364+
refer('foo').equalTo(literalString('bar')),
365+
equalsDart("foo == 'bar'"),
366+
);
367+
});
368+
369+
test('should emit an inequality check', () {
370+
expect(
371+
refer('foo').notEqualTo(literalString('bar')),
372+
equalsDart("foo != 'bar'"),
373+
);
374+
});
375+
376+
test('should emit an greater than check', () {
377+
expect(
378+
refer('foo').greaterThan(literalString('bar')),
379+
equalsDart("foo > 'bar'"),
380+
);
381+
});
382+
383+
test('should emit an less than check', () {
384+
expect(
385+
refer('foo').lessThan(literalString('bar')),
386+
equalsDart("foo < 'bar'"),
387+
);
388+
});
389+
390+
test('should emit an greater or equals check', () {
391+
expect(
392+
refer('foo').greaterOrEqualTo(literalString('bar')),
393+
equalsDart("foo >= 'bar'"),
394+
);
395+
});
396+
397+
test('should emit an less or equals check', () {
398+
expect(
399+
refer('foo').lessOrEqualTo(literalString('bar')),
400+
equalsDart("foo <= 'bar'"),
401+
);
402+
});
403+
362404
test('should emit a conditional', () {
363405
expect(
364406
refer('foo').conditional(literal(1), literal(2)),

0 commit comments

Comments
 (0)