From 6086f5a36e5ecd7b7968720996417d497a344804 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Thu, 13 Jun 2019 19:43:36 +0300 Subject: [PATCH] add test cases and allow to clear an error state Signed-off-by: Denys Smirnov --- src/libuast.hpp | 7 +++++-- src/src_index.go | 8 ++++++++ tests/main.cc | 2 ++ tests/src_index_test.h | 44 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/src_index_test.h diff --git a/src/libuast.hpp b/src/libuast.hpp index f891aa2..3f5037a 100644 --- a/src/libuast.hpp +++ b/src/libuast.hpp @@ -642,10 +642,13 @@ namespace uast { UastSourceIndexFree(idx); idx = nullptr; } - // CheckError throws a last encountered error, if any. + // CheckError throws a last encountered error, if any, and clear the error state of an index. void CheckError() { char* err = UastSourceIndex_LastError(idx); - if (err) throw std::runtime_error(err); + if (err) { + UastSourceIndex_ClearError(idx); + throw std::runtime_error(err); + } } // FromLineCol converts one-based line-column pair (in bytes) in the indexed // source file to a zero-based byte offset. It return -1 in case of failure. diff --git a/src/src_index.go b/src/src_index.go index 836b0a2..9f8ff73 100644 --- a/src/src_index.go +++ b/src/src_index.go @@ -90,6 +90,14 @@ func UastSourceIndex_LastError(idx *C.UastSourceIndex) *C.char { return C.CString(err.Error()) } +//export UastSourceIndex_ClearError +// UastSourceIndex_ClearError clears an error state. +func UastSourceIndex_ClearError(idx *C.UastSourceIndex) { + if index := getSourceIndexFrom(idx); index != nil { + index.setError(nil) + } +} + // getSourceIndexFrom returns an index for a given C.UastSourceIndex pointer. It returns nil if C structure is invalid. func getSourceIndexFrom(idx *C.UastSourceIndex) *sourceIndex { if idx == nil || idx.handle == 0 { diff --git a/tests/main.cc b/tests/main.cc index 72f2363..71f5528 100644 --- a/tests/main.cc +++ b/tests/main.cc @@ -5,6 +5,7 @@ extern "C" { #include "nodes_test.h" #include "roles_test.h" +#include "src_index_test.h" #define ADD_TEST(__SUITE__, __MESSAGE__, __FUNC__) \ if (CU_add_test(__SUITE__, __MESSAGE__, __FUNC__) == NULL) { \ @@ -95,6 +96,7 @@ int main() { ADD_TEST(suite, "test of UastFilter() with bad query", TestUastFilterBadQuery); ADD_TEST(suite, "test failing UastFilter() (bad Xpath)", TestXpath); ADD_TEST(suite, "test empty UastFilter() result", TestEmptyResult); + ADD_TEST(suite, "test source index", TestSourceIndex); // run all tests using the CUnit Basic interface CU_basic_set_mode(CU_BRM_VERBOSE); diff --git a/tests/src_index_test.h b/tests/src_index_test.h new file mode 100644 index 0000000..2ba059f --- /dev/null +++ b/tests/src_index_test.h @@ -0,0 +1,44 @@ +#ifndef LIBUAST_SRC_INDEX_TEST_H_ +#define LIBUAST_SRC_INDEX_TEST_H_ + +#include +#include + +extern "C" { +#include +} + +#include "libuast.h" + +static void TestSourceIndex(void) { + const char src[] = "tёst\ndata\n"; + UastSourceIndex *idx = UastSourceIndexNew((void*)src, sizeof(src)); + CU_ASSERT_FATAL(idx != NULL); + + // Conversions to different offsets + CU_ASSERT_EQUAL_FATAL(UastSourceIndex_FromUnicode(idx, 0), 0); + CU_ASSERT_EQUAL_FATAL(UastSourceIndex_FromUnicode(idx, 2), 3); + CU_ASSERT_EQUAL_FATAL(UastSourceIndex_FromLineCol(idx, 2, 1), 6); + + // Conversion to line-column + UastLineCol lc = UastSourceIndex_ToLineCol(idx, 8); + CU_ASSERT_EQUAL_FATAL(lc.line, 2); + CU_ASSERT_EQUAL_FATAL(lc.col, 3); + + // There should be no error this far + CU_ASSERT_FATAL(UastSourceIndex_LastError(idx) == NULL); + + // Invalid offset should lead to -1 + CU_ASSERT_EQUAL_FATAL(UastSourceIndex_ToUnicode(idx, sizeof(src)+1), -1); + char* err = UastSourceIndex_LastError(idx); + CU_ASSERT_FATAL(err != NULL); + delete(err); + + // Clear error state + UastSourceIndex_ClearError(idx); + CU_ASSERT_FATAL(UastSourceIndex_LastError(idx) == NULL); + + UastSourceIndexFree(idx); +} + +#endif // LIBUAST_SRC_INDEX_TEST_H_