kana / vim-vspec

Vim plugin: Testing framework for Vim script
http://www.vim.org/scripts/script.php?script_id=3012
222 stars 13 forks source link

"Undefined variable" error when testing autoloaded variables #29

Closed glts closed 10 years ago

glts commented 10 years ago

I am trying to write a test that uses an autoloaded variable. The variable is part of my API and I can use it everywhere without problems, but I can't seem to use it in vspec it ... end blocks.

path/to/bundles/autovar/autoload/autovar.vim:

function! autovar#Number()
  return 12
endfunction

let autovar#NUMBER = 12

path/to/bundles/autovar/t/test.vim:

describe 'autovar#Number'
  it 'returns 12'
    Expect autovar#Number() == 12
  end
end

describe 'autovar#NUMBER'
  it 'fails'
    Expect autovar#NUMBER == 12
  end
end

Run it:

$ vspec path/to/bundles/{vspec,autovar} t/test.vim
ok 1 - autovar#Number returns 12
not ok 2 - autovar#NUMBER fails
# function <SNR>1_main..vspec#test..6, line 1
# Vim(call):E121: Undefined variable: autovar#NUMBER
1..2
kana commented 10 years ago

Thank you for the report. It might be a bug in Vim. I confirmed a similar problem without vim-vspec. Try the following steps with vim -u NONE -i NONE -N:

set runtimepath+=path/to/bundles/autovar

echo autovar#Number()
" => Echos 12

echo autovar#NUMBER
" => Echos 12

function Foo()
  return autovar#NUMBER
endfunction

echo Foo()
" => Fails with the following message:
" Error detected while processing function Foo:
" line    1:
" E121: Undefined variable: autovar#NUMBER
" E15: Invalid expression: autovar#NUMBER
" 0
glts commented 10 years ago

After reading your reply I found this message and a work-around:

describe 'autovar#NUMBER'
  it 'works now'
    Expect g:autovar#NUMBER == 12
  end
end

So technically this might not be a bug, but a very strange scoping rule in Vim eval.

I am not sure if I should report it on vim_dev, but I agree that this is not a vspec problem. I'm closing it, thank you.

kana commented 10 years ago

Thank you. I've added a note on the problem and the workaround to the document. Because it's hard to guess the scoping rule of autoload variables form :help.