Skip to content

Commit

Permalink
Remove assertRaisesMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jun 9, 2022
1 parent 603b2af commit c9584b5
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 219 deletions.
4 changes: 0 additions & 4 deletions cssutils/tests/basetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ def setup(self):
cssutils.log.setLevel(logging.FATAL)
self.p = cssutils.CSSParser(raiseExceptions=True)

def assertRaisesMsg(self, excClass, msg, callableObj, *args, **kwargs):
with pytest.raises(excClass, match=re.escape(msg)):
callableObj(*args, **kwargs)

def do_equal_p(self, tests, att='cssText', debug=False, raising=True):
"""
if raising self.p is used for parsing, else self.pf
Expand Down
16 changes: 4 additions & 12 deletions cssutils/tests/test_cssstylesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,8 @@ def test_namespaces4(self):
del s.namespaces.namespaces['p']
assert {'p': 'uri'} == s.namespaces.namespaces

self.assertRaisesMsg(
xml.dom.NamespaceErr,
"Prefix undefined not found.",
s.namespaces.__delitem__,
'undefined',
)
with pytest.raises(xml.dom.NamespaceErr, match=r"Prefix undefined not found\."):
del s.namespaces['undefined']

def test_namespaces5(self):
"CSSStyleSheet.namespaces 5"
Expand All @@ -526,12 +522,8 @@ def test_namespaces5(self):
assert s.cssText == ''.encode()

s = cssutils.css.CSSStyleSheet()
self.assertRaisesMsg(
xml.dom.NamespaceErr,
"Prefix a not found.",
s._setCssText,
'a|a { color: red }',
)
with pytest.raises(xml.dom.NamespaceErr, match=r"Prefix a not found\."):
s._setCssText('a|a { color: red }')

def test_deleteRuleIndex(self):
"CSSStyleSheet.deleteRule(index)"
Expand Down
150 changes: 62 additions & 88 deletions cssutils/tests/test_cssvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,8 @@ def test_setFloat(self):
for setType, setValue, exp, cssText in tests[test]:
if type(exp) == type or type(exp) == type: # 2.4 compatibility
if cssText:
self.assertRaisesMsg(
exp, cssText, pv.setFloatValue, setType, setValue
)
with pytest.raises(exp, match=cssText):
pv.setFloatValue(setType, setValue)
else:
with pytest.raises(exp):
pv.setFloatValue(setType, setValue)
Expand Down Expand Up @@ -691,55 +690,43 @@ def test_setString(self):
v.setStringValue(v.CSS_STRING, 'b')
assert ('b', 'STRING') == v._value
assert 'b' == v.getStringValue()
self.assertRaisesMsg(
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: Cannot coerce primitiveType "
"'CSS_STRING' to 'CSS_URI'",
v.setStringValue,
*(v.CSS_URI, 'x')
)
self.assertRaisesMsg(
match="CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_STRING' to 'CSS_URI'",
):
v.setStringValue(v.CSS_URI, 'x')
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: Cannot coerce primitiveType "
"'CSS_STRING' to 'CSS_IDENT'",
v.setStringValue,
*(v.CSS_IDENT, 'x')
)
self.assertRaisesMsg(
match="CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_STRING' to 'CSS_IDENT'",
):
v.setStringValue(v.CSS_IDENT, 'x')
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: Cannot coerce primitiveType "
"'CSS_STRING' to 'CSS_ATTR'",
v.setStringValue,
*(v.CSS_ATTR, 'x')
)
match="CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_STRING' to 'CSS_ATTR'",
):
v.setStringValue(v.CSS_ATTR, 'x')

# CSS_IDENT
v = cssutils.css.CSSPrimitiveValue('new')
v.setStringValue(v.CSS_IDENT, 'ident')
assert v.CSS_IDENT == v.primitiveType
assert ('ident', 'IDENT') == v._value
assert 'ident' == v.getStringValue()
self.assertRaisesMsg(
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: Cannot coerce primitiveType "
"'CSS_IDENT' to 'CSS_URI'",
v.setStringValue,
*(v.CSS_URI, 'x')
)
self.assertRaisesMsg(
match="CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_IDENT' to 'CSS_URI'",
):
v.setStringValue(v.CSS_URI, 'x')
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: Cannot coerce primitiveType "
"'CSS_IDENT' to 'CSS_STRING'",
v.setStringValue,
*(v.CSS_STRING, '"x"')
)
self.assertRaisesMsg(
match="CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_IDENT' to 'CSS_STRING'",
):
v.setStringValue(v.CSS_STRING, '"x"')
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: Cannot coerce primitiveType "
"'CSS_IDENT' to 'CSS_ATTR'",
v.setStringValue,
*(v.CSS_ATTR, 'x')
)
match="CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_IDENT' to 'CSS_ATTR'",
):
v.setStringValue(v.CSS_ATTR, 'x')

# CSS_URI
v = cssutils.css.CSSPrimitiveValue('url(old)')
Expand Down Expand Up @@ -776,74 +763,61 @@ def test_setString(self):
assert ('a', 'URI') == v._value
assert 'a' == v.getStringValue()

self.assertRaisesMsg(
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: Cannot coerce primitiveType "
"'CSS_URI' to 'CSS_IDENT'",
v.setStringValue,
*(v.CSS_IDENT, 'x')
)
self.assertRaisesMsg(
match="CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_URI' to 'CSS_IDENT'",
):
v.setStringValue(v.CSS_IDENT, 'x')
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: Cannot coerce primitiveType "
"'CSS_URI' to 'CSS_STRING'",
v.setStringValue,
*(v.CSS_STRING, '"x"')
)
self.assertRaisesMsg(
match="CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_URI' to 'CSS_STRING'",
):
v.setStringValue(v.CSS_STRING, '"x"')
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: Cannot coerce primitiveType " "'CSS_URI' to 'CSS_ATTR'",
v.setStringValue,
*(v.CSS_ATTR, 'x')
)
match="CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_URI' to 'CSS_ATTR'",
):
v.setStringValue(v.CSS_ATTR, 'x')

# CSS_ATTR
v = cssutils.css.CSSPrimitiveValue('attr(old)')
v.setStringValue(v.CSS_ATTR, 'a')
assert v.CSS_ATTR == v.primitiveType
assert 'a' == v.getStringValue()
self.assertRaisesMsg(
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: Cannot coerce primitiveType "
"'CSS_ATTR' to 'CSS_IDENT'",
v.setStringValue,
*(v.CSS_IDENT, 'x')
)
self.assertRaisesMsg(
match="CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_ATTR' to 'CSS_IDENT'",
):
v.setStringValue(v.CSS_IDENT, 'x')
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: Cannot coerce primitiveType "
"'CSS_ATTR' to 'CSS_STRING'",
v.setStringValue,
*(v.CSS_STRING, '"x"')
)
self.assertRaisesMsg(
match="CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_ATTR' to 'CSS_STRING'",
):
v.setStringValue(v.CSS_STRING, '"x"')
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: Cannot coerce primitiveType " "'CSS_ATTR' to 'CSS_URI'",
v.setStringValue,
*(v.CSS_URI, 'x')
)
match="CSSPrimitiveValue: Cannot coerce primitiveType 'CSS_ATTR' to 'CSS_URI'",
):
v.setStringValue(v.CSS_URI, 'x')

# TypeError as 'x' is no valid type
self.assertRaisesMsg(
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: stringType 'x' (UNKNOWN TYPE) is not a string type",
v.setStringValue,
*('x', 'brown')
)
match="CSSPrimitiveValue: stringType 'x' (UNKNOWN TYPE) is not a string type",
):
v.setStringValue('x', 'brown')
# IndexError as 111 is no valid type
self.assertRaisesMsg(
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: stringType 111 (UNKNOWN TYPE) is not a string type",
v.setStringValue,
*(111, 'brown')
)
match="CSSPrimitiveValue: stringType 111 (UNKNOWN TYPE) is not a string type",
):
v.setStringValue(111, 'brown')
# CSS_PX is no string type
self.assertRaisesMsg(
with pytest.raises(
xml.dom.InvalidAccessErr,
"CSSPrimitiveValue: stringType CSS_PX is not a string type",
v.setStringValue,
*(v.CSS_PX, 'brown')
)
match="CSSPrimitiveValue: stringType CSS_PX is not a string type",
):
v.setStringValue(v.CSS_PX, 'brown')

def test_typeRGBColor(self):
"RGBColor"
Expand Down
76 changes: 41 additions & 35 deletions cssutils/tests/test_prodparser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Testcases for cssutils.css.CSSCharsetRule"""

