Beispiel #1
0
        public void TestSet()
        {
            Parser p = new Parser();

            Ast.SetNode set1 = new Ast.SetNode();
            Ast.SetNode set2 = new Ast.SetNode();
            Assert.AreEqual(set1, set2);

            set1.Elements.Add(new Ast.IntegerNode(42));
            set2.Elements.Add(new Ast.IntegerNode(99));
            Assert.AreNotEqual(set1, set2);
            set2.Elements.Clear();
            set2.Elements.Add(new Ast.IntegerNode(42));
            Assert.AreEqual(set1, set2);

            set2.Elements.Add(new Ast.IntegerNode(43));
            set2.Elements.Add(new Ast.IntegerNode(44));
            Assert.AreNotEqual(set1, set2);

            Assert.AreEqual(set1, p.Parse("{42}").Root);
            Assert.Throws <ParseException>(() => p.Parse("{42,}"));
            Assert.AreEqual(set2, p.Parse("{ 42,43, 44 }").Root);

            Assert.Throws <ParseException>(() => p.Parse("{42,43]"));
            Assert.Throws <ParseException>(() => p.Parse("{42,43}@"));

            set1 = p.Parse("{'first','second','third','fourth','fifth','second', 'first', 'third', 'third' }").Root as Ast.SetNode;
            Assert.AreEqual("'first'", set1.Elements[0].ToString());
            Assert.AreEqual("'second'", set1.Elements[1].ToString());
            Assert.AreEqual("'third'", set1.Elements[2].ToString());
            Assert.AreEqual("'fourth'", set1.Elements[3].ToString());
            Assert.AreEqual("'fifth'", set1.Elements[4].ToString());
            Assert.AreEqual(5, set1.Elements.Count);
        }
Beispiel #2
0
        public void TestTrailingCommas()
        {
            Parser p      = new Parser();
            var    result = p.Parse("[1,2,3,  ]").Root;

            result = p.Parse("[1,2,3  ,  ]").Root;
            result = p.Parse("[1,2,3,]").Root;
            Assert.Equal("[1,2,3]", result.ToString());
            result = p.Parse("(1,2,3,  )").Root;
            result = p.Parse("(1,2,3  ,  )").Root;
            result = p.Parse("(1,2,3,)").Root;
            Assert.Equal("(1,2,3)", result.ToString());

            // for dict and set the asserts are a bit more complex
            // we cannot simply convert to string because the order of elts is undefined.

            result = p.Parse("{'a':1, 'b':2, 'c':3,  }").Root;
            result = p.Parse("{'a':1, 'b':2, 'c':3  ,  }").Root;
            result = p.Parse("{'a':1, 'b':2, 'c':3,}").Root;
            Ast.DictNode dict  = (Ast.DictNode)result;
            var          items = dict.ElementsAsSet();

            Assert.Contains(new Ast.KeyValueNode(new Ast.StringNode("a"), new Ast.IntegerNode(1)), items);
            Assert.Contains(new Ast.KeyValueNode(new Ast.StringNode("b"), new Ast.IntegerNode(2)), items);
            Assert.Contains(new Ast.KeyValueNode(new Ast.StringNode("c"), new Ast.IntegerNode(3)), items);
            result = p.Parse("{1,2,3,  }").Root;
            result = p.Parse("{1,2,3  ,  }").Root;
            result = p.Parse("{1,2,3,}").Root;
            Ast.SetNode set = (Ast.SetNode)result;
            items = set.ElementsAsSet();
            Assert.Contains(new Ast.IntegerNode(1), items);
            Assert.Contains(new Ast.IntegerNode(2), items);
            Assert.Contains(new Ast.IntegerNode(3), items);
            Assert.DoesNotContain(new Ast.IntegerNode(4), items);
        }
