Example #1
0
    public void AddParsableNodesToValidatorChain()
    {
        Validator validator = new Validator();

        ParsableRow row = new ParsableRow();
        row.cells = new string[2] { "foo", "bar" };

        ReadBundle rb = new ReadBundle();
        rb.fieldNames.Add("Foo");
        rb.fieldNames.Add("Bar");
        rb.rows.Add(row);

        validator.AddParsableRows(rb);

        Assert.AreEqual(1, validator.Nodes.Length, "should have added the row to the validation chain");
    }
Example #2
0
    public void PassCheckableNodesToUserValidator()
    {
        Validator validator = new Validator();

        ParsableRow row = new ParsableRow();
        row.cells = new string[2] { "foo", "bar" };

        ReadBundle rb = new ReadBundle();
        rb.fieldNames.Add("Foo");
        rb.fieldNames.Add("Bar");
        rb.rows.Add(row);

        validator.AddParsableRows(rb);

        IValidator userValidator = Substitute.For<IValidator>();

        validator.IsValid(rb, userValidator);

        userValidator.Received().Validate(validator.Nodes[0], validator);
    }
Example #3
0
    /// <summary>
    /// Get the parsable rows from a workbook
    /// </summary>
    /// <param name="rows">a reference to an object that will contain parsable rows</param>
    /// <param name="workbook">the workbook to extract rows from</param>
    public static void GetParsableRows(ref List<ParsableRow> rows, XSSFWorkbook workbook) {

        ISheet sheet = workbook.GetSheetAt(0);

        bool reading = true;
        int rindex = sheet.FirstRowNum;

        while (reading) {
            IRow row = sheet.GetRow(rindex);

            // if there is a row, and it's not empty
            if (row != null && row.Cells.Count > 0) {

                // check to see if the row is parsable by looking at the first value
                ICell firstCell = row.GetCell(0);

                if (firstCell != null) {

                    string firstCellVal = CellValueAsString(firstCell);
                                        
                    // treat the row as parsable if it is:
                    // - not a title
                    // - not metadata
                    // - not a variable
                    if (firstCellVal[0] != '[' && firstCellVal[0] != '#' && firstCellVal[0] != '$') {

                        // cells are stored in parsable row objects
                        ParsableRow pr = new ParsableRow();

                        // copy the line number to the parsable row object
                        pr.linenumber = rindex;

						// make an array which is as long as the last cell number - note that NPOI will show cells **up to** the last cell that
						// contains data, so if a row in the sheet only has a single value in the fourth column, that row will return null for all
						// indices up to 2, then a value for index 3.
                        pr.cells = new string[row.LastCellNum];

                        // for each cell in the row, convert it to a string and copy it to the parsable row object
                        for (int i = 0; i < row.LastCellNum; i++) {
                            pr.cells[i] = CellValueAsString(row.GetCell(i));                   
                        }
                        
                        // add the parsable row object we just set up to the list of rows that was passed to the method
                        rows.Add(pr);
                    }
                }
            }

            rindex++;

            // check to make sure we're not out of bounds
            if (rindex > sheet.LastRowNum) {

                reading = false;
            }
        }
    }