Ejemplo n.º 1
0
        private static void ObjectComparerInternalSorted <T>(
            Table table,
            IEnumerable <T> dataList,
            Action <TableRow, T> postCompareActions,
            IEnumerable <ColumnToActionContainer <IComparerColumnAction> > columnOverrides)
        {
            var compareTableResult = new CompareTableResult();

            compareTableResult.InitCompare(table, dataList.Count());

            // hold which column maps to which property
            var columnActions = ColumnActionFactory.GetActionsFromColumns(
                table, typeof(T), columnOverrides);

            var actualData = dataList.ToList();

            // flip through each row
            for (var tableRowIndex = 0; tableRowIndex < table.RowCount; tableRowIndex++)
            {
                var row = table.Rows[tableRowIndex];

                // Test for null
                if (tableRowIndex >= actualData.Count)
                {
                    var noRowCompareRowResult = ObjectComparerInternalRow(columnActions, row);
                    compareTableResult.AddRowResult(
                        tableRowIndex, int.MaxValue, noRowCompareRowResult, false);
                    continue;
                }

                var dataItem = actualData.ElementAt(tableRowIndex);

                // Compare It
                var compareRowResult = ObjectComparerInternalRow <T>(columnActions, row, dataItem);
                compareTableResult.AddRowResult(tableRowIndex, tableRowIndex, compareRowResult, true);

                if (compareRowResult.totalErrors == 0)
                {
                    if (postCompareActions != null)
                    {
                        postCompareActions(table.Rows[tableRowIndex], dataItem);
                    }
                }
            }

            for (var dataIndex = table.RowCount; dataIndex < dataList.Count(); dataIndex++)
            {
                var dataItem         = dataList.ElementAt(dataIndex);
                var compareRowResult = ObjectComparerInternalRow(columnActions, dataItem);
                compareTableResult.AddRowResult(int.MaxValue, dataIndex, compareRowResult, false);
            }

            compareTableResult.FinalAnalyst();

            // Perform Printing
            Console.WriteLine(compareTableResult.PrintMeSpecflowStyle());

            compareTableResult.TheAssert();
        }
Ejemplo n.º 2
0
        private static void ObjectComparerWithFinderInternal <T>(
            Table table,
            Func <TableRow, T> objectFinder,
            Action <TableRow, T> postCompareActions,
            IEnumerable <ColumnToActionContainer <IComparerColumnAction> > columnOverrides)
        {
            var compareTableResult = new CompareTableResult();

            compareTableResult.InitCompare(table, table.RowCount);

            // hold which column maps to which property
            var columnActions = ColumnActionFactory.GetActionsFromColumns(
                table, typeof(T), columnOverrides);

            // flip through each row
            for (var tableRowIndex = 0; tableRowIndex < table.RowCount; tableRowIndex++)
            {
                var row = table.Rows[tableRowIndex];

                var dataItem = objectFinder(row);

                // can't set anything on null anyway.
                if (dataItem == null)
                {
                    throw new AssertFailedException("Object Not found in Row " + tableRowIndex);
                }

                // Compare It
                var compareRowResult = ObjectComparerInternalRow <T>(columnActions, row, dataItem);
                compareTableResult.AddRowResult(tableRowIndex, tableRowIndex, compareRowResult, true);

                if (compareRowResult.totalErrors == 0)
                {
                    if (postCompareActions != null)
                    {
                        postCompareActions(table.Rows[tableRowIndex], dataItem);
                    }
                }
            }

            compareTableResult.FinalAnalyst();

            // Perform Printing
            Console.WriteLine(compareTableResult.PrintMeSpecflowStyle());

            compareTableResult.TheAssert();
        }
Ejemplo n.º 3
0
        private static void ObjectComparerInternalOne <T>(
            Table verticalOrHorizontalTable,
            T dataItem,
            Action <TableRow, T> postCompareActions,
            IEnumerable <ColumnToActionContainer <IComparerColumnAction> > columnOverrides)
        {
            var table = VerticalToHorizonal(verticalOrHorizontalTable);

            if (table.RowCount != 1)
            {
                Assert.Fail("A direct compare requires one and only one Specflow Row.");
            }

            var compareTableResult = new CompareTableResult();

            compareTableResult.InitCompare(table, 1);

            // hold which column maps to which property
            var columnActions = ColumnActionFactory.GetActionsFromColumns(
                table, typeof(T), columnOverrides);

            var row = table.Rows[0];
            var compareRowResult = ObjectComparerInternalRow <T>(columnActions, row, dataItem);

            compareTableResult.AddRowResult(0, 0, compareRowResult, true);

            compareTableResult.FinalAnalyst();

            // Perform Printing
            Console.WriteLine(compareTableResult.PrintMeSpecflowStyle());

            compareTableResult.TheAssert();

            // trigger postAction if needed.
            if (compareRowResult.totalErrors == 0)
            {
                if (postCompareActions != null)
                {
                    postCompareActions(table.Rows[0], dataItem);
                }
            }
        }