Beispiel #3
0
        public void TestSetEquality()
        {
            Ast.SetNode set1 = new Ast.SetNode();
            set1.Elements.Add(new Ast.IntegerNode(1));
            set1.Elements.Add(new Ast.IntegerNode(2));
            set1.Elements.Add(new Ast.IntegerNode(3));

            Ast.SetNode set2 = new Ast.SetNode();
            set2.Elements.Add(new Ast.IntegerNode(2));
            set2.Elements.Add(new Ast.IntegerNode(3));
            set2.Elements.Add(new Ast.IntegerNode(1));

            Assert.AreEqual(set1, set2);
            set2.Elements.Add(new Ast.IntegerNode(0));
            Assert.AreNotEqual(set1, set2);
        }
Beispiel #4
0
        public void TestEquality()
        {
            Ast.INode n1, n2;

            n1 = new Ast.IntegerNode(42);
            n2 = new Ast.IntegerNode(42);
            Assert.AreEqual(n1, n2);
            n2 = new Ast.IntegerNode(43);
            Assert.AreNotEqual(n1, n2);

            n1 = new Ast.StringNode("foo");
            n2 = new Ast.StringNode("foo");
            Assert.AreEqual(n1, n2);
            n2 = new Ast.StringNode("bar");
            Assert.AreNotEqual(n1, n2);

            n1 = new Ast.ComplexNumberNode()
            {
                Real      = 1.1,
                Imaginary = 2.2
            };
            n2 = new Ast.ComplexNumberNode()
            {
                Real      = 1.1,
                Imaginary = 2.2
            };
            Assert.AreEqual(n1, n2);
            n2 = new Ast.ComplexNumberNode()
            {
                Real      = 1.1,
                Imaginary = 3.3
            };
            Assert.AreNotEqual(n1, n2);

            n1 = new Ast.KeyValueNode()
            {
                Key   = new Ast.IntegerNode(42),
                Value = new Ast.IntegerNode(42)
            };
            n2 = new Ast.KeyValueNode()
            {
                Key   = new Ast.IntegerNode(42),
                Value = new Ast.IntegerNode(42)
            };
            Assert.AreEqual(n1, n2);
            n1 = new Ast.KeyValueNode()
            {
                Key   = new Ast.IntegerNode(43),
                Value = new Ast.IntegerNode(43)
            };
            Assert.AreNotEqual(n1, n2);

            n1 = Ast.NoneNode.Instance;
            n2 = Ast.NoneNode.Instance;
            Assert.AreEqual(n1, n2);
            n2 = new Ast.IntegerNode(42);
            Assert.AreNotEqual(n1, n2);

            n1 = new Ast.DictNode()
            {
                Elements = new List <Ast.INode>()
                {
                    new Ast.KeyValueNode()
                    {
                        Key   = new Ast.IntegerNode(42),
                        Value = new Ast.IntegerNode(42)
                    }
                }
            };
            n2 = new Ast.DictNode()
            {
                Elements = new List <Ast.INode>()
                {
                    new Ast.KeyValueNode()
                    {
                        Key   = new Ast.IntegerNode(42),
                        Value = new Ast.IntegerNode(42)
                    }
                }
            };
            Assert.AreEqual(n1, n2);
            n2 = new Ast.DictNode()
            {
                Elements = new List <Ast.INode>()
                {
                    new Ast.KeyValueNode()
                    {
                        Key   = new Ast.IntegerNode(42),
                        Value = new Ast.IntegerNode(43)
                    }
                }
            };
            Assert.AreNotEqual(n1, n2);

            n1 = new Ast.ListNode()
            {
                Elements = new List <Ast.INode>()
                {
                    new Ast.IntegerNode(42)
                }
            };
            n2 = new Ast.ListNode()
            {
                Elements = new List <Ast.INode>()
                {
                    new Ast.IntegerNode(42)
                }
            };
            Assert.AreEqual(n1, n2);
            n2 = new Ast.ListNode()
            {
                Elements = new List <Ast.INode>()
                {
                    new Ast.IntegerNode(43)
                }
            };
            Assert.AreNotEqual(n1, n2);

            n1 = new Ast.SetNode()
            {
                Elements = new List <Ast.INode>()
                {
                    new Ast.IntegerNode(42)
                }
            };
            n2 = new Ast.SetNode()
            {
                Elements = new List <Ast.INode>()
                {
                    new Ast.IntegerNode(42)
                }
            };
            Assert.AreEqual(n1, n2);
            n2 = new Ast.SetNode()
            {
                Elements = new List <Ast.INode>()
                {
                    new Ast.IntegerNode(43)
                }
            };
            Assert.AreNotEqual(n1, n2);

            n1 = new Ast.TupleNode()
            {
                Elements = new List <Ast.INode>()
                {
                    new Ast.IntegerNode(42)
                }
            };
            n2 = new Ast.TupleNode()
            {
                Elements = new List <Ast.INode>()
                {
                    new Ast.IntegerNode(42)
                }
            };
            Assert.AreEqual(n1, n2);
            n2 = new Ast.TupleNode()
            {
                Elements = new List <Ast.INode>()
                {
                    new Ast.IntegerNode(43)
                }
            };
            Assert.AreNotEqual(n1, n2);
        }
