Include: ../include/setup.vader

Execute (Setup: simulate non-testing):
  Save g:neomake_verbose
  " Unset g:neomake_test_messages for s:is_testing in autoload/neomake/log.vim.
  unlet g:neomake_test_messages
  unlet! g:neomake_verbose
  call NeomakeTestsSetVimMessagesMarker()
  runtime autoload/neomake/log.vim
  runtime autoload/neomake.vim
  let g:neomake_test_messages = []

Execute (neomake#GetMakeOptions works without testing):
  let jobinfo = NeomakeTestsFakeJobinfo()
  AssertEqual keys(neomake#GetMakeOptions(jobinfo.make_id)),
  \ ['options', 'jobs_queue', 'verbosity', 'finished_jobs', 'make_id', 'active_jobs']

Execute (neomake#log#error uses echohl):
  Save g:neomake_verbose
  let g:neomake_verbose = 1
  call NeomakeTestsSetVimMessagesMarker()
  call neomake#log#error('some error with context.', {})
  call neomake#log#error('some error without context.')
  AssertEqual NeomakeTestsGetVimMessages(), [
  \ 'Neomake: some error with context.',
  \ 'Neomake: some error without context.',
  \ ]

  let g:neomake_verbose = 3
  call neomake#log#warning('some warning with context.', {})
  call neomake#log#warning('some warning without context.')
  AssertEqual NeomakeTestsGetVimMessages(), [
  \ 'Neomake: [-.-:-:1] some warning with context.',
  \ 'Neomake: some warning without context.',
  \ ]

Execute (neomake#log#debug handles missing make_options):
  let s:log_calls = []
  function! neomake#log#warning(...)
    let s:log_calls += [a:000]
  endfunction

  call neomake#log#debug('msg1')
  AssertEqual NeomakeTestsGetVimMessages(), []

  " Non-existing make_id should not make it verbose.
  call neomake#log#debug('msg1', {'make_id': -42})
  AssertEqual s:log_calls, [['warning: missing make_info key: -42.']]
  AssertEqual NeomakeTestsGetVimMessages(), []

  let g:neomake_verbose = 3
  let s:log_calls = []
  call neomake#log#debug('msg1', {'make_id': -42})
  AssertEqual s:log_calls, [['warning: missing make_info key: -42.']]
  let msgs = NeomakeTestsGetVimMessages()
  AssertEqual len(msgs), 1
  Assert msgs[0] =~# '\VNeomake\.\*: [-42.-:-:1] msg1', 'msg matches ('.msgs[0].')'

  " Produces no message with level 0.
  let g:neomake_verbose = 0
  let s:log_calls = []
  call neomake#log#debug('msg1', {'make_id': -42})
  AssertEqual s:log_calls, [['warning: missing make_info key: -42.']]
  AssertEqual NeomakeTestsGetVimMessages(), []

Execute (neomake#log#debug_obj does not call neomake#utils#Stringify if not necessary):
  Save g:neomake_verbose
  unlet! g:neomake_verbose

  let s:calls = []
  function! neomake#utils#Stringify(...)
    let s:calls += [a:000]
  endfunction

  call neomake#log#debug_obj('msg', 'obj')
  AssertEqual s:calls, []

  let g:neomake_verbose = 3
  call neomake#log#debug_obj('msg', 'obj')
  AssertEqual s:calls, [['obj']]

Execute (reltime_lastmsg: cover all cases):
  if has('patch-7.4.503')
    Save g:neomake_logfile
    let g:neomake_logfile = tempname()

    let s:cur = 0
    let s:diffs = [0, 9, 99, 999, 9999, 99999]
    function! neomake#compat#reltimefloat(...)
      let s:cur += remove(s:diffs, 0)
      return s:cur
    endfunction

    call neomake#log#debug('msg1.')
    call neomake#log#debug('msg2.')
    call neomake#log#debug('msg3.')
    call neomake#log#debug('msg4.')
    call neomake#log#debug('msg5.')

    let log_msgs = readfile(g:neomake_logfile)
    call map(log_msgs, 'substitute(v:val, ''\v^\d\d:\d\d:\d\d \d+ '', ''HH:MM:SS PID '', '''')')
    AssertEqual log_msgs, [
    \ 'HH:MM:SS PID [D      ] msg1.',
    \ 'HH:MM:SS PID [D +9.00] msg2.',
    \ 'HH:MM:SS PID [D +99.0] msg3.',
    \ 'HH:MM:SS PID [D  +999] msg4.',
    \ 'HH:MM:SS PID [D +9999] msg5.',
    \ ]
  else
    NeomakeTestsSkip 'only with patch-7.4.503'
  endif

Execute (neomake#log#indent):
  Save g:neomake_verbose

  AssertThrows call neomake#log#indent(-1)
  AssertEqual g:vader_exception, 'invalid offset (already 0)'

  call neomake#log#indent(1)
  call NeomakeTestsSetVimMessagesMarker()
  call neomake#log#error('error1.')
  call neomake#log#error('error2.', {'bufnr': 1})
  AssertEqual NeomakeTestsGetVimMessages(), ['Neomake: error1.', 'Neomake: error2.']

  let g:neomake_verbose = 2
  call NeomakeTestsSetVimMessagesMarker()
  call neomake#log#error('error1.')
  call neomake#log#error('error2.', {'bufnr': 1})
  AssertEqual NeomakeTestsGetVimMessages(), ['Neomake: error1.', 'Neomake: error2.']

  let g:neomake_verbose = 3
  call NeomakeTestsSetVimMessagesMarker()
  call neomake#log#debug('debug1.')
  call neomake#log#debug('debug2.', {'bufnr': 1})
  AssertEqual NeomakeTestsGetVimMessages(), ['Neomake:   debug1.', 'Neomake: [-.-:1:1]   debug2.']

  " Restore if not profiling.
  if !v:profiling
    runtime autoload/neomake/utils.vim
    runtime autoload/neomake/log.vim
    runtime autoload/neomake/compat.vim
    runtime autoload/neomake.vim
  endif
