<commit_before><commit_msg>Add unit tests for messager functions.<commit_after>#! /usr/bin/env python
from nose.tools import assert_equal, assert_raises

from landlab.core.utils import (format_message, error_message,
                                warning_message, assert_or_print)


LOREM_IPSUM = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Pharetra pharetra massa massa ultricies mi quis hendrerit.

Dictumst vestibulum rhoncus est pellentesque. Sed viverra tellus in hac habitasse platea dictumst vestibulum rhoncus.
"""

def test_empty_message():
    """Test formatting an empty string."""
    assert_equal(format_message(''), '')


def test_one_line():
    """Test a single line message."""
    assert_equal(format_message('lorem ipsum'), 'lorem ipsum')


def test_leading_whitespace():
    """Test a single line message."""
    assert_equal(format_message('   lorem ipsum'), 'lorem ipsum')


def test_one_long_line():
    """Test a line that needs to be wrapped."""
    msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

    assert_equal(format_message(msg),
                 """
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.""".strip())

def test_multiline():
    msg = """lorem
ipsum
    """
    assert_equal(format_message(msg), 'lorem ipsum')


def test_multiple_paragraphs():
    assert_equal(format_message(LOREM_IPSUM), """
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.

Pharetra pharetra massa massa ultricies mi quis hendrerit.

Dictumst vestibulum rhoncus est pellentesque. Sed viverra tellus in
hac habitasse platea dictumst vestibulum rhoncus.
""".strip())


def test_warning_message():
    msg = "Pharetra pharetra massa massa ultricies mi quis hendrerit."

    assert_equal(warning_message(msg),"""
WARNING
=======

Pharetra pharetra massa massa ultricies mi quis hendrerit.
                """.strip())


def test_error_message():
    msg = "Pharetra pharetra massa massa ultricies mi quis hendrerit."

    assert_equal(error_message(msg),"""
ERROR
=====

Pharetra pharetra massa massa ultricies mi quis hendrerit.
                """.strip())


def test_warning_message_is_none():
    assert_equal(warning_message(),"WARNING\n=======")


def test_error_message_is_none():
    assert_equal(error_message(),"ERROR\n=====")


def test_assert_or_pass():
    assert_or_print(True, onerror='pass')
    assert_or_print(False, onerror='pass')


def test_assert_or_warn():
    assert_or_print(True, onerror='warn')
    assert_or_print(False, onerror='warn')


def test_assert_or_raise():
    assert_or_print(True, onerror='raise')
    with assert_raises(AssertionError):
        assert_or_print(False, onerror='raise')
{'fraction_non_alphanumeric': 0.10219966159052453, 'fraction_numerical': 0.0, 'mean_word_length': 4.484230055658627, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 5, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4314341', 'n_tokens_mistral': 1006, 'n_tokens_neox': 970, 'n_words': 268}