def test_SyntaxFactory_index_definitions(): from editxt.valuetrans import SyntaxDefTransformer class FakeDef(object): def __init__(self, name): self.name = name def __repr__(self): return "<%s %x>" % (self.name, id(self)) text1 = FakeDef("Plain Text") text2 = FakeDef("Plain Text") python = FakeDef("Python") sf = SyntaxFactory() sf.registry = { "*.txt": text1, "*.text": text2, "*.txtx": text1, "*.py": python, } defs = sorted([text1, text2, python], key=lambda d:(d.name, id(d))) m = Mocker() vt = m.replace(NSValueTransformer, passthrough=False) st = vt.valueTransformerForName_("SyntaxDefTransformer") >> \ m.mock(SyntaxDefTransformer) st.update_definitions(defs) with m: sf.index_definitions() eq_(sf.definitions, defs)
def test_SyntaxFactory_index_definitions(): from editxt.valuetrans import SyntaxDefTransformer class FakeDef(object): def __init__(self, name): self.name = name def __repr__(self): return "<%s %x>" % (self.name, id(self)) text1 = FakeDef("Plain Text") text2 = FakeDef("Plain Text") python = FakeDef("Python") sf = SyntaxFactory() sf.registry = { "*.txt": text1, "*.text": text2, "*.txtx": text1, "*.py": python, } defs = sorted([text1, text2, python], key=lambda d:(d.name, id(d))) m = Mocker() vt = m.replace(mod, 'NSValueTransformer') st = vt.valueTransformerForName_("SyntaxDefTransformer") >> \ m.mock(SyntaxDefTransformer) st.update_definitions(defs) with m: sf.index_definitions() eq_(sf.definitions, defs)
def test(c): m = Mocker() sf = SyntaxFactory() log = m.replace("editxt.syntax.log", passthrough=False) glob = m.replace("glob.glob", passthrough=False) exists = m.replace("os.path.exists", passthrough=False) load = sf.load_definition = m.mock() if c.path and exists(c.path) >> c.exists: info = { "disabled": dict(name="dis", disabled=True, filepatterns=[]), # should cause info log # these should cause error log "incomplete1": {}, "incomplete2": dict(name="none"), "incomplete3": dict(filepatterns=[]), # should only register twice despite duplication "python": dict(name="python", filepatterns=["*.py", "*.py", "*.pyw"]), "sql": dict(name="sequel", filepatterns=["*.sql"]), "text": dict(name="text", filepatterns=["*.txt"]), "text2": dict(name="text", filepatterns=["*.txt", "*.text"]), "text3": dict(name="text", disabled=True, filepatterns=["*.txt", "*.text"]), } glob(os.path.join(c.path, "*" + const.SYNTAX_DEF_EXTENSION)) >> sorted(info) def do(name): data = dict(info[name]) if name.startswith("incomplete"): raise Exception("incomplete definition: %r" % (data,)) data.setdefault("disabled", False) if "filepatterns" in data: data["filepatterns"] = set(data["filepatterns"]) return type("sdef", (object,), data)() expect(load(ANY)).count(len(info)).call(do) for fname, data in sorted(info.iteritems()): if fname.startswith("incomplete"): log.error(ANY, fname, exc_info=True) else: pats = ", ".join(sorted(set(data.get("filepatterns", [])))) stat = [data["name"], "[%s]" % pats] if fname in ("text", "text2", "text4"): stat.extend(["overrides", "*.txt"]) elif fname in ("disabled", "text3"): stat.append("DISABLED") stat.append(fname) log.info(ANY, " ".join(stat)) patterns = set(["*.py", "*.pyw", "*.sql", "*.text", "*.txt"]) else: patterns = set(["*.txt"]) with m: sf.load_definitions(c.path) eq_(set(sf.registry), patterns)
def test(c): m = Mocker() sf = SyntaxFactory() log = m.replace("editxt.syntax.log") glob = m.replace("glob.glob") exists = m.replace("os.path.exists") load = sf.load_definition = m.mock() if c.path and exists(c.path) >> c.exists: info = { "disabled": dict(name="dis", disabled=True, filepatterns=[]), # should cause info log # these should cause error log "incomplete1": {}, "incomplete2": dict(name="none"), "incomplete3": dict(filepatterns=[]), # should only register twice despite duplication "python": dict(name="python", filepatterns=["*.py", "*.py", "*.pyw"]), "sql": dict(name="sequel", filepatterns=["*.sql"]), "text": dict(name="text", filepatterns=["*.txt"]), "text2": dict(name="text", filepatterns=["*.txt", "*.text"]), "text3": dict(name="text", disabled=True, filepatterns=["*.txt", "*.text"]), } glob(os.path.join(c.path, "*" + const.SYNTAX_DEF_EXTENSION)) >> sorted(info) def do(name): data = dict(info[name]) if name.startswith("incomplete"): raise Exception("incomplete definition: %r" % (data,)) data.setdefault("disabled", False) if "filepatterns" in data: data["filepatterns"] = set(data["filepatterns"]) return type("sdef", (object,), data)() expect(load(ANY)).count(len(info)).call(do) for fname, data in sorted(info.items()): if fname.startswith("incomplete"): log.error(ANY, fname, exc_info=True) else: pats = ", ".join(sorted(set(data.get("filepatterns", [])))) stat = [data["name"], "[%s]" % pats] if fname in ("text", "text2", "text4"): stat.extend(["overrides", "*.txt"]) elif fname in ("disabled", "text3"): stat.append("DISABLED") stat.append(fname) log.info(ANY, " ".join(stat)) patterns = set(["*.py", "*.pyw", "*.sql", "*.text", "*.txt"]) else: patterns = set(["*.txt"]) with m: sf.load_definitions(c.path) eq_(set(sf.registry), patterns)
def test_init_syntax_definitions(): import editxt.syntax as syntax m = Mocker() app = Application(profile='/editxtdev') rsrc_path = m.method(app.resource_path)() >> "/tmp/resources" SyntaxFactory = m.replace(syntax, 'SyntaxFactory', spec=False) sf = SyntaxFactory() >> m.mock(syntax.SyntaxFactory) app_log = m.replace("editxt.application.log") for path, info in [(rsrc_path, False), ('/editxtdev', True)]: sf.load_definitions(join(path, const.SYNTAX_DEFS_DIR), info) sf.index_definitions() with m: app.init_syntax_definitions()
def get_syntax_definition(name, cache={}): if isinstance(name, NoHighlight): return name try: return cache[name].get(name) except KeyError: pass from os.path import abspath, dirname, join root = dirname(dirname(dirname(abspath(__file__)))) loader = SyntaxFactory() loader.load_definitions(join(root, "resources/syntax"), False) cache[name] = loader return loader.get(name)
def test(c): with tempdir() as tmp: sf = SyntaxFactory() filename = os.path.join(tmp, "file") with open(filename, "w", encoding="utf-8") as fh: if c.error is not Exception: fh.write("\n".join("{} = {!r}".format(k, v) for k, v in c.info.items())) else: fh.write("raise {}".format(c.error.__name__)) values = dict(defaults) values.update(c.info) if c.error is None: res = sf.load_definition(filename) for name, value in values.items(): eq_(getattr(res, name), value, name) else: assert_raises(c.error, sf.load_definition, filename)
def init_syntax_definitions(self): from editxt.syntax import SyntaxFactory self.syntax_factory = sf = SyntaxFactory() paths = [(self.resource_path(), False), (self.profile_path, True)] for path, log_info in paths: path = os.path.join(path, const.SYNTAX_DEFS_DIR) sf.load_definitions(path, log_info) sf.index_definitions()
def test(c): with tempdir() as tmp: sf = SyntaxFactory() filename = os.path.join(tmp, "file") with open(filename, "w", encoding="utf-8") as fh: if c.error is not Exception: fh.write("\n".join("{} = {!r}".format(k, v) for k, v in c.info.items())) else: fh.write("raise {}".format(c.error.__name__)) values = dict(defaults) values.update(c.info) if c.error is None: res = sf.load_definition(filename) for name, value in values.items(): if name == "filepatterns": value = set(value) eq_(getattr(res, name), value, name) else: assert_raises(c.error, sf.load_definition, filename)
def test(c): m = Mocker() sf = SyntaxFactory() execf = m.replace(execfile, passthrough=False) def do(filename, ns): ns.update(__builtins__="__builtins__") return ns.update(c.info) if c.error is not Exception: expect(execf("<filename>", ANY)).call(do) else: expect(execf("<filename>", ANY)).throw(c.error) values = dict(defaults) values.update(c.info) with m: if c.error is None: res = sf.load_definition("<filename>") for name, value in values.iteritems(): if name == "filepatterns": value = set(value) eq_(getattr(res, name), value, name) else: assert_raises(c.error, sf.load_definition, "<filename>")
def test_init_syntax_definitions(): import editxt.syntax as syntax m = Mocker() app = Application(profile='/editxtdev') rsrc_path = m.method(app.resource_path)() >> "/tmp/resources" SyntaxFactory = m.replace(syntax, 'SyntaxFactory', spec=False) sf = SyntaxFactory() >> m.mock(syntax.SyntaxFactory) app_log = m.replace("editxt.application.log") for path, info in [(rsrc_path, False), ('/editxtdev', True)]: sf.load_definitions(os.path.join(path, const.SYNTAX_DEFS_DIR), info) sf.index_definitions() with m: app.init_syntax_definitions()
def test_SyntaxFactory_get_definition(): sf = SyntaxFactory() sf.registry["*.txt"] = "<syntax def>" eq_(sf.get_definition("somefile.txt"), "<syntax def>") eq_(sf.get_definition("somefile.text"), PLAIN_TEXT)
def test_SyntaxFactory_init(): sf = SyntaxFactory() eq_(sf.registry, {"*.txt": PLAIN_TEXT}) eq_(sf.definitions, [PLAIN_TEXT])