<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# name:             test_parsable.py
# author:           <NAME>
# email:            <EMAIL>
# created on:       01/16/2016
#
# description:      A unit test for ext_pylib file module's Parsable mixin
#                   class.
#

from ext_pylib.files import File, Parsable
import pytest


FILE = """This is a sample file.
This is a sample file.
This is a sample file.
DocumentRoot /var/www/google.com
This is a sample file.
DEBUG = True
SECURE = False
DocumentRoot /var/www/example.com
LIST = first_item
LIST = second_item
"""

# Monkey Patch function for read() method
def read(self):
    """Monkey Patch read function."""
    return self.data

class ParsableFile(Parsable, File):
    """Dummy class extending Parsable and File."""


def test_parsable_parse_with_existing_attribute():
    """Test Parsable setup_parsing() method on an existing attribute."""
    parsable = ParsableFile()
    parsable.existing = 'already exists'
    with pytest.raises(AttributeError):
        parsable.setup_parsing({ 'existing' : '*' })

def test_parsable_setup_parsing():
    """Test Parsable setup_parsing() method."""
    file = Parsable()
    Parsable.read = read
    file.data = FILE
    file.setup_parsing({
        'htdocs' : ('DocumentRoot (.*)',),
        'debug'  :  'DEBUG = (.*)',
        'secure' : ('SECURE[ ]*=[ ]*([^ \n]*)', 'SECURE = {0}'),
        'speed'  : ('SPEED[ ]*=[ ]*([^ \n]*)', 'SPEED = {0}'),
        'list'   : ('LIST[ ]*=[ ]*([^ \n]*)', 'LIST = {0}'),
    })
    assert file.htdocs[0] == '/var/www/google.com'
    assert file.htdocs[1] == '/var/www/example.com'
    assert file.debug == 'True'
    assert file.secure == 'False'
    file.secure = 'True'
    assert file.secure == 'True'
    assert file.speed == None
    file.speed = 'fastest'
    assert file.speed == 'fastest'
    file.speed = 'fastest' # setting it more than once with the same value
                           # shouldn't affect the number of times it is added.
    assert isinstance(file.speed, str) # Shouldn't be a list
    assert len(file.list) == 2 # Should be a list<commit_msg>Changes from string to unicode.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# name:             test_parsable.py
# author:           <NAME>
# email:            <EMAIL>
# created on:       01/16/2016
#
# description:      A unit test for ext_pylib file module's Parsable mixin
#                   class.
#

from ext_pylib.files import File, Parsable
import pytest


FILE = """This is a sample file.
This is a sample file.
This is a sample file.
DocumentRoot /var/www/google.com
This is a sample file.
DEBUG = True
SECURE = False
DocumentRoot /var/www/example.com
LIST = first_item
LIST = second_item
"""

# Monkey Patch function for read() method
def read(self):
    """Monkey Patch read function."""
    return self.data

class ParsableFile(Parsable, File):
    """Dummy class extending Parsable and File."""


def test_parsable_parse_with_existing_attribute():
    """Test Parsable setup_parsing() method on an existing attribute."""
    parsable = ParsableFile()
    parsable.existing = 'already exists'
    with pytest.raises(AttributeError):
        parsable.setup_parsing({ 'existing' : '*' })

def test_parsable_setup_parsing():
    """Test Parsable setup_parsing() method."""
    file = Parsable()
    Parsable.read = read
    file.data = FILE
    file.setup_parsing({
        'htdocs' : ('DocumentRoot (.*)',),
        'debug'  :  'DEBUG = (.*)',
        'secure' : ('SECURE[ ]*=[ ]*([^ \n]*)', 'SECURE = {0}'),
        'speed'  : ('SPEED[ ]*=[ ]*([^ \n]*)', 'SPEED = {0}'),
        'list'   : ('LIST[ ]*=[ ]*([^ \n]*)', 'LIST = {0}'),
    })
    assert file.htdocs[0] == '/var/www/google.com'
    assert file.htdocs[1] == '/var/www/example.com'
    assert file.debug == 'True'
    assert file.secure == 'False'
    file.secure = 'True'
    assert file.secure == 'True'
    assert file.speed == None
    file.speed = 'fastest'
    assert file.speed == 'fastest'
    file.speed = 'fastest' # setting it more than once with the same value
                           # shouldn't affect the number of times it is added.
    assert type(file.speed) == unicode  # Shouldn't be a list
    assert len(file.list) == 2 # Should be a list
{'fraction_non_alphanumeric': 0.15402567094515754, 'fraction_numerical': 0.007001166861143524, 'mean_word_length': 2.7465034965034967, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, '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': '2334864', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1379, 'n_words': 397}