"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/test_util.py $
$Id: test_util.py 23690 2004-03-15 15:12:48Z dbinger $
"""

from time import time
from datetime import datetime
from sancho.unittest import TestScenario, parse_args, run_scenarios
from dulcinea.util import format_text, sanitize_url, wrap_paragraphs, \
     replace_characters, insert_paragraph_tags, activate_links, \
     htmlescape_except, datetime_to_int, beginning_of_next_month

tested_modules = ["dulcinea.util"]

class UtilTest (TestScenario):

    def check_format_text(self):
        self.test_val("format_text('text')", 'text')
        self.test_stmt("format_text('http://www.memsnet.org')")

    def check_htmlescape_except(self):
        self.test_val("htmlescape_except('<Br><bru>')", '<br>&lt;bru&gt;')

    def check_sanitize_url(self):
        self.test_val("sanitize_url(None)", None)
        self.test_val("sanitize_url('www.example.org')",
                      'http://www.example.org')
        self.test_val("sanitize_url('nobody@example.com')",
                      'mailto:nobody@example.com')
        self.test_val('sanitize_url("ftp://ftp.example.com")',
                      'ftp://ftp.example.com')

    def check_wrap_paragraphs(self):
        self.test_val('wrap_paragraphs("text\\n\\nboo")',
                      "text\n\nboo")
        self.test_val("wrap_paragraphs(None)", '')

    def check_replace_characters(self):
        self.test_val("replace_characters(None)", None)
        self.test_val("replace_characters('\x92\x93\x94\x96\x99')",
                      "'\"\"-(tm)")

    def check_insert_paragraph_tags(self):
        self.test_val("insert_paragraph_tags(None)", None)
        self.test_val("str(insert_paragraph_tags('\\n'))",
                      '\n<p>\n</p>')

    def check_activate_links(self):
        self.test_val("activate_links(None)", None)
        s = 'this http://example.org is fine'
        s_active = ('this <a href="http://example.org">http://example.org</a>'
                    ' is fine')
        self.test_val("str(activate_links(s))", s_active)
        s = 'this mailto:nobody@example.org is fine'
        s_active = (
            'this '
            'mailto:<a href="mailto:nobody@example.org">nobody@example.org</a>'
            ' is fine')
        self.test_val("str(activate_links(s))", s_active)
        s = 'this nobody@example.org is fine'
        s_active = (
            'this '
            '<a href="mailto:nobody@example.org">nobody@example.org</a>'
            ' is fine')
        self.test_val("str(activate_links(s))", s_active)
        s = 'this www.example.org/a. is fine'
        s_active = (
            'this '
            '<a href="http://www.example.org/a">www.example.org/a</a>.'
            ' is fine')
        self.test_val("str(activate_links(s))", s_active)

    def check_datetime_to_int(self):
        t = int(time())
        self.test_val('datetime_to_int(datetime.fromtimestamp(t))', t)
        for t in range(1, 10):
            self.test_val('datetime_to_int(datetime.fromtimestamp(t))', t)

    def check_beginning_of_next_month(self):
        a = datetime(year=2004, month=12, day=3, hour=1, minute=2, second=3,
                     microsecond=3)
        self.test_val('beginning_of_next_month(a).year', 2005)
        self.test_val('beginning_of_next_month(a).month', 1)
        self.test_val('beginning_of_next_month(a).day', 1)
        self.test_val('beginning_of_next_month(a).hour', 0)
        self.test_val('beginning_of_next_month(a).minute', 0)
        self.test_val('beginning_of_next_month(a).second', 0)
        self.test_val('beginning_of_next_month(a).microsecond', 0)


if __name__ == "__main__":
    (scenarios, options) = parse_args()
    run_scenarios(scenarios, options)
