Package lepl :: Package stream :: Package _test :: Module simple
[hide private]
[frames] | no frames]

Source Code for Module lepl.stream._test.simple

  1   
  2  # The contents of this file are subject to the Mozilla Public License 
  3  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  4  # in compliance with the License. You may obtain a copy of the License 
  5  # at http://www.mozilla.org/MPL/ 
  6  # 
  7  # Software distributed under the License is distributed on an "AS IS" 
  8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
  9  # the License for the specific language governing rights and 
 10  # limitations under the License. 
 11  # 
 12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 13  # The Initial Developer of the Original Code is Andrew Cooke. 
 14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 16  # 
 17  # Alternatively, the contents of this file may be used under the terms 
 18  # of the LGPL license (the GNU Lesser General Public License, 
 19  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 20  # of the LGPL License are applicable instead of those above. 
 21  # 
 22  # If you wish to allow use of your version of this file only under the 
 23  # terms of the LGPL License and not to allow others to use your version 
 24  # of this file under the MPL, indicate your decision by deleting the 
 25  # provisions above and replace them with the notice and other provisions 
 26  # required by the LGPL License.  If you do not delete the provisions 
 27  # above, a recipient may use your version of this file under either the 
 28  # MPL or the LGPL License. 
 29   
 30   
 31  from lepl.support.lib import fmt 
 32  from lepl._test.base import BaseTest 
 33  from lepl.stream.core import s_empty, s_fmt, s_line, s_next, s_stream 
 34  from lepl.stream.factory import DEFAULT_STREAM_FACTORY 
 35   
 36   
37 -class GenericTest(BaseTest):
38
39 - def test_empty(self):
40 f = DEFAULT_STREAM_FACTORY 41 for (constructor, data) in ((f.from_sequence, ''), 42 (f.from_sequence, []), 43 (f.from_sequence, ()), 44 (f.from_string, ''), 45 (f.from_list, [])): 46 s = constructor(data) 47 assert s_empty(s) 48 try: 49 s_next(s) 50 assert False, fmt('expected error: {0}', s) 51 except StopIteration: 52 pass 53 try: 54 s_line(s, False) 55 assert False, fmt('expected error: {0}', s) 56 except StopIteration: 57 pass
58
59 - def test_single_value(self):
60 f = DEFAULT_STREAM_FACTORY 61 for (constructor, data) in ((f.from_sequence, 'a'), 62 (f.from_sequence, [1]), 63 (f.from_sequence, (2,)), 64 (f.from_string, 'b'), 65 (f.from_list, ['c'])): 66 s = constructor(data) 67 assert not s_empty(s) 68 (value, n) = s_next(s) 69 assert value == data 70 assert s_empty(n) 71 (line, n) = s_line(s, False) 72 assert line == data 73 assert s_empty(n)
74
75 - def test_two_values(self):
76 f = DEFAULT_STREAM_FACTORY 77 for (constructor, data) in ((f.from_sequence, 'ab'), 78 (f.from_sequence, [1, 2]), 79 (f.from_sequence, (2,3)), 80 (f.from_string, 'bc'), 81 (f.from_list, ['c', 6])): 82 s = constructor(data) 83 assert not s_empty(s) 84 (value, n) = s_next(s) 85 assert value == data[0:1] 86 (value, n) = s_next(n) 87 assert value == data[1:2] 88 assert s_empty(n) 89 (line, n) = s_line(s, False) 90 assert line == data 91 assert s_empty(n)
92
93 - def test_string_lines(self):
94 f = DEFAULT_STREAM_FACTORY 95 s = f.from_string('line 1\nline 2\nline 3\n') 96 (l, s) = s_line(s, False) 97 assert l == 'line 1\n', l 98 (l, _) = s_line(s, False) 99 assert l == 'line 2\n', repr(l) 100 locn = s_fmt(s, '{location}') 101 assert locn == 'line 2, character 1', locn 102 sl = s_stream(s, l) 103 (_, sl) = s_next(sl, count=2) 104 locn = s_fmt(sl, '{location}') 105 assert locn == 'line 2, character 3', locn
106