Example #1
0
 def test_can_parse_properly(self):
     cparser = ClassParser()
     for case in TestClassParser.TEST_CASES:
         try:
             cparser.parse(case)
         except Exception as e:
             self.fail("Doesn't parse case:\n%s\n%s"%(case,e.message))
Example #2
0
 def test_raises_exception_bad_field(self):
     cparser = ClassParser()
     for case in TestClassParser.BAD_CASES:
         try:
             cparser.parse(case)
             self.fail("Shouldn't parse case: {0}".format(case))
         except ParseException:
             pass
Example #3
0
 def _deserialize(self):
     doc = yaml.load(self.fd)
     pp = PackageParser()
     cp = ClassParser()
     self.fd.close()
     if doc is None or len(doc.keys()) != 2:
         raise FileException(self.filename,"Script must contain one package declaration and one class declaration.")
     for key in doc.keys():
         try:
             item = pp.parse(key)
         except ParseException as pe:
             self.collect_errors(pe)
             continue
         if type(item) is Class:
             class_body = doc[key]
             if class_body is not None:
                 for decl in class_body.keys():
                     try:
                         decl_obj = cp.parse(decl)
                     except ParseException as pe:
                         self.collect_errors(pe)
                         continue
                     if decl_obj["type"] == "constructor":
                         item.constructor_params = decl_obj["params"]
                         if decl_obj.has_key("super"):
                             item.super_params = decl_obj["super"]
                         item.constructor_body = class_body[decl]
                     elif decl_obj["type"] == "function":
                         PATTERN = "function(%s){%s}";
                         getattr(item,decl_obj["visibility"])[decl_obj["name"]] = PATTERN%(','.join(decl_obj["params"]),class_body[decl])
                     elif decl_obj["type"] == "var":
                         getattr(item,decl_obj["visibility"])[decl_obj["name"]] = class_body[decl]
             if len(self.errors) > 0:
                 self.display_errors()
                 sys.exit(1)
             else:
                 if self.class_ is not None:
                     raise FileException(self.filename,"Script has already declared a class named <{0}>.".format(self.class_.name))
                 self.class_ = item
         else:
             self.package = tuple(doc["package"].split("."))