# -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. import odoo.tests from odoo.tests.common import BaseCase from odoo.addons.web_editor.models.diff_utils import ( generate_patch, generate_comparison, apply_patch, ) @odoo.tests.tagged("post_install", "-at_install", "html_history") class TestPatchUtils(BaseCase): def test_new_content_add_line(self): initial_content = "
foo
baz
" new_content = "foo
bar
baz
" patch = generate_patch(new_content, initial_content) # Even if we added content in the new_content, we expect a remove # operation, because the patch would be used to restore the initial # content from the new content. self.assertEqual(patch, "-@3,4") restored_initial_content = apply_patch(new_content, patch) self.assertEqual(restored_initial_content, initial_content) comparison = generate_comparison(new_content, initial_content) self.assertEqual( comparison, "foo
baz
" ) def test_new_content_remove_line(self): initial_content = "foo
bar
baz
" new_content = "foo
baz
" patch = generate_patch(new_content, initial_content) self.assertEqual(patch, "+@2:bar
") restored_initial_content = apply_patch(new_content, patch) self.assertEqual(restored_initial_content, initial_content) comparison = generate_comparison(new_content, initial_content) self.assertEqual( comparison, "foo
baz
" ) def test_new_content_replace_line(self): initial_content = "foo
bar
bor
bir
baz
" new_content = "foo
buz
baz
" patch = generate_patch(new_content, initial_content) self.assertEqual(patch, "R@3:bar
bor
bir") restored_initial_content = apply_patch(new_content, patch) self.assertEqual(restored_initial_content, initial_content) comparison = generate_comparison(new_content, initial_content) self.assertEqual( comparison, "
foo
" "baz
", ) def test_new_content_is_falsy(self): initial_content = "foo
bar
" new_content = "" patch = generate_patch(new_content, initial_content) self.assertEqual(patch, "+@0:foo
bar
") restored_initial_content = apply_patch(new_content, patch) self.assertEqual(restored_initial_content, initial_content) comparison = generate_comparison(new_content, initial_content) self.assertEqual( comparison, "foo
bar
" new_content = "foo
bar
" patch = generate_patch(new_content, initial_content) self.assertEqual(patch, "") restored_initial_content = apply_patch(new_content, patch) self.assertEqual(restored_initial_content, initial_content) initial_content = "" new_content = "" patch = generate_patch(new_content, initial_content) self.assertEqual(patch, "") restored_initial_content = apply_patch(new_content, patch) self.assertEqual(restored_initial_content, initial_content) def test_new_content_multiple_operation(self): initial_content = "foo
bar
baz
buz
boz
" new_content = ( "foo
bar
baz
boz
end
" ) patch = generate_patch(new_content, initial_content) self.assertEqual( patch, """-@3,6 +@10:buz
-@13,14""", ) restored_initial_content = apply_patch(new_content, patch) self.assertEqual(restored_initial_content, initial_content) comparison = generate_comparison(new_content, initial_content) self.assertEqual( comparison, "foo
" "bar
baz
boz
foo
bar
", "foo
", "fui
baz
", "fi
boz
", "completely different
foo
boz
buz
", "buz
", ] patches = [] for i in range(len(contents) - 1): patches.append(generate_patch(contents[i + 1], contents[i])) patches.reverse() reconstruct_content = contents[-1] for patch in patches: reconstruct_content = apply_patch(reconstruct_content, patch) self.assertEqual(reconstruct_content, contents[0]) def test_replace_tag(self): initial_content = "foo" new_content = "
foo
"
comparison = generate_comparison(new_content, initial_content)
self.assertEqual(
comparison,
"" "foo
foo
",
)
def test_replace_complex(self):
initial_content = (
"foo" "
bar" "
baz" "
---***---
" "only content change" "
+++~~~+++
" "content and tag change" "
???===???
" "111" "
222" "
333" ) new_content = ( "
foo
"
"bar
"
"baz
"
"---***---
" "lorem ipsum" "
+++~~~+++
" "dolor sit amet
"
"???===???
" "aaa" "
bbb" "
ccc" ) comparison = generate_comparison(new_content, initial_content) self.assertEqual( comparison, "
" "foo
" "bar
" "baz
foo
"
"bar
"
"baz
"
"---***---
" "" "only content change " "lorem ipsum
+++~~~+++
" "" "content and tag change
dolor sit amet
"
"???===???
" "" "111 aaa
" "222 bbb
", ) def test_replace_tag_multiline(self): initial_content = ( "333 ccc
foo" "
bar lorem ipsum dolor
"
"baz" ) new_content = ( "
foo
"
"bar lorem ipsum dolor" "
baz
"
)
comparison = generate_comparison(new_content, initial_content)
self.assertEqual(
comparison,
"" "foo
bar lorem ipsum dolor "
"foo
"
"" "baz " "bar lorem ipsum dolor
baz
",
)
def test_replace_nested_divs(self):
initial_content = "AB
B
A
A
A
A
A
A
abc
abc
abc
abc
def
A
B
"
"