Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_reusing_same_splitter(self):
c = Chunk('asdf', {'fg': 32})
s1 = c.splitter()
self.assertEqual(s1.request(3), (3, Chunk('asd', {'fg': 32})))
s1.reinit(c)
self.assertEqual(s1.request(3), (3, Chunk('asd', {'fg': 32})))
s1.reinit(c)
self.assertEqual(s1.request(3), (3, Chunk('asd', {'fg': 32})))
c2 = Chunk('abcdef', {})
s1.reinit(c2)
self.assertEqual(s1.request(3), (3, Chunk('abc')))
def test_width_awareness(self):
s = Chunk('asdf')
self.assertEqual(Chunk('ab\u0300c').splitter().request(3), (3, Chunk('ab\u0300c')))
self.assertEqual(Chunk('ab\u0300c').splitter().request(2), (2, Chunk('ab\u0300')))
s = Chunk('ab\u0300c').splitter()
self.assertEqual(s.request(1), (1, Chunk('a')))
self.assertEqual(s.request(2), (2, Chunk('b\u0300c')))
c = Chunk('aEbc')
self.assertEqual(c.splitter().request(4), (4, Chunk('aEb')))
s = c.splitter()
self.assertEqual(s.request(2), (2, Chunk('a ')))
self.assertEqual(s.request(2), (2, Chunk('E')))
self.assertEqual(s.request(2), (2, Chunk('bc')))
self.assertEqual(s.request(2), None)
def test_width_awareness(self):
s = Chunk('asdf')
self.assertEqual(Chunk('ab\u0300c').splitter().request(3), (3, Chunk('ab\u0300c')))
self.assertEqual(Chunk('ab\u0300c').splitter().request(2), (2, Chunk('ab\u0300')))
s = Chunk('ab\u0300c').splitter()
self.assertEqual(s.request(1), (1, Chunk('a')))
self.assertEqual(s.request(2), (2, Chunk('b\u0300c')))
c = Chunk('aEbc')
self.assertEqual(c.splitter().request(4), (4, Chunk('aEb')))
s = c.splitter()
self.assertEqual(s.request(2), (2, Chunk('a ')))
self.assertEqual(s.request(2), (2, Chunk('E')))
self.assertEqual(s.request(2), (2, Chunk('bc')))
self.assertEqual(s.request(2), None)
def test_width_awareness(self):
s = Chunk('asdf')
self.assertEqual(Chunk('ab\u0300c').splitter().request(3), (3, Chunk('ab\u0300c')))
self.assertEqual(Chunk('ab\u0300c').splitter().request(2), (2, Chunk('ab\u0300')))
s = Chunk('ab\u0300c').splitter()
self.assertEqual(s.request(1), (1, Chunk('a')))
self.assertEqual(s.request(2), (2, Chunk('b\u0300c')))
c = Chunk('aEbc')
self.assertEqual(c.splitter().request(4), (4, Chunk('aEb')))
s = c.splitter()
self.assertEqual(s.request(2), (2, Chunk('a ')))
self.assertEqual(s.request(2), (2, Chunk('E')))
self.assertEqual(s.request(2), (2, Chunk('bc')))
self.assertEqual(s.request(2), None)
def test_width_awareness(self):
s = Chunk('asdf')
self.assertEqual(Chunk('ab\u0300c').splitter().request(3), (3, Chunk('ab\u0300c')))
self.assertEqual(Chunk('ab\u0300c').splitter().request(2), (2, Chunk('ab\u0300')))
s = Chunk('ab\u0300c').splitter()
self.assertEqual(s.request(1), (1, Chunk('a')))
self.assertEqual(s.request(2), (2, Chunk('b\u0300c')))
c = Chunk('aEbc')
self.assertEqual(c.splitter().request(4), (4, Chunk('aEb')))
s = c.splitter()
self.assertEqual(s.request(2), (2, Chunk('a ')))
self.assertEqual(s.request(2), (2, Chunk('E')))
self.assertEqual(s.request(2), (2, Chunk('bc')))
self.assertEqual(s.request(2), None)
def test_chunk_splitter(self):
splitter = Chunk('asdf', {'fg': 32}).splitter()
self.assertEqual(splitter.request(1), (1, Chunk('a', {'fg': 32})))
self.assertEqual(splitter.request(4), (3, Chunk('sdf', {'fg': 32})))
self.assertEqual(splitter.request(4), None)
def test_width_awareness(self):
s = Chunk('asdf')
self.assertEqual(Chunk('ab\u0300c').splitter().request(3), (3, Chunk('ab\u0300c')))
self.assertEqual(Chunk('ab\u0300c').splitter().request(2), (2, Chunk('ab\u0300')))
s = Chunk('ab\u0300c').splitter()
self.assertEqual(s.request(1), (1, Chunk('a')))
self.assertEqual(s.request(2), (2, Chunk('b\u0300c')))
c = Chunk('aEbc')
self.assertEqual(c.splitter().request(4), (4, Chunk('aEb')))
s = c.splitter()
self.assertEqual(s.request(2), (2, Chunk('a ')))
self.assertEqual(s.request(2), (2, Chunk('E')))
self.assertEqual(s.request(2), (2, Chunk('bc')))
self.assertEqual(s.request(2), None)
else:
chunks = []
cur_fmt = {}
for x in tokens_and_strings:
if isinstance(x, dict):
cur_fmt.update(x)
elif isinstance(x, (bytes, unicode)):
atts = parse_args('', dict((k, v)
for k, v in cur_fmt.items()
if v is not None))
chunks.append(Chunk(x, atts=atts))
else:
raise Exception("logic error")
return FmtStr(*chunks)
else:
return FmtStr(Chunk(s))
inserted = True
elif bfs_start <= start < bfs_end:
divide = start - bfs_start
head = Chunk(bfs.s[:divide], atts=bfs.atts)
tail = Chunk(bfs.s[end - bfs_start:], atts=bfs.atts)
new_components.extend([head] + new_fs.chunks)
inserted = True
if bfs_start < end < bfs_end:
tail = Chunk(bfs.s[end - bfs_start:], atts=bfs.atts)
new_components.append(tail)
elif bfs_start < end < bfs_end:
divide = start - bfs_start
tail = Chunk(bfs.s[end - bfs_start:], atts=bfs.atts)
new_components.append(tail)
elif bfs_start >= end or bfs_end <= start:
new_components.append(bfs)
if not inserted:
new_components.extend(new_fs.chunks)
inserted = True
return FmtStr(*[s for s in new_components if s.s])
def __getitem__(self, index):
index = normalize_slice(len(self), index)
counter = 0
parts = []
for chunk in self.chunks:
if index.start < counter + len(chunk) and index.stop > counter:
start = max(0, index.start - counter)
end = min(index.stop - counter, len(chunk))
if end - start == len(chunk):
parts.append(chunk)
else:
s_part = chunk.s[max(0, index.start - counter): index.stop - counter]
parts.append(Chunk(s_part, chunk.atts))
counter += len(chunk)
if index.stop < counter:
break
return FmtStr(*parts) if parts else fmtstr('')