diff --git a/src/test_writer.py b/src/test_writer.py new file mode 100644 index 0000000..37e2b5e --- /dev/null +++ b/src/test_writer.py @@ -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() diff --git a/src/writer.py b/src/writer.py index 19e2fb4..eed945a 100644 --- a/src/writer.py +++ b/src/writer.py @@ -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: