コード例 #1
0
    def test3get_match_element_valid_match(self):
        """Parse data and check if the MatchContext was not changed."""
        old_stderr = sys.stderr
        output = StringIO()
        sys.stderr = output
        debug_model_element = DebugModelElement(self.id_)
        self.assertEqual(output.getvalue(), "DebugModelElement %s added\n" % self.id_)

        output.seek(0)
        output.truncate(0)

        data = b"some data"
        match_context = DummyMatchContext(data)
        match_element = debug_model_element.get_match_element(self.path, match_context)
        self.assertEqual(
            output.getvalue(), 'DebugModelElement path = "%s", unmatched = "%s"\n' % (match_element.get_path(), repr(
                match_context.match_data)))
        self.compare_match_results(data, match_element, match_context, self.id_, self.path, b"", b"", None)

        output.seek(0)
        output.truncate(0)

        data = b"123 0x2a. [\"abc\"]:"
        match_context = DummyMatchContext(data)
        match_element = debug_model_element.get_match_element(self.path, match_context)
        self.assertEqual(
            output.getvalue(), 'DebugModelElement path = "%s", unmatched = "%s"\n' % (match_element.get_path(), repr(
                match_context.match_data)))
        self.compare_match_results(data, match_element, match_context, self.id_, self.path, b"", b"", None)

        sys.stderr = old_stderr
コード例 #2
0
ファイル: apptest_stringio.py プロジェクト: zcxowwww/pypy
def test_truncate():

    s = u"1234567890"
    sio = StringIO(s)

    raises(ValueError, sio.truncate, -1)
    sio.seek(6)
    res = sio.truncate()
    assert res == 6
    assert sio.getvalue() == s[:6]
    res = sio.truncate(4)
    assert res == 4
    assert sio.getvalue() == s[:4]
    # truncate() accepts long objects
    res = sio.truncate(4L)
    assert res == 4
    assert sio.getvalue() == s[:4]
    assert sio.tell() == 6
    sio.seek(0, 2)
    sio.write(s)
    assert sio.getvalue() == s[:4] + s
    pos = sio.tell()
    res = sio.truncate(None)
    assert res == pos
    assert sio.tell() == pos
    raises(TypeError, sio.truncate, '0')
    sio.close()
    raises(ValueError, sio.truncate, 0)
コード例 #3
0
class TestBase(unittest.TestCase):
    __configFilePath = os.getcwd() + '/unit/testutilities/config.py'

    def setUp(self):
        self.aminer_config = AMinerConfig.load_config(self.__configFilePath)
        self.analysis_context = AnalysisContext(self.aminer_config)
        self.output_stream = StringIO()
        self.stream_printer_event_handler = StreamPrinterEventHandler(
            self.analysis_context, self.output_stream)
        persistence_file_name = AMinerConfig.build_persistence_file_name(
            self.aminer_config)
        if os.path.exists(persistence_file_name):
            shutil.rmtree(persistence_file_name)
        if not os.path.exists(persistence_file_name):
            os.makedirs(persistence_file_name)

    def tearDown(self):
        self.aminer_config = AMinerConfig.load_config(self.__configFilePath)
        persistence_file_name = AMinerConfig.build_persistence_file_name(
            self.aminer_config)
        if os.path.exists(persistence_file_name):
            shutil.rmtree(persistence_file_name)
        if not os.path.exists(persistence_file_name):
            os.makedirs(persistence_file_name)

    def reset_output_stream(self):
        self.output_stream.seek(0)
        self.output_stream.truncate(0)
コード例 #4
0
class TestBase(unittest.TestCase):
    """This is the base class for all unittests."""

    __configFilePath = os.getcwd() + '/unit/config/config.py'

    def setUp(self):
        """Set up all needed variables and remove persisted data."""
        PersistenceUtil.persistable_components = []
        self.aminer_config = AminerConfig.load_config(self.__configFilePath)
        self.analysis_context = AnalysisContext(self.aminer_config)
        self.output_stream = StringIO()
        self.stream_printer_event_handler = StreamPrinterEventHandler(
            self.analysis_context, self.output_stream)
        persistence_dir_name = AminerConfig.build_persistence_file_name(
            self.aminer_config)
        if os.path.exists(persistence_dir_name):
            shutil.rmtree(persistence_dir_name)
        if not os.path.exists(persistence_dir_name):
            os.makedirs(persistence_dir_name)
        initialize_loggers(self.aminer_config,
                           getpwnam('aminer').pw_uid,
                           getgrnam('aminer').gr_gid)
        if isinstance(persistence_dir_name, str):
            persistence_dir_name = persistence_dir_name.encode()
        SecureOSFunctions.secure_open_base_directory(
            persistence_dir_name, os.O_RDONLY | os.O_DIRECTORY | os.O_PATH)
        PersistenceUtil.SKIP_PERSISTENCE_ID_WARNING = True

    def tearDown(self):
        """Delete all persisted data after the tests."""
        self.aminer_config = AminerConfig.load_config(self.__configFilePath)
        persistence_file_name = AminerConfig.build_persistence_file_name(
            self.aminer_config)
        if os.path.exists(persistence_file_name):
            shutil.rmtree(persistence_file_name)
        if not os.path.exists(persistence_file_name):
            os.makedirs(persistence_file_name)
        SecureOSFunctions.close_base_directory()

    def reset_output_stream(self):
        """Reset the output stream."""
        self.output_stream.seek(0)
        self.output_stream.truncate(0)
