<s> import datetime <EOL> import warnings <EOL> import unittest <EOL> from itertools import product <EOL> class Test_Assertions ( unittest . TestCase ) : <EOL> def test_AlmostEqual ( self ) : <EOL> self . assertAlmostEqual ( <NUM_LIT> , <NUM_LIT:1.0> ) <EOL> self . assertNotAlmostEqual ( <NUM_LIT> , <NUM_LIT:1.0> ) <EOL> self . assertRaises ( self . failureException , <EOL> self . assertAlmostEqual , <NUM_LIT> , <NUM_LIT:1.0> ) <EOL> self . assertRaises ( self . failureException , <EOL> self . assertNotAlmostEqual , <NUM_LIT> , <NUM_LIT:1.0> ) <EOL> self . assertAlmostEqual ( <NUM_LIT> , <NUM_LIT:1.0> , places = <NUM_LIT:0> ) <EOL> self . assertRaises ( self . failureException , <EOL> self . assertAlmostEqual , <NUM_LIT> , <NUM_LIT:1.0> , places = <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( <NUM_LIT:0> , <NUM_LIT> + <NUM_LIT> , places = <NUM_LIT:0> ) <EOL> self . assertNotAlmostEqual ( <NUM_LIT:0> , <NUM_LIT> + <NUM_LIT> , places = <NUM_LIT:1> ) <EOL> self . assertRaises ( self . failureException , <EOL> self . assertAlmostEqual , <NUM_LIT:0> , <NUM_LIT> + <NUM_LIT> , places = <NUM_LIT:1> ) <EOL> self . assertRaises ( self . failureException , <EOL> self . assertNotAlmostEqual , <NUM_LIT:0> , <NUM_LIT> + <NUM_LIT> , places = <NUM_LIT:0> ) <EOL> self . assertAlmostEqual ( float ( '<STR_LIT>' ) , float ( '<STR_LIT>' ) ) <EOL> self . assertRaises ( self . failureException , self . assertNotAlmostEqual , <EOL> float ( '<STR_LIT>' ) , float ( '<STR_LIT>' ) ) <EOL> def test_AmostEqualWithDelta ( self ) : <EOL> self . assertAlmostEqual ( <NUM_LIT> , <NUM_LIT:1.0> , delta = <NUM_LIT:0.5> ) <EOL> self . assertAlmostEqual ( <NUM_LIT:1.0> , <NUM_LIT> , delta = <NUM_LIT:0.5> ) <EOL> self . assertNotAlmostEqual ( <NUM_LIT> , <NUM_LIT:1.0> , delta = <NUM_LIT> ) <EOL> self . assertNotAlmostEqual ( <NUM_LIT:1.0> , <NUM_LIT> , delta = <NUM_LIT> ) <EOL> self . assertRaises ( self . failureException , self . assertAlmostEqual , <EOL> <NUM_LIT> , <NUM_LIT:1.0> , delta = <NUM_LIT> ) <EOL> self . assertRaises ( self . failureException , self . assertNotAlmostEqual , <EOL> <NUM_LIT> , <NUM_LIT:1.0> , delta = <NUM_LIT:0.5> ) <EOL> self . assertRaises ( TypeError , self . assertAlmostEqual , <EOL> <NUM_LIT> , <NUM_LIT:1.0> , places = <NUM_LIT:2> , delta = <NUM_LIT:2> ) <EOL> self . assertRaises ( TypeError , self . assertNotAlmostEqual , <EOL> <NUM_LIT> , <NUM_LIT:1.0> , places = <NUM_LIT:2> , delta = <NUM_LIT:2> ) <EOL> first = datetime . datetime . now ( ) <EOL> second = first + datetime . timedelta ( seconds = <NUM_LIT:10> ) <EOL> self . assertAlmostEqual ( first , second , <EOL> delta = datetime . timedelta ( seconds = <NUM_LIT:20> ) ) <EOL> self . assertNotAlmostEqual ( first , second , <EOL> delta = datetime . timedelta ( seconds = <NUM_LIT:5> ) ) <EOL> def test_assertRaises ( self ) : <EOL> def _raise ( e ) : <EOL> raise e <EOL> self . assertRaises ( KeyError , _raise , KeyError ) <EOL> self . assertRaises ( KeyError , _raise , KeyError ( "<STR_LIT:key>" ) ) <EOL> try : <EOL> self . assertRaises ( KeyError , lambda : None ) <EOL> except self . failureException as e : <EOL> self . assertIn ( "<STR_LIT>" , str ( e ) ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> try : <EOL> self . assertRaises ( KeyError , _raise , ValueError ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> with self . assertRaises ( KeyError ) as cm : <EOL> try : <EOL> raise KeyError <EOL> except Exception as e : <EOL> exc = e <EOL> raise <EOL> self . assertIs ( cm . exception , exc ) <EOL> with self . assertRaises ( KeyError ) : <EOL> raise KeyError ( "<STR_LIT:key>" ) <EOL> try : <EOL> with self . assertRaises ( KeyError ) : <EOL> pass <EOL> except self . failureException as e : <EOL> self . assertIn ( "<STR_LIT>" , str ( e ) ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> try : <EOL> with self . assertRaises ( KeyError ) : <EOL> raise ValueError <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def testAssertNotRegex ( self ) : <EOL> self . assertNotRegex ( '<STR_LIT>' , r'<STR_LIT>' ) <EOL> try : <EOL> self . assertNotRegex ( '<STR_LIT>' , r'<STR_LIT>' , '<STR_LIT>' ) <EOL> except self . failureException as e : <EOL> self . assertIn ( "<STR_LIT>" , e . args [ <NUM_LIT:0> ] ) <EOL> self . assertIn ( '<STR_LIT>' , e . args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> class TestLongMessage ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> class TestableTestFalse ( unittest . TestCase ) : <EOL> longMessage = False <EOL> failureException = self . failureException <EOL> def testTest ( self ) : <EOL> pass <EOL> class TestableTestTrue ( unittest . TestCase ) : <EOL> longMessage = True <EOL> failureException = self . failureException <EOL> def testTest ( self ) : <EOL> pass <EOL> self . testableTrue = TestableTestTrue ( '<STR_LIT>' ) <EOL> self . testableFalse = TestableTestFalse ( '<STR_LIT>' ) <EOL> def testDefault ( self ) : <EOL> self . assertTrue ( unittest . TestCase . longMessage ) <EOL> def test_formatMsg ( self ) : <EOL> self . assertEqual ( self . testableFalse . _formatMessage ( None , "<STR_LIT:foo>" ) , "<STR_LIT:foo>" ) <EOL> self . assertEqual ( self . testableFalse . _formatMessage ( "<STR_LIT:foo>" , "<STR_LIT:bar>" ) , "<STR_LIT:foo>" ) <EOL> self . assertEqual ( self . testableTrue . _formatMessage ( None , "<STR_LIT:foo>" ) , "<STR_LIT:foo>" ) <EOL> self . assertEqual ( self . testableTrue . _formatMessage ( "<STR_LIT:foo>" , "<STR_LIT:bar>" ) , "<STR_LIT>" ) <EOL> self . testableTrue . _formatMessage ( object ( ) , '<STR_LIT:foo>' ) <EOL> def test_formatMessage_unicode_error ( self ) : <EOL> one = '<STR_LIT>' . join ( chr ( i ) for i in range ( <NUM_LIT:255> ) ) <EOL> self . testableTrue . _formatMessage ( one , '<STR_LIT>' ) <EOL> def assertMessages ( self , methodName , args , errors ) : <EOL> """<STR_LIT>""" <EOL> def getMethod ( i ) : <EOL> useTestableFalse = i < <NUM_LIT:2> <EOL> if useTestableFalse : <EOL> test = self . testableFalse <EOL> else : <EOL> test = self . testableTrue <EOL> return getattr ( test , methodName ) <EOL> for i , expected_regex in enumerate ( errors ) : <EOL> testMethod = getMethod ( i ) <EOL> kwargs = { } <EOL> withMsg = i % <NUM_LIT:2> <EOL> if withMsg : <EOL> kwargs = { "<STR_LIT>" : "<STR_LIT>" } <EOL> with self . assertRaisesRegex ( self . failureException , <EOL> expected_regex = expected_regex ) : <EOL> testMethod ( * args , ** kwargs ) <EOL> def testAssertTrue ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( False , ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def testAssertFalse ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( True , ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def testNotEqual ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def testAlmostEqual ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( <NUM_LIT:1> , <NUM_LIT:2> ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def testNotAlmostEqual ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def test_baseAssertEqual ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( <NUM_LIT:1> , <NUM_LIT:2> ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def testAssertSequenceEqual ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( [ ] , [ None ] ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , r"<STR_LIT>" , <EOL> r"<STR_LIT>" ] ) <EOL> def testAssertSetEqual ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( set ( ) , set ( [ None ] ) ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def testAssertIn ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( None , [ ] ) , <EOL> [ '<STR_LIT>' , "<STR_LIT>" , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def testAssertNotIn ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( None , [ None ] ) , <EOL> [ '<STR_LIT>' , "<STR_LIT>" , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def testAssertDictEqual ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( { } , { '<STR_LIT:key>' : '<STR_LIT:value>' } ) , <EOL> [ r"<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def testAssertDictContainsSubset ( self ) : <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" , DeprecationWarning ) <EOL> self . assertMessages ( '<STR_LIT>' , ( { '<STR_LIT:key>' : '<STR_LIT:value>' } , { } ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def testAssertMultiLineEqual ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( "<STR_LIT>" , "<STR_LIT:foo>" ) , <EOL> [ r"<STR_LIT>" , "<STR_LIT>" , <EOL> r"<STR_LIT>" , <EOL> r"<STR_LIT>" ] ) <EOL> def testAssertLess ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( <NUM_LIT:2> , <NUM_LIT:1> ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def testAssertLessEqual ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( <NUM_LIT:2> , <NUM_LIT:1> ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def testAssertGreater ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( <NUM_LIT:1> , <NUM_LIT:2> ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def testAssertGreaterEqual ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( <NUM_LIT:1> , <NUM_LIT:2> ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def testAssertIsNone ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( '<STR_LIT>' , ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def testAssertIsNotNone ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( None , ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def testAssertIs ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( None , '<STR_LIT:foo>' ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def testAssertIsNot ( self ) : <EOL> self . assertMessages ( '<STR_LIT>' , ( None , None ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def assertMessagesCM ( self , methodName , args , func , errors ) : <EOL> """<STR_LIT>""" <EOL> p = product ( ( self . testableFalse , self . testableTrue ) , <EOL> ( { } , { "<STR_LIT>" : "<STR_LIT>" } ) ) <EOL> for ( cls , kwargs ) , err in zip ( p , errors ) : <EOL> method = getattr ( cls , methodName ) <EOL> with self . assertRaisesRegex ( cls . failureException , err ) : <EOL> with method ( * args , ** kwargs ) as cm : <EOL> func ( ) <EOL> def testAssertRaises ( self ) : <EOL> self . assertMessagesCM ( '<STR_LIT>' , ( TypeError , ) , lambda : None , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def testAssertRaisesRegex ( self ) : <EOL> self . assertMessagesCM ( '<STR_LIT>' , ( TypeError , '<STR_LIT>' ) , <EOL> lambda : None , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def raise_wrong_message ( ) : <EOL> raise TypeError ( '<STR_LIT:foo>' ) <EOL> self . assertMessagesCM ( '<STR_LIT>' , ( TypeError , '<STR_LIT>' ) , <EOL> raise_wrong_message , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def testAssertWarns ( self ) : <EOL> self . assertMessagesCM ( '<STR_LIT>' , ( UserWarning , ) , lambda : None , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def testAssertWarnsRegex ( self ) : <EOL> self . assertMessagesCM ( '<STR_LIT>' , ( UserWarning , '<STR_LIT>' ) , <EOL> lambda : None , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def raise_wrong_message ( ) : <EOL> warnings . warn ( '<STR_LIT:foo>' ) <EOL> self . assertMessagesCM ( '<STR_LIT>' , ( UserWarning , '<STR_LIT>' ) , <EOL> raise_wrong_message , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) </s>
