diff options
| author | lucario387 <hoangtun0810@gmail.com> | 2023-01-07 19:22:20 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-07 11:22:20 +0100 |
| commit | 85d953449125702c7c11f4a2242c328f7a65fd5b (patch) | |
| tree | 11c2d7a83fd9d267227690622e3eec6a1b01527a | |
| parent | fix(utils): swap_nodes calculates correct char_delta (#4110) (diff) | |
| download | nvim-treesitter-85d953449125702c7c11f4a2242c328f7a65fd5b.tar nvim-treesitter-85d953449125702c7c11f4a2242c328f7a65fd5b.tar.gz nvim-treesitter-85d953449125702c7c11f4a2242c328f7a65fd5b.tar.bz2 nvim-treesitter-85d953449125702c7c11f4a2242c328f7a65fd5b.tar.lz nvim-treesitter-85d953449125702c7c11f4a2242c328f7a65fd5b.tar.xz nvim-treesitter-85d953449125702c7c11f4a2242c328f7a65fd5b.tar.zst nvim-treesitter-85d953449125702c7c11f4a2242c328f7a65fd5b.zip | |
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
| -rw-r--r-- | tests/query/injection_spec.lua | 54 | ||||
| -rw-r--r-- | tests/query/injections/html/test-html-injections.html | 42 | ||||
| -rw-r--r-- | tests/query/injections/svelte/test-svelte-injections.svelte | 35 | ||||
| -rw-r--r-- | tests/query/injections/vue/negative-assertions.vue | 4 | ||||
| -rw-r--r-- | tests/query/injections/vue/test-vue-injections.vue | 84 |
5 files changed, 84 insertions, 135 deletions
diff --git a/tests/query/injection_spec.lua b/tests/query/injection_spec.lua index b10b7450f..28e6c727f 100644 --- a/tests/query/injection_spec.lua +++ b/tests/query/injection_spec.lua @@ -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 diff --git a/tests/query/injections/html/test-html-injections.html b/tests/query/injections/html/test-html-injections.html index 536ec8734..4449923c6 100644 --- a/tests/query/injections/html/test-html-injections.html +++ b/tests/query/injections/html/test-html-injections.html @@ -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 diff --git a/tests/query/injections/svelte/test-svelte-injections.svelte b/tests/query/injections/svelte/test-svelte-injections.svelte index a821dd080..5a9a03300 100644 --- a/tests/query/injections/svelte/test-svelte-injections.svelte +++ b/tests/query/injections/svelte/test-svelte-injections.svelte @@ -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> diff --git a/tests/query/injections/vue/negative-assertions.vue b/tests/query/injections/vue/negative-assertions.vue new file mode 100644 index 000000000..fdc871919 --- /dev/null +++ b/tests/query/injections/vue/negative-assertions.vue @@ -0,0 +1,4 @@ +<script lang="ts"> const foo: number = "1" </script> +<!-- ^ !javascript --> +<style lang="scss"> .bar { &-baz { &.page{ } } } </style> +<!-- ^ !css --> diff --git a/tests/query/injections/vue/test-vue-injections.vue b/tests/query/injections/vue/test-vue-injections.vue index c7442a027..4cb4ae215 100644 --- a/tests/query/injections/vue/test-vue-injections.vue +++ b/tests/query/injections/vue/test-vue-injections.vue @@ -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 --> |
