From 2859834e58e37e7b15a15f7df60feef3e1527c97 Mon Sep 17 00:00:00 2001 From: Balajiganapathi S Date: Sun, 29 Oct 2017 13:38:40 +0530 Subject: Suggest alternatives when identifier not found. --- test/libsolidity/SolidityNameAndTypeResolution.cpp | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'test') diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index e757c755..51d81a28 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2081,6 +2081,93 @@ BOOST_AUTO_TEST_CASE(external_visibility) CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier."); } +BOOST_AUTO_TEST_CASE(similar_name_suggestions_expected) +{ + char const* sourceCode = R"( + contract c { + function func() {} + function g() public { fun(); } + } + )"; + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean func?"); +} + +BOOST_AUTO_TEST_CASE(no_name_suggestion) +{ + char const* sourceCode = R"( + contract c { + function g() public { fun(); } + } + )"; + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier."); +} + +BOOST_AUTO_TEST_CASE(multiple_similar_suggestions) +{ + char const* sourceCode = R"( + contract c { + function g() public { + uint var1 = 1; + uint var2 = 1; + uint var3 = 1; + uint var4 = 1; + uint var5 = varx; + } + } + )"; + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean var1, var2, var3, var4 or var5?"); +} + +BOOST_AUTO_TEST_CASE(multiple_scopes_suggestions) +{ + char const* sourceCode = R"( + contract c { + uint log9 = 2; + function g() public { + uint log8 = 3; + uint var1 = lgox; + } + } + )"; + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean log8, log9, log0, log1, log2, log3 or log4?"); +} + +BOOST_AUTO_TEST_CASE(inheritence_suggestions) +{ + char const* sourceCode = R"( + contract a { function func() public {} } + contract c is a { + function g() public { + uint var1 = fun(); + } + } + )"; + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean func?"); +} + +BOOST_AUTO_TEST_CASE(no_spurious_suggestions) +{ + char const* sourceCode = R"( + contract c { + function g() public { + uint va = 1; + uint vb = vaxyz; + } + } + )"; + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier."); + + sourceCode = R"( + contract c { + function g() public { + uint va = 1; + uint vb = x; + } + } + )"; + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier."); +} + BOOST_AUTO_TEST_CASE(external_base_visibility) { char const* sourceCode = R"( -- cgit v1.2.3 From b1417b318f3b3cbfd0bf79a4cabc656825a84e5a Mon Sep 17 00:00:00 2001 From: Balajiganapathi S Date: Fri, 17 Nov 2017 21:41:15 +0530 Subject: Move string distance function to utils and format error message --- test/libsolidity/SolidityNameAndTypeResolution.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 51d81a28..c4741657 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2089,7 +2089,7 @@ BOOST_AUTO_TEST_CASE(similar_name_suggestions_expected) function g() public { fun(); } } )"; - CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean func?"); + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean \"func\"?"); } BOOST_AUTO_TEST_CASE(no_name_suggestion) @@ -2115,7 +2115,7 @@ BOOST_AUTO_TEST_CASE(multiple_similar_suggestions) } } )"; - CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean var1, var2, var3, var4 or var5?"); + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean \"var1\", \"var2\", \"var3\", \"var4\" or \"var5\"?"); } BOOST_AUTO_TEST_CASE(multiple_scopes_suggestions) @@ -2129,7 +2129,7 @@ BOOST_AUTO_TEST_CASE(multiple_scopes_suggestions) } } )"; - CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean log8, log9, log0, log1, log2, log3 or log4?"); + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean \"log8\", \"log9\", \"log0\", \"log1\", \"log2\", \"log3\" or \"log4\"?"); } BOOST_AUTO_TEST_CASE(inheritence_suggestions) @@ -2142,7 +2142,7 @@ BOOST_AUTO_TEST_CASE(inheritence_suggestions) } } )"; - CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean func?"); + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean \"func\"?"); } BOOST_AUTO_TEST_CASE(no_spurious_suggestions) -- cgit v1.2.3 From d123e777d33be3134ebbcda969f149e0e7ad0b0f Mon Sep 17 00:00:00 2001 From: Balajiganapathi S Date: Sat, 18 Nov 2017 12:24:17 +0530 Subject: Add tests for similarity routine --- test/libdevcore/StringUtils.cpp | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 test/libdevcore/StringUtils.cpp (limited to 'test') diff --git a/test/libdevcore/StringUtils.cpp b/test/libdevcore/StringUtils.cpp new file mode 100644 index 00000000..7a5fe26e --- /dev/null +++ b/test/libdevcore/StringUtils.cpp @@ -0,0 +1,56 @@ +/* + This file is part of solidity. + + solidity 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 3 of the License, or + (at your option) any later version. + + solidity 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 solidity. If not, see . +*/ +/** + * Unit tests for the StringUtils routines. + */ + +#include + +#include "../TestHelper.h" + +using namespace std; + +namespace dev +{ +namespace test +{ + +BOOST_AUTO_TEST_SUITE(StringUtils) + +BOOST_AUTO_TEST_CASE(test_similarity) +{ + BOOST_CHECK_EQUAL(stringWithinDistance("hello", "hello", 0), true); + BOOST_CHECK_EQUAL(stringWithinDistance("hello", "hello", 1), true); + BOOST_CHECK_EQUAL(stringWithinDistance("hello", "hellw", 1), true); + BOOST_CHECK_EQUAL(stringWithinDistance("hello", "helol", 1), true); + BOOST_CHECK_EQUAL(stringWithinDistance("hello", "helo", 1), true); + BOOST_CHECK_EQUAL(stringWithinDistance("hello", "helllo", 1), true); + BOOST_CHECK_EQUAL(stringWithinDistance("hello", "hlllo", 1), true); + BOOST_CHECK_EQUAL(stringWithinDistance("hello", "hllllo", 1), false); + BOOST_CHECK_EQUAL(stringWithinDistance("hello", "hllllo", 2), true); + BOOST_CHECK_EQUAL(stringWithinDistance("hello", "hlllo", 2), true); + BOOST_CHECK_EQUAL(stringWithinDistance("a", "", 2), false); + BOOST_CHECK_EQUAL(stringWithinDistance("abc", "ba", 2), false); + BOOST_CHECK_EQUAL(stringWithinDistance("abc", "abcdef", 2), false); + BOOST_CHECK_EQUAL(stringWithinDistance("abcd", "wxyz", 2), false); + BOOST_CHECK_EQUAL(stringWithinDistance("", "", 2), true); +} + +BOOST_AUTO_TEST_SUITE_END() + +} +} -- cgit v1.2.3 From 8a491c77ba9680afdf8c33664e905b978152b095 Mon Sep 17 00:00:00 2001 From: Balajiganapathi S Date: Tue, 21 Nov 2017 22:20:35 +0530 Subject: Restructure code for alternative identifier suggestions --- test/libdevcore/StringUtils.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'test') diff --git a/test/libdevcore/StringUtils.cpp b/test/libdevcore/StringUtils.cpp index 7a5fe26e..597457cc 100644 --- a/test/libdevcore/StringUtils.cpp +++ b/test/libdevcore/StringUtils.cpp @@ -50,6 +50,38 @@ BOOST_AUTO_TEST_CASE(test_similarity) BOOST_CHECK_EQUAL(stringWithinDistance("", "", 2), true); } +BOOST_AUTO_TEST_CASE(test_dldistance) +{ + BOOST_CHECK_EQUAL(stringDistance("hello", "hellw"), 1); + BOOST_CHECK_EQUAL(stringDistance("hello", "helol"), 1); + BOOST_CHECK_EQUAL(stringDistance("hello", "helo"), 1); + BOOST_CHECK_EQUAL(stringDistance("hello", "helllo"), 1); + BOOST_CHECK_EQUAL(stringDistance("hello", "hlllo"), 1); + BOOST_CHECK_EQUAL(stringDistance("hello", "hllllo"), 2); + BOOST_CHECK_EQUAL(stringDistance("a", ""), 1); + BOOST_CHECK_EQUAL(stringDistance("abc", "ba"), 2); + BOOST_CHECK_EQUAL(stringDistance("abc", "abcdef"), 3); + BOOST_CHECK_EQUAL(stringDistance("abcd", "wxyz"), 4); + BOOST_CHECK_EQUAL(stringDistance("", ""), 0); + BOOST_CHECK_EQUAL(stringDistance("abcdefghijklmnopqrstuvwxyz", "abcabcabcabcabcabcabcabca"), 23); + +} + +BOOST_AUTO_TEST_CASE(test_alternatives_list) +{ + vector strings; + BOOST_CHECK_EQUAL(quotedAlternativesList(strings), ""); + strings.push_back("a"); + BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "\"a\""); + strings.push_back("b"); + BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "\"a\" or \"b\""); + strings.push_back("c"); + BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "\"a\", \"b\" or \"c\""); + strings.push_back("d"); + BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "\"a\", \"b\", \"c\" or \"d\""); +} + + BOOST_AUTO_TEST_SUITE_END() } -- cgit v1.2.3