[kgraphviewer-devel] [KGraphViewer/libkgraphviz] 27bfa20: Add tests for GraphIO.

Kevin Funk krf at electrostorm.net
Mon Dec 13 18:18:33 CET 2010


	A	 tests/kgraphviz/graphiotests.cpp	 [License: UNKNOWN]


	A	 tests/kgraphviz/examples/undirected-acyclic-graph.dot	 [License: Trivialfile.]


	A	 tests/kgraphviz/examples/directed-acyclic-graph.dot	 [License: Trivialfile.]


	A	 tests/kgraphviz/examples/CMakeLists.txt	 [License: Trivialfile.]

commit 27bfa2056329cff61242e2553ebe8cc764b10063
Author: Kevin Funk <krf at electrostorm.net>
Date:   Mon Dec 13 16:43:21 2010 +0100

    Add tests for GraphIO.
    
    * Also add example dot files for testing purposes.

diff --git a/tests/kgraphviz/CMakeLists.txt b/tests/kgraphviz/CMakeLists.txt
index f391430..cac5983 100644
--- a/tests/kgraphviz/CMakeLists.txt
+++ b/tests/kgraphviz/CMakeLists.txt
@@ -9,6 +9,13 @@ link_directories(
 set(dotgraphtests_SRCS
   dotgraphtests.cpp
 )
-
 kde4_add_unit_test(dotgraphtests ${dotgraphtests_SRCS} )
-target_link_libraries(dotgraphtests ${KDE4_KDECORE_LIBS} ${QT_QTTEST_LIBRARY} kgraphviz)
+target_link_libraries(dotgraphtests ${QT_QTTEST_LIBRARY} kgraphviz)
+
+set(graphiotests_SRCS
+  graphiotests.cpp
+)
+kde4_add_unit_test(graphiotests ${graphiotests_SRCS} )
+target_link_libraries(graphiotests ${QT_QTTEST_LIBRARY} kgraphviz)
+
+add_subdirectory(examples)
diff --git a/tests/kgraphviz/examples/CMakeLists.txt b/tests/kgraphviz/examples/CMakeLists.txt
new file mode 100644
index 0000000..451a48b
--- /dev/null
+++ b/tests/kgraphviz/examples/CMakeLists.txt
@@ -0,0 +1,2 @@
+install( FILES directed-acyclic-graph.dot DESTINATION ${CMAKE_CURRENT_BINARY_DIR} )
+install( FILES undirected-acyclic-graph.dot DESTINATION ${CMAKE_CURRENT_BINARY_DIR} )
diff --git a/tests/kgraphviz/examples/directed-acyclic-graph.dot b/tests/kgraphviz/examples/directed-acyclic-graph.dot
new file mode 100644
index 0000000..c0763b3
--- /dev/null
+++ b/tests/kgraphviz/examples/directed-acyclic-graph.dot
@@ -0,0 +1,4 @@
+digraph graphname {
+    a -> b -> c;
+    b -> d;
+}
diff --git a/tests/kgraphviz/examples/undirected-acyclic-graph.dot b/tests/kgraphviz/examples/undirected-acyclic-graph.dot
new file mode 100644
index 0000000..a739e6d
--- /dev/null
+++ b/tests/kgraphviz/examples/undirected-acyclic-graph.dot
@@ -0,0 +1,4 @@
+graph graphname {
+    a -- b -- c;
+    b -- d;
+}
diff --git a/tests/kgraphviz/graphiotests.cpp b/tests/kgraphviz/graphiotests.cpp
new file mode 100644
index 0000000..78d1148
--- /dev/null
+++ b/tests/kgraphviz/graphiotests.cpp
@@ -0,0 +1,101 @@
+/*
+    Copyright (C) 2010 Kevin Funk <krf at electrostorm.net>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Library General Public
+    License as published by the Free Software Foundation; either
+    version 2 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Library General Public License for more details.
+
+    You should have received a copy of the GNU Library General Public License
+    along with this library; see the file COPYING.LIB.  If not, write to
+    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+    Boston, MA 02110-1301, USA.
+*/
+
+#include <kgraphviz/graphio.h>
+#include <kgraphviz/dotgraph.h>
+
+#include <qtest_kde.h>
+
+#include <KDebug>
+
+#define TEST_FILENAME "/tmp/kgraphviz-test.dot"
+
+class GraphIOTests : public QObject
+{
+  Q_OBJECT
+
+private Q_SLOTS:
+  void testImportGraphFromFile();
+  void testImportGraphFromInvalidFile();
+
+  void testExportImportGraph();
+};
+
+using namespace KGraphViz;
+
+void GraphIOTests::testImportGraphFromFile()
+{
+  GraphIO io;
+  io.loadFromDotFile("examples/directed-acyclic-graph.dot");
+  QVERIFY(QTest::kWaitForSignal(&io, SIGNAL(finished()), 3000) == true);
+
+  DotGraph* graph = io.readData();
+  QVERIFY(graph != 0);
+  QVERIFY(graph->nodes().size() == 4);
+  QVERIFY(graph->edges().size() == 3);
+}
+
+void GraphIOTests::testImportGraphFromInvalidFile()
+{
+  GraphIO io;
+  io.loadFromDotFile("examples/DOES_NOT_EXIST.abc");
+  QVERIFY(QTest::kWaitForSignal(&io, SIGNAL(error(QString)), 3000) == true);
+
+  DotGraph* graph = io.readData();
+  QVERIFY(graph == 0);
+}
+
+void GraphIOTests::testExportImportGraph()
+{
+  // populate and save graph to file
+  {
+    // populate
+    QMap <QString, QString> map;
+    DotGraph graph;
+    map["id"] = "State1";
+    graph.addNewNode(map);
+    map["id"] = "State2";
+    graph.addNewNode(map);
+    map.clear();
+    graph.addNewEdge("State1", "State2", map);
+
+    // save back
+    GraphIO io;
+    io.saveToDotFile(&graph, TEST_FILENAME);
+  }
+
+  // read graph from dot file and check attributes
+  {
+    GraphIO io;
+    io.loadFromDotFile(TEST_FILENAME);
+    QSignalSpy spy(&io, SIGNAL(finished()));
+    QVERIFY(QTest::kWaitForSignal(&io, SIGNAL(finished()), 3000) == true);
+
+    DotGraph* graph = io.readData();
+    QVERIFY(graph != 0);
+    QVERIFY(graph->nodes().size() == 2);
+    QVERIFY(graph->edges().size() == 1);
+  }
+
+  // cleanup
+  QFile::remove(TEST_FILENAME);
+}
+
+QTEST_KDEMAIN_CORE(GraphIOTests)
+#include "graphiotests.moc"


More information about the kgraphviewer-devel mailing list