Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void btnOpen_Click(object sender, EventArgs e)
        // Created OpenFileDialog control using a Forms designer at design-time
        {
            openFileDialog1.ShowDialog();
            //called the ShowDialog method to browse a selected file

            txtfilepath.Text = openFileDialog1.FileName;
            BindDataCSV(txtfilepath.Text);
            //set data into textfilepath
        }
        private void BindDataCSV(string filePath)
        {
            DataTable dt = new DataTable();
            //opened an instance of Datatable

            string[] lines = System.IO.File.ReadAllLines(filePath);
            //ReadAllLines gets a string array from the file

            if (lines.Length > 0)
            {
                //first line to create header

                string firstline = lines[0];
                //reads first line of string array at index 0

                string[] headerLabels = firstline.Split(',');
                //splits the firstline using comma delimited string

                foreach (string headerWord in headerLabels)
                {
                    dt.Columns.Add(new DataColumn(headerWord));
                    //added DataColumns for header
                }

                //for data


                for (int r = 1; r < lines.Length; r++)
                {
                    string[] dataWords = lines[r].Split(',');
                    //split strings into lines
                    DataRow dr = dt.NewRow();
                    //inset a new row into a data table
                    int columnIndex = 0;
                    //start of column is 0 index
                    foreach (string headerWord in headerLabels)
                    {
                        dr[headerWord] = dataWords[columnIndex++];
                        //increment the value by 1 in columnIndex

                    }
                    dt.Rows.Add(dr);
                    //adds DataRow in the DataTable
                }
            }
            if (dt.Rows.Count > 0)
            {
                dataGridView1.DataSource = dt;
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            // Displays a SaveFileDialog so the user can save the Image            
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "|*.csv";
            saveFileDialog1.Title = "Save CSV File";
            saveFileDialog1.ShowDialog();

            // If the file name is not an empty string open it for saving.
            if (saveFileDialog1.FileName != "")
            {
                using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
                {
                    WriteDataTable(dataGridView1.DataSource as DataTable, sw, true);
                }


            }
        }

        // Write data to csv

        public static void WriteDataTable(DataTable sourceTable, TextWriter writer, bool includeHeaders)
        {
            if (includeHeaders)
            {
                List<string> headerValues = new List<string>();
                //opened an instance of List<T> for headerValues
                foreach (DataColumn column in sourceTable.Columns)
                {
                    headerValues.Add(QuoteValue(column.ColumnName));
                }

                writer.WriteLine(String.Join(",", headerValues.ToArray()));
            }

            string[] items = null;
            // if string array is null or empty
            foreach (DataRow row in sourceTable.Rows)
            {
                items = row.ItemArray.Select(o => QuoteValue(o.ToString())).ToArray();
                writer.WriteLine(String.Join(",", items));
                // adds rows to the array and joins comma delimited strings
            }

            writer.Flush();
        }

        private static string QuoteValue(string value)
        {
            return String.Concat("\"", value.Replace("\"", "\"\""), "\"");
        }

    }
}


What I have tried:

Hey guys, I am working on a project were I have been asked to perform a unit test for a class.I have written a simple GUI project in Visual Studio using C#.

I have done some research on unit testing but am unable to figure out even were to start with the code I have written. Any help or guidance in the right direction would be much appreciated.

Thanks
Posted
Updated 3-Nov-18 21:51pm
v3
Comments
F-ES Sitecore 5-Nov-18 5:19am    
Your code isn't very unit-testable. A unit test is something that runs automatically without needing external resources in order to test business logic. Your code needs pop-ups, interaction with a UI, files and so on....how do you automate that?

Identify the logic you want to test and separate it into a different class. For example in BindDataCSV if you get lines[] as normal then pass that string array to a function to carry out the work, you should be able to unit test that function.
Vacate 8-Nov-18 4:21am    
Thanks very much for your help and feedback I really appreciate it.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900