Просмотр исходного кода

Read test info from tests.json and display it in TreeView

Dexter89 12 лет назад
Родитель
Сommit
bdc35cbedd

+ 21 - 12
tools/gui/crown-tests/MainWindow.cs

@@ -1,5 +1,7 @@
 using System;
 using Gtk;
+using Newtonsoft.Json;
+using crown_tests.tests;
 
 public partial class MainWindow : Gtk.Window
 {
@@ -9,7 +11,6 @@ public partial class MainWindow : Gtk.Window
     //gxml.Autoconnect(this);
     Title = "Test Browser";
     SetSizeRequest(500, 300);
-
     
     // Create a column for the artist name
     Gtk.TreeViewColumn artistColumn = new Gtk.TreeViewColumn();
@@ -25,17 +26,7 @@ public partial class MainWindow : Gtk.Window
     treeview1.AppendColumn(artistColumn);
     treeview1.AppendColumn(songColumn);
 
-    Gtk.TreeStore testsStore = new Gtk.TreeStore(typeof(string), typeof(string));
-
-    Gtk.TreeIter iter = testsStore.AppendValues("FileSystem");
-    testsStore.AppendValues(iter, "Test 1", "Test file existence");
-    testsStore.AppendValues(iter, "Test 2", "Create empty file");
-
-    iter = testsStore.AppendValues("Json");
-    testsStore.AppendValues(iter, "Test 3", "Loading a simple file");
-    testsStore.AppendValues(iter, "Test 4", "Saving a simple file");
-
-    treeview1.Model = testsStore;
+    treeview1.Model = LoadData();
 
     Gtk.CellRendererText testNameCell = new Gtk.CellRendererText();
     artistColumn.PackStart(testNameCell, true);
@@ -46,6 +37,24 @@ public partial class MainWindow : Gtk.Window
     // Tell the Cell Renderers which items in the model to display
     artistColumn.AddAttribute(testNameCell, "text", 0);
     songColumn.AddAttribute(testDescriptionCell, "text", 1);
+
+    LoadData();
+  }
+
+  private Gtk.TreeStore LoadData()
+  {
+    var container = JsonConvert.DeserializeObject<TestContainer>(System.IO.File.ReadAllText("tests.json"));
+
+    Gtk.TreeStore testsStore = new Gtk.TreeStore(typeof(string), typeof(string));
+    foreach (var category in container.Categories)
+    {
+      var iter = testsStore.AppendValues(category.Name);
+      foreach (var test in category.Tests)
+      {
+        testsStore.AppendValues(iter, test.Name, test.Description);
+      }
+    }
+    return testsStore;
   }
 
   protected override bool OnDeleteEvent(Gdk.Event evnt)

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

@@ -33,6 +33,9 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Newtonsoft.Json">
+      <HintPath>..\..\third\Newtonsoft.Json.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="gtk-sharp, Version=2.4.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
       <SpecificVersion>False</SpecificVersion>
@@ -58,6 +61,7 @@
     <Compile Include="MainWindow.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="tests\Test.cs" />
     <Compile Include="tests\TestCategory.cs" />
     <Compile Include="tests\TestContainer.cs" />
   </ItemGroup>

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

@@ -0,0 +1,32 @@
+{
+	"Categories" : [
+    {
+      "Name" : "Filesystem",
+      "Description" : "Filesystem access with OS-dependent implementation",
+      "Tests" : [
+        { 
+          "Name" : "FileCreateThenFileExists",
+          "Description" : "After file_create, file_exists returns true"
+        },
+        { 
+          "Name" : "FileDeleteThenFileExists",
+          "Description" : "After file_delete, file_exists returns false"
+        }
+      ]
+    },
+    {
+      "Name" : "Json",
+      "Description" : "Reading and Writing objects in Json format",
+      "Tests" : [
+        { 
+          "Name" : "SerializeObjectWithTwoProperties",
+          "Description" : "Serializaton of an object with an int and a string property"
+        },
+        { 
+          "Name" : "DeserializeObjectWithTwoProperties",
+          "Description" : "Deserializaton of an object with an int and a string property"
+        }
+      ]
+    }
+  ]
+}

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

@@ -0,0 +1,17 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace crown_tests.tests
+{
+  [JsonObject(MemberSerialization.OptIn)]
+  public class Test
+  {
+    [JsonProperty]
+    public String Name;
+    [JsonProperty]
+    public String Description;
+  }
+}

+ 18 - 2
tools/gui/crown-tests/tests/TestCategory.cs

@@ -1,11 +1,27 @@
-using System;
+using Newtonsoft.Json;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
 namespace crown_tests.tests
 {
-  class TestCategory
+  [JsonObject(MemberSerialization.OptIn)]
+  public class TestCategory
   {
+    [JsonProperty]
+    public String Name;
+    [JsonProperty]
+    public String Description;
+    [JsonProperty]
+    public List<Test> Tests;
+
+    public TestCategory(String name, String description)
+    {
+      Tests = new List<Test>();
+      Name = name;
+      Description = description;
+    }
+
   }
 }

+ 10 - 1
tools/gui/crown-tests/tests/TestContainer.cs

@@ -1,11 +1,20 @@
-using System;
+using Newtonsoft.Json;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
 namespace crown_tests.tests
 {
+  [JsonObject(MemberSerialization.OptIn)]
   public class TestContainer
   {
+    [JsonProperty]
+    public List<TestCategory> Categories;
+
+    public TestContainer()
+    {
+      Categories = new List<TestCategory>();
+    }
   }
 }

BIN
tools/third/Newtonsoft.Json.dll