#!/usr/bin/python3

from newfol.database import Database
from newfol.main import KeyboardShortcuts
import tempfile
import unittest


class TestKeyboardShortcuts(unittest.TestCase):
    def create_temp_db(self, keys):
        ddir = tempfile.TemporaryDirectory()
        with open(ddir.name + "/schema", "w") as fp:
            fp.write("fmt:0:newfol schema file:\ntxn:git\n")
            for k in keys:
                fp.write("ksh:%s\n" % ':'.join(k))
        db = Database.load(ddir.name)
        return (ddir, db)

    def test_default_keys(self):
        ddir, db = self.create_temp_db([])
        ks = KeyboardShortcuts(db.schema().keys())
        self.assertEqual(ks['ctrl x'], 'about')
        with self.assertRaises(KeyError):
            ks['ctrl p']
        ddir.cleanup()

    def test_override_keys(self):
        ddir, db = self.create_temp_db([['ctrl x', 'quit']])
        ks = KeyboardShortcuts(db.schema().keys())
        self.assertEqual(ks['ctrl x'], 'quit')
        ddir.cleanup()

    def test_new_keys(self):
        ddir, db = self.create_temp_db([['ctrl p', 'quit']])
        ks = KeyboardShortcuts(db.schema().keys())
        self.assertEqual(ks['ctrl p'], 'quit')
        ddir.cleanup()

if __name__ == '__main__':
    unittest.main()
