11from __future__ import annotations
22
33import logging
4- import re
54import sys
65from contextlib import ExitStack
76from pathlib import Path
8- from test .utils .exceptions import ExceptionChecker
7+ from test .utils .outcome import ExceptionChecker , OutcomeChecker , OutcomePrimitive
98from typing import TYPE_CHECKING , Any , Dict , Mapping , Optional , Set , Tuple , Type , Union
109
1110import pytest
@@ -374,7 +373,7 @@ def test_compute_qname(
374373 manager_prefixes : Optional [Mapping [str , Namespace ]],
375374 graph_prefixes : Optional [Mapping [str , Namespace ]],
376375 store_prefixes : Optional [Mapping [str , Namespace ]],
377- expected_result : Union [Tuple [str , URIRef , str ], Type [ Exception ], Exception ],
376+ expected_result : OutcomePrimitive [Tuple [str , URIRef , str ]],
378377) -> None :
379378 """
380379 :param uri: argument to compute_qname()
@@ -403,25 +402,13 @@ def test_compute_qname(
403402 nm .bind (prefix , ns )
404403
405404 def check () -> None :
406- catcher : Optional [pytest .ExceptionInfo [Exception ]] = None
407- with ExitStack () as xstack :
408- if isinstance (expected_result , type ) and issubclass (
409- expected_result , Exception
410- ):
411- catcher = xstack .enter_context (pytest .raises (expected_result ))
412- if isinstance (expected_result , Exception ):
413- catcher = xstack .enter_context (pytest .raises (type (expected_result )))
405+ checker = OutcomeChecker [Tuple [str , URIRef , str ]].from_primitive (
406+ expected_result
407+ )
408+ with checker .context ():
414409 actual_result = nm .compute_qname (uri , generate )
415410 logging .debug ("actual_result = %s" , actual_result )
416- if catcher is not None :
417- assert catcher is not None
418- assert catcher .value is not None
419- if isinstance (expected_result , Exception ):
420- assert re .match (expected_result .args [0 ], f"{ catcher .value } " )
421- else :
422- assert isinstance (expected_result , tuple )
423- assert isinstance (actual_result , tuple )
424- assert actual_result == expected_result
411+ checker .check (actual_result )
425412
426413 check ()
427414 # Run a second time to check caching
@@ -452,7 +439,7 @@ def test_compute_qname_strict(
452439 generate : bool ,
453440 bind_namespaces : _NamespaceSetString ,
454441 additional_prefixes : Optional [Mapping [str , Namespace ]],
455- expected_result : Union [Tuple [str , URIRef , str ], Type [ Exception ], Exception ],
442+ expected_result : OutcomePrimitive [Tuple [str , str , str ]],
456443) -> None :
457444 graph = Graph (bind_namespaces = bind_namespaces )
458445 nm = graph .namespace_manager
@@ -462,25 +449,11 @@ def test_compute_qname_strict(
462449 nm .bind (prefix , ns )
463450
464451 def check () -> None :
465- catcher : Optional [pytest .ExceptionInfo [Exception ]] = None
466- with ExitStack () as xstack :
467- if isinstance (expected_result , type ) and issubclass (
468- expected_result , Exception
469- ):
470- catcher = xstack .enter_context (pytest .raises (expected_result ))
471- if isinstance (expected_result , Exception ):
472- catcher = xstack .enter_context (pytest .raises (type (expected_result )))
452+ checker = OutcomeChecker [Tuple [str , str , str ]].from_primitive (expected_result )
453+ with checker .context ():
473454 actual_result = nm .compute_qname_strict (uri , generate )
474455 logging .debug ("actual_result = %s" , actual_result )
475- if catcher is not None :
476- assert catcher is not None
477- assert catcher .value is not None
478- if isinstance (expected_result , Exception ):
479- assert re .match (expected_result .args [0 ], f"{ catcher .value } " )
480- else :
481- assert isinstance (expected_result , tuple )
482- assert isinstance (actual_result , tuple )
483- assert actual_result == expected_result
456+ checker .check (actual_result )
484457
485458 check ()
486459 # Run a second time to check caching
@@ -538,16 +511,15 @@ def test_nsm_function() -> NamespaceManager:
538511def test_expand_curie (
539512 test_nsm_session : NamespaceManager ,
540513 curie : str ,
541- expected_result : Union [ ExceptionChecker , str ],
514+ expected_result : OutcomePrimitive [ str ],
542515) -> None :
543516 nsm = test_nsm_session
544- with ExitStack () as xstack :
545- if isinstance (expected_result , ExceptionChecker ):
546- xstack .enter_context (expected_result )
547- result = nsm .expand_curie (curie )
548-
549- if not isinstance (expected_result , ExceptionChecker ):
550- assert URIRef (expected_result ) == result
517+ if isinstance (expected_result , str ):
518+ expected_result = URIRef (expected_result )
519+ checker = OutcomeChecker [str ].from_primitive (expected_result )
520+ with checker .context ():
521+ actual_result = nsm .expand_curie (curie )
522+ checker .check (actual_result )
551523
552524
553525@pytest .mark .parametrize (
@@ -578,7 +550,7 @@ def test_generate_curie(
578550 test_nsm_function : NamespaceManager ,
579551 uri : str ,
580552 generate : Optional [bool ],
581- expected_result : Union [ ExceptionChecker , str ],
553+ expected_result : OutcomePrimitive [ str ],
582554) -> None :
583555 """
584556 .. note::
@@ -587,13 +559,10 @@ def test_generate_curie(
587559 effects and will modify the namespace manager.
588560 """
589561 nsm = test_nsm_function
590- with ExitStack () as xstack :
591- if isinstance (expected_result , ExceptionChecker ):
592- xstack .enter_context (expected_result )
562+ checker = OutcomeChecker [str ].from_primitive (expected_result )
563+ with checker .context ():
593564 if generate is None :
594- result = nsm .curie (uri )
565+ actual_result = nsm .curie (uri )
595566 else :
596- result = nsm .curie (uri , generate = generate )
597-
598- if not isinstance (expected_result , ExceptionChecker ):
599- assert expected_result == result
567+ actual_result = nsm .curie (uri , generate = generate )
568+ checker .check (actual_result )
0 commit comments