Skip to content

Commit 588d13a

Browse files
authored
Add unittests for verification.py (#10)
This adds previously missing unittests for the `verification.py` module.
1 parent 9112c53 commit 588d13a

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

test/test_verification.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import unittest
2+
from typing import List, Set
3+
4+
from bibtex_linter.verification import check_required_fields, check_omitted_fields, verify, linter_rule
5+
from bibtex_linter.parser import BibTeXEntry
6+
7+
8+
@linter_rule(entry_type="test_entry_type")
9+
def example_linter_rule(entry: BibTeXEntry) -> List[str]:
10+
violations = []
11+
required_fields: Set[str] = {"author", "title", "howpublished", "year"}
12+
omitted_fields: Set[str] = {"language", "organization", "address", "pages", "url"}
13+
violations.extend(check_required_fields(entry, required_fields))
14+
violations.extend(check_omitted_fields(entry, omitted_fields))
15+
return violations
16+
17+
18+
class TestVerification(unittest.TestCase):
19+
def test_check_required_fields_missing(self) -> None:
20+
entry = BibTeXEntry(
21+
entry_type="test_entry_type",
22+
name="missing_fields",
23+
fields={"author": "Jane"}
24+
)
25+
expected = ["Entry 'missing_fields' misses the following required fields: [howpublished, title, year]"]
26+
actual = check_required_fields(entry, {"author", "title", "howpublished", "year"})
27+
self.assertEqual(expected, actual)
28+
29+
def test_check_required_fields_complete(self) -> None:
30+
entry = BibTeXEntry(
31+
entry_type="test_entry_type",
32+
name="complete",
33+
fields={"author": "Jane", "title": "Work", "howpublished": "Online", "year": "2020"}
34+
)
35+
self.assertEqual([], check_required_fields(entry, {"author", "title", "howpublished", "year"}))
36+
37+
def test_check_omitted_fields_present(self) -> None:
38+
entry = BibTeXEntry(
39+
entry_type="test_entry_type",
40+
name="omit_test",
41+
fields={"author": "Jane", "language": "en", "url": "example.com"}
42+
)
43+
expected = [
44+
"Entry 'omit_test' has fields present that would be omitted in the compiled document: "
45+
"[language, url]. This could lead to a loss of information."
46+
]
47+
actual = check_omitted_fields(entry, {"language", "organization", "address", "pages", "url"})
48+
self.assertEqual(expected, actual)
49+
50+
def test_check_omitted_fields_absent(self) -> None:
51+
entry = BibTeXEntry(
52+
entry_type="test_entry_type",
53+
name="omit_ok",
54+
fields={"author": "Jane", "title": "Work"}
55+
)
56+
self.assertEqual([], check_omitted_fields(entry, {"url"}))
57+
58+
def test_verify_combined_rule(self) -> None:
59+
entry = BibTeXEntry(
60+
entry_type="test_entry_type",
61+
name="bad_entry",
62+
fields={"author": "Jane", "url": "http://example.org"}
63+
)
64+
expected = [
65+
"Entry 'bad_entry' misses the following required fields: [howpublished, title, year]",
66+
"Entry 'bad_entry' has fields present that would be omitted in the compiled document: [url]. "
67+
"This could lead to a loss of information."
68+
]
69+
actual = verify(entry)
70+
self.assertEqual(expected, actual)
71+
72+
def test_verify_skips_different_entry_type(self) -> None:
73+
entry = BibTeXEntry(
74+
entry_type="unrelated_type", # does not match the rule's "test_entry_type"
75+
name="skipped_entry",
76+
fields={"author": "Someone", "url": "http://example.org"}
77+
)
78+
actual = verify(entry)
79+
expected: List[str] = [] # No rules should apply
80+
self.assertEqual(expected, actual)
81+
82+
83+
if __name__ == "__main__":
84+
unittest.main()

0 commit comments

Comments
 (0)