Add litwiki unit test to pre commit hook

This commit is contained in:
Leni Aniva 2023-09-11 17:19:26 -07:00 committed by Leni Aniva
parent 4e336fb6b2
commit 957af95ba1
5 changed files with 83 additions and 5 deletions

View File

@ -8,9 +8,7 @@ repos:
- id: check-added-large-files
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
- id: litwiki-tests
name: litwiki-tests
entry: ./test.sh
language: system
types: [python]
require_serial: true

12
Pipfile Normal file
View File

@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
orgparse = "*"
[dev-packages]
[requires]
python_version = "3.11"

29
Pipfile.lock generated Normal file
View File

@ -0,0 +1,29 @@
{
"_meta": {
"hash": {
"sha256": "22f5663e77fd5f550bbb84f958573b3be232bb1211f2944c495839868e6f8553"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.11"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"orgparse": {
"hashes": [
"sha256:451050e79acb7a51c65dc99b9095eae4d50bd598541354f9e763cb4cbdf59a55",
"sha256:f8c8b6c07e8c5a99a27ad3962eedab3aa3844129cbdfd3f2cd32a2197da462fe"
],
"index": "pypi",
"version": "==0.3.2"
}
},
"develop": {}
}

36
litwiki/citation.py Normal file
View File

@ -0,0 +1,36 @@
import unittest
from typing import Optional
import dataclasses
@dataclasses.dataclass
class Citation:
volume: int
chapter: int
line: int
def parse(src: str) -> Optional[Citation]:
try:
volume, chapter, line = src.split(".")
volume = int(volume)
chapter = int(chapter)
line = int(line)
if volume <= 0 or chapter <= 0 or line <= 0:
raise ValueError(f"Invalid citation point {volume}.{chapter}.{line}")
return Citation(volume, chapter, line)
except ValueError:
return None
class TestCitation(unittest.TestCase):
def test_parse(self):
self.assertEqual(parse("1.2.3"), Citation(1, 2, 3))
self.assertEqual(parse("1.2.0"), None)
self.assertEqual(parse("(I.2.3)"), None)
self.assertEqual(parse("I.5.1"), None)
self.assertEqual(parse(""), None)
if __name__ == "__main__":
unittest.main()

3
test.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
python -m litwiki.citation