1#!/usr/bin/env python32import unittest34from saneterm.color import Color, ColorType5from saneterm.pty import PositionedIterator67from gi.repository import Gdk89TEST_STRING = 'foo;bar'1011class TestPositionedIterator(unittest.TestCase):12 """Tests for saneterm.pty.PositionedIterator"""1314 def test_lossless(self):15 """Test that the iterator doesn't loose any content"""1617 it = PositionedIterator(TEST_STRING)1819 self.assertEqual([x for x in it], list(TEST_STRING))20 self.assertEqual(it.wrapped, TEST_STRING)2122 def test_indices(self):23 """Test that the iterator's pos matches the string indices"""24 it = PositionedIterator(TEST_STRING)2526 self.assertEqual(it.pos, -1)2728 for x in it:29 assert x == TEST_STRING[it.pos]3031 if x == ';':32 break3334 self.assertEqual(it.pos, 3)3536 for x in it:37 self.assertEqual(x, it.wrapped[it.pos])3839 self.assertTrue(it.empty())4041 def test_backtracking(self):42 """Test waypoint() and backtrack() methods"""43 it = PositionedIterator(TEST_STRING)4445 semicolon_index = None4647 for x in it:48 if x == ';':49 it.waypoint()50 semicolon_index = it.pos5152 self.assertEqual(semicolon_index, TEST_STRING.index(';'))5354 self.assertTrue(it.empty())5556 with self.assertRaises(StopIteration):57 _ = it.next()5859 it.backtrack()6061 self.assertEqual(it.next(), ';')62 self.assertEqual(it.pos, semicolon_index)6364 def test_takewhile_greedy(self):65 """Test takewhile_greedy() method"""66 it = PositionedIterator(TEST_STRING)6768 s = it.takewhile_greedy(lambda x: x != ';')6970 self.assertEqual(s, TEST_STRING.split(';')[0])71 self.assertEqual(it.pos, len(s) - 1)72 self.assertEqual(it.next(), ';')7374 def test_empty(self):75 """Test empty() predicate"""76 it = PositionedIterator(TEST_STRING)7778 for x in it:79 if it.pos + 1 == len(TEST_STRING):80 self.assertTrue(it.empty())8182 self.assertTrue(it.empty())8384 with self.assertRaises(StopIteration):85 _ = it.next()8687 def test_take(self):88 """Test take() method"""89 length = 390 it1 = PositionedIterator(TEST_STRING)91 it2 = PositionedIterator(TEST_STRING)9293 s1 = it1.take(length)94 s2 = ''95 for x in it2:96 if it2.pos >= length:97 break98 else:99 s2 += x100101 self.assertEqual(s1, s2)102 self.assertEqual(s1, TEST_STRING[0:length])103104 # using take does not consume the next element!105 self.assertEqual(it1.pos, length - 1)106107class TestColor(unittest.TestCase):108 """Tests for saneterm.color"""109110 def test_256_colors(self):111 """112 Check divmod based RGB value calculation113 against 256 color table generation as implemented114 in XTerm's 256colres.pl.115 """116 def channel_val(c):117 return (c * 40 + 55 if c > 0 else 0) / 255118119 for r in range(6):120 for g in range(6):121 for b in range(6):122 n = 16 + (r * 36) + (g * 6) + b123124 expected = Gdk.RGBA(*map(channel_val, (r, g, b)))125 col = Color(ColorType.NUMBERED_256, n).to_gdk()126127 self.assertTrue(128 expected.equal(col),129 'Color {}: expected: {}; got: {}'.format(130 n, expected.to_string(), col.to_string()131 )132 )133134if __name__ == '__main__':135 unittest.main()