MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_context_subclassing

Method test_context_subclassing

Lib/test/test_decimal.py:4308–4413  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

4306class ContextSubclassing:
4307
4308 def test_context_subclassing(self):
4309 decimal = self.decimal
4310 Decimal = decimal.Decimal
4311 Context = decimal.Context
4312 Clamped = decimal.Clamped
4313 DivisionByZero = decimal.DivisionByZero
4314 Inexact = decimal.Inexact
4315 Overflow = decimal.Overflow
4316 Rounded = decimal.Rounded
4317 Subnormal = decimal.Subnormal
4318 Underflow = decimal.Underflow
4319 InvalidOperation = decimal.InvalidOperation
4320
4321 class MyContext(Context):
4322 def __init__(self, prec=None, rounding=None, Emin=None, Emax=None,
4323 capitals=None, clamp=None, flags=None,
4324 traps=None):
4325 Context.__init__(self)
4326 if prec is not None:
4327 self.prec = prec
4328 if rounding is not None:
4329 self.rounding = rounding
4330 if Emin is not None:
4331 self.Emin = Emin
4332 if Emax is not None:
4333 self.Emax = Emax
4334 if capitals is not None:
4335 self.capitals = capitals
4336 if clamp is not None:
4337 self.clamp = clamp
4338 if flags is not None:
4339 if isinstance(flags, list):
4340 flags = {v:(v in flags) for v in OrderedSignals[decimal] + flags}
4341 self.flags = flags
4342 if traps is not None:
4343 if isinstance(traps, list):
4344 traps = {v:(v in traps) for v in OrderedSignals[decimal] + traps}
4345 self.traps = traps
4346
4347 c = Context()
4348 d = MyContext()
4349 for attr in ('prec', 'rounding', 'Emin', 'Emax', 'capitals', 'clamp',
4350 'flags', 'traps'):
4351 self.assertEqual(getattr(c, attr), getattr(d, attr))
4352
4353 # prec
4354 self.assertRaises(ValueError, MyContext, **{'prec':-1})
4355 c = MyContext(prec=1)
4356 self.assertEqual(c.prec, 1)
4357 self.assertRaises(InvalidOperation, c.quantize, Decimal('9e2'), 0)
4358
4359 # rounding
4360 self.assertRaises(TypeError, MyContext, **{'rounding':'XYZ'})
4361 c = MyContext(rounding=ROUND_DOWN, prec=1)
4362 self.assertEqual(c.rounding, ROUND_DOWN)
4363 self.assertEqual(c.plus(Decimal('9.9')), 9)
4364
4365 # Emin

Callers

nothing calls this directly

Calls 15

getattrFunction · 0.85
DecimalClass · 0.85
strFunction · 0.85
assertTrueMethod · 0.80
create_decimalMethod · 0.80
to_sci_stringMethod · 0.80
clear_flagsMethod · 0.80
assertFalseMethod · 0.80
clear_trapsMethod · 0.80
ContextClass · 0.70
MyContextClass · 0.70
assertEqualMethod · 0.45

Tested by

no test coverage detected