示例#1
0
 def test_function_ignored(self):
     def return_hi(): dictionary['key'] = 'value'
     dictionary = {}
     return_hi = groot(return_hi)
     with redirect_stdout(StringIO()) as stdout:
         self.assertEqual(return_hi(), None)
     self.assertEqual(dictionary, {})
示例#2
0
 def test_returned(self):
     def return_hi(): return 'hi'
     return_hi = call_logger(return_hi)
     with redirect_stdout(StringIO()) as stdout:
         self.assertEqual(return_hi(), 'hi')
     self.assertEqual(stdout.getvalue(), dedent("""
         Function started
         Function returned
     """).lstrip())
示例#3
0
 def test_prints_before_and_after(self):
     def greet(): print("Hello")
     greet = call_logger(greet)
     with redirect_stdout(StringIO()) as stdout:
         greet()
     self.assertEqual(stdout.getvalue(), dedent("""
         Function started
         Hello
         Function returned
     """).lstrip())
示例#4
0
 def test_suppress_error(self):
     def divide(x, y): return x / y
     divide = catch_all(divide)
     with redirect_stdout(StringIO()) as stdout:
         with patch('builtins.input', side_effect=['y']) as input:
             divide(1, 0)
     self.assertEqual(
         stdout.getvalue().strip(),
         "Exception occurred: division by zero"
     )
     input.assert_called_with("Should we ignore this exception (Y/n)? ")
示例#5
0
def test_cleanup():
    # If ascii art works, then do the test otherwise just end the function call
    if ASCII_AVAILABLE:
        scr_height = 40
        scr_width = 100
        fake_result = struct.pack('HHHH', scr_height, scr_width, 600, 616)

        with redirect_stdout() as f:
            sys.stdout.fileno = lambda: 1
            with patch('fcntl.ioctl', return_value=fake_result):
                device = asciiblock()
                device.cleanup()

        device.cleanup = noop
        out = f.getvalue().encode('utf-8')

        digest = hashlib.md5(out).hexdigest()
        assert digest == '3139690363a9edf4c03d553b36a37fe6'
示例#6
0
def test_display():
    # If ascii art works, then do the test otherwise just end the function call
    if ASCII_AVAILABLE:
        scr_height = 40
        scr_width = 100
        fake_result = struct.pack('HHHH', scr_height, scr_width, 600, 616)

        with redirect_stdout() as f:
            sys.stdout.fileno = lambda: 1
            with patch('fcntl.ioctl', return_value=fake_result):
                device = asciiblock()
                with canvas(device) as draw:
                    baseline_data.primitives(device, draw)

        device.cleanup = noop
        out = f.getvalue().encode('utf-8')

        digest = hashlib.md5(out).hexdigest()
        fname = Path(__file__).resolve().parent.joinpath(
            'reference', 'asciiblock.txt')
        assert md5(str(fname)) == digest
示例#7
0
 def test_import(self):
     with redirect_stdout(StringIO()) as output:
         import_module('hello')
     self.assertIn("command-line", output.getvalue())
示例#8
0
 def test_takes_arguments(self):
     def add(x, y): return x + y
     add = groot(add)
     with redirect_stdout(StringIO()) as stdout:
         add(1, 2)
         add(x=1, y=2)
示例#9
0
 def test_nothing_returned(self):
     def return_hi(): return 'hi'
     return_hi = groot(return_hi)
     with redirect_stdout(StringIO()) as stdout:
         self.assertEqual(return_hi(), None)
示例#10
0
 def test_print_groot(self):
     def greet(name): print("Hello {}".format(name))
     greet = groot(greet)
     with redirect_stdout(StringIO()) as stdout:
         greet("Trey")
     self.assertEqual(stdout.getvalue(), "Groot\n")
示例#11
0
 def test_takes_arguments(self):
     def add(x, y): return x + y
     add = call_logger(add)
     with redirect_stdout(StringIO()) as stdout:
         self.assertEqual(add(1, 2), 3)
         self.assertEqual(add(x=1, y=2), 3)
示例#12
0
 def test_return_four(self):
     def greet(name): print("Hello {}".format(name))
     with redirect_stdout(StringIO()) as stdout:
         greet = four(greet)
     self.assertEqual(stdout.getvalue(), '')
     self.assertEqual(greet, 4)