-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Any | ||
|
||
|
||
def calc_diff(old_tree: dict[str, Any], new_tree: dict[str, Any]) -> dict[str, Any]: | ||
diff = {} | ||
for key in new_tree: | ||
if key not in old_tree: | ||
diff[key] = new_tree[key] | ||
elif ( | ||
isinstance(new_tree[key], dict) | ||
and "s" in new_tree[key] | ||
and "d" in new_tree[key] | ||
): | ||
# Handle special case of for loop | ||
old_dynamic = old_tree[key]["d"] | ||
new_dynamic = new_tree[key]["d"] | ||
|
||
old_static = old_tree[key]["s"] | ||
new_static = new_tree[key]["s"] | ||
|
||
if old_static != new_static: | ||
diff[key] = {"s": new_static, "d": new_dynamic} | ||
continue | ||
|
||
if old_dynamic != new_dynamic: | ||
diff[key] = {"d": new_dynamic} | ||
elif isinstance(new_tree[key], dict): | ||
nested_diff = calc_diff(old_tree[key], new_tree[key]) | ||
if nested_diff: | ||
diff[key] = nested_diff | ||
elif old_tree[key] != new_tree[key]: | ||
diff[key] = new_tree[key] | ||
|
||
return diff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from pyview.vendor.ibis import Template | ||
from pyview.template.render_diff import calc_diff | ||
|
||
|
||
def test_simple_diff_no_changes(): | ||
t = Template("<div>{% if greeting %}<span>{{greeting}}</span>{% endif %}</div>") | ||
assert calc_diff(t.tree({"greeting": "Hello"}), t.tree({"greeting": "Hello"})) == {} | ||
|
||
|
||
def test_simple_diff(): | ||
t = Template("<div>{% if greeting %}<span>{{greeting}}</span>{% endif %}</div>") | ||
|
||
hello = t.tree({"greeting": "Hello"}) | ||
goodbye = t.tree({"greeting": "Goodbye"}) | ||
|
||
assert calc_diff(hello, goodbye) == {"0": {"0": "Goodbye"}} | ||
|
||
|
||
def test_conditional_diff(): | ||
t = Template("<div>{% if greeting %}<span>{{greeting}}</span>{% endif %}</div>") | ||
|
||
hello = t.tree({"greeting": "Hello"}) | ||
empty = t.tree({}) | ||
|
||
# Going from hello to empty | ||
assert calc_diff(hello, empty) == {"0": ""} | ||
|
||
# Going from empty to hello | ||
assert calc_diff(empty, hello) == {"0": {"s": ["<span>", "</span>"], "0": "Hello"}} | ||
|
||
|
||
def test_loop_diff(): | ||
t = Template("<div>{% for item in items %}<span>{{item}}</span>{% endfor %}</div>") | ||
|
||
old = t.tree({"items": ["One", "Two", "Three"]}) | ||
new = t.tree({"items": ["One", "Two", "Four"]}) | ||
|
||
# diffs for loops always return all values, regardless of changes | ||
# at least, I *think* that's the right behavior based on some qick liveview testing | ||
assert calc_diff(old, new) == {"0": {"d": [["One"], ["Two"], ["Four"]]}} | ||
|
||
|
||
def test_loop_diff_no_change(): | ||
t = Template("<div>{% for item in items %}<span>{{item}}</span>{% endfor %}</div>") | ||
|
||
old = t.tree({"items": ["One", "Two", "Three"]}) | ||
new = t.tree({"items": ["One", "Two", "Three"]}) | ||
|
||
assert calc_diff(old, new) == {} | ||
|
||
|
||
def test_loop_diff_static_change(): | ||
t = Template("<div>{% for item in items %}<span>{{item}}</span>{% endfor %}</div>") | ||
t2 = Template("<div>{% for item in items %}<div>{{item}}</div>{% endfor %}</div>") | ||
|
||
old = t.tree({"items": ["One", "Two", "Three"]}) | ||
new = t2.tree({"items": ["One", "Two", "Three", "Four"]}) | ||
|
||
assert calc_diff(old, new) == { | ||
"0": {"s": ["<div>", "</div>"], "d": [["One"], ["Two"], ["Three"], ["Four"]]} | ||
} | ||
|
||
|
||
def test_diff_template_change(): | ||
t = Template("<div><span>{{greeting}}</span></div>") | ||
t2 = Template( | ||
"<div>{% if greeting %}<span>{{greeting}}</span>{% endif %}<p>{{farewell}}</p></div>" | ||
) | ||
|
||
r1 = t.tree({"greeting": "Hello"}) | ||
r2 = t2.tree({"greeting": "Hello", "farewell": "Goodbye"}) | ||
|
||
assert calc_diff(r1, r2) == r2 | ||
|
||
|
||
def test_statics_only_change(): | ||
t = Template("<div><span>{{greeting}}</span></div>") | ||
t2 = Template("<div>{{greeting}}</div>") | ||
|
||
r1 = t.tree({"greeting": "Hello"}) | ||
r2 = t2.tree({"greeting": "Hello"}) | ||
|
||
assert calc_diff(r1, r2) == {"s": ["<div>", "</div>"]} |