2022-09-13 12:07:04 +02:00
|
|
|
import unittest
|
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
from cpl_query.enumerable.enumerable import Enumerable
|
2022-09-13 12:07:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EnumerableTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
2022-09-13 19:33:26 +02:00
|
|
|
self._list = Enumerable(int)
|
2022-09-13 12:07:04 +02:00
|
|
|
|
|
|
|
def _clear(self):
|
|
|
|
self._list.clear()
|
|
|
|
self.assertEqual(self._list, [])
|
|
|
|
|
|
|
|
def test_append(self):
|
|
|
|
self._list.add(1)
|
|
|
|
self._list.add(2)
|
|
|
|
self._list.add(3)
|
|
|
|
|
|
|
|
self.assertEqual(self._list.to_list(), [1, 2, 3])
|
|
|
|
self.assertRaises(Exception, lambda v: self._list.add(v), '3')
|
|
|
|
|
|
|
|
def test_default(self):
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(Enumerable.empty().to_list(), [])
|
|
|
|
self.assertEqual(Enumerable.range(0, 100).to_list(), list(range(0, 100)))
|
2022-09-13 12:07:04 +02:00
|
|
|
|
|
|
|
def test_iter(self):
|
|
|
|
n = 0
|
2022-09-13 19:33:26 +02:00
|
|
|
elements = Enumerable.range(0, 100)
|
2022-09-13 12:07:04 +02:00
|
|
|
while n < 100:
|
|
|
|
self.assertEqual(elements.next(), n)
|
|
|
|
n += 1
|
|
|
|
|
|
|
|
def test_for(self):
|
|
|
|
n = 0
|
2022-09-13 19:33:26 +02:00
|
|
|
for i in Enumerable.range(0, 100):
|
2022-09-13 12:07:04 +02:00
|
|
|
self.assertEqual(i, n)
|
|
|
|
n += 1
|
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
self._list.add(1)
|
|
|
|
self._list.add(2)
|
|
|
|
self._list.add(3)
|
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(self._list.element_at(2), [1, 2, 3][2])
|
2022-09-13 12:07:04 +02:00
|
|
|
|
|
|
|
def test_count(self):
|
|
|
|
self._list.add(1)
|
|
|
|
self._list.add(2)
|
|
|
|
self._list.add(3)
|
|
|
|
|
|
|
|
self.assertEqual(self._list.count(), 3)
|
|
|
|
|
|
|
|
def test_remove(self):
|
|
|
|
old_values = self._list._values
|
|
|
|
self._list.add(1)
|
|
|
|
self.assertNotEqual(old_values, self._list._values)
|
|
|
|
self._list.add(2)
|
|
|
|
self._list.add(3)
|
|
|
|
|
|
|
|
self.assertEqual(self._list.to_list(), [1, 2, 3])
|
|
|
|
self.assertRaises(Exception, lambda v: self._list.add(v), '3')
|
|
|
|
old_values = self._list._values
|
|
|
|
self._list.remove(3)
|
|
|
|
self.assertNotEqual(old_values, self._list._values)
|
|
|
|
self.assertEqual(self._list.to_list(), [1, 2])
|
|
|
|
self.assertRaises(Exception, lambda v: self._list.add(v), '3')
|