Mercurial > vim
comparison src/testdir/test_diffmode.vim @ 34324:6d9f59e88fc2 v9.1.0096
patch 9.1.0096: diff() function uses 'diffexpr'
Commit: https://github.com/vim/vim/commit/be156a31c5400eb12025e480c477b1df88244801
Author: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Sun Feb 11 17:08:29 2024 +0100
patch 9.1.0096: diff() function uses 'diffexpr'
Problem: diff() function uses 'diffexpr'
(rickhowe)
Solution: Make diff() always use internal diff(), add support for
unified diff context length, sort diff() options in help
(Yegappan Lakshmanan)
fixes: #13989
closes: #14010
Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 11 Feb 2024 17:15:05 +0100 |
parents | cce6b834635c |
children | 7ccaadd7cf0b |
comparison
equal
deleted
inserted
replaced
34323:857f6a19ca6a | 34324:6d9f59e88fc2 |
---|---|
1720 endfunc | 1720 endfunc |
1721 | 1721 |
1722 " Test for the diff() function | 1722 " Test for the diff() function |
1723 def Test_diff_func() | 1723 def Test_diff_func() |
1724 # string is added/removed/modified at the beginning | 1724 # string is added/removed/modified at the beginning |
1725 assert_equal("@@ -0,0 +1 @@\n+abc\n", | 1725 assert_equal("@@ -1 +1,2 @@\n+abc\n def\n", |
1726 diff(['def'], ['abc', 'def'], {output: 'unified'})) | 1726 diff(['def'], ['abc', 'def'], {output: 'unified'})) |
1727 assert_equal([{from_idx: 0, from_count: 0, to_idx: 0, to_count: 1}], | 1727 assert_equal([{from_idx: 0, from_count: 0, to_idx: 0, to_count: 1}], |
1728 diff(['def'], ['abc', 'def'], {output: 'indices'})) | 1728 diff(['def'], ['abc', 'def'], {output: 'indices'})) |
1729 assert_equal("@@ -1 +0,0 @@\n-abc\n", | 1729 assert_equal("@@ -1,2 +1 @@\n-abc\n def\n", |
1730 diff(['abc', 'def'], ['def'], {output: 'unified'})) | 1730 diff(['abc', 'def'], ['def'], {output: 'unified'})) |
1731 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 0}], | 1731 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 0}], |
1732 diff(['abc', 'def'], ['def'], {output: 'indices'})) | 1732 diff(['abc', 'def'], ['def'], {output: 'indices'})) |
1733 assert_equal("@@ -1 +1 @@\n-abc\n+abx\n", | 1733 assert_equal("@@ -1,2 +1,2 @@\n-abc\n+abx\n def\n", |
1734 diff(['abc', 'def'], ['abx', 'def'], {output: 'unified'})) | 1734 diff(['abc', 'def'], ['abx', 'def'], {output: 'unified'})) |
1735 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}], | 1735 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}], |
1736 diff(['abc', 'def'], ['abx', 'def'], {output: 'indices'})) | 1736 diff(['abc', 'def'], ['abx', 'def'], {output: 'indices'})) |
1737 | 1737 |
1738 # string is added/removed/modified at the end | 1738 # string is added/removed/modified at the end |
1739 assert_equal("@@ -1,0 +2 @@\n+def\n", | 1739 assert_equal("@@ -1 +1,2 @@\n abc\n+def\n", |
1740 diff(['abc'], ['abc', 'def'], {output: 'unified'})) | 1740 diff(['abc'], ['abc', 'def'], {output: 'unified'})) |
1741 assert_equal([{from_idx: 1, from_count: 0, to_idx: 1, to_count: 1}], | 1741 assert_equal([{from_idx: 1, from_count: 0, to_idx: 1, to_count: 1}], |
1742 diff(['abc'], ['abc', 'def'], {output: 'indices'})) | 1742 diff(['abc'], ['abc', 'def'], {output: 'indices'})) |
1743 assert_equal("@@ -2 +1,0 @@\n-def\n", | 1743 assert_equal("@@ -1,2 +1 @@\n abc\n-def\n", |
1744 diff(['abc', 'def'], ['abc'], {output: 'unified'})) | 1744 diff(['abc', 'def'], ['abc'], {output: 'unified'})) |
1745 assert_equal([{from_idx: 1, from_count: 1, to_idx: 1, to_count: 0}], | 1745 assert_equal([{from_idx: 1, from_count: 1, to_idx: 1, to_count: 0}], |
1746 diff(['abc', 'def'], ['abc'], {output: 'indices'})) | 1746 diff(['abc', 'def'], ['abc'], {output: 'indices'})) |
1747 assert_equal("@@ -2 +2 @@\n-def\n+xef\n", | 1747 assert_equal("@@ -1,2 +1,2 @@\n abc\n-def\n+xef\n", |
1748 diff(['abc', 'def'], ['abc', 'xef'], {output: 'unified'})) | 1748 diff(['abc', 'def'], ['abc', 'xef'], {output: 'unified'})) |
1749 assert_equal([{from_idx: 1, from_count: 1, to_idx: 1, to_count: 1}], | 1749 assert_equal([{from_idx: 1, from_count: 1, to_idx: 1, to_count: 1}], |
1750 diff(['abc', 'def'], ['abc', 'xef'], {output: 'indices'})) | 1750 diff(['abc', 'def'], ['abc', 'xef'], {output: 'indices'})) |
1751 | 1751 |
1752 # string is added/removed/modified in the middle | 1752 # string is added/removed/modified in the middle |
1753 assert_equal("@@ -2,0 +3 @@\n+xxx\n", | 1753 assert_equal("@@ -2,2 +2,3 @@\n 222\n+xxx\n 333\n", |
1754 diff(['111', '222', '333'], ['111', '222', 'xxx', '333'], {output: 'unified'})) | 1754 diff(['111', '222', '333'], ['111', '222', 'xxx', '333'], {output: 'unified'})) |
1755 assert_equal([{from_idx: 2, from_count: 0, to_idx: 2, to_count: 1}], | 1755 assert_equal([{from_idx: 2, from_count: 0, to_idx: 2, to_count: 1}], |
1756 diff(['111', '222', '333'], ['111', '222', 'xxx', '333'], {output: 'indices'})) | 1756 diff(['111', '222', '333'], ['111', '222', 'xxx', '333'], {output: 'indices'})) |
1757 assert_equal("@@ -3 +2,0 @@\n-333\n", | 1757 assert_equal("@@ -2,3 +2,2 @@\n 222\n-333\n 444\n", |
1758 diff(['111', '222', '333', '444'], ['111', '222', '444'], {output: 'unified'})) | 1758 diff(['111', '222', '333', '444'], ['111', '222', '444'], {output: 'unified'})) |
1759 assert_equal([{from_idx: 2, from_count: 1, to_idx: 2, to_count: 0}], | 1759 assert_equal([{from_idx: 2, from_count: 1, to_idx: 2, to_count: 0}], |
1760 diff(['111', '222', '333', '444'], ['111', '222', '444'], {output: 'indices'})) | 1760 diff(['111', '222', '333', '444'], ['111', '222', '444'], {output: 'indices'})) |
1761 assert_equal("@@ -3 +3 @@\n-333\n+xxx\n", | 1761 assert_equal("@@ -2,3 +2,3 @@\n 222\n-333\n+xxx\n 444\n", |
1762 diff(['111', '222', '333', '444'], ['111', '222', 'xxx', '444'], {output: 'unified'})) | 1762 diff(['111', '222', '333', '444'], ['111', '222', 'xxx', '444'], {output: 'unified'})) |
1763 assert_equal([{from_idx: 2, from_count: 1, to_idx: 2, to_count: 1}], | 1763 assert_equal([{from_idx: 2, from_count: 1, to_idx: 2, to_count: 1}], |
1764 diff(['111', '222', '333', '444'], ['111', '222', 'xxx', '444'], {output: 'indices'})) | 1764 diff(['111', '222', '333', '444'], ['111', '222', 'xxx', '444'], {output: 'indices'})) |
1765 | 1765 |
1766 # new strings are added to an empty List | 1766 # new strings are added to an empty List |
1826 tolist =<< trim END | 1826 tolist =<< trim END |
1827 one abc two | 1827 one abc two |
1828 three four | 1828 three four |
1829 five six | 1829 five six |
1830 END | 1830 END |
1831 assert_equal("@@ -1 +1 @@\n-one two\n+one abc two\n@@ -3 +3 @@\n-five abc six\n+five six\n", | 1831 assert_equal("@@ -1,3 +1,3 @@\n-one two\n+one abc two\n three four\n-five abc six\n+five six\n", |
1832 diff(fromlist, tolist, {output: 'unified'})) | 1832 diff(fromlist, tolist, {output: 'unified'})) |
1833 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}, | 1833 assert_equal([{from_idx: 0, from_count: 3, to_idx: 0, to_count: 3}], |
1834 {from_idx: 2, from_count: 1, to_idx: 2, to_count: 1}], | |
1835 diff(fromlist, tolist, {output: 'indices'})) | 1834 diff(fromlist, tolist, {output: 'indices'})) |
1836 | 1835 |
1837 # add/remove blank lines | 1836 # add/remove blank lines |
1838 assert_equal("@@ -2,2 +1,0 @@\n-\n-\n", | 1837 assert_equal("@@ -1,4 +1,2 @@\n one\n-\n-\n two\n", |
1839 diff(['one', '', '', 'two'], ['one', 'two'], {output: 'unified'})) | 1838 diff(['one', '', '', 'two'], ['one', 'two'], {output: 'unified'})) |
1840 assert_equal([{from_idx: 1, from_count: 2, to_idx: 1, to_count: 0}], | 1839 assert_equal([{from_idx: 1, from_count: 2, to_idx: 1, to_count: 0}], |
1841 diff(['one', '', '', 'two'], ['one', 'two'], {output: 'indices'})) | 1840 diff(['one', '', '', 'two'], ['one', 'two'], {output: 'indices'})) |
1842 assert_equal("@@ -1,0 +2,2 @@\n+\n+\n", | 1841 assert_equal("@@ -1,2 +1,4 @@\n one\n+\n+\n two\n", |
1843 diff(['one', 'two'], ['one', '', '', 'two'], {output: 'unified'})) | 1842 diff(['one', 'two'], ['one', '', '', 'two'], {output: 'unified'})) |
1844 assert_equal([{'from_idx': 1, 'from_count': 0, 'to_idx': 1, 'to_count': 2}], | 1843 assert_equal([{'from_idx': 1, 'from_count': 0, 'to_idx': 1, 'to_count': 2}], |
1845 diff(['one', 'two'], ['one', '', '', 'two'], {output: 'indices'})) | 1844 diff(['one', 'two'], ['one', '', '', 'two'], {output: 'indices'})) |
1846 | 1845 |
1847 # diff ignoring case | 1846 # diff ignoring case |
1888 assert_equal('', diff(['111', '222'], ['111', '222'], {output: 'unified'})) | 1887 assert_equal('', diff(['111', '222'], ['111', '222'], {output: 'unified'})) |
1889 assert_equal([], diff(['111', '222'], ['111', '222'], {output: 'indices'})) | 1888 assert_equal([], diff(['111', '222'], ['111', '222'], {output: 'indices'})) |
1890 assert_equal('', diff([], [], {output: 'unified'})) | 1889 assert_equal('', diff([], [], {output: 'unified'})) |
1891 assert_equal([], diff([], [], {output: 'indices'})) | 1890 assert_equal([], diff([], [], {output: 'indices'})) |
1892 | 1891 |
1892 # If 'diffexpr' is set, it should not be used for diff() | |
1893 def MyDiffExpr() | |
1894 enddef | |
1895 var save_diffexpr = &diffexpr | |
1896 :set diffexpr=MyDiffExpr() | |
1897 assert_equal("@@ -1 +1 @@\n-abc\n+\n", | |
1898 diff(['abc'], [''], {output: 'unified'})) | |
1899 assert_equal([{'from_idx': 0, 'from_count': 1, 'to_idx': 0, 'to_count': 1}], | |
1900 diff(['abc'], [''], {output: 'indices'})) | |
1901 assert_equal('MyDiffExpr()', &diffexpr) | |
1902 &diffexpr = save_diffexpr | |
1903 | |
1904 # try different values for unified diff 'context' | |
1905 assert_equal("@@ -0,0 +1 @@\n+x\n", | |
1906 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'])) | |
1907 assert_equal("@@ -0,0 +1 @@\n+x\n", | |
1908 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 0})) | |
1909 assert_equal("@@ -1 +1,2 @@\n+x\n a\n", | |
1910 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 1})) | |
1911 assert_equal("@@ -1,2 +1,3 @@\n+x\n a\n b\n", | |
1912 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 2})) | |
1913 assert_equal("@@ -1,3 +1,4 @@\n+x\n a\n b\n c\n", | |
1914 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 3})) | |
1915 assert_equal("@@ -1,3 +1,4 @@\n+x\n a\n b\n c\n", | |
1916 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 4})) | |
1917 assert_equal("@@ -1 +1,2 @@\n+x\n a\n", | |
1918 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: -1})) | |
1919 | |
1893 # Error cases | 1920 # Error cases |
1894 assert_fails('call diff({}, ["a"])', 'E1211:') | 1921 assert_fails('call diff({}, ["a"])', 'E1211:') |
1895 assert_fails('call diff(["a"], {})', 'E1211:') | 1922 assert_fails('call diff(["a"], {})', 'E1211:') |
1896 assert_fails('call diff(["a"], ["a"], [])', 'E1206:') | 1923 assert_fails('call diff(["a"], ["a"], [])', 'E1206:') |
1897 assert_fails('call diff(["a"], ["a"], {output: "xyz"})', 'E106: Unsupported diff output format: xyz') | 1924 assert_fails('call diff(["a"], ["a"], {output: "xyz"})', 'E106: Unsupported diff output format: xyz') |
1925 assert_fails('call diff(["a"], ["a"], {context: []})', 'E745: Using a List as a Number') | |
1898 enddef | 1926 enddef |
1899 | 1927 |
1900 " vim: shiftwidth=2 sts=2 expandtab | 1928 " vim: shiftwidth=2 sts=2 expandtab |