Ejemplo n.º 4
0
        private static void ObjectComparerInternal <T>(
            Table table,
            IEnumerable <T> dataList,
            Action <TableRow, T> postCompareActions,
            IEnumerable <ColumnToActionContainer <IComparerColumnAction> > columnOverrides)
        {
            if (dataList == null)
            {
                throw new Exception("Actual is Null.  TableAid can't compare a table against null.");
            }

            var compareTableResult = new CompareTableResult();

            compareTableResult.InitCompare(table, dataList.Count());

            // hold which column maps to which property
            var columnActions = ColumnActionFactory.GetActionsFromColumns(
                table, typeof(T), columnOverrides);

            // flip through each row
            for (var tableRowIndex = 0; tableRowIndex < table.RowCount; tableRowIndex++)
            {
                var isMatch = false;

                // And each data
                for (var dataIndex = 0; dataIndex < dataList.Count(); dataIndex++)
                {
                    // Has a match Happened
                    if (compareTableResult.DoesDataHaveMatch(dataIndex))
                    {
                        continue;
                    }

                    // Ready the Compare
                    var row      = table.Rows[tableRowIndex];
                    var dataItem = dataList.ElementAt(dataIndex);

                    // Compare It
                    var compareRowResult = ObjectComparerInternalRow <T>(columnActions, row, dataItem);

                    isMatch = (compareRowResult.totalErrors == 0);

                    // This makes the Match Happen
                    compareTableResult.AddRowResult(
                        tableRowIndex, dataIndex, compareRowResult, isMatch);

                    if (compareRowResult.totalErrors == 0)
                    {
                        if (postCompareActions != null)
                        {
                            postCompareActions(table.Rows[tableRowIndex], dataItem);
                        }

                        break; // We have a match.  Stop looping data.
                    }
                }

                // Add the missing Table Records
                if (isMatch == false)
                {
                    var row = table.Rows[tableRowIndex];
                    var compareRowResult = ObjectComparerInternalRow(columnActions, row);
                    compareTableResult.AddRowResult(
                        tableRowIndex, int.MaxValue, compareRowResult, false);
                }
            }

            // Add the missing Data Records
            for (var dataIndex = 0; dataIndex < dataList.Count(); dataIndex++)
            {
                // Has a match Happened
                if (compareTableResult.DoesDataHaveMatch(dataIndex))
                {
                    continue;
                }

                // No?  Well add a blank to the compare.
                var dataItem         = dataList.ElementAt(dataIndex);
                var compareRowResult = ObjectComparerInternalRow(columnActions, dataItem);
                compareTableResult.AddRowResult(int.MaxValue, dataIndex, compareRowResult, false);
            }

            compareTableResult.FinalAnalyst();

            // Perform Printing
            Console.WriteLine(compareTableResult.PrintMeSpecflowStyle());

            compareTableResult.TheAssert();
        }
Ejemplo n.º 5
0
        private static IEnumerable <T> ObjectCreatorUpdaterInternal <T>(
            Table verticalOrHorizontalTable,
            Func <T> defaultValue,
            Func <TableRow, T> objectFinder,
            Action <TableRow, T> postMatchActions,
            IEnumerable <ColumnToActionContainer <ICreatorColumnAction> > columnOverrides)
        {
            var table = VerticalToHorizonal(verticalOrHorizontalTable);

            //set up the return value
            var result = new List <T>();

            //hold which column maps to which property
            var matches = ColumnActionFactory.GetActionsFromColumns(table, typeof(T), columnOverrides);

            var valueToRow = new Dictionary <T, TableRow>();

            //flip through each row and setup the data
            for (var i = 0; i < table.RowCount; i++)
            {
                T obj = default(T);

                //get the default value from the given method if specified
                if (defaultValue != null)
                {
                    obj = defaultValue();
                }

                //go find one that exists.  If one exists.
                else if (objectFinder != null)
                {
                    obj = objectFinder(table.Rows[i]);
                    // can't set anything on null anyway.
                    if (obj == null)
                    {
                        continue;
                    }
                }
                else
                {
                    // Be like Assist too pop Constructor.
                    // Until then

                    if (!ObjectHelper.ThisTypeHasADefaultConstructor <T>())
                    {
                        throw new Exception(
                                  "Type needs a Default Constructor if you don't provide a method for creating one.");
                    }

                    obj = ObjectHelper.CreateObjectWithTheDefaultConstructor <T>();
                }

                //collect the errors so we can report a little nicer
                try
                {
                    //run through each column and set the matching prop
                    foreach (var item in matches)
                    {
                        item.MatchAction.GoGoCreateColumnAction(
                            obj, table.Rows[i][item.ColumnIndex.Value]);
                    }
                }
                catch (AssertFailedException ex)
                {
                    throw new AssertFailedException("Error on row: " + i, ex);
                }

                if (postMatchActions != null)
                {
                    postMatchActions(table.Rows[i], obj);
                }

                //add this item to our return collection
                result.Add(obj);
                valueToRow.Add(obj, table.Rows[i]);
            }

            // Ignore ScenarioContext if we are not using it,
            //   for instance in a Manual Test on ColumnActions
            if (ScenarioContext.Current != null)
            {
                RecallAid.It[table.GetHashCode().ToString()] = valueToRow;
            }

            //return the collection of updated items
            return(result);
        }