Commits

Anonymous committed a271c05

add the literal and RegEx literal test cases

Comments (0)

Files changed (3)

             }
         """))
 
-    def testPrettyPrint(self):
-        pp = PrettyPrint()
+    def testLiterals(self):
+        class LiteralChecker(TestAST.Checker):
+            def onExpressionStatement(self, stmt):
+                stmt.expression.visit(self)
 
-        with JSContext() as ctxt:
-            script = JSEngine().compile("function hello(name) { return 'hello ' + name; }")
-            script.visit(pp)
+            def onLiteral(self, litr):
+                self.called += 1
 
-        self.assertEquals("", str(pp))
+                self.assert_(litr.isTrivial)
+                self.assertFalse(litr.isPropertyName)
+                self.assertFalse(litr.isNull)
+                self.assertFalse(litr.isTrue)
+                self.assertTrue(litr.isFalse)
+
+            def onRegExpLiteral(self, litr):
+                self.called += 1
+
+                self.assertEquals("test", litr.pattern)
+                self.assertEquals("g", litr.flags)
+
+        self.assertEquals(2, LiteralChecker(self).test("false; /test/g"))
 
 if __name__ == '__main__':
     if "-v" in sys.argv:
     ;
 
   py::class_<CAstLiteral, py::bases<CAstExpression> >("AstLiteral", py::no_init)
+    .add_property("isTrivial", &CAstLiteral::IsTrivial)
+    .add_property("isPropertyName", &CAstLiteral::IsPropertyName)
+    .add_property("isNull", &CAstLiteral::IsNull)
+    .add_property("isTrue", &CAstLiteral::IsTrue)
+    .add_property("isFalse", &CAstLiteral::IsFalse)
     ;
 
   py::class_<CAstMaterializedLiteral, py::bases<CAstExpression> >("AstMaterializedLiteral", py::no_init)
+    .add_property("index", &CAstMaterializedLiteral::GetIndex)
+    .add_property("isSimple", &CAstMaterializedLiteral::IsSimple)
+    .add_property("depth", &CAstMaterializedLiteral::GetDepth)
     ;
 
   py::class_<CAstObjectLiteral, py::bases<CAstMaterializedLiteral> >("AstObjectLiteral", py::no_init)
     ;
 
   py::class_<CAstRegExpLiteral, py::bases<CAstMaterializedLiteral> >("AstRegExpLiteral", py::no_init)
+    .add_property("pattern", &CAstRegExpLiteral::GetPattern)
+    .add_property("flags", &CAstRegExpLiteral::GetFlags)
     ;
 
   py::class_<CAstArrayLiteral, py::bases<CAstMaterializedLiteral> >("AstArrayLiteral", py::no_init)
   return py::str(buf.start(), buf.length());
 }
 
+inline const std::string to_string(v8i::Handle<v8i::String> str)
+{ 
+  if (str.is_null()) return std::string();
+
+  v8i::Vector<const char> buf = str->ToAsciiVector();
+
+  return std::string(buf.start(), buf.length());
+}
+
 class CAstVisitor;
 class CAstVariable;
 class CAstVariableProxy;
 {
 public:
   CAstLiteral(v8i::Literal *lit) : CAstExpression(lit) {}
+
+  bool IsTrivial(void) const { return as<v8i::Literal>()->IsTrivial(); }
+  bool IsPropertyName(void) const { return as<v8i::Literal>()->IsPropertyName(); }
+  bool IsNull(void) const { return as<v8i::Literal>()->IsNull(); }
+  bool IsTrue(void) const { return as<v8i::Literal>()->IsTrue(); }
+  bool IsFalse(void) const { return as<v8i::Literal>()->IsFalse(); }
 };
 
 class CAstMaterializedLiteral : public CAstExpression
 {
 protected:
   CAstMaterializedLiteral(v8i::MaterializedLiteral *lit) : CAstExpression(lit) {}
+public:
+  int GetIndex(void) const { return as<v8i::MaterializedLiteral>()->literal_index(); }
+  bool IsSimple(void) const { return as<v8i::MaterializedLiteral>()->is_simple(); }
+  int GetDepth(void) const { return as<v8i::MaterializedLiteral>()->depth(); }
 };
 
 class CAstObjectLiteral : public CAstMaterializedLiteral
 {
 public:
   CAstRegExpLiteral(v8i::RegExpLiteral *lit) : CAstMaterializedLiteral(lit) {}
+
+  const std::string GetPattern(void) const { return to_string(as<v8i::RegExpLiteral>()->pattern()); }
+  const std::string GetFlags(void) const { return to_string(as<v8i::RegExpLiteral>()->flags()); }
 };
 
 class CAstArrayLiteral : public CAstMaterializedLiteral