[Python-checkins] CVS: python/dist/src/Lib/test test_iter.py,1.10,1.11

Tim Peters tim_one@users.sourceforge.net
Fri, 04 May 2001 22:36:50 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv31444/python/dist/src/Lib/test

Modified Files:
	test_iter.py 
Log Message:
Make unicode.join() work nice with iterators.  This also required a change
to string.join(), so that when the latter figures out in midstream that
it really needs unicode.join() instead, unicode.join() can actually get
all the sequence elements (i.e., there's no guarantee that the sequence
passed to string.join() can be iterated over *again* by unicode.join(),
so string.join() must not pass on the original sequence object anymore).


Index: test_iter.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_iter.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -r1.10 -r1.11
*** test_iter.py	2001/05/05 03:56:37	1.10
--- test_iter.py	2001/05/05 05:36:48	1.11
***************
*** 432,434 ****
--- 432,475 ----
          self.assertEqual(reduce(add, d), "".join(d.keys()))
  
+     def test_unicode_join_endcase(self):
+ 
+         # This class inserts a Unicode object into its argument's natural
+         # iteration, in the 3rd position.
+         class OhPhooey:
+             def __init__(self, seq):
+                 self.it = iter(seq)
+                 self.i = 0
+ 
+             def __iter__(self):
+                 return self
+ 
+             def next(self):
+                 i = self.i
+                 self.i = i+1
+                 if i == 2:
+                     return u"fooled you!"
+                 return self.it.next()
+ 
+         f = open(TESTFN, "w")
+         try:
+             f.write("a\n" + "b\n" + "c\n")
+         finally:
+             f.close()
+ 
+         f = open(TESTFN, "r")
+         # Nasty:  string.join(s) can't know whether unicode.join() is needed
+         # until it's seen all of s's elements.  But in this case, f's
+         # iterator cannot be restarted.  So what we're testing here is
+         # whether string.join() can manage to remember everything it's seen
+         # and pass that on to unicode.join().
+         try:
+             got = " - ".join(OhPhooey(f))
+             self.assertEqual(got, u"a\n - b\n - fooled you! - c\n")
+         finally:
+             f.close()
+             try:
+                 unlink(TESTFN)
+             except OSError:
+                 pass
+ 
  run_unittest(TestCase)