import sys
import re
import xml.dom
from . import basetest
from cssutils.prodparser import (
Expand Down Expand Up @@ -193,7 +194,8 @@ def test_nextProd(self):
seq = Sequence(*seqitems)
for t, p in result:
if isinstance(p, str):
self.assertRaisesMsg(ParseError, p, seq.nextProd, t)
with pytest.raises(ParseError, match=p):
seq.nextProd(t)
else:
assert p == seq.nextProd(t)

Expand Down Expand Up @@ -245,7 +247,8 @@ def test_nextProd(self):
seq = Sequence(minmax=lambda: (1, 2), *seqitems)
for t, p in result:
if isinstance(p, str):
self.assertRaisesMsg(ParseError, p, seq.nextProd, t)
with pytest.raises(ParseError, match=re.escape(p)):
seq.nextProd(t)
else:
assert p == seq.nextProd(t)

Expand All @@ -260,26 +263,32 @@ def test_init(self):
t2 = (2, 0, 0, 0)

ch = Choice(p1, p2)
self.assertRaisesMsg(
ParseError, 'No match for (0, 0, 0, 0) in Choice(p1, p2)', ch.nextProd, t0
)
with pytest.raises(
ParseError, match=re.escape('No match for (0, 0, 0, 0) in Choice(p1, p2)')
):
ch.nextProd(t0)
assert p1 == ch.nextProd(t1)
self.assertRaisesMsg(Exhausted, 'Extra token', ch.nextProd, t1)
with pytest.raises(Exhausted, match='Extra token'):
ch.nextProd(t1)

ch = Choice(p1, p2)
assert p2 == ch.nextProd(t2)
self.assertRaisesMsg(Exhausted, 'Extra token', ch.nextProd, t2)
with pytest.raises(Exhausted, match='Extra token'):
ch.nextProd(t2)

ch = Choice(p2, p1)
self.assertRaisesMsg(
ParseError, 'No match for (0, 0, 0, 0) in Choice(p2, p1)', ch.nextProd, t0
)
with pytest.raises(
ParseError, match=re.escape('No match for (0, 0, 0, 0) in Choice(p2, p1)')
):
ch.nextProd(t0)
assert p1 == ch.nextProd(t1)
self.assertRaisesMsg(Exhausted, 'Extra token', ch.nextProd, t1)
with pytest.raises(Exhausted, match='Extra token'):
ch.nextProd(t1)

ch = Choice(p2, p1)
assert p2 == ch.nextProd(t2)
self.assertRaisesMsg(Exhausted, 'Extra token', ch.nextProd, t2)
with pytest.raises(Exhausted, match='Extra token'):
ch.nextProd(t2)

def test_matches(self):
"Choice.matches()"
Expand Down Expand Up @@ -311,18 +320,19 @@ def test_nested(self):
t2 = (2, 0, 0, 0)

ch = Choice(s1, s2)
self.assertRaisesMsg(
ParseError,
'No match for (0, 0, 0, 0) in Choice(Sequence(p1, p1), Sequence(p2, p2))',
ch.nextProd,
t0,
expected = (
'No match for (0, 0, 0, 0) in Choice(Sequence(p1, p1), Sequence(p2, p2))'
)
with pytest.raises(ParseError, match=re.escape(expected)):
ch.nextProd(t0)
assert s1 == ch.nextProd(t1)
self.assertRaisesMsg(Exhausted, 'Extra token', ch.nextProd, t1)
with pytest.raises(Exhausted, match='Extra token'):
ch.nextProd(t1)

ch = Choice(s1, s2)
assert s2 == ch.nextProd(t2)
self.assertRaisesMsg(Exhausted, 'Extra token', ch.nextProd, t1)
with pytest.raises(Exhausted, match='Extra token'):
ch.nextProd(t1)

def test_reset(self):
"Choice.reset()"
Expand Down Expand Up @@ -383,14 +393,12 @@ def test_combi(self):
wellformed, seq, store, unused = ProdParser().parse(text, 'T', prods)
assert wellformed == exp
else:
self.assertRaisesMsg(
xml.dom.SyntaxErr,
'T: %s' % exp,
ProdParser().parse,
text,
'T',
prods,
)
with pytest.raises(xml.dom.SyntaxErr, match=re.escape(f'T: {exp}')):
ProdParser().parse(
text,
'T',
prods,
)

tests = {
'1 3': True,
Expand All @@ -411,11 +419,9 @@ def test_combi(self):
wellformed, seq, store, unused = ProdParser().parse(text, 'T', prods)
assert wellformed == exp
else:
self.assertRaisesMsg(
xml.dom.SyntaxErr,
'T: %s' % exp,
ProdParser().parse,
text,
'T',
prods,
)
with pytest.raises(xml.dom.SyntaxErr, match=re.escape(f'T: {exp}')):
ProdParser().parse(
text,
'T',
prods,
)
4 changes: 3 additions & 1 deletion cssutils/tests/test_property.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Testcases for cssutils.css.property._Property."""

import re
import xml.dom

import pytest
Expand Down Expand Up @@ -121,7 +122,8 @@ def test_cssText(self):
}
for test in tests:
ecp, msg = tests[test]
self.assertRaisesMsg(ecp, msg, p._setCssText, test)
with pytest.raises(ecp, match=re.escape(msg)):
p._setCssText(test)

def test_name(self):
"Property.name"
Expand Down

0 comments on commit c9584b5

Please sign in to comment.