<commit_before>#--
# Copyright (c) 2008, 2009 Net-ng.
# All rights reserved.
#
# This software is licensed under the BSD License, as described in
# the file LICENSE.txt, which you should have received as part of
# this distribution.
#--

from __future__ import with_statement

from nagare.namespaces import xhtml
from nagare.namespaces import xhtml_base
from nagare.namespaces import xml
from nagare import callbacks
from nagare import wsgi
from nagare.sessions import threaded_sessions
from nagare import presentation

from types import ListType

from paste.fixture import TestApp

import os

def create_FixtureApp(app):
    app = wsgi.create_WSGIApp(app)
    app.start(threaded_sessions.SessionsFactory('', {}, lambda x: None)())

    return TestApp(app)


# Test for XHTML namespace

def head_render_init_test1():
    """ XHTML namespace unit test - HeadRender - Allowed tags """
    try:
        h = xhtml.HeadRenderer('/tmp/static_directory/')
        with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
            h << h.base({'id':'id', 'href':'href', 'target':'target'})
            h << h.link({'charset':'charset', 'href':'href', 'hreflang':'hreflang', 'type':'type', 'rel':'rel', 'rev':'rev', 'media':'media', 'target':'target'})
            h << h.meta({'id':'id', 'http_equiv':'http_equiv', 'name':'name', 'content':'content', 'scheme':'scheme'})
            h << h.title({'title':'title'})
            h << h.style({'id':'id'})
            h << h.script({'id':'id'})
    except AttributeError:
        assert False
    else:
        assert True


def head_render_init_test2():
    """ XHTML namespace unit test - HeadRender - Forbiden tag """
    try:
        h = xhtml.HeadRenderer('/tmp/static_directory/')
        h << h.test({'id':'id', 'href':'href', 'target':'target'})
    except AttributeError:
        assert True
    else:
        assert False


def head_render_css_url_test1():
    """ XHTML namespace unit test - HeadRender - css_url - relative url """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.css_url('css')

    assert h._css_url == {'/tmp/static_directory/css':0}


def head_render_javascript_css_test2():
    """ XHTML namespace unit test - HeadRender - css_url - absolute url """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.css_url('/css')

    assert h._css_url == {'/css':0}


def head_render_css_url_test3():
    """ XHTML namespace unit test - HeadRender - css_url - absolute url + relative url """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.css_url('/css')
        h << h.css_url('css')

    assert h._css_url['/css'] == 0
    assert h._css_url['/tmp/static_directory/css'] == 1


def head_render_javascript_url_test1():
    """ XHTML namespace unit test - HeadRender - javascript_url - relative url """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript_url('test.js')

    assert h._javascript_url == {'/tmp/static_directory/test.js':0}


def head_render_javascript_url_test2():
    """ XHTML namespace unit test - HeadRender - javascript_url - absolute url """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript_url('/test.js')
    assert h._javascript_url == {'/test.js':0}


def head_render_javascript_url_test3():
    """ XHTML namespace unit test - HeadRender - javascript_url - absolute url + relative url """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript_url('/test.js')
        h << h.javascript_url('test.js')

    assert h._javascript_url['/test.js'] == 0
    assert h._javascript_url['/tmp/static_directory/test.js'] == 1


def head_render_javascript_url_test4():
    """ XHTML namespace unit test - HeadRender - javascript_url - Add twice the same js_url"""
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript_url('test.js')
        h << h.javascript_url('test.js')

    assert h._javascript_url == {'/tmp/static_directory/test.js':0}


def head_render_javascript_test1():
    """ XHTML namespace unit test - HeadRender - javascript - use string """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript('test', 'function test(arg1) { return true }')

    assert h._named_javascript == {'test': (0, 'function test(arg1) { return true }')}


def head_render_javascript_test2():
    """ XHTML namespace unit test - HeadRender - javascript - use python method """

    def js_method(arg1):
        return True

    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript('test', js_method)

    assert h._javascript_url == {'/static/nagare/pyjslib.js':0}
    assert h._named_javascript.has_key('test')


def head_render_javascript_test3():
    """ XHTML namespace unit test - HeadRender - javascript - add 2 js with same name """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript('test', 'function test(arg1) { return true }')
        h << h.javascript('test', 'function test(arg1) { return true }')

    assert h._named_javascript == {'test': (0, 'function test(arg1) { return true }')}


def head_render_render_test1():
    """ XHTML namespace unit test - HeadRender - Render - render only style tag """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    h << h.style()
    assert presentation.render(h, None, None, None).write_htmlstring().replace('\n', '') == '<head><style></style></head>'


