-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolvable boolean expressions #164
Conversation
670e440
to
594f603
Compare
22ec16b
to
624bc5a
Compare
removed static boolean expressions added test for rendering template with boolean expression
624bc5a
to
83113ab
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than the changelog entry, LGTM.
CHANGELOG.md
Outdated
[#178](https://github.com/stencilproject/Stencil/pull/178) | ||
|
||
- Now boolean expressions results can be rendered, i.e `{{ name == "John" }}` will render `true` or `false` depending on the evaluation result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot the .
and 2 spaces. Also remove the empty line above.
/// Resolves a variable in the given context as boolean | ||
func resolve(context: Context, variable: Resolvable) throws -> Bool { | ||
func evaluate(context: Context) throws -> Bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird, I think I've seen this change a few times in other PRs? 😕😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe, some features required similar changes
Before I forget: This PR should document the changes in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started reviewing this a while back, gonna submit my old review for now.
let expression = try! parseExpression(components: ["lhs", "or", "rhs"], tokenParser: parser) | ||
|
||
let components = ["lhs", "or", "rhs"] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing newlines
@@ -95,11 +100,17 @@ func testExpressions() { | |||
$0.it("returns truthy for positive expressions") { | |||
let expression = NotExpression(expression: StaticExpression(value: true)) | |||
try expect(expression.evaluate(context: Context())).to.beFalse() | |||
|
|||
try expect(Template(templateString: "{% if true %}true{% else %}false{% endif %}").render(Context())) == "true" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is the unit tests for expression, I do not want to test other units in this file. This is further becoming an integration test when we are creating a template and testing other functionality such as the `if statements.
@@ -136,163 +147,172 @@ func testExpressions() { | |||
} | |||
} | |||
|
|||
func expectExpression(with components: [String], context: [String: Any], toBe expected: Bool) throws { | |||
let expression = try parseExpression(components: components, tokenParser: parser) | |||
try expect(expression.evaluate(context: Context(dictionary: context))) == expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will cause the source maps for the thrown error when the tests fail to be raised from here making test failures harder to pinpoint. To solve this we can grab the file and line number from the expectExpression
invocation and pass it to expect:
func expectExpression(..., file: String = #file, line: Int = #line, function: String = #function) {
...
try expect(..., file: file, line: line, function: function) == expected
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!
@ilyapuchka why close this? The docs are there, it should be GTM. |
there were conflicts and tests started to fail for some reason after I merged, I'll need to to investigate that but I also don't feel like this is a top priority considering other open PRs |
Which tests? CI succeeds for the latest commit in this PR. |
Only because I didn't push 😉 |
This PR adds ability to resolve boolean expressions the same way as variables like
{{ this == that }}
This have a potential to simplify templates when there is a need to evaluate boolean expression and output its result, true or false, in a template, i.e. when generating html to enable or disable html elements. Instead of
{% if this == that %}true{% else %}false{% endif %}
it will be possible to write just{{ this == that }}
Also this PR adds support for parsing static boolean expressions, i.e. if
{% if true %}
. There was implementation for such expression and tests for it, but it was never actually parsed. It may not be very important, but can be helpful during template development.