|
1 | | -import folium |
2 | | -from folium.template import tojavascript |
3 | | -from folium.utilities import JsCode |
4 | | - |
5 | | - |
6 | | -def test_tojavascript(): |
7 | | - trail_coordinates = [ |
8 | | - (-71.351871840295871, -73.655963711222626), |
9 | | - (-71.374144382613707, -73.719861619751498), |
10 | | - (-71.391042575973145, -73.784922248007007), |
11 | | - (-71.400964450973134, -73.851042243124397), |
12 | | - (-71.402411391077322, -74.050048183880477), |
13 | | - ] |
| 1 | +from branca.element import Element |
| 2 | + |
| 3 | +from folium import JsCode |
| 4 | +from folium.template import Environment, Template, _to_escaped_json, tojavascript |
| 5 | + |
| 6 | + |
| 7 | +def test_tojavascript_with_jscode(): |
| 8 | + js_code = JsCode("console.log('Hello, World!')") |
| 9 | + assert tojavascript(js_code) == "console.log('Hello, World!')" |
| 10 | + |
| 11 | + |
| 12 | +def test_tojavascript_with_element(): |
| 13 | + element = Element() |
| 14 | + assert tojavascript(element) == element.get_name() |
| 15 | + |
| 16 | + |
| 17 | +def test_tojavascript_with_dict(): |
| 18 | + dict_obj = {"key": "value"} |
| 19 | + assert tojavascript(dict_obj) == '{\n "key": "value",\n}' |
| 20 | + |
| 21 | + |
| 22 | +def test_tojavascript_with_list(): |
| 23 | + list_obj = ["value1", "value2"] |
| 24 | + assert tojavascript(list_obj) == '[\n"value1",\n"value2",\n]' |
14 | 25 |
|
15 | | - trail = folium.PolyLine(trail_coordinates, tooltip="Coast") |
16 | | - d = { |
17 | | - "label": "Base Layers", |
18 | | - "children": [ |
19 | | - { |
20 | | - "label": "World 🗺", |
21 | | - "children": [ |
22 | | - {"label": "trail", "layer": trail}, |
23 | | - {"jscode": JsCode('function(){return "hi"}')}, |
24 | | - ], |
25 | | - } |
26 | | - ], |
| 26 | + |
| 27 | +def test_tojavascript_with_string(): |
| 28 | + assert tojavascript("Hello, World!") == _to_escaped_json("Hello, World!") |
| 29 | + |
| 30 | + |
| 31 | +def test_tojavascript_with_combined_elements(): |
| 32 | + js_code = JsCode("console.log('Hello, World!')") |
| 33 | + element = Element() |
| 34 | + combined_dict = { |
| 35 | + "key": "value", |
| 36 | + "list": ["value1", "value2", element, js_code], |
| 37 | + "nested_dict": {"nested_key": "nested_value"}, |
27 | 38 | } |
28 | | - js = tojavascript(d) |
29 | | - assert "poly_line" in js |
30 | | - assert 'return "hi"' in js |
| 39 | + result = tojavascript(combined_dict) |
| 40 | + expected_lines = [ |
| 41 | + "{", |
| 42 | + ' "key": "value",', |
| 43 | + ' "list": [', |
| 44 | + '"value1",', |
| 45 | + '"value2",', |
| 46 | + element.get_name() + ",", |
| 47 | + "console.log('Hello, World!'),", |
| 48 | + "],", |
| 49 | + ' "nestedDict": {', |
| 50 | + ' "nestedKey": "nested_value",', |
| 51 | + "},", |
| 52 | + "}", |
| 53 | + ] |
| 54 | + for result_line, expected_line in zip(result.splitlines(), expected_lines): |
| 55 | + assert result_line == expected_line |
| 56 | + |
| 57 | + |
| 58 | +def test_to_escaped_json(): |
| 59 | + assert _to_escaped_json("hi<>&'") == '"hi\\u003c\\u003e\\u0026\\u0027"' |
| 60 | + |
| 61 | + |
| 62 | +def test_environment_filter(): |
| 63 | + env = Environment() |
| 64 | + assert "tojavascript" in env.filters |
| 65 | + |
| 66 | + |
| 67 | +def test_template_environment_class(): |
| 68 | + assert Template.environment_class == Environment |
0 commit comments