예제 #1
0
class Edgecases(unittest.TestCase):
    def setUp(self):
        self.f = TextReader(PREFIX + "/LICENSE.txt", linesep=TextReader.UNIX)

    def testPeekPastEOF(self):
        self.assertRaises(IOError, self.f.peekline, 4711)

    def testPrevPastBOF(self):
        self.assertRaises(IOError, self.f.prevline, 4711)

    def testReadPastEOF(self):
        self.assertEqual(len(self.f.read(1)), 1)
        self.f.read(
            sys.maxsize)  # read past end of file - no license text is THAT big
        self.assertNotEqual(self.f.currpos, sys.maxsize + 1)
        self.assertEqual(len(self.f.read(1)), 0)  # no more to read
        self.assertEqual(len(self.f.readline()), 0)
        self.f.seek(0)

    def testReadlineUntilEOF(self):
        for line in self.f:
            prev = line
            pass
        self.assertEqual(
            prev,
            'OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.'
        )
        self.assertEqual(self.f.readline(), '')

    def testSearchInVain(self):
        self.assertRaises(IOError, self.f.cue, 'I am a little teapot')
        self.f.seek(0)
예제 #2
0
class Edgecases(unittest.TestCase):
    def setUp(self):
        self.f = TextReader(PREFIX + "/LICENSE.txt",linesep=TextReader.UNIX)

    def testPeekPastEOF(self):
        self.assertRaises(IOError,
                          self.f.peekline, 4711)

    def testPrevPastBOF(self):
        self.assertRaises(IOError,
                          self.f.prevline, 4711)

    def testReadPastEOF(self):
        self.assertEqual(len(self.f.read(1)), 1)
        self.f.read(sys.maxsize) # read past end of file - no license text is THAT big
        self.assertNotEqual(self.f.currpos, sys.maxsize+1)
        self.assertEqual(len(self.f.read(1)), 0) # no more to read
        self.assertEqual(len(self.f.readline()), 0)
        self.f.seek(0)
        
    def testReadlineUntilEOF(self):
        for line in self.f:
            prev = line
            pass
        self.assertEqual(prev,
                         'OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.')
        self.assertEqual(self.f.readline(), '')

    def testSearchInVain(self):
        self.assertRaises(IOError,
                          self.f.cue, 'I am a little teapot')
        self.f.seek(0)
예제 #3
0
 def testClose(self):
     f = open("textreader.tmp","w")
     f.write("foo")
     f.close()
     r = TextReader("textreader.tmp")
     # make sure TextReader isn't keeping the file open
     os.rename("textreader.tmp", "textreader2.tmp")
     os.unlink("textreader2.tmp")
     self.assertEqual("foo",r.readline())
예제 #4
0
 def testClose(self):
     f = open("textreader.tmp", "w")
     f.write("foo")
     f.close()
     r = TextReader("textreader.tmp")
     # make sure TextReader isn't keeping the file open
     os.rename("textreader.tmp", "textreader2.tmp")
     os.unlink("textreader2.tmp")
     self.assertEqual("foo", r.readline())
예제 #5
0
class Basic(unittest.TestCase):
    def setUp(self):
        self.f = TextReader(PREFIX + "/LICENSE.txt", linesep=TextReader.UNIX)

    def testReadline(self):
        self.assertEqual(self.f.readline(),
                         'A. HISTORY OF THE SOFTWARE')

        self.assertEqual(self.f.readline(),
                         '==========================')
        self.f.seek(0)

    def testIterateFile(self):
        self.assertEqual(self.f.bof(), True)
        self.assertEqual(self.f.eof(), False)
        for line in self.f:
            pass
        self.assertEqual(self.f.bof(), False)
        self.assertEqual(self.f.eof(), True)
        self.f.seek(0)
        
    def testReadparagraph(self):
        l = self.f.readparagraph()
        self.assertEqual(l, 'A. HISTORY OF THE SOFTWARE'+self.f.linesep+'==========================')
        l = self.f.readparagraph()
        self.assertEqual(l, 'Python was created in the early 1990s by Guido van Rossum at Stichting'+self.f.linesep+
                         'Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands'+self.f.linesep+
                         'as a successor of a language called ABC.  Guido remains Python\'s'+self.f.linesep+
                         'principal author, although it includes many contributions from others.')
        self.f.cuepast("to make these releases possible.") # next paragraph is separated by three newlines
        t = self.f.readparagraph()[:23]
        self.assertEqual(t,"B. TERMS AND CONDITIONS")
        self.f.seek(0)

    def testReadChunk(self):
        l = self.f.readchunk('(')
        l = self.f.readchunk(')')
        self.assertEqual(l,'CWI, see http://www.cwi.nl')
        self.f.seek(0)

    def testPeekLine(self):
        l = self.f.peekline()
        self.assertEqual(l, 'A. HISTORY OF THE SOFTWARE')
        l = self.f.peekline(4)
        self.assertEqual(l, 'Python was created in the early 1990s by Guido van Rossum at Stichting')
        self.f.seek(0)
        
    def testPeekParagraph(self):
        l = self.f.peekparagraph()
        self.assertEqual(l, 'A. HISTORY OF THE SOFTWARE'+self.f.linesep+'==========================')
        l = self.f.peekparagraph(2)
        self.assertEqual(l, 'Python was created in the early 1990s by Guido van Rossum at Stichting'+self.f.linesep+
                         'Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands'+self.f.linesep+
                         'as a successor of a language called ABC.  Guido remains Python\'s'+self.f.linesep+
                         'principal author, although it includes many contributions from others.')
        self.f.seek(0)

    

    def testPrevLine(self):
        self.f.readparagraph()
        self.f.readparagraph()
        self.assertEqual(self.f.prevline(3), # first two newlines, then the actual previous line (does this make sense?)
                         'principal author, although it includes many contributions from others.')
        self.assertEqual(self.f.prevline(6),
                         'Python was created in the early 1990s by Guido van Rossum at Stichting')
        self.f.seek(0)

    def testCue(self):
        self.f.cue("Guido")
        self.assertEqual(self.f.readline(),
                          'Guido van Rossum at Stichting')
        self.f.seek(0)

    def testCuePast(self):
        self.f.cuepast("Guido")
        self.assertEqual(self.f.readline(),
                          ' van Rossum at Stichting')
        self.f.seek(0)

    def testReadTo(self):
        self.assertEqual(self.f.readto("SOFTWARE"),
                          'A. HISTORY OF THE ')
