From f9d45d556264e4d4f38fc8954b1acc3bfde4b475 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Thu, 16 Jan 2025 16:02:41 -0800 Subject: [PATCH] fix tests for cond and var --- tests/units/components/core/test_cond.py | 74 ++++++++++++------------ tests/units/test_var.py | 36 ++++++------ 2 files changed, 56 insertions(+), 54 deletions(-) diff --git a/tests/units/components/core/test_cond.py b/tests/units/components/core/test_cond.py index e88f35c9a..71e19c7cc 100644 --- a/tests/units/components/core/test_cond.py +++ b/tests/units/components/core/test_cond.py @@ -3,8 +3,7 @@ from typing import Any, Union import pytest -from reflex.components.base.fragment import Fragment -from reflex.components.core.cond import Cond, cond +from reflex.components.core.cond import cond from reflex.components.radix.themes.typography.text import Text from reflex.state import BaseState from reflex.utils.format import format_state_name @@ -22,7 +21,10 @@ def cond_state(request): def test_f_string_cond_interpolation(): # make sure backticks inside interpolation don't get escaped var = LiteralVar.create(f"x {cond(True, 'a', 'b')}") - assert str(var) == '("x "+(true ? "a" : "b"))' + assert ( + str(var) + == '("x "+((((_condition, _if_true, _if_false) => (_condition ? _if_true : _if_false))(true, (() => "a"), (() => "b")))()))' + ) @pytest.mark.parametrize( @@ -40,32 +42,23 @@ def test_validate_cond(cond_state: BaseState): Args: cond_state: A fixture. """ - cond_component = cond( + first_component = Text.create("cond is True") + second_component = Text.create("cond is False") + cond_var = cond( cond_state.value, - Text.create("cond is True"), - Text.create("cond is False"), + first_component, + second_component, ) - cond_dict = cond_component.render() if type(cond_component) is Fragment else {} - assert cond_dict["name"] == "Fragment" - [condition] = cond_dict["children"] - assert condition["cond_state"] == f"isTrue({cond_state.get_full_name()}.value)" + assert isinstance(cond_var, Var) + assert ( + str(cond_var) + == f'((((_condition, _if_true, _if_false) => (_condition ? _if_true : _if_false))({cond_state.value.bool()!s}, (() => (jsx(RadixThemesText, ({{ ["as"] : "p" }}), (jsx(Fragment, ({{ }}), "cond is True"))))), (() => (jsx(RadixThemesText, ({{ ["as"] : "p" }}), (jsx(Fragment, ({{ }}), "cond is False")))))))())' + ) - # true value - true_value = condition["true_value"] - assert true_value["name"] == "Fragment" - - [true_value_text] = true_value["children"] - assert true_value_text["name"] == "RadixThemesText" - assert true_value_text["children"][0]["contents"] == '{"cond is True"}' - - # false value - false_value = condition["false_value"] - assert false_value["name"] == "Fragment" - - [false_value_text] = false_value["children"] - assert false_value_text["name"] == "RadixThemesText" - assert false_value_text["children"][0]["contents"] == '{"cond is False"}' + var_data = cond_var._get_all_var_data() + assert var_data is not None + assert var_data.components == (first_component, second_component) @pytest.mark.parametrize( @@ -96,25 +89,31 @@ def test_prop_cond(c1: Any, c2: Any): c1 = json.dumps(c1) if not isinstance(c2, Var): c2 = json.dumps(c2) - assert str(prop_cond) == f"(true ? {c1!s} : {c2!s})" + assert ( + str(prop_cond) + == f"((((_condition, _if_true, _if_false) => (_condition ? _if_true : _if_false))(true, (() => {c1!s}), (() => {c2!s})))())" + ) -def test_cond_no_mix(): - """Test if cond can't mix components and props.""" - with pytest.raises(ValueError): - cond(True, LiteralVar.create("hello"), Text.create("world")) +def test_cond_mix(): + """Test if cond can mix components and props.""" + x = cond(True, LiteralVar.create("hello"), Text.create("world")) + assert isinstance(x, Var) + assert ( + str(x) + == '((((_condition, _if_true, _if_false) => (_condition ? _if_true : _if_false))(true, (() => "hello"), (() => (jsx(RadixThemesText, ({ ["as"] : "p" }), (jsx(Fragment, ({ }), "world")))))))())' + ) def test_cond_no_else(): """Test if cond can be used without else.""" # Components should support the use of cond without else comp = cond(True, Text.create("hello")) - assert isinstance(comp, Fragment) - comp = comp.children[0] - assert isinstance(comp, Cond) - assert comp.cond._decode() is True # type: ignore - assert comp.comp1.render() == Fragment.create(Text.create("hello")).render() - assert comp.comp2 == Fragment.create() + assert isinstance(comp, Var) + assert ( + str(comp) + == '((((_condition, _if_true, _if_false) => (_condition ? _if_true : _if_false))(true, (() => (jsx(RadixThemesText, ({ ["as"] : "p" }), (jsx(Fragment, ({ }), "hello"))))), (() => (jsx(Fragment, ({ }))))))())' + ) # Props do not support the use of cond without else with pytest.raises(ValueError): @@ -140,7 +139,8 @@ def test_cond_computed_var(): state_name = format_state_name(CondStateComputed.get_full_name()) assert ( - str(comp) == f"(true ? {state_name}.computed_int : {state_name}.computed_str)" + str(comp) + == f"((((_condition, _if_true, _if_false) => (_condition ? _if_true : _if_false))(true, (() => {state_name}.computed_int), (() => {state_name}.computed_str)))())" ) assert comp._var_type == Union[int, str] diff --git a/tests/units/test_var.py b/tests/units/test_var.py index a09df1088..48f6eea43 100644 --- a/tests/units/test_var.py +++ b/tests/units/test_var.py @@ -346,8 +346,14 @@ def test_basic_operations(TestObj): str(LiteralNumberVar.create(1) ** 2) == "(((_lhs, _rhs) => (_lhs ** _rhs))(1, 2))" ) - assert str(LiteralNumberVar.create(1) & v(2)) == "(((_a, _b) => (_a && _b))(1, 2))" - assert str(LiteralNumberVar.create(1) | v(2)) == "(((_a, _b) => (_a || _b))(1, 2))" + assert ( + str(LiteralNumberVar.create(1) & v(2)) + == "(((_a, _b) => (_a() && _b()))((() => 1), (() => 2)))" + ) + assert ( + str(LiteralNumberVar.create(1) | v(2)) + == "(((_a, _b) => (_a() || _b()))((() => 1), (() => 2)))" + ) assert ( str(LiteralArrayVar.create([1, 2, 3])[0]) == "(((...args) => (((_array, _index_or_slice) => atSliceOrIndex(_array, _index_or_slice))([1, 2, 3], ...args)))(0))" @@ -507,25 +513,21 @@ def test_var_types(var, var_type): def test_str_contains(var, expected): assert ( str(var.contains("1")) - == f'(((...args) => (((_haystack, _needle, _field = "") => isTrue(_field) ? _haystack.some(obj => obj[_field] === _needle) : _haystack.some(obj => obj === _needle))({expected!s}, ...args)))("1"))' + == f'(((...args) => (((_haystack, _needle) => _haystack.includes(_needle))({expected!s}, ...args)))("1"))' ) assert ( str(var.contains(v("1"))) - == f'(((...args) => (((_haystack, _needle, _field = "") => isTrue(_field) ? _haystack.some(obj => obj[_field] === _needle) : _haystack.some(obj => obj === _needle))({expected!s}, ...args)))("1"))' + == f'(((...args) => (((_haystack, _needle) => _haystack.includes(_needle))({expected!s}, ...args)))("1"))' ) other_state_var = Var(_js_expr="other")._var_set_state("state").to(str) other_var = Var(_js_expr="other").to(str) assert ( str(var.contains(other_state_var)) - == f'(((...args) => (((_haystack, _needle, _field = "") => isTrue(_field) ? _haystack.some(obj => obj[_field] === _needle) : _haystack.some(obj => obj === _needle))({expected!s}, ...args)))(state.other))' + == f"(((...args) => (((_haystack, _needle) => _haystack.includes(_needle))({expected!s}, ...args)))(state.other))" ) assert ( str(var.contains(other_var)) - == f'(((...args) => (((_haystack, _needle, _field = "") => isTrue(_field) ? _haystack.some(obj => obj[_field] === _needle) : _haystack.some(obj => obj === _needle))({expected!s}, ...args)))(other))' - ) - assert ( - str(var.contains("1", "hello")) - == f'(((...args) => (((_haystack, _needle, _field = "") => isTrue(_field) ? _haystack.some(obj => obj[_field] === _needle) : _haystack.some(obj => obj === _needle))({expected!s}, ...args)))("1", "hello"))' + == f"(((...args) => (((_haystack, _needle) => _haystack.includes(_needle))({expected!s}, ...args)))(other))" ) @@ -1133,11 +1135,11 @@ def test_string_operations(): ) assert ( str(basic_string.contains("World")) - == '(((...args) => (((_haystack, _needle, _field = "") => isTrue(_field) ? _haystack.some(obj => obj[_field] === _needle) : _haystack.some(obj => obj === _needle))("Hello, World!", ...args)))("World"))' + == '(((...args) => (((_haystack, _needle) => _haystack.includes(_needle))("Hello, World!", ...args)))("World"))' ) assert ( str(basic_string.split(" ").join(",")) - == '(((...args) => (((_array, _sep = "") => _array.join(_sep))((((...args) => (((_string, _sep = "") => isTrue(_sep) ? _string.split(_sep) : [..._string])("Hello, World!", ...args)))(" ")), ...args)))(","))' + == '(((...args) => (((_array, _sep = "") => Array.prototype.join.apply(_array,[_sep]))((((...args) => (((_string, _sep = "") => isTrue(_sep) ? _string.split(_sep) : [..._string])("Hello, World!", ...args)))(" ")), ...args)))(","))' ) @@ -1157,7 +1159,7 @@ def test_all_number_operations(): assert ( str(even_more_complicated_number) - == "(((_value) => !(_value))((isTrue((((_a, _b) => (_a || _b))((Math.abs((Math.floor((((_lhs, _rhs) => (_lhs ** _rhs))((((_lhs, _rhs) => (_lhs % _rhs))((((_lhs, _rhs) => Math.floor(_lhs / _rhs))((((_lhs, _rhs) => (_lhs / _rhs))((((_lhs, _rhs) => (_lhs * _rhs))((((_value) => -(_value))((((_lhs, _rhs) => (_lhs + _rhs))(-5.4, 1)))), 2)), 3)), 2)), 3)), 2)))))), (((_a, _b) => (_a && _b))(2, (((_value) => Math.round(_value))((((_lhs, _rhs) => (_lhs ** _rhs))((((_lhs, _rhs) => (_lhs % _rhs))((((_lhs, _rhs) => Math.floor(_lhs / _rhs))((((_lhs, _rhs) => (_lhs / _rhs))((((_lhs, _rhs) => (_lhs * _rhs))((((_value) => -(_value))((((_lhs, _rhs) => (_lhs + _rhs))(-5.4, 1)))), 2)), 3)), 2)), 3)), 2))))))))))))" + == "(((_value) => !(_value))((isTrue((((_a, _b) => (_a() || _b()))((() => (Math.abs((Math.floor((((_lhs, _rhs) => (_lhs ** _rhs))((((_lhs, _rhs) => (_lhs % _rhs))((((_lhs, _rhs) => Math.floor(_lhs / _rhs))((((_lhs, _rhs) => (_lhs / _rhs))((((_lhs, _rhs) => (_lhs * _rhs))((((_value) => -(_value))((((_lhs, _rhs) => (_lhs + _rhs))(-5.4, 1)))), 2)), 3)), 2)), 3)), 2))))))), (() => (((_a, _b) => (_a() && _b()))((() => 2), (() => (((_value) => Math.round(_value))((((_lhs, _rhs) => (_lhs ** _rhs))((((_lhs, _rhs) => (_lhs % _rhs))((((_lhs, _rhs) => Math.floor(_lhs / _rhs))((((_lhs, _rhs) => (_lhs / _rhs))((((_lhs, _rhs) => (_lhs * _rhs))((((_value) => -(_value))((((_lhs, _rhs) => (_lhs + _rhs))(-5.4, 1)))), 2)), 3)), 2)), 3)), 2))))))))))))))" ) assert ( @@ -1250,19 +1252,19 @@ def test_array_operations(): ) assert ( str(ArrayVar.range(10)) - == "(((_e1, _e2 = null, _step = 1) => range(_e1, _e2, _step))(10))" + == "(((_e1, _e2 = null, _step = 1) => [...range(_e1, _e2, _step)])(10))" ) assert ( str(ArrayVar.range(1, 10)) - == "(((_e1, _e2 = null, _step = 1) => range(_e1, _e2, _step))(1, 10))" + == "(((_e1, _e2 = null, _step = 1) => [...range(_e1, _e2, _step)])(1, 10))" ) assert ( str(ArrayVar.range(1, 10, 2)) - == "(((_e1, _e2 = null, _step = 1) => range(_e1, _e2, _step))(1, 10, 2))" + == "(((_e1, _e2 = null, _step = 1) => [...range(_e1, _e2, _step)])(1, 10, 2))" ) assert ( str(ArrayVar.range(1, 10, -1)) - == "(((_e1, _e2 = null, _step = 1) => range(_e1, _e2, _step))(1, 10, -1))" + == "(((_e1, _e2 = null, _step = 1) => [...range(_e1, _e2, _step)])(1, 10, -1))" )