def test_main_if_export_img_raises_error(self, export_img, _):
        try:
            loadpath, sys.path = sys.path, []  # disable to load all modules
            sys.modules['win32com'] = True
            args = ['input.vsd', 'output.png']

            # case of IOError
            export_img.side_effect = IOError
            ret = main(args)
            self.assertEqual(-1, ret)

            # case of OSError
            export_img.side_effect = OSError
            ret = main(args)
            self.assertEqual(-1, ret)

            # case of IndexError
            export_img.side_effect = IndexError
            ret = main(args)
            self.assertEqual(-1, ret)

            # case of other exception (does not handle it)
            export_img.side_effect = Exception
            with self.assertRaises(Exception):
                main(args)
        finally:
            sys.path = loadpath  # write back library loading paths
            sys.modules.pop('win32com', None)  # unload forcely
            sys.modules.pop('win32com.client', None)  # unload forcely
    def test_main_if_export_img_raises_error(self, export_img, _):
        try:
            loadpath, sys.path = sys.path, []  # disable to load all modules
            sys.modules['win32com'] = True
            args = ['input.vsd', 'output.png']

            # case of IOError
            export_img.side_effect = IOError
            ret = main(args)
            self.assertEqual(-1, ret)

            # case of OSError
            export_img.side_effect = OSError
            ret = main(args)
            self.assertEqual(-1, ret)

            # case of IndexError
            export_img.side_effect = IndexError
            ret = main(args)
            self.assertEqual(-1, ret)

            # case of other exception (does not handle it)
            export_img.side_effect = Exception
            with self.assertRaises(Exception):
                main(args)
        finally:
            sys.path = loadpath  # write back library loading paths
            sys.modules.pop('win32com', None)  # unload forcely
            sys.modules.pop('win32com.client', None)  # unload forcely
    def test_check_image_formats(self, export_img, _):
        try:
            loadpath, sys.path = sys.path, []  # disable to load all modules
            sys.modules['win32com'] = True

            # .png
            args = ['input.vsd', 'output.png']
            ret = main(args)
            self.assertEqual(0, ret)

            # .gif
            args = ['input.vsd', 'output.gif']
            ret = main(args)
            self.assertEqual(0, ret)

            # .jpg
            args = ['input.vsd', 'output.jpg']
            ret = main(args)
            self.assertEqual(0, ret)

            # .pdf
            with self.assertRaises(SystemExit):
                args = ['input.vsd', 'output.pdf']
                main(args)

            # .PNG (capital)
            args = ['input.vsd', 'output.PNG']
            ret = main(args)
            self.assertEqual(0, ret)

            # no extension
            with self.assertRaises(SystemExit):
                args = ['input.vsd', 'output_without_ext']
                main(args)
        finally:
            sys.path = loadpath  # write back library loading paths
            sys.modules.pop('win32com', None)  # unload forcely
            sys.modules.pop('win32com.client', None)  # unload forcely
    def test_check_image_formats(self, export_img, _):
        try:
            loadpath, sys.path = sys.path, []  # disable to load all modules
            sys.modules['win32com'] = True

            # .png
            args = ['input.vsd', 'output.png']
            ret = main(args)
            self.assertEqual(0, ret)

            # .gif
            args = ['input.vsd', 'output.gif']
            ret = main(args)
            self.assertEqual(0, ret)

            # .jpg
            args = ['input.vsd', 'output.jpg']
            ret = main(args)
            self.assertEqual(0, ret)

            # .pdf
            with self.assertRaises(SystemExit):
                args = ['input.vsd', 'output.pdf']
                main(args)

            # .PNG (capital)
            args = ['input.vsd', 'output.PNG']
            ret = main(args)
            self.assertEqual(0, ret)

            # no extension
            with self.assertRaises(SystemExit):
                args = ['input.vsd', 'output_without_ext']
                main(args)
        finally:
            sys.path = loadpath  # write back library loading paths
            sys.modules.pop('win32com', None)  # unload forcely
            sys.modules.pop('win32com.client', None)  # unload forcely
    def test_parse_option(self, export_img, _):
        try:
            loadpath, sys.path = sys.path, []  # disable to load all modules
            sys.modules['win32com'] = True

            # no arguments, win32com available
            with self.assertRaises(SystemExit):
                args = []
                main(args)
            self.assertEqual(0, export_img.call_count)

            # one argument, win32com available
            with self.assertRaises(SystemExit):
                args = ['input.vsd']
                main(args)
            self.assertEqual(0, export_img.call_count)

            # two arguments, win32com available
            args = ['input.vsd', 'output.png']
            ret = main(args)
            self.assertEqual(0, ret)
            self.assertEqual(1, export_img.call_count)
            export_img.assert_called_with('input.vsd', 'output.png', None,
                                          None)

            # three arguments, win32com available
            with self.assertRaises(SystemExit):
                args = ['input.vsd', 'output.png', 'other_args']
                main(args)
            self.assertEqual(1, export_img.call_count)

            # two arguments, --page option, win32com available
            args = ['-p', '3', 'input.vsd', 'output.png']
            ret = main(args)
            self.assertEqual(0, ret)
            self.assertEqual(2, export_img.call_count)
            export_img.assert_called_with('input.vsd', 'output.png', 3, None)

            # two arguments, --name option, win32com available
            args = ['-n', 'sheet1', 'input.vsd', 'output.png']
            ret = main(args)
            self.assertEqual(0, ret)
            self.assertEqual(3, export_img.call_count)
            export_img.assert_called_with('input.vsd', 'output.png', None,
                                          'sheet1')

            # two arguments, --page and --name option, win32com available
            with self.assertRaises(SystemExit):
                args = ['-p', '3', '-n', 'sheet1', 'input.vsd', 'output.png']
                main(args)
            self.assertEqual(3, export_img.call_count)

            # two arguments, win32com unavailable
            sys.modules.pop('win32com', None)  # unload forcely
            args = ['input.vsd', 'output.png']
            ret = main(args)
            self.assertEqual(-1, ret)
            self.assertEqual(3, export_img.call_count)
        finally:
            sys.path = loadpath  # write back library loading paths
            sys.modules.pop('win32com', None)  # unload forcely
    def test_parse_option(self, export_img, _):
        try:
            loadpath, sys.path = sys.path, []  # disable to load all modules
            sys.modules['win32com'] = True

            # no arguments, win32com available
            with self.assertRaises(SystemExit):
                args = []
                main(args)
            self.assertEqual(0, export_img.call_count)

            # one argument, win32com available
            with self.assertRaises(SystemExit):
                args = ['input.vsd']
                main(args)
            self.assertEqual(0, export_img.call_count)

            # two arguments, win32com available
            args = ['input.vsd', 'output.png']
            ret = main(args)
            self.assertEqual(0, ret)
            self.assertEqual(1, export_img.call_count)
            export_img.assert_called_with('input.vsd', 'output.png',
                                          None, None)

            # three arguments, win32com available
            with self.assertRaises(SystemExit):
                args = ['input.vsd', 'output.png', 'other_args']
                main(args)
            self.assertEqual(1, export_img.call_count)

            # two arguments, --page option, win32com available
            args = ['-p', '3', 'input.vsd', 'output.png']
            ret = main(args)
            self.assertEqual(0, ret)
            self.assertEqual(2, export_img.call_count)
            export_img.assert_called_with('input.vsd', 'output.png',
                                          3, None)

            # two arguments, --name option, win32com available
            args = ['-n', 'sheet1', 'input.vsd', 'output.png']
            ret = main(args)
            self.assertEqual(0, ret)
            self.assertEqual(3, export_img.call_count)
            export_img.assert_called_with('input.vsd', 'output.png',
                                          None, 'sheet1')

            # two arguments, --page and --name option, win32com available
            with self.assertRaises(SystemExit):
                args = ['-p', '3', '-n', 'sheet1', 'input.vsd', 'output.png']
                main(args)
            self.assertEqual(3, export_img.call_count)

            # two arguments, win32com unavailable
            sys.modules.pop('win32com', None)  # unload forcely
            args = ['input.vsd', 'output.png']
            ret = main(args)
            self.assertEqual(-1, ret)
            self.assertEqual(3, export_img.call_count)
        finally:
            sys.path = loadpath  # write back library loading paths
            sys.modules.pop('win32com', None)  # unload forcely