Example #1
0
        public void CompositeSequenceMultipleVersion()
        {
            var particle = new CompositeParticle(ParticleType.Sequence, 1, 1)
            {
                new ElementParticle(typeof(ParticleTests), 1, 1),
                new ElementParticle(typeof(ParticleTests), 1, 1, version: FileFormatVersions.Office2010),
                new NsAnyParticle(0, 1, 1),
            };

            var built2007 = Assert.IsType <CompositeParticle>(particle.Build(FileFormatVersions.Office2007));

            Assert.NotSame(particle, built2007);

            Assert.Collection(
                built2007.ChildrenParticles,
                p => Assert.Same(p, particle.ChildrenParticles[0]),
                p => Assert.Same(p, particle.ChildrenParticles[2]));

            var built2010 = Assert.IsType <CompositeParticle>(particle.Build(FileFormatVersions.Office2010));

            Assert.NotSame(particle, built2010);

            Assert.Collection(
                built2010.ChildrenParticles,
                p => Assert.Same(p, particle.ChildrenParticles[1]),
                p => Assert.Same(p, particle.ChildrenParticles[2]));
        }
Example #2
0
        private void VisitComposite(CompositeParticle seq)
        {
            var  stack = new Stack <ParticleConstraint>(seq.Reverse());
            byte count = 0;

            while (stack.Count > 0)
            {
                var item = stack.Pop();

                if (item.ParticleType == ParticleType.Group && item is CompositeParticle group)
                {
                    foreach (var child in group)
                    {
                        stack.Push(child);
                    }
                }
                else
                {
                    _values.Push(ParticlePathItem.Create(seq, count));
                    count++;
                    Visit(item);
                    _values.Pop();
                }
            }
        }
        public void TwoElementOneCollectionInSequenceTest()
        {
            var particle = new CompositeParticle(ParticleType.Sequence, 0, 1)
            {
                new ElementParticle(typeof(T1), 0, 1),
                new ElementParticle(typeof(T2), 1, 10),
                new ElementParticle(typeof(T3), 0, 1),
            }.Compile();
            var data = new TestOpenXmlCompositeElement();

            var t1  = new T1();
            var t2a = new T2();
            var t2b = new T2();
            var t3  = new T3();

            Assert.True(particle.Set(data, t3));
            Assert.True(particle.GetCollection <T2>(data).Add(t2a));
            Assert.True(particle.Set(data, t1));
            Assert.True(particle.GetCollection <T2>(data).Add(t2b));

            Assert.Collection(
                data,
                e => Assert.Same(t1, e),
                e => Assert.Same(t2a, e),
                e => Assert.Same(t2b, e),
                e => Assert.Same(t3, e));
        }
