Пример #1
0
        public void TestObjectifyDictToClass()
        {
            Parser p = new Parser();

            byte[] ser = File.ReadAllBytes("testserpent.utf8.bin");
            Ast    ast = p.Parse(ser);

            var visitor = new ObjectifyVisitor(ZerodivisionFromDict);

            ast.Accept(visitor);
            object thing = visitor.GetObject();

            IDictionary dict = (IDictionary)thing;

            Assert.AreEqual(11, dict.Count);
            DivideByZeroException ex = (DivideByZeroException)dict["exc"];

            Assert.AreEqual("fault", ex.Message);

            thing = ast.GetData(ZerodivisionFromDict);
            dict  = (IDictionary)thing;
            Assert.AreEqual(11, dict.Count);
            ex = (DivideByZeroException)dict["exc"];
            Assert.AreEqual("fault", ex.Message);
        }
Пример #2
0
        public void TestObjectify()
        {
            Parser p = new Parser();

            byte[] ser     = File.ReadAllBytes("testserpent.utf8.bin");
            Ast    ast     = p.Parse(ser);
            var    visitor = new ObjectifyVisitor();

            ast.Accept(visitor);
            object thing = visitor.GetObject();

            IDictionary dict = (IDictionary)thing;

            Assert.AreEqual(11, dict.Count);
            IList <object> list = dict["numbers"] as IList <object>;

            Assert.AreEqual(4, list.Count);
            Assert.AreEqual(999.1234, list[1]);
            Assert.AreEqual(new ComplexNumber(-3, 8), list[3]);
            string euro = dict["unicode"] as string;

            Assert.AreEqual("\u20ac", euro);
            IDictionary exc = (IDictionary)dict["exc"];

            object[] args = (object[])exc["args"];
            Assert.AreEqual("fault", args[0]);
            Assert.AreEqual("ZeroDivisionError", exc["__class__"]);
        }
Пример #3
0
        public void TestComments()
        {
            Parser p = new Parser();

            Ast ast = p.Parse("[ 1, 2 ]");  // no header whatsoever
            var visitor = new ObjectifyVisitor();
            ast.Accept(visitor);
            Object obj = visitor.GetObject();
            Assert.AreEqual(new int[] {1,2}, obj);

            ast = p.Parse(@"# serpent utf-8 python2.7
            [ 1, 2,
               # some comments here
               3, 4]    # more here
            # and here.
            ");
            visitor = new ObjectifyVisitor();
            ast.Accept(visitor);
            obj = visitor.GetObject();
            Assert.AreEqual(new int[] {1,2,3,4}, obj);
        }
Пример #4
0
        public void TestComments()
        {
            Parser p = new Parser();

            Ast ast     = p.Parse("[ 1, 2 ]");          // no header whatsoever
            var visitor = new ObjectifyVisitor();

            ast.Accept(visitor);
            Object obj = visitor.GetObject();

            Assert.AreEqual(new int[] { 1, 2 }, obj);

            ast     = p.Parse(@"# serpent utf-8 python2.7
[ 1, 2,
   # some comments here
   3, 4]    # more here
# and here.
");
            visitor = new ObjectifyVisitor();
            ast.Accept(visitor);
            obj = visitor.GetObject();
            Assert.AreEqual(new int[] { 1, 2, 3, 4 }, obj);
        }
Пример #5
0
        public void TestObjectifyDictToClass()
        {
            Parser p = new Parser();
            byte[] ser=File.ReadAllBytes("testserpent.utf8.bin");
            Ast ast = p.Parse(ser);

            var visitor = new ObjectifyVisitor(ZerodivisionFromDict);
            ast.Accept(visitor);
            object thing = visitor.GetObject();

            IDictionary dict = (IDictionary) thing;
            Assert.AreEqual(11, dict.Count);
            DivideByZeroException ex = (DivideByZeroException) dict["exc"];
            Assert.AreEqual("fault", ex.Message);

            thing = ast.GetData(ZerodivisionFromDict);
            dict = (IDictionary) thing;
            Assert.AreEqual(11, dict.Count);
            ex = (DivideByZeroException) dict["exc"];
            Assert.AreEqual("fault", ex.Message);
        }
Пример #6
0
        public void TestObjectify()
        {
            Parser p = new Parser();
            byte[] ser=File.ReadAllBytes("testserpent.utf8.bin");
            Ast ast = p.Parse(ser);
            var visitor = new ObjectifyVisitor();
            ast.Accept(visitor);
            object thing = visitor.GetObject();

            IDictionary dict = (IDictionary) thing;
            Assert.AreEqual(11, dict.Count);
            IList<object> list = dict["numbers"] as IList<object>;
            Assert.AreEqual(4, list.Count);
            Assert.AreEqual(999.1234, list[1]);
            Assert.AreEqual(new ComplexNumber(-3, 8), list[3]);
            string euro = dict["unicode"] as string;
            Assert.AreEqual("\u20ac", euro);
            IDictionary exc = (IDictionary)dict["exc"];
            object[] args = (object[]) exc["args"];
            Assert.AreEqual("fault", args[0]);
            Assert.AreEqual("ZeroDivisionError", exc["__class__"]);
        }