1import unittest
2import sys
3
4from curtsies.window import BaseWindow, FullscreenWindow, CursorAwareWindow
5from io import StringIO
6from unittest import skipIf
7
8
9fds_closed = not sys.stdin.isatty() or not sys.stdout.isatty()
10
11
12class FakeFullscreenWindow(FullscreenWindow):
13    width = property(lambda self: 10)
14    height = property(lambda self: 4)
15
16
17@skipIf(fds_closed, "blessings Terminal needs streams open")
18class TestBaseWindow(unittest.TestCase):
19    """Pretty pathetic tests for window"""
20
21    def test_window(self):
22        fakestdout = StringIO()
23        window = BaseWindow(fakestdout)
24        window.write("hi")
25        fakestdout.seek(0)
26        self.assertEqual(fakestdout.read(), "hi")
27
28    def test_array_from_text(self):
29        window = BaseWindow()
30        a = window.array_from_text(".\n.\n.")
31        self.assertEqual(a.height, 3)
32        self.assertEqual(a[0], ".")
33        self.assertEqual(a[1], ".")
34
35    def test_array_from_text_rc(self):
36        a = BaseWindow.array_from_text_rc("asdfe\nzx\n\n123", 3, 4)
37        self.assertEqual(a.height, 3)
38        self.assertEqual(a.width, 4)
39        self.assertEqual(a[0], "asdf")
40        self.assertEqual(a[1], "e")
41        self.assertEqual(a[2], "zx")
42
43    def test_fullscreen_window(self):
44        fakestdout = StringIO()
45        window = FullscreenWindow(fakestdout)
46        window.write("hi")
47        fakestdout.seek(0)
48        self.assertEqual(fakestdout.read(), "hi")
49
50    def test_fullscreen_render_to_terminal(self):
51        fakestdout = StringIO()
52        window = FakeFullscreenWindow(fakestdout)
53        window.render_to_terminal(["hello", "hello", "hello"])
54        fakestdout.seek(0)
55        output = fakestdout.read()
56        self.assertEqual(output.count("hello"), 3)
57