<commit_before># -*- coding: utf-8 -*-
"""
    flaskext.testing
    ~~~~~~~~~~~~~~~~~

    Flask unittest integration.

    :copyright: (c) 2010 by <NAME>.
    :license: BSD, see LICENSE for more details.
"""
import StringIO
import unittest
import twill
import simplejson

from werkzeug import cached_property

__all__ = ["TestCase", "TwillTestCase"]

class JsonResponseMixin(object):
    """
    Mixin with testing helper methods
    """
    @cached_property
    def json(self):
        return simplejson.loads(self.data)


def _make_test_response(response_class):
    class TestResponse(response_class, JsonResponseMixin):
        pass

    return TestResponse


class TestCase(unittest.TestCase):
    
    def create_app(self):
        """
        Create your Flask app here, with any
        configuration you need
        """
        raise NotImplementedError

    def __call__(self, result=None):
        """
        Does the required setup, doing it here
        means you don't have to call super.setUp
        in subclasses.
        """
        try:
            self._pre_setup()
            super(TestCase, self).__call__(result)
        finally:
            self._post_teardown()

    def _pre_setup(self):
        self.app = self.create_app()

        self._orig_response_class = self.app.response_class
        self.app.response_class = _make_test_response(self.app.response_class)

        self.client = self.app.test_client()

        self._ctx = self.app.test_request_context()
        self._ctx.push()

    def _post_teardown(self):
        self._ctx.pop()

        self.app.response_class = self._orig_response_class

    def assertRedirects(self, response, location):
        assert response.status_code in (301, 302)
        assert response.location == "http://localhost" + location

    assert_redirects = assertRedirects

    def assertStatus(self, response, status_code):
        self.assertEqual(response.status_code, status_code)

    assert_status = assertStatus

    def assert200(self, response):

        self.assertStatus(response, 200)

    assert_200 = assert200

    def assert404(self, response):

        self.assertStatus(response, 404)

    assert_404 = assert404


class TwillTestCase(TestCase):
    """
    TestCase with Twill helper methods.<commit_msg>fix ctx error if create_app broken<commit_after># -*- coding: utf-8 -*-
"""
    flaskext.testing
    ~~~~~~~~~~~~~~~~~

    Flask unittest integration.

    :copyright: (c) 2010 by <NAME>.
    :license: BSD, see LICENSE for more details.
"""
import StringIO
import unittest
import twill
import simplejson

from werkzeug import cached_property

__all__ = ["TestCase", "TwillTestCase"]

class JsonResponseMixin(object):
    """
    Mixin with testing helper methods
    """
    @cached_property
    def json(self):
        return simplejson.loads(self.data)


def _make_test_response(response_class):
    class TestResponse(response_class, JsonResponseMixin):
        pass

    return TestResponse


class TestCase(unittest.TestCase):
    
    def create_app(self):
        """
        Create your Flask app here, with any
        configuration you need
        """
        raise NotImplementedError

    def __call__(self, result=None):
        """
        Does the required setup, doing it here
        means you don't have to call super.setUp
        in subclasses.
        """
        try:
            self._pre_setup()
            super(TestCase, self).__call__(result)
        finally:
            self._post_teardown()

    def _pre_setup(self):
        self.app = self._ctx = None

        self.app = self.create_app()

        self._orig_response_class = self.app.response_class
        self.app.response_class = _make_test_response(self.app.response_class)

        self.client = self.app.test_client()

        self._ctx = self.app.test_request_context()
        self._ctx.push()

    def _post_teardown(self):
        if self._ctx is not None:
            self._ctx.pop()
        if self.app is not None:
            self.app.response_class = self._orig_response_class

    def assertRedirects(self, response, location):
        assert response.status_code in (301, 302)
        assert response.location == "http://localhost" + location

    assert_redirects = assertRedirects

    def assertStatus(self, response, status_code):
        self.assertEqual(response.status_code, status_code)

    assert_status = assertStatus

    def assert200(self, response):

        self.assertStatus(response, 200)

    assert_200 = assert200

    def assert404(self, response):

        self.assertStatus(response, 404)

    assert_404 = assert404


class TwillTestCase(TestCase):
    """
    TestCase with Twill helper methods.
{'fraction_non_alphanumeric': 0.09665112335735482, 'fraction_numerical': 0.01483679525222552, 'mean_word_length': 2.644015444015444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '875186', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1319, 'n_words': 331}