<commit_before>from __future__ import unicode_literals
from decimal import Decimal
from functools import wraps
from itertools import count
import sys
import warnings
import os
import json

if sys.version_info[:2] < (2, 7):  # pragma: no cover
    from unittest2 import TestCase
else:
    from unittest import TestCase

try:
    from unittest import mock
except ImportError:
    import mock

from jsonschema import (
    PY3, SchemaError, UnknownType, ValidationError, ErrorTree,
    Draft3Validator, Validator, iteritems, validate
)


if PY3:
    basestring = unicode = str


def make_case(schema, data, valid, cls):
    def test_case(self):
        if valid:
            validate(data, schema, cls=cls)
        else:
            with self.assertRaises(ValidationError):
                validate(data, schema, cls=cls)
    return test_case


def load_json_cases(test_dir):
    def add_test_methods(test_class):
        for filename in os.listdir(test_dir):
            if not filename.endswith('.json'):
                continue
            with open(test_dir + filename) as test_file:
                data = json.load(test_file)
                for case in data:
                    for test in case["tests"]:
                        a_test = make_case(
                            case["schema"], test["data"], test["valid"],
                            test_class.validator_class)
                        setattr(test_class,
                            filename[:-5] + ": " + test["description"], a_test)
        return test_class
    return add_test_methods
<commit_msg>Make test method names into valid identifiers.<commit_after>from __future__ import unicode_literals
from decimal import Decimal
from functools import wraps
import re
import sys
import warnings
import os
import json

if sys.version_info[:2] < (2, 7):  # pragma: no cover
    from unittest2 import TestCase
else:
    from unittest import TestCase

try:
    from unittest import mock
except ImportError:
    import mock

from jsonschema import (
    PY3, SchemaError, UnknownType, ValidationError, ErrorTree,
    Draft3Validator, Validator, iteritems, validate
)


if PY3:
    basestring = unicode = str


def make_case(schema, data, valid, cls):
    def test_case(self):
        if valid:
            validate(data, schema, cls=cls)
        else:
            with self.assertRaises(ValidationError):
                validate(data, schema, cls=cls)
    return test_case


def load_json_cases(test_dir):
    def add_test_methods(test_class):
        for filename in os.listdir(test_dir):
            if not filename.endswith('.json'):
                continue
            with open(test_dir + filename) as test_file:
                data = json.load(test_file)
                for case in data:
                    for test in case["tests"]:
                        a_test = make_case(
                            case["schema"], test["data"], test["valid"],
                            test_class.validator_class)
                        test_name = "test_%s_%s" % (filename[:-5],
                                                    test["description"])
                        test_name = re.sub(r"[\W ]+", "_", test_name)
                        a_test.__name__ = str(test_name)
                        setattr(test_class, test_name, a_test)
        return test_class
    return add_test_methods

{'fraction_non_alphanumeric': 0.0792520035618878, 'fraction_numerical': 0.004749183734045711, 'mean_word_length': 2.0333033303330335, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3412502', 'n_tokens_mistral': 943, 'n_tokens_neox': 913, 'n_words': 266}