Added observables

This commit is contained in:
2023-04-15 16:17:31 +02:00
parent 8dee4d8f70
commit efc9cf9c83
11 changed files with 254 additions and 1 deletions

View File

@@ -0,0 +1 @@
# imports

View File

@@ -0,0 +1,71 @@
import unittest
from threading import Timer
from cpl_reactive_extensions.observable import Observable
from cpl_reactive_extensions.observer import Observer
class ReactiveTestCase(unittest.TestCase):
def setUp(self):
pass
def test_observer(self):
called = 0
has_error = False
completed = False
test_x = 1
def callback(observer: Observer):
nonlocal test_x
observer.next(test_x)
test_x += 1
observer.next(test_x)
test_x += 1
observer.next(test_x)
def complete():
nonlocal test_x
test_x += 1
observer.next(test_x)
observer.complete()
Timer(1.0, complete).start()
observable = Observable(callback)
def on_next(x):
nonlocal called
called += 1
self.assertEqual(test_x, x)
def on_err():
nonlocal has_error
has_error = True
def on_complete():
nonlocal completed
completed = True
self.assertEqual(called, 0)
self.assertFalse(has_error)
self.assertFalse(completed)
observable.subscribe(
Observer(
on_next,
on_err,
on_complete,
)
)
self.assertEqual(called, 3)
self.assertFalse(has_error)
self.assertFalse(completed)
def complete():
self.assertEqual(called, 4)
self.assertFalse(has_error)
self.assertTrue(completed)
Timer(1.0, complete).start()
def test_subject(self):
pass

View File

@@ -0,0 +1,24 @@
import unittest
from unittests_query.enumerable_query_test_case import EnumerableQueryTestCase
from unittests_query.enumerable_test_case import EnumerableTestCase
from unittests_query.iterable_query_test_case import IterableQueryTestCase
from unittests_query.iterable_test_case import IterableTestCase
from unittests_query.sequence_test_case import SequenceTestCase
from unittests_reactive_extenstions.reactive_test_case import ReactiveTestCase
class ReactiveTestSuite(unittest.TestSuite):
def __init__(self):
unittest.TestSuite.__init__(self)
loader = unittest.TestLoader()
self.addTests(loader.loadTestsFromTestCase(ReactiveTestCase))
def run(self, *args):
super().run(*args)
if __name__ == "__main__":
runner = unittest.TextTestRunner()
runner.run(ReactiveTestSuite())

View File

@@ -0,0 +1,46 @@
{
"ProjectSettings": {
"Name": "unittests_reactive_extenstions",
"Version": {
"Major": "0",
"Minor": "0",
"Micro": "0"
},
"Author": "",
"AuthorEmail": "",
"Description": "",
"LongDescription": "",
"URL": "",
"CopyrightDate": "",
"CopyrightName": "",
"LicenseName": "",
"LicenseDescription": "",
"Dependencies": [
"cpl-core>=2023.4.0"
],
"DevDependencies": [
"cpl-cli>=2023.4.0"
],
"PythonVersion": ">=3.10.4",
"PythonPath": {
"linux": ""
},
"Classifiers": []
},
"BuildSettings": {
"ProjectType": "unittest",
"SourcePath": "",
"OutputPath": "../../dist",
"Main": "unittests_reactive_extenstions.main",
"EntryPoint": "unittests_reactive_extenstions",
"IncludePackageData": false,
"Included": [],
"Excluded": [
"*/__pycache__",
"*/logs",
"*/tests"
],
"PackageData": {},
"ProjectReferences": []
}
}