from hashlib import sha1
from tempfile import TemporaryFile
from contextlib import contextmanager

from penchy.compat import unittest, nested, update_hasher


class NestedTest(unittest.TestCase):
    def test_reraising_exception(self):
        e = Exception('reraise this')
        with self.assertRaises(Exception) as raised:
            with nested(TemporaryFile(), TemporaryFile()) as (a, b):
                raise e

        self.assertEqual(raised.exception, e)

    def test_raising_on_exit(self):
        @contextmanager
        def raising_cm(exception):
            yield
            raise exception

        on_exit = Exception('throw on exit')
        with self.assertRaises(Exception) as raised:
            with nested(raising_cm(on_exit)):
                pass
        self.assertEqual(raised.exception, on_exit)


class HasherTest(unittest.TestCase):
    def setUp(self):
        self.control = sha1()
        self.h = sha1()

    def test_str_hash(self):
        s = 'foo'
        self.control.update(s)
        update_hasher(self.h, s)
        self.assertEqual(self.control.hexdigest(),
                         self.h.hexdigest())

    def test_unicode_hash(self):
        u = u'foo'
        self.control.update(u.encode('utf8'))
        update_hasher(self.h, u)
        self.assertEqual(self.control.hexdigest(),
                         self.h.hexdigest())

from hashlib import sha1
from tempfile import TemporaryFile
from contextlib import contextmanager

from penchy.compat import unittest, nested, update_hasher, unicode_


class NestedTest(unittest.TestCase):
    def test_reraising_exception(self):
        e = Exception('reraise this')
        with self.assertRaises(Exception) as raised:
            with nested(TemporaryFile(), TemporaryFile()) as (a, b):
                raise e

        self.assertEqual(raised.exception, e)

    def test_raising_on_exit(self):
        @contextmanager
        def raising_cm(exception):
            yield
            raise exception

        on_exit = Exception('throw on exit')
        with self.assertRaises(Exception) as raised:
            with nested(raising_cm(on_exit)):
                pass
        self.assertEqual(raised.exception, on_exit)


class HasherTest(unittest.TestCase):
    def setUp(self):
        self.control = sha1()
        self.h = sha1()

    def test_str_hash(self):
        s = str('foo')
        update_hasher(self.h, s)
        self.assertEqual(self.h.hexdigest(),
                         '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33')

    def test_unicode_hash(self):
        u = unicode_('foo')
        update_hasher(self.h, u)
        self.assertEqual(self.h.hexdigest(),
                         '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33')

Replace control hasher with constant hexdigest.
tests: Replace control hasher with constant hexdigest.

Signed-off-by: Michael Markert <5eb998b7ac86da375651a4cd767b88c9dad25896@googlemail.com>

  from hashlib import sha1
  from tempfile import TemporaryFile
  from contextlib import contextmanager
  
- from penchy.compat import unittest, nested, update_hasher
+ from penchy.compat import unittest, nested, update_hasher, unicode_
  
  
  class NestedTest(unittest.TestCase):
      def test_reraising_exception(self):
          e = Exception('reraise this')
          with self.assertRaises(Exception) as raised:
              with nested(TemporaryFile(), TemporaryFile()) as (a, b):
                  raise e
  
          self.assertEqual(raised.exception, e)
  
      def test_raising_on_exit(self):
          @contextmanager
          def raising_cm(exception):
              yield
              raise exception
  
          on_exit = Exception('throw on exit')
          with self.assertRaises(Exception) as raised:
              with nested(raising_cm(on_exit)):
                  pass
          self.assertEqual(raised.exception, on_exit)
  
  
  class HasherTest(unittest.TestCase):
      def setUp(self):
          self.control = sha1()
          self.h = sha1()
  
      def test_str_hash(self):
-         s = 'foo'
+         s = str('foo')
-         self.control.update(s)
          update_hasher(self.h, s)
-         self.assertEqual(self.control.hexdigest(),
+         self.assertEqual(self.h.hexdigest(),
-                          self.h.hexdigest())
+                          '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33')
  
      def test_unicode_hash(self):
-         u = u'foo'
+         u = unicode_('foo')
-         self.control.update(u.encode('utf8'))
          update_hasher(self.h, u)
-         self.assertEqual(self.control.hexdigest(),
+         self.assertEqual(self.h.hexdigest(),
-                          self.h.hexdigest())
+                          '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33')
  
Replace control hasher with constant hexdigest.
## Code Before:
from hashlib import sha1
from tempfile import TemporaryFile
from contextlib import contextmanager

from penchy.compat import unittest, nested, update_hasher


class NestedTest(unittest.TestCase):
    def test_reraising_exception(self):
        e = Exception('reraise this')
        with self.assertRaises(Exception) as raised:
            with nested(TemporaryFile(), TemporaryFile()) as (a, b):
                raise e

        self.assertEqual(raised.exception, e)

    def test_raising_on_exit(self):
        @contextmanager
        def raising_cm(exception):
            yield
            raise exception

        on_exit = Exception('throw on exit')
        with self.assertRaises(Exception) as raised:
            with nested(raising_cm(on_exit)):
                pass
        self.assertEqual(raised.exception, on_exit)


class HasherTest(unittest.TestCase):
    def setUp(self):
        self.control = sha1()
        self.h = sha1()

    def test_str_hash(self):
        s = 'foo'
        self.control.update(s)
        update_hasher(self.h, s)
        self.assertEqual(self.control.hexdigest(),
                         self.h.hexdigest())

    def test_unicode_hash(self):
        u = u'foo'
        self.control.update(u.encode('utf8'))
        update_hasher(self.h, u)
        self.assertEqual(self.control.hexdigest(),
                         self.h.hexdigest())

## Instruction:
Replace control hasher with constant hexdigest.
## Code After:
from hashlib import sha1
from tempfile import TemporaryFile
from contextlib import contextmanager

from penchy.compat import unittest, nested, update_hasher, unicode_


class NestedTest(unittest.TestCase):
    def test_reraising_exception(self):
        e = Exception('reraise this')
        with self.assertRaises(Exception) as raised:
            with nested(TemporaryFile(), TemporaryFile()) as (a, b):
                raise e

        self.assertEqual(raised.exception, e)

    def test_raising_on_exit(self):
        @contextmanager
        def raising_cm(exception):
            yield
            raise exception

        on_exit = Exception('throw on exit')
        with self.assertRaises(Exception) as raised:
            with nested(raising_cm(on_exit)):
                pass
        self.assertEqual(raised.exception, on_exit)


class HasherTest(unittest.TestCase):
    def setUp(self):
        self.control = sha1()
        self.h = sha1()

    def test_str_hash(self):
        s = str('foo')
        update_hasher(self.h, s)
        self.assertEqual(self.h.hexdigest(),
                         '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33')

    def test_unicode_hash(self):
        u = unicode_('foo')
        update_hasher(self.h, u)
        self.assertEqual(self.h.hexdigest(),
                         '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33')
