import os

from dask.utils import textblock, filetext, takes_multiple_arguments, Dispatch

def test_textblock():
    text = b'123 456 789 abc def ghi'.replace(b' ', os.linesep.encode())
    with filetext(text, mode='wb') as fn:
        with open(fn, 'rb') as f:
            text = textblock(f, 1, 11)
            assert text == ('456 789 '.replace(' ', os.linesep)).encode()
            assert set(map(len, text.split())) == set([3])

            assert textblock(f, 1, 10) == textblock(fn, 1, 10)

            assert textblock(f, 0, 3) == ('123' + os.linesep).encode()
            assert textblock(f, 3 + len(os.linesep), 6) == ('456' + os.linesep).encode()


def test_takes_multiple_arguments():
    assert takes_multiple_arguments(map)
    assert not takes_multiple_arguments(sum)

    def multi(a, b, c):
        return a, b, c

    class Singular(object):
        def __init__(self, a):
            pass

    class Multi(object):
        def __init__(self, a, b):
            pass

    assert takes_multiple_arguments(multi)
    assert not takes_multiple_arguments(Singular)
    assert takes_multiple_arguments(Multi)


def test_dispatch():
    foo = Dispatch()
    foo.register(int, lambda a: a + 1)
    foo.register(float, lambda a: a - 1)
    foo.register(tuple, lambda a: tuple(foo(i) for i in a))
    foo.register(object, lambda a: a)

    class Bar(object):
        pass
    b = Bar()
    assert foo(1) == 2
    assert foo(1.0) == 0.0
    assert foo(b) == b
    assert foo((1, 2.0, b)) == (2, 1.0, b)

@@ -27,16 +27,17 @@
  import 
+(
 textbloc
@@ -74,25 +74,74 @@
 guments,
- Dispatch
+%0A                        Dispatch, tmpfile, next_newline)%0A
 %0A%0Adef te
@@ -1512,16 +1512,16 @@
 b) == b%0A
-
     asse
@@ -1555,8 +1555,1615 @@
 1.0, b)%0A
+%0A%0Adef test_nextnewline():%0A    encoding = 'utf-16-le'%0A%0A    euro = u'%5Cu20ac'%0A    yen = u'%5Cu00a5'%0A%0A    bin_euro = u'%5Cu20ac'.encode(encoding)%0A    bin_yen = u'%5Cu00a5'.encode(encoding)%0A    bin_newline = '%5Cn'.encode(encoding)%0A%0A    data = (euro * 10) + '%5Cn' + (yen * 10) + '%5Cn' + (euro * 10)%0A    bin_data = data.encode(encoding)%0A%0A    with tmpfile() as fn:%0A        with open(fn, 'w+b') as f:%0A            f.write(bin_data)%0A            f.seek(0)%0A%0A            start, stop = next_newline(f, 2, encoding)%0A            assert start == len(bin_euro) * 10%0A            assert stop == len(bin_euro) * 10 + len('%5Cn'.encode(encoding))%0A%0A            start, stop = next_newline(f, len(bin_euro)*11, encoding)%0A            assert start == len(bin_euro) * 20 + len(bin_newline)%0A            assert stop == len(bin_euro) * 20 + len(bin_newline) * 2%0A%0A%0Adef test_gh606():%0A    encoding = 'utf-16-le'%0A    euro = u'%5Cu20ac'%0A    yen = u'%5Cu00a5'%0A    bin_euro = u'%5Cu20ac'.encode(encoding)%0A    bin_yen = u'%5Cu00a5'.encode(encoding)%0A%0A    data = (euro * 10) + '%5Cn' + (yen * 10) + '%5Cn' + (euro * 10)%0A    bin_data = data.encode(encoding)%0A%0A    with tmpfile() as fn:%0A        with open(fn, 'w+b') as f:%0A            f.write(bin_data)%0A            f.seek(0)%0A%0A            stop = len(bin_euro) * 10 + len('%5Cn'.encode(encoding))%0A            res = textblock(f, 1, stop, encoding=encoding)%0A            assert res == ((yen * 10) + '%5Cn').encode(encoding)%0A%0A            stop = len(bin_euro) * 10 + len('%5Cn'.encode(encoding))%0A            res = textblock(f, 0, stop, encoding=encoding)%0A            assert res == ((euro * 10) + '%5Cn' + (yen * 10) + '%5Cn').encode(encoding)%0A

Add regression test for textblock, next_newline.