allow negative assertion in injection tests (#4107)

* tests(vue, svelte): strengthen tests

* fix(html, vue, svelte): fix wrong test format

* allow negative assertions in injection tests
This commit is contained in:
lucario387 2023-01-07 19:22:20 +09:00 committed by GitHub
parent 0922634d37
commit 85d9534491
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 135 deletions

View file

@ -28,33 +28,51 @@ local function check_assertions(file)
local row = assertion.position.row
local col = assertion.position.column
local neg_assert = assertion.expected_capture_name:match "^!"
assertion.expected_capture_name = neg_assert and assertion.expected_capture_name:sub(2)
or assertion.expected_capture_name
local found = false
self.tree:for_each_tree(function(tstree, tree)
if not tstree then
return
end
local root = tstree:root()
if
ts_utils.is_in_node_range(root, row, col)
and assertion.expected_capture_name == tree:lang()
and root ~= top_level_root
then
--- If there are multiple tree with the smallest range possible
--- Check all of them to see if they fit or not
if not ts_utils.is_in_node_range(root, row, col) or root == top_level_root then
return
end
if assertion.expected_capture_name == tree:lang() then
found = true
end
end, true)
assert.True(
found,
"Error in at "
.. file
.. ":"
.. (row + 1)
.. ":"
.. (col + 1)
.. ': expected "'
.. assertion.expected_capture_name
.. '" to be injected here!'
)
if neg_assert then
assert.False(
found,
"Error in at "
.. file
.. ":"
.. (row + 1)
.. ":"
.. (col + 1)
.. ': expected "'
.. assertion.expected_capture_name
.. '" not to be injected here!'
)
else
assert.True(
found,
"Error in at "
.. file
.. ":"
.. (row + 1)
.. ":"
.. (col + 1)
.. ': expected "'
.. assertion.expected_capture_name
.. '" to be injected here!'
)
end
end
end

View file

@ -5,38 +5,20 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/style.css" rel="stylesheet">
<style>
footer{
/* ^ css
*/
}
</style>
<style title="Test Style without type attribute">
footer{
/* ^ css
*/
}
</style>
<style type="text/css" title="test style with defined type attribute">
footer{
/* ^ css
*/
}
</style>
<style> footer{ } </style>
<!-- ^ css -->
<style title="Test Style without type attribute"> footer{ } </style>
<!-- ^ css -->
<style type="text/css" title="test style with defined type attribute"> footer{ } </style>
<!-- ^ css -->
</head>
<body>
<script>
const x = 1
// ^ javascript
</script>
<script defer>
const x = 1
// ^ javascript
</script>
<script type="text/javascript">
const x = 1
// ^ javascript
</script>
<script> const x = 1 </script>
<!-- ^ javascript -->
<script defer> const x = 1 </script>
<!-- ^ javascript -->
<script type="text/javascript"> const x = 1 </script>
<!-- ^ javascript -->
<div style="height: 100%">
<!-- ^ css -->
Test div to test css injections for style attributes

View file

@ -1,29 +1,14 @@
<script>
import Button from "./Button.svelte";
// ^ javascript
</script>
<script lang="ts">
const foo: number = 1
// ^ typescript
</script>
<script> import Button from "./Button.svelte"; </script>
<!-- ^ javascript -->
<script lang="ts"> const foo: number = 1 </script>
<!-- ^ typescript -->
<!-- ^ !javascript -->
<style>
main {
font-family: sans-serif;
text-align: center;
/* ^ css
*/
}
</style>
<style lang="scss">
main {
font-family: sans-serif;
text-align: center;
&:hover {
// ^ scss
}
}
</style>
<style> main { font-family: sans-serif; text-align: center; } </style>
<!-- ^ css -->
<style lang="scss"> main { &:hover { } } </style>
<!-- ^ scss -->
<!-- ^ !css -->
<main>
<h1>Test file</h1>

View file

@ -0,0 +1,4 @@
<script lang="ts"> const foo: number = "1" </script>
<!-- ^ !javascript -->
<style lang="scss"> .bar { &-baz { &.page{ } } } </style>
<!-- ^ !css -->

View file

@ -1,68 +1,28 @@
<template>
<span>{{"Text inside interpolation"}}</span>
<!-- ^ javascript
-->
<!-- ^ javascript -->
<template lang="pug">
ul
li(v-for="item in items")
a(v-if="item.type == 'link'" :href="item.url") some link title in pug:
<!-- ^ pug
-->
</template>
<template lang="pug"> a(:href="url") some link title in pug: </template>
<!-- ^ pug -->
<template v-if="'text inside directives'"></template>
<!-- ^ javascript
-->
<!-- ^ javascript -->
</template>
<script>
const foo = "1"
// ^ javascript
</script>
<script defer>
const foo = "1"
// ^ javascript
</script>
<script lang="js">
const foo = "1"
// ^ javascript
</script>
<script lang="ts">
const foo: number = "1"
// ^ typescript
</script>
<style>
.bar {
/* ^ css
*/
}
</style>
<style scoped>
.page.page--news {
padding: calc(var(--header-height)) 1rem 0 1rem;
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
min-height: calc(var(--vh, 1vh) * 100 - var(--header-height));
background: rebeccapurple;
/* ^ css
*/
}
</style>
<style lang="css">
.bar {
justify-content: center;
/* ^ css
*/
}
</style>
<style lang="scss">
.bar {
&-baz {
&.page{
// ^ scss
}
}
}
</style>
<script> const foo = "1" </script>
<!-- ^ javascript -->
<script defer> const foo = "1" </script>
<!-- ^ javascript -->
<script lang="js">function x(){ return 1;}</script>
<!-- ^ javascript -->
<script lang="ts"> const foo: number = "1" </script>
<!-- ^ typescript -->
<!-- ^ !javascript -->
<style> .bar { .foo{ } } </style>
<!-- ^ css -->
<style scoped> .page.page--news { background: rebeccapurple; } </style>
<!-- ^ css -->
<style lang="css"> .bar { justify-content: center; } </style>
<!-- ^ css -->
<style lang="scss"> .bar { &-baz { } } </style>
<!-- ^ scss -->
<!-- ^ !css -->