Skip to content

Commit f34e0d9

Browse files
committed
move error check to func
1 parent 8ccb9c7 commit f34e0d9

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

quartodoc/tests/test_validation.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,43 @@
2323
def sections():
2424
return copy.deepcopy(EXAMPLE_SECTIONS)
2525

26+
def check_ValueError(sections):
27+
"Check that a ValueError is raised when creating a `Builder` instance. Return the error message as a string."
28+
with pytest.raises(ValueError) as e:
29+
Builder(sections=sections, package='quartodoc')
30+
return str(e.value)
31+
2632
def test_valid_yaml(sections):
2733
"Test that valid YAML passes validation"
2834
Builder(sections=sections, package='quartodoc')
2935

3036
def test_missing_title(sections):
3137
"Test that missing title raises an error"
3238
del sections[0]['title']
33-
with pytest.raises(ValueError) as e:
34-
Builder(sections=sections, package='quartodoc')
35-
assert '- Missing field `title` for element 0 in the list for `sections`' in str(e.value)
39+
msg = check_ValueError(sections)
40+
assert '- Missing field `title` for element 0 in the list for `sections`' in msg
3641

3742
def test_missing_desc(sections):
3843
"Test that a missing description raises an error"
3944
sections = copy.deepcopy(EXAMPLE_SECTIONS)
4045
del sections[2]['desc']
41-
with pytest.raises(ValueError) as e:
42-
Builder(sections=sections, package='quartodoc')
43-
assert '- Missing field `desc` for element 2 in the list for `sections`' in str(e.value)
46+
msg = check_ValueError(sections)
47+
assert '- Missing field `desc` for element 2 in the list for `sections`' in msg
4448

4549
def test_missing_name_contents_1(sections):
4650
"Test that a missing name in contents raises an error"
4751
del sections[2]['contents'][0]['name']
48-
with pytest.raises(ValueError) as e:
49-
Builder(sections=sections, package='quartodoc')
50-
assert '- Missing field `name` for element 0 in the list for `contents` located in element 2 in the list for `sections`' in str(e.value)
52+
msg = check_ValueError(sections)
53+
assert '- Missing field `name` for element 0 in the list for `contents` located in element 2 in the list for `sections`' in msg
5154

5255
def test_missing_name_contents_2(sections):
5356
"Test that a missing name in contents raises an error in a different section."
5457
del sections[1]['contents'][0]['name']
55-
with pytest.raises(ValueError) as e:
56-
Builder(sections=sections, package='quartodoc')
57-
assert '- Missing field `name` for element 0 in the list for `contents` located in element 1 in the list for `sections`' in str(e.value)
58+
msg = check_ValueError(sections)
59+
assert '- Missing field `name` for element 0 in the list for `contents` located in element 1 in the list for `sections`' in msg
5860

5961
def test_misplaced_kindpage(sections):
6062
"Test that a misplaced kind: page raises an error"
6163
sections[0]['kind'] = 'page'
62-
with pytest.raises(ValueError) as e:
63-
Builder(sections=sections, package='quartodoc')
64-
assert ' - Missing field `path` for element 0 in the list for `sections`, which you need when setting `kind: page`.' in str(e.value)
64+
msg = check_ValueError(sections)
65+
assert ' - Missing field `path` for element 0 in the list for `sections`, which you need when setting `kind: page`.' in msg

0 commit comments

Comments
 (0)