Beispiel #5
0
        public void TestSetEquality()
        {
            Ast.SetNode set1 = new Ast.SetNode();
            set1.Elements.Add(new Ast.IntegerNode(1));
            set1.Elements.Add(new Ast.IntegerNode(2));
            set1.Elements.Add(new Ast.IntegerNode(3));

            Ast.SetNode set2 = new Ast.SetNode();
            set2.Elements.Add(new Ast.IntegerNode(2));
            set2.Elements.Add(new Ast.IntegerNode(3));
            set2.Elements.Add(new Ast.IntegerNode(1));

            Assert.AreEqual(set1, set2);
            set2.Elements.Add(new Ast.IntegerNode(0));
            Assert.AreNotEqual(set1, set2);
        }
Beispiel #6
0
        public void TestSet()
        {
            Parser p = new Parser();
            Ast.SetNode set1 = new Ast.SetNode();
            Ast.SetNode set2 = new Ast.SetNode();
            Assert.AreEqual(set1, set2);

            set1.Elements.Add(new Ast.IntegerNode(42));
            set2.Elements.Add(new Ast.IntegerNode(99));
            Assert.AreNotEqual(set1, set2);
            set2.Elements.Clear();
            set2.Elements.Add(new Ast.IntegerNode(42));
            Assert.AreEqual(set1, set2);

            set2.Elements.Add(new Ast.IntegerNode(43));
            set2.Elements.Add(new Ast.IntegerNode(44));
            Assert.AreNotEqual(set1, set2);

            Assert.AreEqual(set1, p.Parse("{42}").Root);
            Assert.AreEqual(set2, p.Parse("{ 42,43, 44 }").Root);

            Assert.Throws<ParseException>(()=>p.Parse("{42,43]"));
            Assert.Throws<ParseException>(()=>p.Parse("{42,43}@"));

            set1 = p.Parse("{'first','second','third','fourth','fifth','second', 'first', 'third', 'third' }").Root as Ast.SetNode;
            Assert.AreEqual("'first'", set1.Elements[0].ToString());
            Assert.AreEqual("'second'", set1.Elements[1].ToString());
            Assert.AreEqual("'third'", set1.Elements[2].ToString());
            Assert.AreEqual("'fourth'", set1.Elements[3].ToString());
            Assert.AreEqual("'fifth'", set1.Elements[4].ToString());
            Assert.AreEqual(5, set1.Elements.Count);
        }
