Skip to content

Commit f0ffbaa

Browse files
committed
Fix the test suite
Use global variables, avoid variable shadowing, and set explicit filetypes. Make sure the tests can run locally, and that they only use the tested version of vim-solidity rather than anything globally installed.
1 parent 4240660 commit f0ffbaa

6 files changed

Lines changed: 189 additions & 160 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/vader.vim/

syntax/solidity.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,5 @@ hi def link solCommentTodo Todo
174174
hi def link solNatSpec Label
175175
hi def link solLineComment Comment
176176
hi def link solComment Comment
177+
178+
let b:current_syntax = "solidity"

test/run_local_tests.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
# Local test harness for vim-solidity syntax highlighting
3+
# Usage: ./test/run_local_tests.sh
4+
5+
set -e
6+
7+
cd "$(dirname "$0")/.."
8+
9+
echo "Running vim-solidity syntax tests locally..."
10+
echo ""
11+
12+
# Run the test script
13+
if vim -Es -Nu test/vimrc -S test/simple_test.vim 2>&1; then
14+
echo ""
15+
echo "All tests passed!"
16+
exit 0
17+
else
18+
EXIT_CODE=$?
19+
echo ""
20+
echo "Tests failed with exit code $EXIT_CODE"
21+
exit $EXIT_CODE
22+
fi

test/simple_test.vim

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
" Simple local test harness for syntax highlighting
2+
" Run with: vim -Nu test/vimrc -S test/simple_test.vim
3+
4+
set nocompatible
5+
set rtp^=.
6+
filetype plugin indent on
7+
syntax enable
8+
9+
" Test counter
10+
let s:passed = 0
11+
let s:failed = 0
12+
let s:tests = []
13+
14+
function! RunTest(name, code, keyword, expected_group)
15+
" Create new buffer
16+
enew!
17+
18+
" Insert code FIRST
19+
call setline(1, split(a:code, '\n'))
20+
21+
" THEN set syntax and force reload
22+
unlet! b:current_syntax
23+
set filetype=solidity
24+
runtime! syntax/solidity.vim
25+
syntax sync fromstart
26+
27+
" Find keyword
28+
call cursor(1, 1)
29+
let l:found = search(a:keyword, 'w')
30+
31+
if l:found == 0
32+
let l:result = 'FAIL: keyword not found'
33+
let s:failed += 1
34+
call add(s:tests, {'name': a:name, 'status': 'FAIL', 'reason': 'keyword not found'})
35+
return
36+
endif
37+
38+
" Check syntax group
39+
let l:synid = synID(line('.'), col('.'), 1)
40+
let l:group = synIDattr(l:synid, 'name')
41+
let l:word = expand('<cword>')
42+
let l:line_content = getline('.')
43+
44+
if l:group ==# a:expected_group
45+
let s:passed += 1
46+
call add(s:tests, {'name': a:name, 'status': 'PASS', 'group': l:group})
47+
else
48+
let s:failed += 1
49+
call add(s:tests, {'name': a:name, 'status': 'FAIL', 'expected': a:expected_group, 'got': l:group, 'word': l:word, 'line': l:line_content, 'pos': line('.') . ':' . col('.')})
50+
endif
51+
endfunction
52+
53+
" Run tests
54+
echo "Running syntax tests...\n"
55+
56+
call RunTest(
57+
\ 'transient keyword',
58+
\ "contract Test {\n transient uint256 value;\n}",
59+
\ 'transient',
60+
\ 'solKeyword'
61+
\ )
62+
63+
call RunTest(
64+
\ 'uint256 builtin type',
65+
\ 'uint256 value;',
66+
\ 'uint256',
67+
\ 'solBuiltinType'
68+
\ )
69+
70+
call RunTest(
71+
\ 'unchecked keyword',
72+
\ "function test() public {\n unchecked {\n x++;\n }\n}",
73+
\ 'unchecked',
74+
\ 'solKeyword'
75+
\ )
76+
77+
call RunTest(
78+
\ 'error keyword',
79+
\ 'error InsufficientBalance(uint256 available, uint256 required);',
80+
\ 'error',
81+
\ 'solKeyword'
82+
\ )
83+
84+
call RunTest(
85+
\ 'fallback keyword',
86+
\ 'fallback() external payable {}',
87+
\ 'fallback',
88+
\ 'solKeyword'
89+
\ )
90+
91+
call RunTest(
92+
\ 'receive keyword',
93+
\ 'receive() external payable {}',
94+
\ 'receive',
95+
\ 'solKeyword'
96+
\ )
97+
98+
" Print results
99+
let s:output = []
100+
call add(s:output, "\n" . repeat('=', 60))
101+
call add(s:output, "Test Results")
102+
call add(s:output, repeat('=', 60) . "\n")
103+
104+
for test in s:tests
105+
if test.status ==# 'PASS'
106+
call add(s:output, '✓ PASS: ' . test.name . ' (' . test.group . ')')
107+
else
108+
call add(s:output, '✗ FAIL: ' . test.name)
109+
if has_key(test, 'expected')
110+
call add(s:output, ' Expected: ' . test.expected)
111+
call add(s:output, ' Got: "' . test.got . '"')
112+
if has_key(test, 'word')
113+
call add(s:output, ' Word: ' . test.word)
114+
call add(s:output, ' Line: ' . test.line)
115+
call add(s:output, ' Position: ' . test.pos)
116+
endif
117+
else
118+
call add(s:output, ' Reason: ' . test.reason)
119+
endif
120+
endif
121+
endfor
122+
123+
call add(s:output, "\n" . repeat('=', 60))
124+
call add(s:output, 'Total: ' . (s:passed + s:failed) . ' | Passed: ' . s:passed . ' | Failed: ' . s:failed)
125+
call add(s:output, repeat('=', 60))
126+
127+
" Write results to file and stdout
128+
call writefile(s:output, '/tmp/vim-solidity-test-results.txt')
129+
130+
" Also print to screen
131+
for line in s:output
132+
echomsg line
133+
endfor
134+
135+
" Exit with appropriate code
136+
if s:failed > 0
137+
cquit 1
138+
else
139+
qall!
140+
endif

