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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
# Copyright 1999 by Jeffrey Chang. All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
"""Tests for SearchIO legacy module."""
import string
import unittest
from io import StringIO
import warnings
from Bio import BiopythonWarning
with warnings.catch_warnings():
warnings.simplefilter("ignore", BiopythonWarning)
from Bio.SearchIO._legacy import ParserSupport
class UndoHandleTests(unittest.TestCase):
"""Tests for the UndoHandle."""
def test_one(self):
"""First test."""
data = """\
This
is
a multi-line
file"""
h = ParserSupport.UndoHandle(StringIO(data))
self.assertEqual(h.readline(), "This\n")
self.assertEqual(h.peekline(), "is\n")
self.assertEqual(h.readline(), "is\n")
# TODO - Meaning of saveline lacking \n?
h.saveline("saved\n")
self.assertEqual(h.peekline(), "saved\n")
h.saveline("another\n")
self.assertEqual(h.readline(), "another\n")
self.assertEqual(h.readline(), "saved\n")
# Test readlines after saveline
h.saveline("saved again\n")
lines = h.readlines()
self.assertEqual(len(lines), 3)
self.assertEqual(lines[0], "saved again\n")
self.assertEqual(lines[1], "a multi-line\n")
self.assertEqual(lines[2], "file") # no trailing \n
# should be empty now
self.assertEqual(h.readline(), "")
h.saveline("save after empty\n")
self.assertEqual(h.readline(), "save after empty\n")
self.assertEqual(h.readline(), "")
def test_read(self):
"""Test read method."""
h = ParserSupport.UndoHandle(StringIO("some text"))
h.saveline("more text")
self.assertEqual(h.read(), "more textsome text")
def test_undohandle_read_block(self):
"""Test reading in blocks."""
data = """\
This
is
a multi-line
file"""
for block in [1, 2, 10]:
s = StringIO(data)
h = ParserSupport.UndoHandle(s)
h.peekline()
new = ""
while True:
tmp = h.read(block)
if not tmp:
break
new += tmp
self.assertEqual(data, new)
h.close()
class TestParserSupport(unittest.TestCase):
def test_TaggingConsumer(self):
h = StringIO()
tc = ParserSupport.TaggingConsumer(handle=h, colwidth=5)
tc.start_section()
self.assertEqual(h.getvalue(), "***** start_section\n")
h.seek(0)
h.truncate(0)
tc.test1("myline")
self.assertEqual(h.getvalue(), "test1: myline\n")
h.seek(0)
h.truncate(0)
tc.end_section()
self.assertEqual(h.getvalue(), "***** end_section\n")
def test_is_blank_line(self):
is_blank_line = ParserSupport.is_blank_line
self.assertTrue(is_blank_line("\n"))
self.assertTrue(is_blank_line("\r\n"))
self.assertTrue(is_blank_line("\r"))
self.assertTrue(is_blank_line(""))
self.assertTrue(is_blank_line("", allow_spaces=1))
self.assertTrue(is_blank_line("", allow_spaces=0))
self.assertTrue(is_blank_line(string.whitespace, allow_spaces=1))
self.assertFalse(is_blank_line("hello"))
self.assertFalse(is_blank_line("hello", allow_spaces=1))
self.assertFalse(is_blank_line("hello", allow_spaces=0))
self.assertFalse(is_blank_line(string.whitespace, allow_spaces=0))
def test_safe_readline(self):
data = """\
This
file"""
h = ParserSupport.UndoHandle(StringIO(data))
safe_readline = ParserSupport.safe_readline
self.assertEqual(safe_readline(h), "This\n")
self.assertEqual(safe_readline(h), "file")
self.assertRaises(ValueError, safe_readline, h)
def test_safe_peekline(self):
safe_peekline = ParserSupport.safe_peekline
data = """\
This
file"""
h = ParserSupport.UndoHandle(StringIO(data))
self.assertEqual(safe_peekline(h), "This\n")
h.readline()
self.assertEqual(safe_peekline(h), "file")
h.readline()
self.assertRaises(ValueError, safe_peekline, h)
h.saveline("hello")
self.assertEqual(safe_peekline(h), "hello")
def test_read_and_call(self):
data = """\
>gi|132871|sp|P19947|RL30_BACSU 50S RIBOSOMAL PROTEIN L30 (BL27)
MAKLEITLKRSVIGRPEDQRVTVRTLGLKKTNQTVVHEDNAAIRGMINKVSHLVSVKEQ
>gi|132679|sp|P19946|RL15_BACSU 50S RIBOSOMAL PROTEIN L15
MKLHELKPSEGSRKTRNRVGRGIGSGNGKTAGKGHKGQNARSGGGVRPGFEGGQMPLFQRLPKRGFTNIN
RKEYAVVNLDKLNGFAEGTEVTPELLLETGVISKLNAGVKILGNGKLEKKLTVKANKFSASAKEAVEAAG
GTAEVI
"""
h = ParserSupport.UndoHandle(StringIO(data))
lines = []
rac = ParserSupport.read_and_call
rac(h, lines.append)
self.assertEqual(lines[-1][:10], ">gi|132871")
rac(h, lines.append, start="MAKLE", end="KEQ", contains="SVIG")
self.assertRaises(ValueError, rac, h, lines.append, blank=1)
self.assertRaises(ValueError, rac, h, lines.append, start="foobar")
self.assertRaises(ValueError, rac, h, lines.append, end="foobar")
self.assertRaises(ValueError, rac, h, lines.append, contains="foobar")
self.assertRaises(ValueError, rac, h, lines.append, blank=0)
def test_attempt_read_and_call(self):
data = """\
>gi|132871|sp|P19947|RL30_BACSU 50S RIBOSOMAL PROTEIN L30 (BL27)
MAKLEITLKRSVIGRPEDQRVTVRTLGLKKTNQTVVHEDNAAIRGMINKVSHLVSVKEQ
>gi|132679|sp|P19946|RL15_BACSU 50S RIBOSOMAL PROTEIN L15
MKLHELKPSEGSRKTRNRVGRGIGSGNGKTAGKGHKGQNARSGGGVRPGFEGGQMPLFQRLPKRGFTNIN
RKEYAVVNLDKLNGFAEGTEVTPELLLETGVISKLNAGVKILGNGKLEKKLTVKANKFSASAKEAVEAAG
GTAEVI"""
h = ParserSupport.UndoHandle(StringIO(data))
lines = []
arac = ParserSupport.attempt_read_and_call
self.assertTrue(arac(h, lines.append, contains="RIBOSOMAL PROTEIN"))
self.assertFalse(arac(h, lines.append, start="foobar"))
self.assertFalse(arac(h, lines.append, blank=1))
self.assertTrue(arac(h, lines.append, end="LVSVKEQ"))
if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity=2)
unittest.main(testRunner=runner)
|