def head_render_render_test2():
    """ XHTML namespace unit test - HeadRender - Render - render only css_url method """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    h << h.css_url('css')<commit_msg>The ``css()``, ``css_url()``, `javascript()`` and ``javascript_url()`` of a ``HeadRenderer`` now accept keywords that will be the attributes of the generated tag<commit_after>#--
# Copyright (c) 2008, 2009 Net-ng.
# All rights reserved.
#
# This software is licensed under the BSD License, as described in
# the file LICENSE.txt, which you should have received as part of
# this distribution.
#--

from __future__ import with_statement

from nagare.namespaces import xhtml
from nagare.namespaces import xhtml_base
from nagare.namespaces import xml
from nagare import callbacks
from nagare import wsgi
from nagare.sessions import threaded_sessions
from nagare import presentation

from types import ListType

from paste.fixture import TestApp

import os

def create_FixtureApp(app):
    app = wsgi.create_WSGIApp(app)
    app.start(threaded_sessions.SessionsFactory('', {}, lambda x: None)())

    return TestApp(app)


# Test for XHTML namespace

def head_render_init_test1():
    """ XHTML namespace unit test - HeadRender - Allowed tags """
    try:
        h = xhtml.HeadRenderer('/tmp/static_directory/')
        with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
            h << h.base({'id':'id', 'href':'href', 'target':'target'})
            h << h.link({'charset':'charset', 'href':'href', 'hreflang':'hreflang', 'type':'type', 'rel':'rel', 'rev':'rev', 'media':'media', 'target':'target'})
            h << h.meta({'id':'id', 'http_equiv':'http_equiv', 'name':'name', 'content':'content', 'scheme':'scheme'})
            h << h.title({'title':'title'})
            h << h.style({'id':'id'})
            h << h.script({'id':'id'})
    except AttributeError:
        assert False
    else:
        assert True


def head_render_init_test2():
    """ XHTML namespace unit test - HeadRender - Forbiden tag """
    try:
        h = xhtml.HeadRenderer('/tmp/static_directory/')
        h << h.test({'id':'id', 'href':'href', 'target':'target'})
    except AttributeError:
        assert True
    else:
        assert False


def head_render_css_url_test1():
    """ XHTML namespace unit test - HeadRender - css_url - relative url """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.css_url('css')

    assert h._css_url == {'/tmp/static_directory/css' : (0, {})}


def head_render_javascript_css_test2():
    """ XHTML namespace unit test - HeadRender - css_url - absolute url """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.css_url('/css')

    assert h._css_url == {'/css' : (0, {})}


def head_render_css_url_test3():
    """ XHTML namespace unit test - HeadRender - css_url - absolute url + relative url """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.css_url('/css')
        h << h.css_url('css')

    assert h._css_url['/css'] == (0, {})
    assert h._css_url['/tmp/static_directory/css'] == (1, {})


def head_render_javascript_url_test1():
    """ XHTML namespace unit test - HeadRender - javascript_url - relative url """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript_url('test.js')

    assert h._javascript_url == {'/tmp/static_directory/test.js' : (0, {})}


def head_render_javascript_url_test2():
    """ XHTML namespace unit test - HeadRender - javascript_url - absolute url """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript_url('/test.js')
    assert h._javascript_url == {'/test.js' : (0, {})}


def head_render_javascript_url_test3():
    """ XHTML namespace unit test - HeadRender - javascript_url - absolute url + relative url """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript_url('/test.js')
        h << h.javascript_url('test.js')

    assert h._javascript_url['/test.js'] == (0, {})
    assert h._javascript_url['/tmp/static_directory/test.js'] == (1, {})


def head_render_javascript_url_test4():
    """ XHTML namespace unit test - HeadRender - javascript_url - Add twice the same js_url"""
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript_url('test.js')
        h << h.javascript_url('test.js')

    assert h._javascript_url == {'/tmp/static_directory/test.js' : (0, {})}


def head_render_javascript_test1():
    """ XHTML namespace unit test - HeadRender - javascript - use string """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript('test', 'function test(arg1) { return true }')

    assert h._named_javascript == {'test': (0, 'function test(arg1) { return true }', {})}


def head_render_javascript_test2():
    """ XHTML namespace unit test - HeadRender - javascript - use python method """

    def js_method(arg1):
        return True

    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript('test', js_method)

    assert h._javascript_url == {'/static/nagare/pyjslib.js' : (0, {})}
    assert h._named_javascript.has_key('test')


def head_render_javascript_test3():
    """ XHTML namespace unit test - HeadRender - javascript - add 2 js with same name """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    with h.head({'lang':'lang', 'dir':'dir', 'id':'id', 'profile':'profile'}):
        h << h.javascript('test', 'function test(arg1) { return true }')
        h << h.javascript('test', 'function test(arg1) { return true }')

    assert h._named_javascript == {'test' : (0, 'function test(arg1) { return true }', {})}


def head_render_render_test1():
    """ XHTML namespace unit test - HeadRender - Render - render only style tag """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    h << h.style()
    assert presentation.render(h, None, None, None).write_htmlstring().replace('\n', '') == '<head><style></style></head>'


def head_render_render_test2():
    """ XHTML namespace unit test - HeadRender - Render - render only css_url method """
    h = xhtml.HeadRenderer('/tmp/static_directory/')
    h << h.css_url('css')
{'fraction_non_alphanumeric': 0.18483484628880922, 'fraction_numerical': 0.006255244488519338, 'mean_word_length': 3.709051724137931, 'pattern_counts': {'":': 0, '<': 103, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6186744', 'n_tokens_mistral': 4331, 'n_tokens_neox': 4108, 'n_words': 973}