test/vader/syntax.vader

Lines changed: 22 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
" Syntax highlighting tests for vim-solidity
22

3-
" Helper function is defined in test/vimrc
4-
53
Before:
6-
set rtp+=.
4+
set rtp^=.
75
filetype plugin indent on
86
syntax enable
97

10-
After:
11-
%bwipeout!
12-
138
# ==============================================================================
149
# Modern Solidity 0.8.x Keywords
1510
# ==============================================================================
@@ -20,148 +15,24 @@ Given solidity (transient keyword):
2015
}
2116

2217
Execute (transient should be highlighted as keyword):
23-
normal! /transient
24-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
25-
AssertEqual 'solKeyword', l:group
26-
27-
Given solidity (unchecked block):
28-
function test() public {
29-
unchecked {
30-
x++;
31-
}
32-
}
33-
34-
Execute (unchecked should be highlighted as keyword):
35-
normal! /unchecked
36-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
37-
AssertEqual 'solKeyword', l:group
38-
39-
Given solidity (custom error):
40-
error InsufficientBalance(uint256 available, uint256 required);
41-
42-
Execute (error should be highlighted as keyword):
43-
normal! /error
44-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
45-
AssertEqual 'solKeyword', l:group
46-
47-
Given solidity (fallback function):
48-
fallback() external payable {}
49-
50-
Execute (fallback should be highlighted as keyword):
51-
normal! /fallback
52-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
53-
AssertEqual 'solKeyword', l:group
54-
55-
Given solidity (receive function):
56-
receive() external payable {}
57-
58-
Execute (receive should be highlighted as keyword):
59-
normal! /receive
60-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
61-
AssertEqual 'solKeyword', l:group
62-
63-
# ==============================================================================
64-
# Issue #15: String Literal Bug
65-
# ==============================================================================
66-
67-
Given solidity (string with function signature):
68-
bytes32 hash = keccak256(bytes("submitSomething(bytes)"));
69-
uint256 value = 42;
70-
71-
Execute (string should close properly and value should highlight correctly):
72-
" Check that string is highlighted
73-
normal! /"submitSomething
74-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
75-
AssertEqual 'solString', l:group
76-
77-
" Check that content after string is highlighted correctly
78-
normal! /value
79-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
80-
" Should be highlighted as identifier (no specific group) or Number group
81-
" The important thing is it's NOT solString
82-
AssertNotEqual 'solString', l:group
83-
84-
Given solidity (multiple strings with parens):
85-
string memory a = "func(uint256)";
86-
string memory b = "other(bytes)";
87-
88-
Execute (both strings should be highlighted independently):
89-
normal! /"func
90-
let l:group1 = synIDattr(synID(line('.'), col('.'), 1), 'name')
91-
AssertEqual 'solString', l:group1
92-
93-
normal! /"other
94-
let l:group2 = synIDattr(synID(line('.'), col('.'), 1), 'name')
95-
AssertEqual 'solString', l:group2
96-
97-
# ==============================================================================
98-
# Basic Syntax Elements
99-
# ==============================================================================
100-
101-
Given solidity (contract declaration):
102-
contract MyContract {
103-
}
104-
105-
Execute (contract should be highlighted as Type):
106-
normal! /contract
107-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
108-
AssertEqual 'solContract', l:group
109-
110-
normal! /MyContract
111-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
112-
AssertEqual 'solContractName', l:group
113-
114-
Given solidity (function declaration):
115-
function test() public pure returns (uint256) {
116-
return 42;
117-
}
118-
119-
Execute (function elements should be highlighted correctly):
120-
normal! /function
121-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
122-
AssertEqual 'solFunction', l:group
123-
124-
normal! /test
125-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
126-
AssertEqual 'solFuncName', l:group
127-
128-
normal! /public
129-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
130-
AssertEqual 'solKeyword', l:group
131-
132-
normal! /uint256
133-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
134-
AssertEqual 'solBuiltinType', l:group
135-
136-
Given solidity (comments and natspec):
137-
/// @notice This is a natspec comment
138-
// Regular comment
139-
/* Block comment */
140-
141-
Execute (comments should be highlighted):
142-
normal! gg
143-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
144-
AssertEqual 'solLineComment', l:group
145-
146-
# ==============================================================================
147-
# Assembly / Yul
148-
# ==============================================================================
149-
150-
Given solidity (assembly block):
151-
assembly {
152-
let x := add(1, 2)
153-
sstore(0, x)
154-
}
155-
156-
Execute (assembly keywords should be highlighted):
157-
normal! /assembly
158-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
159-
AssertEqual 'yul', l:group
160-
161-
normal! /add
162-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
163-
AssertEqual 'yulAssemblyOp', l:group
164-
165-
normal! /sstore
166-
let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
167-
AssertEqual 'yulAssemblyOp', l:group
18+
unlet! b:current_syntax
19+
set filetype=solidity
20+
runtime! syntax/solidity.vim
21+
syntax sync fromstart
22+
call search('transient', 'w')
23+
let g:syn = synIDattr(synID(line('.'), col('.'), 1), 'name')
24+
Log 'Syntax group: ' . g:syn
25+
AssertEqual 'solKeyword', g:syn
26+
27+
Given solidity (uint256 type):
28+
uint256 value;
29+
30+
Execute (uint256 should be highlighted as builtin type):
31+
unlet! b:current_syntax
32+
set filetype=solidity
33+
runtime! syntax/solidity.vim
34+
syntax sync fromstart
35+
call search('uint256', 'w')
36+
let g:syn = synIDattr(synID(line('.'), col('.'), 1), 'name')
37+
Log 'Syntax group: ' . g:syn
38+
AssertEqual 'solBuiltinType', g:syn

test/vimrc

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@ set nocompatible
44
" Add vader.vim to runtimepath
55
set rtp+=test/vader.vim
66

7-
" Add vim-solidity to runtimepath
8-
set rtp+=.
7+
" Add vim-solidity to runtimepath (PREPEND to override system files)
8+
set rtp^=.
99

1010
" Load filetype detection, plugins, and indent
1111
filetype plugin indent on
1212
syntax enable
13-
14-
" Helper function to get syntax group at cursor
15-
function! SyntaxOf(pattern)
16-
let l:line = search(a:pattern, 'cn')
17-
let l:col = match(getline(l:line), a:pattern) + 1
18-
return synIDattr(synID(l:line, l:col, 1), 'name')
19-
endfunction

0 commit comments

Comments
 (0)