<commit_before>#!/usr/bin/env python

"""
@package mi.core.test.test_tcp_client
@file mi/core/test/test_tcp_client.py
@author <NAME>
@brief Test class for the TCPClient Modules
provides unit tests for the TCPClient.  When the TCP client was written there were no tests.  We should add
unit test whenever we touch this code.
"""

__author__ = '<NAME>'
__license__ = 'Apache 2.0'

from os.path import basename, dirname
from os import makedirs
from os.path import exists
import sys

from mi.core.log import get_logger ; log = get_logger()

from nose.plugins.attrib import attr
import unittest
from mi.core.unit_test import MiUnitTest

from mi.core.tcp_client import TcpClient


@attr('UNIT', group='mi')
class TestTCPClient(MiUnitTest):
    """
    Test the logger object
    """    
    def setUp(self):
        """
        Setup the test case
        """

    def test_remove_from_buffer(self):
        """
        Test the remove from buffer method.  Verify that all bytes before the match string are removed.
        """
        client = TcpClient()
        target = "MATCH"

        # Remove with an None target
        self.assertFalse(client.remove_from_buffer(None))

        # Remove with a zero length target
        self.assertFalse(client.remove_from_buffer(""))

        # Remove from buffer when buffer empty
        client.buf = None
        self.assertFalse(client.remove_from_buffer(target))
        self.assertIsNone(client.buf)

        # Remove from buffer when target the only thing in the list
        client.buf = target
        self.assertTrue(client.remove_from_buffer(target))
        self.assertEqual(len(client.buf), 0)

        # Remove from buffer when target the last thing in the list
        client.buf = "foo" + target
        self.assertTrue(client.remove_from_buffer(target))
        self.assertEqual(len(client.buf), 0)

        # Remove from buffer when target in the middle
        client.buf = "foo" + target + "remains"
        self.assertTrue(client.remove_from_buffer(target))
        self.assertEqual(client.buf, "remains")

        # Remove from buffer when target not in the buff
        client.buf = "target not in the list"
        self.assertFalse(client.remove_from_buffer(target))
        self.assertEqual(client.buf, "target not in the list")

        # Remove from buffer when target in the list more than once        client.buf = "foo" + target + "remains"
        client.buf = "foo" + target + "remains" + target
        self.assertTrue(client.remove_from_buffer(target))
        self.assertEqual(client.buf, "remains" + target)

<commit_msg>added tests for expect and expect_regex<commit_after>#!/usr/bin/env python

"""
@package mi.core.test.test_tcp_client
@file mi/core/test/test_tcp_client.py
@author <NAME>
@brief Test class for the TCPClient Modules
provides unit tests for the TCPClient.  When the TCP client was written there were no tests.  We should add
unit test whenever we touch this code.
"""

__author__ = '<NAME>'
__license__ = 'Apache 2.0'

from mi.core.log import get_logger
log = get_logger()

import mock
import re
from nose.plugins.attrib import attr

from mi.core.unit_test import MiUnitTest
from mi.core.tcp_client import TcpClient


@attr('UNIT', group='mi')
class TestTCPClient(MiUnitTest):
    """
    Test the logger object
    """    
    def setUp(self):
        """
        Setup the test case
        """

    def test_expect(self):
        client = TcpClient()
        client.s = mock.Mock()
        client.s.recv.return_value = 'abcdefghi'

        # true if string is found
        self.assertTrue(client.expect('def'))
        # buf should contain only the text following the expected string
        self.assertEqual(client.buf, 'ghi')

    def test_expect_regex(self):
        client = TcpClient()
        client.s = mock.Mock()
        client.s.recv.return_value = 'abcdefghi'

        # un-compiled regex
        # expect_regex returns a match object
        self.assertEqual(client.expect_regex('[def]{3}').group(), 'def')
        # buf should contain only the text following the matched string
        self.assertEqual(client.buf, 'ghi')

        # pre-compiled regex with groups
        result = client.expect_regex(re.compile(r'(abc)(def)'))
        self.assertEqual(result.group(1), 'abc')
        self.assertEqual(result.group(2), 'def')
        self.assertEqual(client.buf, 'ghi')
{'fraction_non_alphanumeric': 0.09844677935130196, 'fraction_numerical': 0.002055733211512106, 'mean_word_length': 2.8889875666074603, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2339710', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1256, 'n_words': 388}