Skip to content

Commit

Permalink
test: add more tests for edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 28, 2020
1 parent 4e86c7d commit 9c67e6a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Tags/Each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function getLoopItemAndIndex (lhsExpression: any, parser: Parser, filename: stri
[expressions.SequenceExpression, expressions.Identifier],
() => {
unallowedExpression(
`invalid left hand side "${lhsExpression.type}" for the @each tag`,
`invalid left hand side "${lhsExpression.type}" expression for the @each tag`,
filename,
parser.utils.getExpressionLoc(lhsExpression),
)
Expand Down Expand Up @@ -119,7 +119,7 @@ export const eachTag: TagContract = {
*/
isSubsetOf(
expression,
[expressions.BinaryExpression, expression.SequenceExpression],
[expressions.BinaryExpression],
() => {
unallowedExpression(
`"${token.properties.jsArg}" is not valid expression for the @each tag`,
Expand Down
93 changes: 93 additions & 0 deletions test/tags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,57 @@ test.group('Include', (group) => {
})
})

test.group('IncludeIf', (group) => {
group.afterEach(async () => {
await fs.cleanup()
})

test('raise errors when not passing sequence expression', async (assert) => {
assert.plan(2)

const templateContent = dedent`We are writing a bad include condition
@includeIf(foo)`

await fs.add('foo.edge', templateContent)
try {
compiler.compile('foo')
} catch (error) {
assert.equal(error.message, '"foo" is not a valid argument type for the @includeIf tag')
assert.equal(error.stack.split('\n')[1], ` at anonymous (${join(fs.basePath, 'foo.edge')}:2:11)`)
}
})

test('raise errors when passing more than 2 arguments', async (assert) => {
assert.plan(2)

const templateContent = dedent`We are writing a bad include condition
@includeIf(foo, bar, baz)`

await fs.add('foo.edge', templateContent)
try {
compiler.compile('foo')
} catch (error) {
assert.equal(error.message, '@includeIf expects a total of 2 arguments')
assert.equal(error.stack.split('\n')[1], ` at anonymous (${join(fs.basePath, 'foo.edge')}:2:11)`)
}
})

test('raise errors when 1st argument is a sequence expression', async (assert) => {
assert.plan(2)

const templateContent = dedent`We are writing a bad include condition
@includeIf((foo, bar), baz)`

await fs.add('foo.edge', templateContent)
try {
compiler.compile('foo')
} catch (error) {
assert.equal(error.message, '"SequenceExpression" is not a valid 1st argument type for the @includeIf tag')
assert.equal(error.stack.split('\n')[1], ` at anonymous (${join(fs.basePath, 'foo.edge')}:2:12)`)
}
})
})

test.group('Component', (group) => {
group.afterEach(async () => {
await fs.cleanup()
Expand Down Expand Up @@ -292,3 +343,45 @@ test.group('Layouts', (group) => {
}
})
})

test.group('Each tag', (group) => {
group.afterEach(async () => {
await fs.cleanup()
})

test('raise errors when arg is not a binary expression', async (assert) => {
assert.plan(2)

const templateContent = dedent`Hello everyone!
We are writing a bad each condition
@each(users)
@endeach`

await fs.add('foo.edge', templateContent)
try {
compiler.compile('foo')
} catch (error) {
assert.equal(error.message, '"users" is not valid expression for the @each tag')
assert.equal(error.line, 4)
}
})

test('raise errors when lhs of binary expression is not an indentifier', async (assert) => {
assert.plan(2)

const templateContent = dedent`Hello everyone!
We are writing a bad each condition
@each('user' in users)
@endeach`

await fs.add('foo.edge', templateContent)
try {
compiler.compile('foo')
} catch (error) {
assert.equal(error.message, 'invalid left hand side \"Literal\" expression for the @each tag')
assert.equal(error.line, 4)
}
})
})

0 comments on commit 9c67e6a

Please sign in to comment.