[icinga-checkins] icinga.org: icinga2/master: Implement unit tests for the config parser

git at icinga.org git at icinga.org
Sun Nov 23 09:54:16 CET 2014


Module: icinga2
Branch: master
Commit: 81acd34f52042c29317baa712196ce82a99435b6
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=81acd34f52042c29317baa712196ce82a99435b6

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Sun Nov 23 09:53:28 2014 +0100

Implement unit tests for the config parser

refs #7805

---

 lib/config/expression.cpp |    9 ++-
 test/CMakeLists.txt       |    3 +-
 test/config-ops.cpp       |  161 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 170 insertions(+), 3 deletions(-)

diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp
index 8d1e36c..bee6b6b 100644
--- a/lib/config/expression.cpp
+++ b/lib/config/expression.cpp
@@ -248,11 +248,16 @@ Value DictExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
 		dframe = &frame;
 	}
 
+	Value result;
+
 	BOOST_FOREACH(Expression *aexpr, m_Expressions) {
-		aexpr->Evaluate(*dframe, dhint);
+		result = aexpr->Evaluate(*dframe, dhint);
 	}
 
-	return dframe->Self;
+	if (m_Inline)
+		return result;
+	else
+		return dframe->Self;
 }
 
 Value SetExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index beacbf5..71fd610 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -22,7 +22,7 @@ set(base_test_SOURCES
   base-json.cpp base-match.cpp base-netstring.cpp base-object.cpp
   base-serialize.cpp base-shellescape.cpp base-stacktrace.cpp
   base-stream.cpp base-string.cpp base-timer.cpp base-type.cpp
-  base-value.cpp icinga-perfdata.cpp test.cpp
+  base-value.cpp config-ops.cpp icinga-perfdata.cpp test.cpp
 )
 
 set_property(SOURCE test.cpp PROPERTY EXCLUDE_UNITY_BUILD TRUE)
@@ -84,6 +84,7 @@ add_boost_test(base
         base_value/scalar
         base_value/convert
         base_value/format
+	config_ops/simple
 	icinga_perfdata/simple
 	icinga_perfdata/multiple
 	icinga_perfdata/uom
diff --git a/test/config-ops.cpp b/test/config-ops.cpp
new file mode 100644
index 0000000..a6c1da7
--- /dev/null
+++ b/test/config-ops.cpp
@@ -0,0 +1,161 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
+ *                                                                            *
+ * This program is free software; you can redistribute it and/or              *
+ * modify it under the terms of the GNU General Public License                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program 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 General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#include "config/configcompiler.hpp"
+#include <boost/test/unit_test.hpp>
+
+using namespace icinga;
+
+BOOST_AUTO_TEST_SUITE(config_ops)
+
+BOOST_AUTO_TEST_CASE(simple)
+{
+	VMFrame frame;
+	Expression *expr;
+	Dictionary::Ptr dict;
+
+	expr = ConfigCompiler::CompileText("<test>", "1 + 3");
+	BOOST_CHECK(expr->Evaluate(frame) == 4);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "3 - 1");
+	BOOST_CHECK(expr->Evaluate(frame) == 2);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "5m * 10");
+	BOOST_CHECK(expr->Evaluate(frame) == 3000);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "5m / 5");
+	BOOST_CHECK(expr->Evaluate(frame) == 60);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "7 & 3");
+	BOOST_CHECK(expr->Evaluate(frame) == 3);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "2 | 3");
+	BOOST_CHECK(expr->Evaluate(frame) == 3);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "true && false");
+	BOOST_CHECK(!expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "true || false");
+	BOOST_CHECK(expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "3 < 5");
+	BOOST_CHECK(expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "3 > 5");
+	BOOST_CHECK(!expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "3 <= 3");
+	BOOST_CHECK(expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "3 >= 3");
+	BOOST_CHECK(expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "2 + 3 * 4");
+	BOOST_CHECK(expr->Evaluate(frame) == 14);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "(2 + 3) * 4");
+	BOOST_CHECK(expr->Evaluate(frame) == 20);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "!0 == true");
+	BOOST_CHECK(expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "!0 == true");
+	BOOST_CHECK(expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "4 << 8");
+	BOOST_CHECK(expr->Evaluate(frame) == 1024);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "1024 >> 4");
+	BOOST_CHECK(expr->Evaluate(frame) == 64);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "2 << 3 << 4");
+	BOOST_CHECK(expr->Evaluate(frame) == 256);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "256 >> 4 >> 3");
+	BOOST_CHECK(expr->Evaluate(frame) == 2);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "\"hello\" == \"hello\"");
+	BOOST_CHECK(expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "\"hello\" != \"hello\"");
+	BOOST_CHECK(!expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "\"foo\" in [ \"foo\", \"bar\" ]");
+	BOOST_CHECK(expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "\"foo\" in [ \"bar\", \"baz\" ]");
+	BOOST_CHECK(!expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "\"foo\" in null");
+	BOOST_CHECK(!expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "\"foo\" in \"bar\"");
+	BOOST_CHECK_THROW(expr->Evaluate(frame), ConfigError);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in [ \"bar\", \"baz\" ]");
+	BOOST_CHECK(expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in [ \"foo\", \"bar\" ]");
+	BOOST_CHECK(!expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in null");
+	BOOST_CHECK(expr->Evaluate(frame));
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in \"bar\"");
+	BOOST_CHECK_THROW(expr->Evaluate(frame), ConfigError);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "{ a += 3 }");
+	dict = expr->Evaluate(frame);
+	delete expr;
+	BOOST_CHECK(dict->GetLength() == 1);
+	BOOST_CHECK(dict->Get("a") == 3);
+
+}
+
+BOOST_AUTO_TEST_SUITE_END()



More information about the icinga-checkins mailing list