Parcourir la source

Test generation and crown-tests project

Dexter89 il y a 12 ans
Parent
commit
b0fd550680

+ 2 - 0
CMakeLists.txt

@@ -80,6 +80,8 @@ if (CROWN_BUILD_SAMPLES)
 	add_subdirectory(samples)
 endif (CROWN_BUILD_SAMPLES)
 
+add_subdirectory(tests)
+
 # add a target to generate API documentation with Doxygen
 if (CROWN_BUILD_DOC)
   find_package(Doxygen)

+ 21 - 0
tests/CMakeLists.txt

@@ -0,0 +1,21 @@
+cmake_minimum_required(VERSION 2.8)
+
+project(crown-tests)
+
+file(GLOB_RECURSE CROWN_TESTS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
+file(GLOB_RECURSE CROWN_TESTS_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
+
+set (CROWN_TESTS_INCLUDE_DIRS "")
+foreach (_headerFile ${CROWN_TESTS_HEADERS})
+    get_filename_component(_dir ${_headerFile} PATH)
+    list (APPEND CROWN_TESTS_INCLUDE_DIRS ${_dir})
+endforeach()
+list(REMOVE_DUPLICATES CROWN_TESTS_INCLUDE_DIRS)
+
+include_directories(${CROWN_TESTS_INCLUDE_DIRS})
+
+add_executable(crown-tests ${CROWN_TESTS_SOURCES})
+
+#target_link_libraries(crown-tests crown)
+
+install (TARGETS crown-tests DESTINATION tests)

+ 0 - 0
tools/gui/crown-tests/tests.json → tests/tests.json


+ 74 - 13
tools/gui/crown-tests/MainWindow.cs

@@ -5,36 +5,97 @@ using crown_tests.tests;
 
 public partial class MainWindow : Gtk.Window
 {
+  Gtk.TreeStore mTreeStore;
+  Gtk.TreeView mTreeView;
+  Gtk.Entry mEntryTestFolder;
+  Gtk.Entry mEntryCrownTestsExe;
+
+  private TestContainer mContainer;
+
   public MainWindow(): base(Gtk.WindowType.Toplevel)
   {
     //Glade.XML gxml = new Glade.XML("main1.glade", "MyWindow", null);
     //gxml.Autoconnect(this);
     Title = "Test Browser";
     SetSizeRequest(500, 300);
+
+    var table = new Gtk.Table(1, 2, false);
+
+    var configTable = new Gtk.Table(1, 1, false);
+    mEntryTestFolder = new Gtk.Entry();
+    configTable.Attach(mEntryTestFolder, 1, 2, 0, 1);
+    mEntryCrownTestsExe = new Gtk.Entry();
+    configTable.Attach(mEntryCrownTestsExe, 1, 2, 1, 2);
+    var label1 = new Gtk.Label("Tests folder");
+    configTable.Attach(label1, 0, 1, 0, 1);
+    var label2 = new Gtk.Label("crown-tests executable");
+    configTable.Attach(label2, 0, 1, 1, 2);
+    table.Attach(configTable, 0, 2, 0, 1);
+    
+    mTreeView = new Gtk.TreeView();
+    mTreeView.AppendColumn("Name", new Gtk.CellRendererText(), "text", 0);
+    mTreeView.AppendColumn("State", new Gtk.CellRendererText(), "text", 1);
+    //treeview1.AppendColumn("Description", new Gtk.CellRendererText(), "text", 1);
     
-    Gtk.TreeView treeview1 = new Gtk.TreeView();
-    treeview1.AppendColumn("Name", new Gtk.CellRendererText(), "text", 0);
-    treeview1.AppendColumn("Description", new Gtk.CellRendererText(), "text", 1);
-    treeview1.Model = LoadData();
-    Add(treeview1);
+    table.Attach(mTreeView, 0, 1, 1, 3);
+
+    Gtk.Button btnCreate = new Gtk.Button();
+    btnCreate.Label = "Create";
+    btnCreate.Clicked += btnCreate_Click;
+    table.Attach(btnCreate, 1, 2, 1, 2);
 
-    LoadData();
+    Gtk.Button btnExecute = new Gtk.Button();
+    btnExecute.Label = "Execute";
+    btnExecute.Clicked += btnExecute_Click;
+    table.Attach(btnExecute, 1, 2, 2, 3);
+
+    Add(table);
+
+    LoadConfigData();
+    LoadTestsData();
   }
 
-  private Gtk.TreeStore LoadData()
+  private void btnCreate_Click(object o, EventArgs args)
   {
-    var container = JsonConvert.DeserializeObject<TestContainer>(System.IO.File.ReadAllText("tests.json"));
+    var creator = new TestSourceCreator(mContainer, mEntryTestFolder.Text);
+    creator.Create();
+  }
 
-    Gtk.TreeStore testsStore = new Gtk.TreeStore(typeof(string), typeof(string));
-    foreach (var category in container.Categories)
+  private void btnExecute_Click(object o, EventArgs args)
+  {
+    var executor = new TestExecutor(mContainer, mEntryCrownTestsExe.Text);
+    executor.ExecuteAll();
+    RefreshData();
+  }
+
+  private void LoadConfigData()
+  {
+    mEntryTestFolder.Text = @"..\..\..\..\..\tests\";
+    mEntryCrownTestsExe.Text = @"..\..\..\..\..\build\tests\Debug\crown-tests.exe";
+  }
+
+  private void LoadTestsData()
+  {
+    var testsJsonFullfileName = System.IO.Path.Combine(mEntryTestFolder.Text, "tests.json");
+    mContainer = JsonConvert.DeserializeObject<TestContainer>(System.IO.File.ReadAllText(testsJsonFullfileName));
+
+    RefreshData();
+  }
+
+  private void RefreshData()
+  {
+    mTreeStore = new Gtk.TreeStore(typeof(string), typeof(string));
+    //mTreeStore.Clear();
+    foreach (var category in mContainer.Categories)
     {
-      var iter = testsStore.AppendValues(category.Name);
+      var iter = mTreeStore.AppendValues(category.Name);
       foreach (var test in category.Tests)
       {
-        testsStore.AppendValues(iter, test.Name, test.Description);
+        mTreeStore.AppendValues(iter, test.Name, test.LastResult == 0 ? "Passed" : "Failed");
       }
     }
-    return testsStore;
+
+    mTreeView.Model = mTreeStore;
   }
 
   protected override bool OnDeleteEvent(Gdk.Event evnt)

+ 2 - 0
tools/gui/crown-tests/crown-tests.csproj

@@ -64,6 +64,8 @@
     <Compile Include="tests\Test.cs" />
     <Compile Include="tests\TestCategory.cs" />
     <Compile Include="tests\TestContainer.cs" />
+    <Compile Include="tests\TestExecutor.cs" />
+    <Compile Include="tests\TestSourceCreator.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

+ 7 - 0
tools/gui/crown-tests/tests/Test.cs

@@ -13,5 +13,12 @@ namespace crown_tests.tests
     public String Name;
     [JsonProperty]
     public String Description;
+
+    public int LastResult;
+
+    public String GetFunctionName()
+    {
+      return "test_" + Name.ToLower().Replace(' ', '_');
+    }
   }
 }

+ 37 - 0
tools/gui/crown-tests/tests/TestExecutor.cs

@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+
+namespace crown_tests.tests
+{
+  public class TestExecutor
+  {
+    private TestContainer mContainer;
+    private String mExeFullFileName;
+
+    public TestExecutor(TestContainer container, String exeFullFileName)
+    {
+      mContainer = container;
+      mExeFullFileName = exeFullFileName;
+    }
+
+    public void ExecuteAll()
+    {
+      foreach (var category in mContainer.Categories)
+      {
+        foreach (var test in category.Tests)
+        {
+          var p = new Process();
+          p.StartInfo.FileName = mExeFullFileName;
+          p.StartInfo.Arguments = string.Format("/test:\"{0}\"", test.Name);
+          p.Start();
+          p.WaitForExit();
+          test.LastResult = p.ExitCode;
+          System.Threading.Thread.Sleep(1500);
+        }
+      }
+    }
+  }
+}

+ 102 - 0
tools/gui/crown-tests/tests/TestSourceCreator.cs

@@ -0,0 +1,102 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+
+namespace crown_tests.tests
+{
+  public class TestSourceCreator
+  {
+    private TestContainer mContainer;
+    private String mDestFolder;
+
+    public TestSourceCreator(TestContainer container, String destfolder)
+    {
+      mContainer = container;
+      mDestFolder = destfolder;
+    }
+
+    public void Create()
+    {
+      if (!Directory.Exists(mDestFolder))
+        Directory.CreateDirectory(mDestFolder);
+
+      CreateMainFile();
+
+      foreach (var category in mContainer.Categories)
+      {
+        String folder = Path.Combine(mDestFolder, category.Name);
+        if (!Directory.Exists(folder))
+          Directory.CreateDirectory(folder);
+        foreach (var test in category.Tests)
+        {
+          MaybeCreateTestFile(folder, test);
+        }
+      }
+    }
+
+    private void CreateMainFile()
+    {
+      var mainFullFileName = Path.Combine(mDestFolder, "main.cpp");
+
+      String content = "";
+
+      foreach (var category in mContainer.Categories)
+      {
+        content += "//Category '" + category.Name + "'" + Environment.NewLine;
+        foreach (var test in category.Tests)
+        {
+          content += "#include \"" + test.Name + ".h\"" + Environment.NewLine;
+        }
+      }
+
+      content += Environment.NewLine;
+      content += "#include <string.h>" + Environment.NewLine;
+      content += "int main(int argc, char** argv)" + Environment.NewLine;
+      content += "{" + Environment.NewLine;
+      content += "  if (argc < 2) return -1;" + Environment.NewLine;
+      foreach (var category in mContainer.Categories)
+      {
+        foreach (var test in category.Tests)
+        {
+          content += "  if (strcmp(argv[1], \"/test:" + test.Name + "\") == 0)" + Environment.NewLine;
+          content += "  return " + test.GetFunctionName() + "();" + Environment.NewLine;
+        }
+      }
+      content += "  return -2;" + Environment.NewLine;
+      content += "}" + Environment.NewLine;
+      File.WriteAllText(mainFullFileName, content);
+    }
+
+    private void MaybeCreateTestFile(String fullFolderName, Test test)
+    {
+      var headerFullFileName = Path.Combine(fullFolderName, test.Name + ".h");
+      var sourceFullFileName = Path.Combine(fullFolderName, test.Name + ".cpp");
+
+      if (File.Exists(headerFullFileName) || File.Exists(sourceFullFileName))
+        return;
+
+      //Header
+      String headerContent = "";
+      headerContent += "#pragma once" + Environment.NewLine;
+      headerContent += Environment.NewLine;
+      headerContent += "int " + test.GetFunctionName() + "();" + Environment.NewLine;
+      File.WriteAllText(headerFullFileName, headerContent);
+
+      //Source
+      String sourceContent = "";
+      sourceContent += "#include \"" + Path.GetFileName(headerFullFileName) + "\"" + Environment.NewLine;
+      sourceContent += "#include <time.h>" + Environment.NewLine;
+      sourceContent += "#include <stdlib.h>" + Environment.NewLine;
+      sourceContent += Environment.NewLine;
+      sourceContent += "int " + test.GetFunctionName() + "()" + Environment.NewLine;
+      sourceContent += "{" + Environment.NewLine;
+      sourceContent += "  srand(time(NULL));" + Environment.NewLine;
+      sourceContent += "  return rand() % 2;" + Environment.NewLine;
+      sourceContent += "}" + Environment.NewLine;
+      File.WriteAllText(sourceFullFileName, sourceContent);
+    }
+  }
+}
+