util.vim 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. function! CheckCurrentLine( expected_value )
  2. return assert_equal( a:expected_value, getline( '.' ) )
  3. endfunction
  4. function! AssertDictHasEntries( actual, expected, name = 'expected dict' )
  5. let l:errs = 0
  6. for key in keys( a:expected )
  7. if !has_key( a:actual, key )
  8. let l:errs += assert_report( 'Key '
  9. \ . key
  10. \ . ' of '
  11. \ . a:name
  12. \ . ' was not found' )
  13. elseif type( a:expected[ key ] ) == v:t_dict
  14. if type( a:actual[ key ] ) != v:t_dict
  15. let l:errs += assert_report( 'Key '
  16. \ . key
  17. \ . ' of '
  18. \ . a:name
  19. \ . ' was expected to be a dict, but was '
  20. \ . string( a:actual[ key ] ) )
  21. else
  22. let l:errs += AssertDictHasEntries( a:actual[ key ],
  23. \ a:expected[ key ],
  24. \ 'entry ' . key . ' in ' . a:name )
  25. endif
  26. else
  27. let l:errs += assert_equal( a:expected[ key ],
  28. \ a:actual[ key ],
  29. \ 'Key '
  30. \ . key
  31. \ . ' of '
  32. \ . a:name
  33. \ . ' did not match' )
  34. endif
  35. endfor
  36. return l:errs
  37. endfunction
  38. function! CheckListOfDicts( actual_list, expected_list )
  39. let l:errs = 0
  40. let l:idx = 0
  41. if len( a:actual_list ) != len( a:expected_list )
  42. let l:errs += assert_report( 'Expected list to contain '
  43. \ . len( a:actual_list )
  44. \ . ' entries, but found '
  45. \ . len( a:expected_list )
  46. \ . ': Expected '
  47. \ . string( a:expected_list )
  48. \ . ' but found '
  49. \ . string( a:actual_list ) )
  50. endif
  51. while l:idx < len( a:expected_list )
  52. let l:expected = a:expected_list[ l:idx ]
  53. if l:idx >= len( a:actual_list )
  54. let l:errs += assert_report( 'The item at index '
  55. \ . l:idx
  56. \ . ' was not found: '
  57. \ . string( l:expected ) )
  58. else
  59. let l:actual = a:actual_list[ l:idx ]
  60. let l:errs += AssertDictHasEntries( l:actual,
  61. \ l:expected,
  62. \ 'item at index ' . l:idx )
  63. endif
  64. let l:idx = l:idx + 1
  65. endwhile
  66. while idx < len( a:actual_list )
  67. let l:actual = a:actual_list[ idx ]
  68. let l:errs += assert_report( 'The following additional property '
  69. \ . 'was found: '
  70. \ . string( l:actual ) )
  71. let l:idx = l:idx + 1
  72. endwhile
  73. return l:errs > 0 ? 1 : 0
  74. endfunction