082_string_comparison_spec.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. -- Tests for case-insensitive UTF-8 comparisons (utf_strnicmp() in mbyte.c)
  2. -- Also test "g~ap".
  3. local n = require('test.functional.testnvim')()
  4. local feed, source = n.feed, n.source
  5. local clear, feed_command, expect = n.clear, n.feed_command, n.expect
  6. describe('case-insensitive string comparison in UTF-8', function()
  7. setup(clear)
  8. it('is working', function()
  9. feed('ggdG<cr>')
  10. source([[
  11. function! Ch(a, op, b, expected)
  12. if eval(printf('"%s" %s "%s"', a:a, a:op, a:b)) != a:expected
  13. call append(line('$'), printf('"%s" %s "%s" should return %d', a:a, a:op, a:b, a:expected))
  14. else
  15. let b:passed += 1
  16. endif
  17. endfunction
  18. function! Chk(a, b, result)
  19. if a:result == 0
  20. call Ch(a:a, '==?', a:b, 1)
  21. call Ch(a:a, '!=?', a:b, 0)
  22. call Ch(a:a, '<=?', a:b, 1)
  23. call Ch(a:a, '>=?', a:b, 1)
  24. call Ch(a:a, '<?', a:b, 0)
  25. call Ch(a:a, '>?', a:b, 0)
  26. elseif a:result > 0
  27. call Ch(a:a, '==?', a:b, 0)
  28. call Ch(a:a, '!=?', a:b, 1)
  29. call Ch(a:a, '<=?', a:b, 0)
  30. call Ch(a:a, '>=?', a:b, 1)
  31. call Ch(a:a, '<?', a:b, 0)
  32. call Ch(a:a, '>?', a:b, 1)
  33. else
  34. call Ch(a:a, '==?', a:b, 0)
  35. call Ch(a:a, '!=?', a:b, 1)
  36. call Ch(a:a, '<=?', a:b, 1)
  37. call Ch(a:a, '>=?', a:b, 0)
  38. call Ch(a:a, '<?', a:b, 1)
  39. call Ch(a:a, '>?', a:b, 0)
  40. endif
  41. endfunction
  42. function! Check(a, b, result)
  43. call Chk(a:a, a:b, a:result)
  44. call Chk(a:b, a:a, -a:result)
  45. endfunction
  46. function! LT(a, b)
  47. call Check(a:a, a:b, -1)
  48. endfunction
  49. function! GT(a, b)
  50. call Check(a:a, a:b, 1)
  51. endfunction
  52. function! EQ(a, b)
  53. call Check(a:a, a:b, 0)
  54. endfunction
  55. let b:passed=0
  56. call EQ('', '')
  57. call LT('', 'a')
  58. call EQ('abc', 'abc')
  59. call EQ('Abc', 'abC')
  60. call LT('ab', 'abc')
  61. call LT('AB', 'abc')
  62. call LT('ab', 'aBc')
  63. call EQ('\xd0\xb9\xd1\x86\xd1\x83\xd0\xba\xd0\xb5\xd0\xbd', '\xd0\xb9\xd0\xa6\xd0\xa3\xd0\xba\xd0\x95\xd0\xbd')
  64. call LT('\xd0\xb9\xd1\x86\xd1\x83\xd0\xba\xd0\xb5\xd0\xbd', '\xd0\xaf\xd1\x86\xd1\x83\xd0\xba\xd0\xb5\xd0\xbd')
  65. call EQ('\xe2\x84\xaa', 'k')
  66. call LT('\xe2\x84\xaa', 'kkkkkk')
  67. call EQ('\xe2\x84\xaa\xe2\x84\xaa\xe2\x84\xaa', 'kkk')
  68. call LT('kk', '\xe2\x84\xaa\xe2\x84\xaa\xe2\x84\xaa')
  69. call EQ('\xe2\x84\xaa\xe2\x84\xa6k\xe2\x84\xaak\xcf\x89', 'k\xcf\x89\xe2\x84\xaakk\xe2\x84\xa6')
  70. call EQ('Abc\x80', 'AbC\x80')
  71. call LT('Abc\x80', 'AbC\x81')
  72. call LT('Abc', 'AbC\x80')
  73. " Case folding stops at the first bad character.
  74. call LT('abc\x80DEF', 'abc\x80def')
  75. call LT('\xc3XYZ', '\xc3xyz')
  76. " FF3A (upper), FF5A (lower).
  77. call EQ('\xef\xbc\xba', '\xef\xbd\x9a')
  78. " First string is ok and equals \xef\xbd\x9a after folding, second
  79. " string is illegal and was left unchanged, then the strings were
  80. " bytewise compared.
  81. call GT('\xef\xbc\xba', '\xef\xbc\xff')
  82. call LT('\xc3', '\xc3\x83')
  83. call EQ('\xc3\xa3xYz', '\xc3\x83XyZ')
  84. for n in range(0x60, 0xFF)
  85. call LT(printf('xYz\x%.2X', n-1), printf('XyZ\x%.2X', n))
  86. endfor
  87. for n in range(0x80, 0xBF)
  88. call EQ(printf('xYz\xc2\x%.2XUvW', n), printf('XyZ\xc2\x%.2XuVw', n))
  89. endfor
  90. for n in range(0xC0, 0xFF)
  91. call LT(printf('xYz\xc2\x%.2XUvW', n), printf('XyZ\xc2\x%.2XuVw', n))
  92. endfor
  93. call append(0, printf('%d checks passed', b:passed))
  94. ]])
  95. -- Test that g~ap changes one paragraph only.
  96. feed_command('new')
  97. feed('iabcd<cr><cr>defg<esc>gg0g~ap')
  98. feed_command('let lns = getline(1,3)')
  99. feed_command('q!')
  100. feed_command([[call append(line('$'), lns)]])
  101. -- Assert buffer contents.
  102. expect([=[
  103. 3732 checks passed
  104. ABCD
  105. defg]=])
  106. end)
  107. end)