Example #4
0
        public void RequireFilter()
        {
            var particle = new CompositeParticle(ParticleType.Sequence, 1, 1, requireFilter: true)
            {
                new CompositeParticle(ParticleType.Group, 1, 1),
                new CompositeParticle(ParticleType.Group, 1, 2, version: FileFormatVersions.Office2010),
            };

            var built2007 = Assert.IsType <CompositeParticle>(particle.Build(FileFormatVersions.Office2007));

            Assert.NotSame(particle, built2007);

            var result2007 = Assert.Single(built2007.ChildrenParticles);

            Assert.Equal(ParticleType.Group, result2007.ParticleType);
            Assert.Equal(1, result2007.MaxOccurs);

            var built2010 = Assert.IsType <CompositeParticle>(particle.Build(FileFormatVersions.Office2010));

            Assert.NotSame(particle, built2010);

            var result2010 = Assert.Single(built2010.ChildrenParticles);

            Assert.Equal(ParticleType.Group, result2010.ParticleType);
            Assert.Equal(2, result2010.MaxOccurs);
        }
        public void ElementInAllList()
        {
            var particle = new CompositeParticle(ParticleType.All, 0, 1)
            {
                new ElementParticle(typeof(T1), 0, 1),
                new ElementParticle(typeof(T2), 0, 1),
            }.Compile();
            var data = new TestOpenXmlCompositeElement();

            var t1a = new T1();
            var t1b = new T1();
            var t1c = new T1();
            var t2  = new T2();

            data.PrependChild(t1a);
            data.PrependChild(t1b);
            data.PrependChild(t2);
            data.PrependChild(t1c);

            Assert.Collection(
                particle.GetCollection <T1>(data),
                e => Assert.Same(t1c, e),
                e => Assert.Same(t1b, e),
                e => Assert.Same(t1a, e));
        }
        public void InvalidElementInSequenceTest()
        {
            var particle = new CompositeParticle(ParticleType.Sequence, 0, 1)
            {
                new ElementParticle(typeof(T1), 0, 1),
            }.Compile();
            var data = new TestOpenXmlCompositeElement();

            Assert.False(particle.Set(data, new T2()));
            Assert.Null(particle.Get <T2>(data));
        }
        public void SequenceInChoice()
        {
            var particle = new CompositeParticle(ParticleType.Choice, 0, 1)
            {
                new ElementParticle(typeof(T1), 0, 1),
                new CompositeParticle(ParticleType.Sequence, 0, 1)
                {
                    new ElementParticle(typeof(T2), 0, 1),
                    new ElementParticle(typeof(T3), 0, 1),
                },
                new ElementParticle(typeof(T4), 0, 1),
            }.Compile();
            var data = new TestOpenXmlCompositeElement();

            var t1 = new T1();
            var t2 = new T2();
            var t3 = new T3();
            var t4 = new T4();

            Assert.True(particle.Set(data, t1));
            Assert.Collection(
                data,
                e => Assert.Same(t1, e));

            Assert.True(particle.Set(data, t2));
            Assert.Collection(
                data,
                e => Assert.Same(t2, e));

            Assert.True(particle.Set(data, t3));
            Assert.Collection(
                data,
                e => Assert.Same(t2, e),
                e => Assert.Same(t3, e));

            Assert.True(particle.Set(data, t4));
            Assert.Collection(
                data,
                e => Assert.Same(t4, e));

            Assert.True(particle.Set(data, t3));
            Assert.Equal(new object[] { t3 }, data.ToArray());
            Assert.Collection(
                data,
                e => Assert.Same(t3, e));

            Assert.True(particle.Set(data, t2));
            Assert.Collection(
                data,
                e => Assert.Same(t2, e),
                e => Assert.Same(t3, e));
        }
        public void ElementCollectionTest()
        {
            var particle = new CompositeParticle(ParticleType.Sequence, 0, 1)
            {
                new ElementParticle(typeof(T1), 0, 10),
            }.Compile();
            var data     = new TestOpenXmlCompositeElement();
            var instance = new T1();

            Assert.True(particle.GetCollection <T1>(data).Add(instance));
            var single = Assert.Single(particle.GetCollection <T1>(data));

            Assert.Equal(instance, single);
        }
        public void SingleChoice()
        {
            var particle = new CompositeParticle(ParticleType.Choice, 0, 1)
            {
                new ElementParticle(typeof(T1), 0, 1),
            }.Compile();
            var data = new TestOpenXmlCompositeElement();

            var t1 = new T1();

            Assert.True(particle.Set(data, t1));

            var single = Assert.Single(data);

            Assert.Same(t1, single);
        }
        public void CollectionInAll()
        {
            var particle = new CompositeParticle(ParticleType.All, 0, 1)
            {
                new ElementParticle(typeof(T1), 0, 1),
                new ElementParticle(typeof(T2), 0, 10),
                new ElementParticle(typeof(T3), 0, 1),
            }.Compile();
            var data = new TestOpenXmlCompositeElement();

            var t1  = new T1();
            var t2a = new T2();
            var t2b = new T2();
            var t2c = new T2();

            Assert.True(particle.Set(data, t1));
            Assert.Collection(
                data,
                e => Assert.Same(t1, e));

            Assert.True(particle.Set(data, t2a));
            Assert.Collection(
                data,
                e => Assert.Same(t1, e),
                e => Assert.Same(t2a, e));

            Assert.True(particle.Set(data, t2b));
            Assert.Collection(
                data,
                e => Assert.Same(t1, e),
                e => Assert.Same(t2b, e));

            Assert.True(particle.GetCollection <T2>(data).Add(t2c));
            Assert.Collection(
                data,
                e => Assert.Same(t1, e),
                e => Assert.Same(t2b, e),
                e => Assert.Same(t2c, e));

            Assert.True(particle.Set(data, t2a));
            Assert.Collection(
                data,
                e => Assert.Same(t1, e),
                e => Assert.Same(t2a, e));
        }
Example #11
0
        public void CompositeSequenceNoVersion(FileFormatVersions version)
        {
            var particle = new CompositeParticle(ParticleType.Sequence, 1, 1)
            {
                new ElementParticle(typeof(ParticleTests), 1, 1),
                new AnyParticle(1, 1),
                new NsAnyParticle(0, 1, 1),
            };

            var built = Assert.IsType <CompositeParticle>(particle.Build(version));

            Assert.NotSame(particle, built);

            Assert.Collection(built.ChildrenParticles,
                              p => Assert.Same(p, particle.ChildrenParticles[0]),
                              p => Assert.Same(p, particle.ChildrenParticles[1]),
                              p => Assert.Same(p, particle.ChildrenParticles[2]));
        }
        public void TwoElementInSequenceTestBackwards()
        {
            var particle = new CompositeParticle(ParticleType.Sequence, 0, 1)
            {
                new ElementParticle(typeof(T1), 0, 1),
                new ElementParticle(typeof(T2), 0, 1),
            }.Compile();
            var data = new TestOpenXmlCompositeElement();

            var t1 = new T1();
            var t2 = new T2();

            Assert.True(particle.Set(data, t2));
            Assert.True(particle.Set(data, t1));

            Assert.Collection(
                data,
                e => Assert.Same(t1, e),
                e => Assert.Same(t2, e));
        }
        public void AllElements()
        {
            var particle = new CompositeParticle(ParticleType.Sequence, 0, 1)
            {
                new ElementParticle(typeof(T1), 0, 1),
                new ElementParticle(typeof(T2), 0, 1),
            }.Compile();
            var data = new TestOpenXmlCompositeElement();
            var t1a  = new T1();
            var t1b  = new T1();
            var t2a  = new T2();

            data.AppendChild(t1a);
            data.AppendChild(t2a);
            data.AppendChild(t1b);

            Assert.Collection(
                particle.GetCollection <T1>(data),
                e => Assert.Same(t1a, e),
                e => Assert.Same(t1b, e));
        }
        public void SequenceAdd()
        {
            var particle = new CompositeParticle(ParticleType.Sequence, 0, 1)
            {
                new ElementParticle(typeof(T1), 0, 1),
                new ElementParticle(typeof(T2), 0, 1),
                new ElementParticle(typeof(T3), 0, 1),
                new ElementParticle(typeof(T4), 0, 1),
                new ElementParticle(typeof(T5), 0, 1),
                new ElementParticle(typeof(T6), 0, 1),
            }.Compile();

            var data = new TestOpenXmlCompositeElement();
            var t1   = new T1();
            var t3   = new T3();
            var t5   = new T5();

            Assert.Empty(data);

            particle.GetCollection <T1>(data).Add(t1);
            Assert.Collection(
                data,
                e => Assert.Same(e, t1));

            particle.GetCollection <T3>(data).Add(t3);
            Assert.Collection(
                data,
                e => Assert.Same(e, t1),
                e => Assert.Same(e, t3));

            particle.GetCollection <T5>(data).Add(t5);
            Assert.Collection(
                data,
                e => Assert.Same(e, t1),
                e => Assert.Same(e, t3),
                e => Assert.Same(e, t5));
        }
        public void TwoSequencesInChoice()
        {
            var particle = new CompositeParticle(ParticleType.Choice, 0, 1)
            {
                new ElementParticle(typeof(T1), 0, 1),
                new CompositeParticle(ParticleType.Sequence, 0, 1)
                {
                    new ElementParticle(typeof(T2), 0, 1),
                    new ElementParticle(typeof(T3), 0, 1),
                },
                new ElementParticle(typeof(T4), 0, 1),
                new CompositeParticle(ParticleType.Sequence, 0, 1)
                {
                    new ElementParticle(typeof(T5), 0, 1),
                    new ElementParticle(typeof(T6), 0, 1),
                },
            }.Compile();
            var data = new TestOpenXmlCompositeElement();

            var t1 = new T1();
            var t2 = new T2();
            var t3 = new T3();
            var t4 = new T4();
            var t5 = new T5();
            var t6 = new T6();

            Assert.True(particle.Set(data, t1));
            Assert.Collection(
                data,
                e => Assert.Same(t1, e));

            Assert.True(particle.Set(data, t2));
            Assert.Collection(
                data,
                e => Assert.Same(t2, e));

            Assert.True(particle.Set(data, t3));
            Assert.Collection(
                data,
                e => Assert.Same(t2, e),
                e => Assert.Same(t3, e));

            Assert.True(particle.Set(data, t4));
            Assert.Collection(
                data,
                e => Assert.Same(t4, e));

            Assert.True(particle.Set(data, t5));
            Assert.Collection(
                data,
                e => Assert.Same(t5, e));

            Assert.True(particle.Set(data, t6));
            Assert.Collection(
                data,
                e => Assert.Same(t5, e),
                e => Assert.Same(t6, e));

            Assert.True(particle.Set(data, t3));
            Assert.Collection(
                data,
                e => Assert.Same(t3, e));

            Assert.True(particle.Set(data, t2));
            Assert.Collection(
                data,
                e => Assert.Same(t2, e),
                e => Assert.Same(t3, e));
        }