private void generateCSV(object sender, DoWorkEventArgs e)
        {
            StringBuilder sbInput = new StringBuilder(), sbAssocInput = new StringBuilder(), sbAssocOutput = new StringBuilder();

             // Column headers
             sbInput.Append("id, parameter, type, value" + Environment.NewLine);
             sbAssocInput.Append("tc_id, input_id" + Environment.NewLine);
             sbAssocOutput.Append("tc_id, parameter, type, expectOutput, actualOutput" + Environment.NewLine);

             IEnumerable<Variation> generatedVariations = model.GenerateVariations(getWayValue(), 1234);
             float progressMaximum = generatedVariations.Count();
             int progressIncrement = 0;
             foreach (Variation v in generatedVariations)
             {
            bWorkerCSV.ReportProgress((int)((progressIncrement / progressMaximum) * 100));
            progressIncrement++;
            inputList = new Dictionary<string, xUnitTestParameter>();
            Guid guid = Guid.NewGuid();
            // remove -'s from the random string
            string guidstring = getPrefixText()+Regex.Replace(guid.ToString(), "-", "");

            // feeding the input parameters in to the test code
            foreach (Parameter param in model.Parameters)
            {
               xUnitTestParameter input = new xUnitTestParameter();
               input.Name = v[param.Name].ToString().Split(new Char[] { ',' })[0];
               input.Type = v[param.Name].ToString().Split(new Char[] { ',' })[1];
               input.Value = v[param.Name].ToString().Split(new Char[] { ',' })[2];

               inputList.Add(input.Name, input);

               Guid guid2 = Guid.NewGuid();
               string idParam = getPrefixText() + Regex.Replace(guid2.ToString(), "-", "");
               sbInput.Append(""+idParam+","+input.Name+","+ input.Type+","+input.Value + Environment.NewLine);
               sbAssocInput.Append(""+guidstring+","+idParam+ Environment.NewLine);

            }

            string errorMsg = "";
            // generate expected output placeholders
            foreach (DataGridViewRow outputRow in outputGridView.Rows)
            {
               // Make sure it's not an empty row.
               if (!outputRow.IsNewRow)
               {
                  string valueOracle = "";
                  string outputType = "";
                  string processedString = "";
                  if (dataTestOutput.ContainsKey(outputRow.Cells[0].Value.ToString()))
                  {
                     valueOracle = dataTestOutput[outputRow.Cells[0].Value.ToString()];
                     outputType = outputRow.Cells[1].Value.ToString();
                  }
                  if (!valueOracle.Equals(""))
                  {
                     string codeToProcess;
                     if (makeCast(outputType).Equals("string"))
                     {
                        codeToProcess = "using autobbut_gui; using System;" + Environment.NewLine +
                           "public class MyStringManipulator : IStringManipulator" + Environment.NewLine +
                           "{" + Environment.NewLine +
                           "    public string processString(string aString)" + Environment.NewLine +
                           "     {" + Environment.NewLine +
                           "return (((" + valueOracle + ").ToString())).ToString();" + Environment.NewLine +
                           "}}";
                     }
                     else
                     {
                        codeToProcess = "using autobbut_gui; using System;" + Environment.NewLine +
                        "public class MyStringManipulator : IStringManipulator" + Environment.NewLine +
                        "{" + Environment.NewLine +
                        "    public string processString(string aString)" + Environment.NewLine +
                        "     {" + Environment.NewLine +
                        "return (" + makeCast(outputType) + ".Parse((" + valueOracle + ").ToString())).ToString();" + Environment.NewLine +
                        "}}";
                     }
                     codeToProcess = addCast(codeToProcess, makeCast(outputType));
                     try
                     {
                        processedString = ScriptRunner.RunScript(codeToProcess, "");
                     }
                     catch (OverflowException e2)
                     {
                        processedString = "";
                        errorMsg = e2.Message;
                     }
                  }
                  //FIXME: error output has to be after all the output in the row
                  if (!errorMsg.Equals("") && (outputRow.Cells[0].Value.ToString().Contains("Error")
                        || outputRow.Cells[0].Value.ToString().Contains("error")))
                  {
                       sbAssocOutput.Append(guidstring+","+ outputRow.Cells[0].Value.ToString().ToLower()+","+outputRow.Cells[1].Value+","+
                           errorMsg+",XXX" + Environment.NewLine);

                     errorMsg = "";
                  }
                  else
                  {
                     sbAssocOutput.Append(guidstring + "," + outputRow.Cells[0].Value.ToString().ToLower() + "," + outputRow.Cells[1].Value + "," +
                           processedString + ",XXX" + Environment.NewLine);
                  }
               }
            }
            bWorkerCSV.ReportProgress((int)((progressIncrement / progressMaximum) * 100));

             }
             string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
             String fileNameTblInput = "tbl_test_case_input.csv";
             String fileNameTblAssocInput = "tbl_tc_input_assoc.csv";
             String fileNameTblOutput = "tbl_test_output.csv";
             writeCSV(sbInput, path, fileNameTblInput);
             writeCSV(sbAssocInput, path, fileNameTblAssocInput);
             writeCSV(sbAssocOutput, path, fileNameTblOutput);
             openFileInExcel(fileNameTblInput);
             openFileInExcel(fileNameTblAssocInput);
             openFileInExcel(fileNameTblOutput);

             //   // Confirm to the user it has been completed.
             MessageBox.Show("CSV files generated");

             return;
        }
        private void generateTestSuiteCode(object sender, DoWorkEventArgs e)
        {
            string testSuiteCode = "";

             IEnumerable<Variation> generatedVariations = model.GenerateVariations(getWayValue(), 1234);
             float progressMaximum = generatedVariations.Count();
             int progressIncrement = 0;

             foreach (Variation v in generatedVariations)
             {
            bWorkerXUNIT.ReportProgress((int) ((progressIncrement / progressMaximum) * 100));
               // progressEvent(xUnitTestSuiteGenerator, new ProgressValueEventArgs(progressIncrement, (int)progressMaximum));
            progressIncrement++;
            inputList = new Dictionary<string, xUnitTestParameter>();
            Guid guid = Guid.NewGuid();
            // remove -'s from the random string
            string guidstring = Regex.Replace(guid.ToString(), "-", "");
            // start generating the test method one by one
            testSuiteCode += TestHeader + Environment.NewLine;
            // test method signature
            testSuiteCode += "public void Test"+getPrefixText()+
               guidstring + "()" + Environment.NewLine + "{"
               + Environment.NewLine;

            // feeding the input parameters in to the test code
            foreach (Parameter param in model.Parameters)
            {
               xUnitTestParameter input = new xUnitTestParameter();
               input.Name = v[param.Name].ToString().Split(new Char[] { ',' })[0];
               input.Type = v[param.Name].ToString().Split(new Char[] { ',' })[1];
               input.Value = v[param.Name].ToString().Split(new Char[] { ',' })[2];

               testSuiteCode += "TD.setInputParameter(FunctionBlockName,\"" + input.Name + "\", \"" +
                  input.Type + "\", \"" + input.Value + "\" );" + Environment.NewLine;

               inputList.Add(input.Name, input);
            }
            testSuiteCode += Environment.NewLine;

            // feeding the output parameters in to the test code
            foreach (DataGridViewRow outputRow in outputGridView.Rows)
            {
               // Make sure it's not an empty row.
               if (!outputRow.IsNewRow)
               {
                  testSuiteCode += "xUnitTestParameter " + outputRow.Cells[0].Value.ToString().ToLower() +
                     "Param = TD.setOutputParameter(FunctionBlockName,\"" + outputRow.Cells[0].Value + "\",\"" +
                     outputRow.Cells[1].Value + "\");" + Environment.NewLine;
               }
            }

            // exercise the component under test
            testSuiteCode += Environment.NewLine + "TD.execute(FunctionBlock, FunctionBlockName);" +
               Environment.NewLine + Environment.NewLine;

            string errorMsg = "";
            // generate expected output placeholders
            foreach (DataGridViewRow outputRow in outputGridView.Rows)
            {
               // Make sure it's not an empty row.
               if (!outputRow.IsNewRow)
               {
                  string valueOracle = "";
                  string outputType = "";
                  string processedString = "";
                  if (dataTestOutput.ContainsKey(outputRow.Cells[0].Value.ToString()))
                  {
                     valueOracle = dataTestOutput[outputRow.Cells[0].Value.ToString()];
                     outputType = outputRow.Cells[1].Value.ToString();
                  }
                  if (!valueOracle.Equals(""))
                  {
                     string codeToProcess;
                     if (makeCast(outputType).Equals("string"))
                     {
                        codeToProcess = "using autobbut_gui; using System;" + Environment.NewLine +
                           "public class MyStringManipulator : IStringManipulator" + Environment.NewLine +
                           "{" + Environment.NewLine +
                           "    public string processString(string aString)" + Environment.NewLine +
                           "     {" + Environment.NewLine +
                           "return (((" + valueOracle + ").ToString())).ToString();" + Environment.NewLine +
                           "}}";
                     }
                     else
                     {
                        codeToProcess = "using autobbut_gui; using System;" + Environment.NewLine +
                        "public class MyStringManipulator : IStringManipulator" + Environment.NewLine +
                        "{" + Environment.NewLine +
                        "    public string processString(string aString)" + Environment.NewLine +
                        "     {" + Environment.NewLine +
                        "return (" + makeCast(outputType) + ".Parse((" + valueOracle + ").ToString())).ToString();" + Environment.NewLine +
                        "}}";
                     }
                     codeToProcess = addCast(codeToProcess, makeCast(outputType));
                     try
                     {
                        processedString = ScriptRunner.RunScript(codeToProcess, "");
                     }
                     catch (OverflowException e2)
                     {
                        processedString = "";
                        errorMsg = e2.Message;
                     }
                  }
                  //FIXME: error output has to be after all the output in the row
                  if (!errorMsg.Equals("") && (outputRow.Cells[0].Value.ToString().Contains("Error")
                        || outputRow.Cells[0].Value.ToString().Contains("error")))
                  {
                     testSuiteCode += Assert+"(\"" + errorMsg + "\",TD.getOutputByName(" + outputRow.Cells[0].Value.ToString().ToLower() +
                       "Param.PointName));" + Environment.NewLine;
                     errorMsg = "";
                  }
                  else
                  {
                     string displayprocessedString = "XXX";
                     if (!(outputRow.Cells[0].Value.ToString().Contains("Error")
                        || outputRow.Cells[0].Value.ToString().Contains("error")) && processedString != "")
                     {
                        displayprocessedString = processedString;
                     }
                     if ((outputRow.Cells[0].Value.ToString().Contains("Error")
                        || outputRow.Cells[0].Value.ToString().Contains("error")) && processedString == "")
                     {
                        displayprocessedString = processedString;
                     }

                     testSuiteCode += Assert+"(\"" + displayprocessedString + "\",TD.getOutputByName(" + outputRow.Cells[0].Value.ToString().ToLower() +
                        "Param.PointName));" + Environment.NewLine;
                  }
               }
            }
            testSuiteCode += "}" + Environment.NewLine + Environment.NewLine;
            bWorkerXUNIT.ReportProgress((int)((progressIncrement / progressMaximum) * 100));
             }

             string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
             string fname = "testcase.txt";
             System.IO.File.WriteAllText(path + "\\" + fname, testSuiteCode);
             MessageBox.Show("Data saved");
             openFile(path + "\\" + fname, "Notepad");

             return;
        }
        //create test paramater based on the list of paramater set in the GUI. Avoid duplicate value.
        private Model createModel(DataGridView inputGridView)
        {
            ParamDictionary = new Dictionary<string, Parameter>();

             // stop if no data
             if (inputGridView.Rows.Count <= 1)
             {
            MessageBox.Show("No Data to process.");
            return null;
             }

             xUnitTestParameter p = new xUnitTestParameter();
             foreach (DataGridViewRow inputGridRow in inputGridView.Rows)
             {
            // Make sure it's not an empty row.
            if (!inputGridRow.IsNewRow)
            {
               if (inputGridRow.Cells[0].Value != null)
               {
                  p.Name = inputGridRow.Cells[0].Value.ToString();
                  if (inputGridRow.Cells[1].Value != null)
                  {
                     ParamDictionary.Add(p.Name, new Parameter(p.Name));
                  }
                  else
                  {
                     MessageBox.Show("Type not specified.");
                     return null;
                  }
               }
            }
             }

             return new Model(ParamDictionary.Values);
        }