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

View file

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

View file

@ -1,29 +1,14 @@
<script> <script> import Button from "./Button.svelte"; </script>
import Button from "./Button.svelte"; <!-- ^ javascript -->
// ^ javascript <script lang="ts"> const foo: number = 1 </script>
</script> <!-- ^ typescript -->
<script lang="ts"> <!-- ^ !javascript -->
const foo: number = 1
// ^ typescript
</script>
<style> <style> main { font-family: sans-serif; text-align: center; } </style>
main { <!-- ^ css -->
font-family: sans-serif; <style lang="scss"> main { &:hover { } } </style>
text-align: center; <!-- ^ scss -->
/* ^ css <!-- ^ !css -->
*/
}
</style>
<style lang="scss">
main {
font-family: sans-serif;
text-align: center;
&:hover {
// ^ scss
}
}
</style>
<main> <main>
<h1>Test file</h1> <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> <template>
<span>{{"Text inside interpolation"}}</span> <span>{{"Text inside interpolation"}}</span>
<!-- ^ javascript <!-- ^ javascript -->
-->
<template lang="pug"> <template lang="pug"> a(:href="url") some link title in pug: </template>
ul <!-- ^ pug -->
li(v-for="item in items")
a(v-if="item.type == 'link'" :href="item.url") some link title in pug:
<!-- ^ pug
-->
</template>
<template v-if="'text inside directives'"></template> <template v-if="'text inside directives'"></template>
<!-- ^ javascript <!-- ^ javascript -->
-->
</template> </template>
<script> <script> const foo = "1" </script>
const foo = "1" <!-- ^ javascript -->
// ^ javascript <script defer> const foo = "1" </script>
</script> <!-- ^ javascript -->
<script defer> <script lang="js">function x(){ return 1;}</script>
const foo = "1" <!-- ^ javascript -->
// ^ javascript <script lang="ts"> const foo: number = "1" </script>
</script> <!-- ^ typescript -->
<script lang="js"> <!-- ^ !javascript -->
const foo = "1" <style> .bar { .foo{ } } </style>
// ^ javascript <!-- ^ css -->
</script> <style scoped> .page.page--news { background: rebeccapurple; } </style>
<script lang="ts"> <!-- ^ css -->
const foo: number = "1" <style lang="css"> .bar { justify-content: center; } </style>
// ^ typescript <!-- ^ css -->
</script> <style lang="scss"> .bar { &-baz { } } </style>
<style> <!-- ^ scss -->
.bar { <!-- ^ !css -->
/* ^ 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>