Skip to main content

Merge pull request #9 from alexwlchan/allow-var

ID
a023b4c
date
2024-08-17 20:03:20+00:00
author
Alex Chan <alex@alexwlchan.net>
parents
64e2d6c, 5f4763f
message
Merge pull request #9 from alexwlchan/allow-var

Allow variables prefixed with `var`, or no prefix at all
changed files
2 files, 13 additions, 11 deletions

Changed files

src/javascript/__init__.py (2413) → src/javascript/__init__.py (2423)

diff --git a/src/javascript/__init__.py b/src/javascript/__init__.py
index f37c2ea..fbced59 100644
--- a/src/javascript/__init__.py
+++ b/src/javascript/__init__.py
@@ -25,7 +25,7 @@ def read_js(p: pathlib.Path | str, *, varname: str) -> typing.Any:
     with open(p) as in_file:
         contents = in_file.read()
 
-    m = re.compile(r"^const %s = " % varname)
+    m = re.compile(r"^(?:const |var )?%s = " % varname)
 
     if not m.match(contents):
         raise ValueError(

tests/test_javascript.py (4431) → tests/test_javascript.py (4509)

diff --git a/tests/test_javascript.py b/tests/test_javascript.py
index e6639a0..c5fb33b 100644
--- a/tests/test_javascript.py
+++ b/tests/test_javascript.py
@@ -7,11 +7,19 @@ from javascript import append_to_js_array, read_js, write_js
 
 
 class TestReadJs:
-    def test_can_read_file(self, tmp_path: pathlib.Path) -> None:
+    @pytest.mark.parametrize(
+        "text",
+        [
+            'const redPentagon = {\n  "sides": 5,\n  "colour": "red"\n};\n',
+            'var redPentagon = {\n  "sides": 5,\n  "colour": "red"\n};\n',
+            'redPentagon = {\n  "sides": 5,\n  "colour": "red"\n};\n',
+            'const redPentagon = {\n  "sides": 5,\n  "colour": "red"\n};',
+            'const redPentagon = {\n  "sides": 5,\n  "colour": "red"\n}',
+        ],
+    )
+    def test_can_read_file(self, tmp_path: pathlib.Path, text: str) -> None:
         js_path = tmp_path / "shape.js"
-        js_path.write_text(
-            'const redPentagon = {\n  "sides": 5,\n  "colour": "red"\n};\n'
-        )
+        js_path.write_text(text)
 
         assert read_js(js_path, varname="redPentagon") == {"sides": 5, "colour": "red"}
 
@@ -37,12 +45,6 @@ class TestReadJs:
         ):
             read_js(js_path, varname="blueTriangle")
 
-    def test_allows_trailing_semicolon(self, tmp_path: pathlib.Path) -> None:
-        js_path = tmp_path / "shape.js"
-        js_path.write_text('const redPentagon = {\n  "sides": 5,\n  "colour": "red"\n}')
-
-        assert read_js(js_path, varname="redPentagon") == {"sides": 5, "colour": "red"}
-
 
 class TestWriteJs:
     def test_can_write_file(self, tmp_path: pathlib.Path) -> None: