test line chooser sticky

master
Bel LaPointe 2022-09-20 15:39:43 -06:00
parent ac7b583f76
commit c6b648195f
2 changed files with 38 additions and 2 deletions

36
src/test_writer.py Normal file
View File

@ -0,0 +1,36 @@
import unittest
import writer
class TestLineChooser(unittest.TestCase):
def test_latest_sticky(self):
stale_something = self.new_line(False, 1, 1)
stale_nothing = self.new_line(False, 1, None)
old_something = self.new_line(True, 2, 2)
old_nothing = self.new_line(True, 2, None)
new_something = self.new_line(True, 3, 3)
new_nothing = self.new_line(True, 3, None)
chooser = writer.LineChooserLatestSticky()
for name, c in ({
"nothing over stale": [new_nothing, stale_something],
"slightly old over nothing": [old_something, new_nothing],
"new and nonzero": [new_something, old_something],
"new over nothing": [new_something, old_nothing],
"new over stale nothing": [new_something, stale_nothing],
}).items():
self.assertEqual(
c[0],
chooser.choose(c[0], c[1]),
name,
)
def new_line(self, is_recent, t, v):
result = writer.Line(v)
result.t = t
result.is_recent = lambda *args: is_recent
return result
if __name__ == "__main__":
unittest.main()

View File

@ -78,10 +78,10 @@ class Line:
return not self.v
def is_recent(self):
return time.time() - self.t < 1
return time.time() - self.t < 5
class LineChooser:
def choose(a, b):
def choose(self, a, b):
if not a:
return b
if not b: