System.Data.Tests.DataRowCollectionTest.InsertAt C# (CSharp) Метод

InsertAt() приватный Метод

private InsertAt ( ) : void
Результат void
        public void InsertAt()
        {
            _tbl.Columns.Add();
            _tbl.Columns.Add();
            _tbl.Columns.Add();
            DataRowCollection Rows = _tbl.Rows;

            Rows.Add(new object[] { "a", "aa", "aaa" });
            Rows.Add(new object[] { "b", "bb", "bbb" });
            Rows.Add(new object[] { "c", "cc", "ccc" });
            Rows.Add(new object[] { "d", "dd", "ddd" });

            DataRow Row = _tbl.NewRow();
            Row[0] = "e";
            Row[1] = "ee";
            Row[2] = "eee";

            try
            {
                Rows.InsertAt(Row, -1);
                Assert.False(true);
            }
            catch (Exception e)
            {
                Assert.Equal(typeof(IndexOutOfRangeException), e.GetType());
                // Never premise English.
                //Assert.Equal ("The row insert position -1 is invalid.", e.Message);
            }

            Rows.InsertAt(Row, 0);
            Assert.Equal("e", Rows[0][0]);
            Assert.Equal("a", Rows[1][0]);

            Row = _tbl.NewRow();
            Row[0] = "f";
            Row[1] = "ff";
            Row[2] = "fff";

            Rows.InsertAt(Row, 5);
            Assert.Equal("f", Rows[5][0]);

            Row = _tbl.NewRow();
            Row[0] = "g";
            Row[1] = "gg";
            Row[2] = "ggg";

            Rows.InsertAt(Row, 500);
            Assert.Equal("g", Rows[6][0]);

            try
            {
                Rows.InsertAt(Row, 6);  //Row already belongs to the table
                Assert.False(true);
            }
            catch (Exception e)
            {
                Assert.Equal(typeof(ArgumentException), e.GetType());
                // Never premise English.
                //Assert.Equal ("This row already belongs to this table.", e.Message);
            }

            DataTable table = new DataTable();
            DataColumn col = new DataColumn("Name");
            table.Columns.Add(col);
            Row = table.NewRow();
            Row["Name"] = "Abc";
            table.Rows.Add(Row);
            try
            {
                Rows.InsertAt(Row, 6);
                Assert.False(true);
            }
            catch (Exception e)
            {
                Assert.Equal(typeof(ArgumentException), e.GetType());
                // Never premise English.
                //Assert.Equal ("This row already belongs to another table.", e.Message);
            }

            table = new DataTable();
            col = new DataColumn("Name");
            col.DataType = typeof(string);
            table.Columns.Add(col);
            UniqueConstraint uk = new UniqueConstraint(col);
            table.Constraints.Add(uk);

            Row = table.NewRow();
            Row["Name"] = "aaa";
            table.Rows.InsertAt(Row, 0);

            Row = table.NewRow();
            Row["Name"] = "aaa";
            try
            {
                table.Rows.InsertAt(Row, 1);
                Assert.False(true);
            }
            catch (Exception e)
            {
                Assert.Equal(typeof(ConstraintException), e.GetType());
            }
            try
            {
                table.Rows.InsertAt(null, 1);
            }
            catch (Exception e)
            {
                Assert.Equal(typeof(ArgumentNullException), e.GetType());
            }
        }