Skip to content

Commit

Permalink
test: Add tests for objects embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
kvankova committed Nov 3, 2024
1 parent ba27426 commit dc9357e
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 2 deletions.
16 changes: 16 additions & 0 deletions tests/data/example_python_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import re


# Function verifying an email is valid
def verify_email(email: str) -> bool:
return re.match(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$", email) is not None


class Person:
def __init__(self, name: str, age: int):
self.name = name
self.age = age

# String representation of the class
def __str__(self) -> str:
return f"Person(name={self.name}, age={self.age})"
25 changes: 25 additions & 0 deletions tests/data/expected_readme3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# README 3

This is a test README file for testing the code embedding process.

## Python objects

This section contains examples of Python objects.

```python:tests/data/example_python_objects.py:verify_email
def verify_email(email: str) -> bool:
return re.match(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$", email) is not None
```

```python:tests/data/example_python_objects.py:Person
class Person:
def __init__(self, name: str, age: int):
self.name = name
self.age = age
# String representation of the class
def __str__(self) -> str:
return f"Person(name={self.name}, age={self.age})"
```
13 changes: 13 additions & 0 deletions tests/data/readme3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# README 3

This is a test README file for testing the code embedding process.

## Python objects

This section contains examples of Python objects.

```python:tests/data/example_python_objects.py:verify_email
```

```python:tests/data/example_python_objects.py:Person
```
2 changes: 2 additions & 0 deletions tests/test_code_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ def test_code_embedder(tmp_path) -> None:
"tests/data/readme0.md",
"tests/data/readme1.md",
"tests/data/readme2.md",
"tests/data/readme3.md",
]
expected_paths = [
"tests/data/expected_readme0.md",
"tests/data/expected_readme1.md",
"tests/data/expected_readme2.md",
"tests/data/expected_readme3.md",
]

# Create a temporary copy of the original file
Expand Down
102 changes: 100 additions & 2 deletions tests/test_script_content_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
readme_end=0,
content="",
),
ScriptMetadata(
path="tests/data/example_python_objects.py",
extraction_part="",
readme_start=0,
readme_end=0,
content="",
),
],
[
ScriptMetadata(
Expand All @@ -44,8 +51,29 @@
"# code_embedder:A end\n"
),
),
ScriptMetadata(
path="tests/data/example_python_objects.py",
extraction_part="",
readme_start=0,
readme_end=0,
content=(
"import re\n"
"# Function verifying an email is valid\n"
"def verify_email(email: str) -> bool:\n"
' return re.match(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+'
'$", email) is not None\n'
"\n"
"class Person:\n"
" def __init__(self, name: str, age: int):\n"
" self.name = name\n"
" self.age = age\n"
"\n"
" # String representation of the class\n"
" def __str__(self) -> str:\n"
' return f"Person(name={self.name}, age={self.age})"\n'
),
),
],
id="one_full_script_one_script_with_section",
),
],
)
Expand Down Expand Up @@ -81,6 +109,50 @@ def test_read_full_script(
"# code_embedder:A end\n"
),
),
ScriptMetadata(
path="tests/data/example_python_objects.py",
extraction_part="verify_email",
readme_start=0,
readme_end=0,
content=(
"import re\n"
"# Function verifying an email is valid\n"
"def verify_email(email: str) -> bool:\n"
' return re.match(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+'
'$", email) is not None\n'
"\n"
"class Person:\n"
" def __init__(self, name: str, age: int):\n"
" self.name = name\n"
" self.age = age\n"
"\n"
" # String representation of the class\n"
" def __str__(self) -> str:\n"
' return f"Person(name={self.name}, age={self.age})"\n'
),
),
ScriptMetadata(
path="tests/data/example_python_objects.py",
extraction_part="Person",
readme_start=0,
readme_end=0,
content=(
"import re\n"
"# Function verifying an email is valid\n"
"def verify_email(email: str) -> bool:\n"
' return re.match(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+'
'$", email) is not None\n'
"\n"
"class Person:\n"
" def __init__(self, name: str, age: int):\n"
" self.name = name\n"
" self.age = age\n"
"\n"
" # String representation of the class\n"
" def __str__(self) -> str:\n"
' return f"Person(name={self.name}, age={self.age})"\n'
),
),
],
[
ScriptMetadata(
Expand All @@ -97,8 +169,34 @@ def test_read_full_script(
readme_end=0,
content='print("Printing only section A")',
),
ScriptMetadata(
path="tests/data/example_python_objects.py",
extraction_part="verify_email",
readme_start=0,
readme_end=0,
content=(
"def verify_email(email: str) -> bool:\n"
' return re.match(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+'
'$", email) is not None\n'
),
),
ScriptMetadata(
path="tests/data/example_python_objects.py",
extraction_part="Person",
readme_start=0,
readme_end=0,
content=(
"class Person:\n"
" def __init__(self, name: str, age: int):\n"
" self.name = name\n"
" self.age = age\n"
"\n"
" # String representation of the class\n"
" def __str__(self) -> str:\n"
' return f"Person(name={self.name}, age={self.age})"\n'
),
),
],
id="one_full_script_one_script_with_section",
),
],
)
Expand Down

0 comments on commit dc9357e

Please sign in to comment.