コード例 #5
0
    def test1start_debugging(self):
        """This test case checks if the DebugModelElement was initiated and the output was correct."""
        output = StringIO()
        sys.stderr = output
        debug_model_element = DebugModelElement('debug')
        self.assertEqual(
            output.getvalue(),
            'DebugModelElement %s added\n' % debug_model_element.element_id)

        output.seek(0)
        output.truncate(0)

        matchContext = MatchContext(b'some data')
        matchElement = debug_model_element.get_match_element(
            'debugMatch', matchContext)
        self.assertEqual(
            output.getvalue(),
            'DebugModelElement path = "%s", unmatched = "%s"\n' %
            (matchElement.get_path(), matchContext.match_data))
コード例 #6
0
class TestBase(unittest.TestCase):
    """This is the base class for all unittests."""

    __configFilePath = os.getcwd() + '/unit/data/config.py'

    def setUp(self):
        """Set up all needed variables and remove persisted data."""
        PersistenceUtil.persistable_components = []
        self.aminer_config = load_config(self.__configFilePath)
        self.analysis_context = AnalysisContext(self.aminer_config)
        self.output_stream = StringIO()
        self.stream_printer_event_handler = StreamPrinterEventHandler(
            self.analysis_context, self.output_stream)
        persistence_dir_name = build_persistence_file_name(self.aminer_config)
        if os.path.exists(persistence_dir_name):
            shutil.rmtree(persistence_dir_name)
        if not os.path.exists(persistence_dir_name):
            os.makedirs(persistence_dir_name)
        initialize_loggers(self.aminer_config, os.getuid(), os.getgid())
        if isinstance(persistence_dir_name, str):
            persistence_dir_name = persistence_dir_name.encode()
        SecureOSFunctions.secure_open_base_directory(
            persistence_dir_name, os.O_RDONLY | os.O_DIRECTORY | os.O_PATH)
        PersistenceUtil.SKIP_PERSISTENCE_ID_WARNING = True

    def tearDown(self):
        """Delete all persisted data after the tests."""
        self.aminer_config = load_config(self.__configFilePath)
        persistence_file_name = build_persistence_file_name(self.aminer_config)
        if os.path.exists(persistence_file_name):
            shutil.rmtree(persistence_file_name)
        if not os.path.exists(persistence_file_name):
            os.makedirs(persistence_file_name)
        SecureOSFunctions.close_base_directory()

    def reset_output_stream(self):
        """Reset the output stream."""
        self.output_stream.seek(0)
        self.output_stream.truncate(0)

    def compare_match_results(self, data, match_element, match_context, id_,
                              path, match_string, match_object, children):
        """Compare the results of get_match_element() if match_element is not None."""
        self.assertEqual(match_element.path, "%s/%s" % (path, id_))
        self.assertEqual(match_element.match_string, match_string)
        self.assertEqual(match_element.match_object, match_object)
        if children is None:
            self.assertIsNone(match_element.children, children)
        else:
            self.assertEqual(len(children), len(match_element.children))
            for i, child in enumerate(children):
                self.assertEqual(match_element.children[i].path, child.path)
                self.assertEqual(match_element.children[i].match_string,
                                 child.match_string)
                self.assertEqual(match_element.children[i].match_object,
                                 child.match_object)
                self.assertIsNone(match_element.children[i].children, children)
        self.assertEqual(match_context.match_string, match_string)
        self.assertEqual(match_context.match_data, data[len(match_string):])

    def compare_no_match_results(self, data, match_element, match_context):
        """Compare the results of get_match_element() if match_element is not None."""
        self.assertIsNone(match_element, None)
        self.assertEqual(match_context.match_data, data)
コード例 #7
0
# Time    : 2018-04-07 21:52
# Author  : MrFiona
# File    : summary_StringIO.py
# Software: PyCharm

try:
    from _io import StringIO
except ImportError:
    from io import StringIO

f = StringIO()
f.write('hello world!!!')
print(f.getvalue(), len(f.getvalue()))
f.write('hello world!!!')
print(f.getvalue(), len(f.getvalue()))
f.truncate(0)
f.write('start write!')
print(f.tell())
print("next:\t", f.getvalue(), len(f.getvalue()))

f_test = StringIO('Hello! Hi! Goodbye!')
print("1:\t", f_test.read())
print(f_test.getvalue())
print(f_test.getvalue())
print("2:\t", f_test.read())
print("3:\t", f_test.read())
print("4:\t", f_test.read())
print("next:\t", f_test.getvalue(), len(f_test.getvalue()))

f_test.close()
f.close()