<commit_before>from pythonzombie.proxy.client  import ZombieProxyClient
from pythonzombie.browser       import Browser
from unittest                   import TestCase

import fudge
import os

class TestServerCommunication(TestCase):
    
    def setUp(self):
        super(TestServerCommunication, self).setUp()
        self.browser = Browser()
       
        # Build the path to the example.html file
        path = os.path.dirname(os.path.abspath(__file__)) 
        path = os.path.join(path, 'helpers', 'example.html')
        self.path = 'file://%s' % path

    def tearDown(self):
        super(TestServerCommunication, self).tearDown()
        fudge.clear_expectations()

    @fudge.with_fakes
    def test_visit(self):

        js = """
        browser.visit("%s", function(err, browser){
            if(err)
                stream.end(JSON.stringify(err.stack));
            else    
                stream.end();
        });
        """ % self.path

        with fudge.patched_context(
            ZombieProxyClient,
            '__send__',
            (fudge.Fake('__send__', expect_call=True).
                with_args(js)
            )):

            assert self.browser.visit(self.path) == self.browser

class TestBrowser(TestCase):

    def setUp(self):
        super(TestBrowser, self).setUp()
        self.browser = Browser()
       
        # Build the path to the example.html file
        path = os.path.dirname(os.path.abspath(__file__)) 
        path = os.path.join(path, 'helpers', 'example.html')
        self.path = 'file://%s' % path
        self.html = open(path, 'r').read()

    def test_html(self):
        assert '<p>This is an HTML document</p>' in self.browser.visit(
            self.path
        ).html

    def test_html(self):
        assert '<p>This is an HTML document</p>' in self.browser.visit(
            self.path
        ).html

    def test_location_get(self):
        assert self.browser.visit(self.path).location['href'] == self.path

    def test_location_set(self):
        self.browser.location = self.path
        assert self.browser.visit(self.path).location['href'] == self.path

    def test_css(self):
        for tag in ['h1', 'p', 'form', 'input', 'button']:
            matches = self.browser.visit(self.path).css(tag)
            assert len(matches) == 1
            assert matches[0].tagName.lower() == tag

    def test_fill(self):
        self.browser.visit(self.path).fill('q', 'Zombie.js')
        assert self.browser.css('input')[0].value == 'Zombie.js'

    def test_press_button(self):
        self.browser.visit(self.path)
        assert self.browser.location['hash'] == ''
        self.browser.pressButton('Search')
        assert self.browser.location['hash'] == '#submit'
<commit_msg>Refactoring and cleaning up some tests.<commit_after>from pythonzombie.proxy.client  import ZombieProxyClient
from pythonzombie               import Browser
from unittest                   import TestCase

import fudge
import os


class BrowserClientTest(TestCase):
    """
    Sets up a pythonzombie.Browser() object to test with.
    """

    def setUp(self):
        super(BrowserClientTest, self).setUp()
        self.browser = Browser()
       
        # Build the path to the example.html file
        path = os.path.dirname(os.path.abspath(__file__)) 
        path = os.path.join(path, 'helpers', 'example.html')
        self.path = 'file://%s' % path

    def tearDown(self):
        super(BrowserClientTest, self).tearDown()
        fudge.clear_expectations()


class TestServerCommunication(BrowserClientTest):

    @fudge.with_fakes
    def test_visit(self):

        js = """
        browser.visit("%s", function(err, browser){
            if(err)
                stream.end(JSON.stringify(err.stack));
            else    
                stream.end();
        });
        """ % self.path

        with fudge.patched_context(
            ZombieProxyClient,
            '__send__',
            (fudge.Fake('__send__', expect_call=True).
                with_args(js)
            )):

            assert self.browser.visit(self.path) == self.browser

class TestBrowser(BrowserClientTest):

    def setUp(self):
        super(TestBrowser, self).setUp()
        self.browser = Browser()
       
        # Build the path to the example.html file
        path = os.path.dirname(os.path.abspath(__file__)) 
        path = os.path.join(path, 'helpers', 'example.html')
        self.path = 'file://%s' % path
        self.html = open(path, 'r').read()

    def test_html(self):
        assert '<p>This is an HTML document</p>' in self.browser.visit(
            self.path
        ).html

    def test_html(self):
        assert '<p>This is an HTML document</p>' in self.browser.visit(
            self.path
        ).html

    def test_location_get(self):
        assert self.browser.visit(self.path).location['href'] == self.path

    def test_location_set(self):
        self.browser.location = self.path
        assert self.browser.visit(self.path).location['href'] == self.path

    def test_css(self):
        for tag in ['h1', 'p', 'form', 'input', 'button']:
            matches = self.browser.visit(self.path).css(tag)
            assert len(matches) == 1
            assert matches[0].tagName.lower() == tag

    def test_fill(self):
        self.browser.visit(self.path).fill('q', 'Zombie.js')
        assert self.browser.css('input')[0].value == 'Zombie.js'

    def test_press_button(self):
        self.browser.visit(self.path)
        assert self.browser.location['hash'] == ''
        self.browser.pressButton('Search')
        assert self.browser.location['hash'] == '#submit'


class TestDOMNode(BrowserClientTest):
    pass

{'fraction_non_alphanumeric': 0.1318604243380677, 'fraction_numerical': 0.0014027704716815712, 'mean_word_length': 2.197309417040359, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4493429', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1671, 'n_words': 346}