1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
# Copyright (c) 2009 Oliver Cope. All rights reserved.
# See LICENSE.txt for terms of redistribution and use.
"""
Tests for pesto/utils.py
"""
from __future__ import with_statement
from cStringIO import StringIO, OutputType as cStringIO_OutputType
from nose.tools import assert_equal, assert_true
from pesto.utils import ExpandableOutput, PutbackInput, DelimitedInput
def test_expandableio():
with ExpandableOutput(10) as eio:
eio.write('abc')
eio.seek(0)
assert_equal(eio.read(), 'abc')
assert_true(isinstance(eio._io, cStringIO_OutputType))
eio.write('defghij')
assert_true(isinstance(eio._io, cStringIO_OutputType))
eio.write('k')
assert_true(isinstance(eio._io, file))
eio.seek(0)
assert_equal(eio.read(), 'abcdefghijk')
def test_putbackio_nested_putbacks():
s = StringIO('one,two,three')
s.seek(0)
p = PutbackInput(s)
a = s.read(3)
b = s.read(3)
p.putback(b)
p.putback(a)
assert_equal(p.read(), 'one,two,three')
def test_putbackio_read_from_putback_short():
"""
Test read for a length smaller than the putback buffer
"""
s = StringIO('one,two,three')
s.seek(0)
p = PutbackInput(s)
a = p.read(4)
b = p.read(4)
p.putback(b)
p.putback(a)
assert_equal(p.read(7), "one,two")
def test_putbackio_read_from_putback_and_stream_long():
"""
Test read for a length longer than the putback buffer
"""
s = StringIO('one,two,three')
s.seek(0)
p = PutbackInput(s)
a = p.read(2)
b = p.read(2)
p.putback(b)
p.putback(a)
assert_equal(p.read(7), "one,two")
def test_putbackio_readline_from_putback_and_stream_short():
"""
Test readline for a length shorter than the putback buffer
"""
s = StringIO('one two\nthree four\n')
s.seek(0)
p = PutbackInput(s)
a = p.read(4)
b = p.read(4)
p.putback(b)
p.putback(a)
assert_equal(p.readline(10), "one two\n")
def test_putbackio_readline_from_putback_and_stream_long():
"""
Test readline for a length longer than the putback buffer
"""
s = StringIO('one two\nthree four\n')
s.seek(0)
p = PutbackInput(s)
a = p.read(2)
b = p.read(2)
p.putback(b)
p.putback(a)
assert_equal(p.readline(10), "one two\n")
def test_delimitedio_read():
s = StringIO('one--two--three')
s.seek(0)
p = PutbackInput(s)
assert_equal(DelimitedInput(p, '--').read(100), 'one')
assert_equal(DelimitedInput(p, '--').read(100), 'two')
assert_equal(DelimitedInput(p, '--').read(100), 'three')
assert_equal(DelimitedInput(p, '--').read(100), '')
# delimiters are handled correctly, even when they straddle a
# block (as determined by ``size``)
s = StringIO('one--two--three')
s.seek(0)
p = PutbackInput(s)
d = DelimitedInput(p, '--')
assert_equal(d.read(2), 'on')
assert_equal(d.read(2), 'e')
assert_equal(d.read(2), '')
def test_delimitedio_readline():
s = StringIO('one\ntwo|\nthree\n')
s.seek(0)
p = PutbackInput(s)
d = DelimitedInput(p, '|')
assert_equal(d.readline(), 'one\n')
assert_equal(d.readline(), 'two')
assert_equal(d.readline(), '')
# The case where a newline forms part of the delimiter
s = StringIO('one\ntwo\n--three\n')
s.seek(0)
p = PutbackInput(s)
d = DelimitedInput(p, '\n--')
assert_equal(d.readline(), 'one\n')
assert_equal(d.readline(), 'two')
assert_equal(d.readline(), '')
|