From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1Q5H3I-0006vq-JN for garchives@archives.gentoo.org; Thu, 31 Mar 2011 12:32:40 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 04001E0686; Thu, 31 Mar 2011 12:32:32 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id ABE4BE0687 for ; Thu, 31 Mar 2011 12:32:32 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 24A241B41A3 for ; Thu, 31 Mar 2011 12:32:32 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 4042D80065 for ; Thu, 31 Mar 2011 12:32:31 +0000 (UTC) From: "Petteri Räty" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Petteri Räty" Message-ID: Subject: [gentoo-commits] proj/libbash:master commit in: test/ X-VCS-Repository: proj/libbash X-VCS-Files: test/walker_test.cpp X-VCS-Directories: test/ X-VCS-Committer: betelgeuse X-VCS-Committer-Name: Petteri Räty X-VCS-Revision: f7c40576759f562a3911b2adbc49eabb4e94a0e4 Date: Thu, 31 Mar 2011 12:32:31 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 26caff71fcda7a33eb12441796e58a28 commit: f7c40576759f562a3911b2adbc49eabb4e94a0e4 Author: Mu Qiao gentoo org> AuthorDate: Thu Mar 31 03:13:53 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Thu Mar 31 08:29:31 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3Df7c40576 Refactor unit test to allow adding new rules Extract some logic into subclass in order to make it possible to add new rules. --- test/walker_test.cpp | 67 +++++++++++++++++++++++++++-----------------= ----- 1 files changed, 37 insertions(+), 30 deletions(-) diff --git a/test/walker_test.cpp b/test/walker_test.cpp index 41720ad..c026605 100644 --- a/test/walker_test.cpp +++ b/test/walker_test.cpp @@ -38,10 +38,9 @@ class walker_test: public ::testing::Test pANTLR3_INPUT_STREAM input; plibbashLexer lxr; pANTLR3_COMMON_TOKEN_STREAM tstream; - plibbashParser psr; - libbashParser_arithmetics_return langAST; - pANTLR3_COMMON_TREE_NODE_STREAM nodes; protected: + pANTLR3_COMMON_TREE_NODE_STREAM nodes; + plibbashParser psr; virtual void SetUp() { walker =3D shared_ptr(new interpreter); @@ -55,30 +54,13 @@ protected: lxr->free(lxr); input->close(input); } - void init_walker(const char* script); + void init_parser(const char*); public: plibbashWalker treePsr; shared_ptr walker; - - int run_arithmetic(const char* script) - { - init_walker(script); - return treePsr->arithmetics(treePsr); - } - - void check_arithmetic_assignment(const char* script, - const string& name, - int exp_value) - { - // the return value of the arithmetic expression should be equal to - // the new value of the variable - EXPECT_EQ(exp_value, run_arithmetic(script)); - EXPECT_EQ(exp_value, walker->resolve(name)); - } }; =20 - -void walker_test::init_walker(const char *script){ +void walker_test::init_parser(const char *script){ =20 auto start =3D reinterpret_cast(const_cast(scri= pt)); input =3D antlr3NewAsciiStringInPlaceStream(start, @@ -115,17 +97,42 @@ void walker_test::init_walker(const char *script){ ANTLR3_FPRINTF(stderr, "Out of memory trying to allocate parser\n"); FAIL(); } +} =20 - langAST =3D psr->arithmetics(psr); - nodes =3D antlr3CommonTreeNodeStreamNewTree(langAST.tree, +class arithmetic_walker: public walker_test +{ + libbashParser_arithmetics_return langAST; +protected: + void init_walker(const char* script) + { + init_parser(script); + langAST =3D psr->arithmetics(psr); + nodes =3D antlr3CommonTreeNodeStreamNewTree(langAST.tree, ANTLR3_SIZE_HINT); - treePsr =3D libbashWalkerNew(nodes); - walker->define("value", 100); - set_interpreter(walker); -} + treePsr =3D libbashWalkerNew(nodes); + walker->define("value", 100); + set_interpreter(walker); + } + + int run_arithmetic(const char* script) + { + init_walker(script); + return treePsr->arithmetics(treePsr); + } + + void check_arithmetic_assignment(const char* script, + const string& name, + int exp_value) + { + // the return value of the arithmetic expression should be equal to + // the new value of the variable + EXPECT_EQ(exp_value, run_arithmetic(script)); + EXPECT_EQ(exp_value, walker->resolve(name)); + } +}; =20 #define TEST_BINARY_ARITHMETIC(name, script, exp_value)\ - TEST_F(walker_test, name)\ + TEST_F(arithmetic_walker, name)\ {EXPECT_EQ(exp_value, run_arithmetic(script));} =20 TEST_BINARY_ARITHMETIC(logicor_true, "0 || -2", 1) @@ -167,7 +174,7 @@ TEST_BINARY_ARITHMETIC(complex_cal2, "10*${val= ue}<<3%2**5", 8000) TEST_BINARY_ARITHMETIC(complex_cal3, "(20&5|3||1*100-20&5*10)+~(2= *5)", -10) =20 #define TEST_ARITHMETIC_ASSIGNMENT(name, script, var_name, exp_value)\ - TEST_F(walker_test, name) \ + TEST_F(arithmetic_walker, name) \ { check_arithmetic_assignment(script, var_name, exp_value); } =20 TEST_ARITHMETIC_ASSIGNMENT(assignment, "new_var=3D10", "ne= w_var", 10)