Beispiel #7
0
        public void TestEquality()
        {
            Ast.INode n1, n2;

            n1 = new Ast.IntegerNode(42);
            n2 = new Ast.IntegerNode(42);
            Assert.AreEqual(n1, n2);
            n2 = new Ast.IntegerNode(43);
            Assert.AreNotEqual(n1, n2);

            n1 = new Ast.StringNode("foo");
            n2 = new Ast.StringNode("foo");
            Assert.AreEqual(n1, n2);
            n2 = new Ast.StringNode("bar");
            Assert.AreNotEqual(n1, n2);

            n1 = new Ast.ComplexNumberNode() {
                Real=1.1,
                Imaginary=2.2
            };
            n2 = new Ast.ComplexNumberNode() {
                Real=1.1,
                Imaginary=2.2
            };
            Assert.AreEqual(n1, n2);
            n2 = new Ast.ComplexNumberNode() {
                Real=1.1,
                Imaginary=3.3
            };
            Assert.AreNotEqual(n1, n2);

            n1=new Ast.KeyValueNode() {
                Key=new Ast.IntegerNode(42),
                Value=new Ast.IntegerNode(42)
            };
            n2=new Ast.KeyValueNode() {
                Key=new Ast.IntegerNode(42),
                Value=new Ast.IntegerNode(42)
            };
            Assert.AreEqual(n1, n2);
            n1=new Ast.KeyValueNode() {
                Key=new Ast.IntegerNode(43),
                Value=new Ast.IntegerNode(43)
            };
            Assert.AreNotEqual(n1,n2);

            n1=Ast.NoneNode.Instance;
            n2=Ast.NoneNode.Instance;
            Assert.AreEqual(n1, n2);
            n2=new Ast.IntegerNode(42);
            Assert.AreNotEqual(n1, n2);

            n1=new Ast.DictNode() {
                Elements=new List<Ast.INode>() {
                    new Ast.KeyValueNode() {
                        Key=new Ast.IntegerNode(42),
                        Value=new Ast.IntegerNode(42)
                    }
                }
            };
            n2=new Ast.DictNode() {
                Elements=new List<Ast.INode>() {
                    new Ast.KeyValueNode() {
                        Key=new Ast.IntegerNode(42),
                        Value=new Ast.IntegerNode(42)
                    }
                }
            };
            Assert.AreEqual(n1, n2);
            n2=new Ast.DictNode() {
                Elements=new List<Ast.INode>() {
                    new Ast.KeyValueNode() {
                        Key=new Ast.IntegerNode(42),
                        Value=new Ast.IntegerNode(43)
                    }
                }
            };
            Assert.AreNotEqual(n1, n2);

            n1=new Ast.ListNode() {
                Elements=new List<Ast.INode>() {
                    new Ast.IntegerNode(42)
                }
            };
            n2=new Ast.ListNode() {
                Elements=new List<Ast.INode>() {
                    new Ast.IntegerNode(42)
                }
            };
            Assert.AreEqual(n1,n2);
            n2=new Ast.ListNode() {
                Elements=new List<Ast.INode>() {
                    new Ast.IntegerNode(43)
                }
            };
            Assert.AreNotEqual(n1,n2);

            n1=new Ast.SetNode() {
                Elements=new List<Ast.INode>() {
                    new Ast.IntegerNode(42)
                }
            };
            n2=new Ast.SetNode() {
                Elements=new List<Ast.INode>() {
                    new Ast.IntegerNode(42)
                }
            };
            Assert.AreEqual(n1,n2);
            n2=new Ast.SetNode() {
                Elements=new List<Ast.INode>() {
                    new Ast.IntegerNode(43)
                }
            };
            Assert.AreNotEqual(n1,n2);

            n1=new Ast.TupleNode() {
                Elements=new List<Ast.INode>() {
                    new Ast.IntegerNode(42)
                }
            };
            n2=new Ast.TupleNode() {
                Elements=new List<Ast.INode>() {
                    new Ast.IntegerNode(42)
                }
            };
            Assert.AreEqual(n1,n2);
            n2=new Ast.TupleNode() {
                Elements=new List<Ast.INode>() {
                    new Ast.IntegerNode(43)
                }
            };
            Assert.AreNotEqual(n1,n2);
        }