예제 #6
0
 def testISO(self):
     f = TextReader(PREFIX + "/test/test_shlex.py", "iso-8859-1")
     f.cue(';|-|)|')
     f.readline()
     self.assertEqual(f.read(5),
                       "\u00e1\u00e9\u00ed\u00f3\u00fa")
예제 #7
0
class Basic(unittest.TestCase):
    def setUp(self):
        self.f = TextReader(PREFIX + "/LICENSE.txt", linesep=TextReader.UNIX)

    def testReadline(self):
        self.assertEqual(self.f.readline(), 'A. HISTORY OF THE SOFTWARE')

        self.assertEqual(self.f.readline(), '==========================')
        self.f.seek(0)

    def testIterateFile(self):
        self.assertEqual(self.f.bof(), True)
        self.assertEqual(self.f.eof(), False)
        for line in self.f:
            pass
        self.assertEqual(self.f.bof(), False)
        self.assertEqual(self.f.eof(), True)
        self.f.seek(0)

    def testReadparagraph(self):
        l = self.f.readparagraph()
        self.assertEqual(
            l, 'A. HISTORY OF THE SOFTWARE' + self.f.linesep +
            '==========================')
        l = self.f.readparagraph()
        self.assertEqual(
            l,
            'Python was created in the early 1990s by Guido van Rossum at Stichting'
            + self.f.linesep +
            'Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands'
            + self.f.linesep +
            'as a successor of a language called ABC.  Guido remains Python\'s'
            + self.f.linesep +
            'principal author, although it includes many contributions from others.'
        )
        self.f.cuepast("to make these releases possible."
                       )  # next paragraph is separated by three newlines
        t = self.f.readparagraph()[:23]
        self.assertEqual(t, "B. TERMS AND CONDITIONS")
        self.f.seek(0)

    def testReadChunk(self):
        l = self.f.readchunk('(')
        l = self.f.readchunk(')')
        self.assertEqual(l, 'CWI, see http://www.cwi.nl')
        self.f.seek(0)

    def testPeekLine(self):
        l = self.f.peekline()
        self.assertEqual(l, 'A. HISTORY OF THE SOFTWARE')
        l = self.f.peekline(4)
        self.assertEqual(
            l,
            'Python was created in the early 1990s by Guido van Rossum at Stichting'
        )
        self.f.seek(0)

    def testPeekParagraph(self):
        l = self.f.peekparagraph()
        self.assertEqual(
            l, 'A. HISTORY OF THE SOFTWARE' + self.f.linesep +
            '==========================')
        l = self.f.peekparagraph(2)
        self.assertEqual(
            l,
            'Python was created in the early 1990s by Guido van Rossum at Stichting'
            + self.f.linesep +
            'Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands'
            + self.f.linesep +
            'as a successor of a language called ABC.  Guido remains Python\'s'
            + self.f.linesep +
            'principal author, although it includes many contributions from others.'
        )
        self.f.seek(0)

    def testPrevLine(self):
        self.f.readparagraph()
        self.f.readparagraph()
        self.assertEqual(
            self.f.prevline(
                3
            ),  # first two newlines, then the actual previous line (does this make sense?)
            'principal author, although it includes many contributions from others.'
        )
        self.assertEqual(
            self.f.prevline(6),
            'Python was created in the early 1990s by Guido van Rossum at Stichting'
        )
        self.f.seek(0)

    def testCue(self):
        self.f.cue("Guido")
        self.assertEqual(self.f.readline(), 'Guido van Rossum at Stichting')
        self.f.seek(0)

    def testCuePast(self):
        self.f.cuepast("Guido")
        self.assertEqual(self.f.readline(), ' van Rossum at Stichting')
        self.f.seek(0)

    def testReadTo(self):
        self.assertEqual(self.f.readto("SOFTWARE"), 'A. HISTORY OF THE ')
예제 #8
0
 def testISO(self):
     f = TextReader(PREFIX + "/test/test_shlex.py", "iso-8859-1")
     f.cue(';|-|)|')
     f.readline()
     self.assertEqual(f.read(5), "\u00e1\u00e9\u00ed\u00f3\u00fa")