<commit_before>from nose.tools import assert_raises
from syn.type.a.ext import (Callable, List, Sequence, Mapping, Dict, Hashable,
                            Tuple)

#-------------------------------------------------------------------------------
# Callable

def test_callable():
    class Foo(object):
        def __call__(self):
            pass

    Foo()()
    t = Callable()
    t.check(Foo())
    t.check(test_callable)
    t.check(int)
    t.validate(int)
    assert_raises(TypeError, t.check, 1)
    assert_raises(TypeError, t.validate, 1)

#-------------------------------------------------------------------------------
# Hashable

def test_hashable():
    t = Hashable()
    assert t.query(3)
    assert not t.query(dict(a = 3))

#-------------------------------------------------------------------------------
# Sequence

def test_sequence():
    int_seq = Sequence(int)

    assert int_seq.query([1, 2, 3])
    assert not int_seq.query([1.2, 2, 3])
    assert int_seq.query((1, 2, 3))
    assert not int_seq.query(1)

    int_list = List(int)

    assert int_list.query([1, 2, 3])
    assert not int_list.query([1.2, 2, 3])
    assert not int_list.query((1, 2, 3))

    bad_list = (1.2, '3', 4)
    good_list = [1, 2, 3]
    assert int_list.coerce(bad_list) == [1, 3, 4]
    assert int_list.coerce(good_list) is good_list

#-------------------------------------------------------------------------------
# Tuple

def test_tuple():
    t = Tuple(int)
    assert t.types.type is int
    assert t.uniform
    assert t.length is None

    assert t.query((1, 2, 3))
    assert t.query((1,))
    assert not t.query([1, 2])
    assert not t.query((1, 2.3, 3))
    assert t.coerce([1, 2.3, '3']) == (1, 2, 3)

    t = Tuple(int, 2)
    assert t.types.type is int
    assert t.uniform
    assert t.length == 2

    assert t.query((1, 2))
    assert not t.query((1, 2.3))
    assert not t.query((1, 2, 3))
    assert t.coerce((1, 2)) == (1, 2)
    assert t.coerce([1.2, '2']) == (1, 2)
    assert_raises(TypeError, t.coerce, [1, 2.3, '3'])

    t = Tuple((int, float))
    assert not t.uniform
    assert t.length == 2

    assert t.query((1, 2.3))
    assert not t.query((2.3, 1))
    assert t.coerce((2.3, 1)) == (2, 1.0)

#-------------------------------------------------------------------------------
# Mapping

def test_mapping():<commit_msg>Adding more tests for syn.type.Tuple<commit_after>from nose.tools import assert_raises
from syn.type.a.ext import (Callable, List, Sequence, Mapping, Dict, Hashable,
                            Tuple)

#-------------------------------------------------------------------------------
# Callable

def test_callable():
    class Foo(object):
        def __call__(self):
            pass

    Foo()()
    t = Callable()
    t.check(Foo())
    t.check(test_callable)
    t.check(int)
    t.validate(int)
    assert_raises(TypeError, t.check, 1)
    assert_raises(TypeError, t.validate, 1)

#-------------------------------------------------------------------------------
# Hashable

def test_hashable():
    t = Hashable()
    assert t.query(3)
    assert not t.query(dict(a = 3))

#-------------------------------------------------------------------------------
# Sequence

def test_sequence():
    int_seq = Sequence(int)

    assert int_seq.query([1, 2, 3])
    assert not int_seq.query([1.2, 2, 3])
    assert int_seq.query((1, 2, 3))
    assert not int_seq.query(1)

    int_list = List(int)

    assert int_list.query([1, 2, 3])
    assert not int_list.query([1.2, 2, 3])
    assert not int_list.query((1, 2, 3))

    bad_list = (1.2, '3', 4)
    good_list = [1, 2, 3]
    assert int_list.coerce(bad_list) == [1, 3, 4]
    assert int_list.coerce(good_list) is good_list

#-------------------------------------------------------------------------------
# Tuple

def test_tuple():
    t = Tuple(int)
    assert t.types.type is int
    assert t.uniform
    assert t.length is None

    assert t.query((1, 2, 3))
    assert t.query((1,))
    assert not t.query([1, 2])
    assert not t.query((1, 2.3, 3))
    assert t.coerce([1, 2.3, '3']) == (1, 2, 3)

    t = Tuple(int, 2)
    assert t.types.type is int
    assert t.uniform
    assert t.length == 2

    assert t.query((1, 2))
    assert not t.query((1, 2.3))
    assert not t.query((1, 2, 3))
    assert t.coerce((1, 2)) == (1, 2)
    assert t.coerce([1.2, '2']) == (1, 2)
    assert_raises(TypeError, t.coerce, [1, 2.3, '3'])

    t = Tuple((int, float))
    assert not t.uniform
    assert t.length == 2

    assert t.query((1, 2.3))
    assert not t.query((2.3, 1))
    assert t.coerce((2.3, 1)) == (2, 1.0)

    t = Tuple((int, float), uniform=True)
    assert t.uniform
    assert t.length is None

    assert t.query((2.3, 2.3))
    assert t.query((1, 1))
    assert t.query((2.3, 1))
    assert not t.query(('abc', 1))

#-------------------------------------------------------------------------------
# Mapping

def test_mapping():
{'fraction_non_alphanumeric': 0.29805746661270743, 'fraction_numerical': 0.03763658437879401, 'mean_word_length': 1.519367991845056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2107640', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1633, 'n_words': 426}