Skip to content

Commit

Permalink
Improve parsing features (#3)
Browse files Browse the repository at this point in the history
* Add more test cases for parsing frames

* Support multi syntax

* Update version
  • Loading branch information
ducquando authored May 3, 2024
1 parent 4a056b3 commit 191b3e5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 15 deletions.
Binary file modified .coverage
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codeslide-server",
"version": "1.3.0",
"version": "1.4.0",
"description": "Server for codeslide.net",
"scripts": {
"start": "nodemon index.ts",
Expand Down
7 changes: 5 additions & 2 deletions utils/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ def parse_frames(script):
if is_next and to_index == '':
is_eof = True

# Continue if indexes are not number (e.g. Python, Graph)
# Add index to indexes and extend frames if needed
start = int(fr_index)
frames = set_frame(frames, range(len(frames), start + 1), eofs, [])
end = int(to_index) if to_index != '' else len(frames) if is_next else start
indexes += range(start, end + 1)

Expand All @@ -178,11 +179,13 @@ def parse_frames(script):
if script[i] == '>':
is_timing = False
is_next = False
elif script[i] == ',':
is_next = False

elif script[i] == ',' and not (is_timing or is_bracket):
curr_index += 1
curr_object.append('')

# Otherwise, append char to to_index/fr_index/curr_object
elif script[i] != ' ':
if is_timing:
Expand Down
99 changes: 87 additions & 12 deletions utils/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_get_script():
"""
Test get_script function
"""
# Passed, with Python code
# Passed, with Python code
script = "<Python>print('Hello, World!')\nprint('Hi')"
expected_code = "print('Hello, World!')\nprint('Hi')"
expected_script = ""
Expand Down Expand Up @@ -144,41 +144,116 @@ def test_parse_frames():

assert actual_frames == expected_frames

# Passed, with from-to and multi syntax
script = "<1-3> Object1 \n <Python>print('<2,4> Object2')"
expected_frames = [["Object1"], ["Object1", "Object2"], ["Object1"], ["Object2"]]
# Passed, with from-to syntax
script = "<1-3> Object1 \n <Python>print('<2-4> Object2')"
expected_frames = [["Object1"], ["Object1", "Object2"], ["Object1", "Object2"], ["Object2"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with multi syntax
script = "<1,3,5> Object1 \n <Python>print('<2,4,6> Object2')"
expected_frames = [["Object1"], ["Object2"], ["Object1"], ["Object2"], ["Object1"], ["Object2"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with single infinity syntax (1)
script = "<3-> Object1 \n <Python>print('<4> Object2')"
expected_frames = [[], [], ["Object1"], ["Object1", "Object2"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with infinity syntax
script = "<1-> Object1 \n <Python>print('<2> Object2')"
expected_frames = [["Object1"], ["Object1", "Object2"]]
# Passed, with single infinity syntax (2)
script = "<4> Object2 \n <Python>print('<3-> Object1')"
expected_frames = [[], [], ["Object1"], ["Object2", "Object1"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with assignment
# Passed, with multiple infinity syntax (1)
script = "<3-> Object1 \n <Python>print('<4-> Object2')"
expected_frames = [[], [], ["Object1"], ["Object1", "Object2"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with multiple infinity syntax (2)
script = "<4-> Object2 \n <Python>print('<3-> Object1')"
expected_frames = [[], [], ["Object1"], ["Object2", "Object1"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with assignment (1)
script = "<1> Object2 \n <Python>print('<2> Object1 = {\"TEXT\": \"Hello, world!\"}')"
expected_frames = [["Object2"], ["Object1={\"TEXT\": \"Hello, world!\"}"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with no overriding
# Passed, with assignment (2)
script = "<2> Object1 = {\"TEXT\": \"Hello, world!\"} \n <Python>print('<1> Object2')"
expected_frames = [["Object2"], ["Object1={\"TEXT\": \"Hello, world!\"}"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with overriding (1)
script = "<1> Object1 \n <Python>print('<1> Object1 = {\"TEXT\": \"Hello, world!\"}')"
expected_frames = [["Object1", "Object1={\"TEXT\": \"Hello, world!\"}"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with overriding (2)
script = "<1> Object1 = {\"TEXT\": \"Hello, world!\"} \n <Python>print('<1> Object1')"
expected_frames = [["Object1={\"TEXT\": \"Hello, world!\"}", "Object1"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with infinite objects
script = "<1-> Object1 \n <Python>print('<2> Object2')"
expected_frames = [["Object1"], ["Object1", "Object2"]]
# Passed, with multi syntax including from-to (1)
script = "<1,3-4> Object1 \n <Python>print('<2,5-6> Object2')"
expected_frames = [["Object1"], ["Object2"], ["Object1"], ["Object1"], ["Object2"], ["Object2"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with multi syntax including from-to (2)
script = "<3-4,1> Object1 \n <Python>print('<5-6,2> Object2')"
expected_frames = [["Object1"], ["Object2"], ["Object1"], ["Object1"], ["Object2"], ["Object2"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with multi syntax including infinite (1)
script = "<1,3-> Object1 \n <Python>print('<2,4-> Object2')"
expected_frames = [["Object1"], ["Object2"], ["Object1"], ["Object1", "Object2"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with multi syntax including infinite (2)
script = "<3-,1> Object1 \n <Python>print('<4-,2> Object2')"
expected_frames = [["Object1"], ["Object2"], ["Object1"], ["Object1", "Object2"]]
actual_frames = parse_frames(script)

# Passed, with multi syntax including both infinite & from-to (1)
script = "<1-2,3-> Object1 \n <Python>print('<2-3,4-> Object2')"
expected_frames = [["Object1"], ["Object1", "Object2"], ["Object1", "Object2"], ["Object1", "Object2"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Passed, with multi syntax including both infinite & from-to (2)
script = "<3-,1-2> Object1 \n <Python>print('<4-,2-3> Object2')"
expected_frames = [["Object1"], ["Object1", "Object2"], ["Object1", "Object2"], ["Object1", "Object2"]]
actual_frames = parse_frames(script)

assert actual_frames == expected_frames

# Failed, with syntax after python tag
script = "<1-> Object1 \n <Python>print('<2> Object2') \n <2> Object2"
with pytest.raises(Exception):
Expand Down

0 comments on commit 191b3e5

Please sign in to comment.