Skip to content

Fixing unlinked traits in WCI due to invalid code_theme (Fix #53) #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/scwidgets/code/_widget_code_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ class CodeInput(WidgetCodeInput):
Small wrapper around WidgetCodeInput that controls the output
"""

valid_code_themes = ["nord", "solarizedLight", "basicLight"]

def __init__(
self,
function: Optional[types.FunctionType] = None,
function_name: Optional[str] = None,
function_parameters: Optional[str] = None,
docstring: Optional[str] = None,
function_body: Optional[str] = None,
code_theme: str = "default",
code_theme: str = "basicLight",
):
if function is not None:
function_name = (
Expand All @@ -46,6 +48,14 @@ def __init__(
function_name, function_parameters, docstring, function_body, code_theme
)

# this list is retrieved from
# https://github.com/osscar-org/widget-code-input/blob/eb10ca0baee65dd3bf62c9ec5d9cb2f152932ff5/js/widget.js#L249-L253
if code_theme not in CodeInput.valid_code_themes:
raise ValueError(
f"Given code_theme {code_theme!r} invalid. Please use one of "
f"the values {CodeInput.valid_code_themes}"
)

@property
def function(self) -> types.FunctionType:
"""
Expand Down
38 changes: 38 additions & 0 deletions tests/notebooks/widget_scwidgets_code_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# ---
# jupyter:
# jupytext:
# cell_metadata_filter: -all
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.15.0
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---

# +
import scwidgets
from scwidgets.code import CodeInput

# -

scwidgets.get_css_style()

# Test 1:
# -------
# Test if CodeInput traits are updading the widget view


# +
def foo():
return "init"


ci = CodeInput(foo)
ci
# -

ci.function_body = """return 'change'"""
6 changes: 6 additions & 0 deletions tests/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ def test_get_code(self):
):
CodeInput.get_code(lambda x: x)

def test_invalid_code_theme_raises_error(self):
with pytest.raises(
ValueError, match=r"Given code_theme 'invalid_theme' invalid.*"
):
CodeInput(TestCodeInput.mock_function_1, code_theme="invalid_theme")


def get_code_exercise(
checks: List[Check],
Expand Down
17 changes: 17 additions & 0 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,23 @@ def test_privacy_policy(selenium_driver):
).click()


def test_scwidgets_code_input(selenium_driver):
"""
Tests the widget of the module code

:param selenium_driver: see conftest.py
"""
driver = selenium_driver("tests/notebooks/widget_scwidgets_code_input.ipynb")

nb_cells = NotebookCellList(driver)
# Test 1:
# -------

# Tests if change in function_body changed the widget view
code_input_lines = nb_cells[2].find_elements(By.CLASS_NAME, CODE_MIRROR_CLASS_NAME)
assert "return 'change'" in code_input_lines[-1].text


class TestExerciseWidgets:
prefix = "pytest"

Expand Down
Loading