Skip to content

Commit 1c9344d

Browse files
authored
Merge pull request #355 from mhinz/fix-pattern-search
spec_helper: detect failed searches
2 parents a87004a + fb36cbf commit 1c9344d

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

spec/spec_helper.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,20 @@ def type(content)
3939

4040
def syntax(content, pattern)
4141
with_file content
42-
# move cursor the pattern
43-
@vim.search pattern
44-
# get a list of the syntax element
42+
43+
# Using this function with a `pattern` that is not in `content` is pointless.
44+
#
45+
# @vim.search() silently fails if a pattern is not found and the cursor
46+
# won't move. So, if the current cursor position happens to sport the
47+
# expected syntax group already, this can lead to false positive tests.
48+
#
49+
# We work around this by using Vim's search() function, which returns 0 if
50+
# there is no match.
51+
if @vim.echo("search(#{pattern.inspect})") == '0'
52+
return []
53+
end
54+
55+
# Return the syntax groups in a list.
4556
@vim.echo <<~EOF
4657
map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
4758
EOF
@@ -55,6 +66,7 @@ def with_file(content = nil)
5566
yield if block_given?
5667

5768
@vim.write
69+
@vim.command 'redraw'
5870
IO.read(@file)
5971
end
6072

spec/syntax/doc_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
EOF
8181
expect(ex).to include_elixir_syntax('elixirDocString', 'doctest')
8282
expect(ex).to include_elixir_syntax('elixirDocTest', 'map')
83-
expect(ex).to include_elixir_syntax('elixirDocTest', 'x * 2')
83+
expect(ex).to include_elixir_syntax('elixirDocTest', 'x \* 2')
8484
expect(ex).to include_elixir_syntax('elixirDocTest', '2, 4, 6')
8585
end
8686

spec/syntax/sigil_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
describe 'upper case' do
1414
it 'string' do
1515
expect('~S(string)').to include_elixir_syntax('elixirSigilDelimiter', 'S')
16-
expect('~S(string)').to include_elixir_syntax('elixirSigil', 'foo')
16+
expect('~S(string)').to include_elixir_syntax('elixirSigil', 'string')
1717
end
1818

1919
it 'character list' do
@@ -47,7 +47,7 @@
4747
end
4848

4949
it 'escapes double quotes unless only preceded by whitespace' do
50-
expect(<<~EOF).to include_elixir_syntax('elixirSigilDelimiter', %q(^\s*\zs'"'))
50+
expect(<<~EOF).to include_elixir_syntax('elixirSigilDelimiter', %q(^\s*\zs"""))
5151
~r"""
5252
foo """
5353
"""
@@ -78,7 +78,7 @@
7878
describe 'lower case' do
7979
it 'string' do
8080
expect('~s(string)').to include_elixir_syntax('elixirSigilDelimiter', 's')
81-
expect('~s(string)').to include_elixir_syntax('elixirSigil', 'foo')
81+
expect('~s(string)').to include_elixir_syntax('elixirSigil', 'string')
8282
end
8383

8484
it 'character list' do

0 commit comments

Comments
 (0)