Skip to content

Commit

Permalink
add test cases and allow to clear an error state
Browse files Browse the repository at this point in the history
Signed-off-by: Denys Smirnov <[email protected]>
  • Loading branch information
Denys Smirnov authored and dennwc committed Jun 20, 2019
1 parent fc6b81d commit 6086f5a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/libuast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions src/src_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions tests/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) { \
Expand Down Expand Up @@ -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);
Expand Down
44 changes: 44 additions & 0 deletions tests/src_index_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef LIBUAST_SRC_INDEX_TEST_H_
#define LIBUAST_SRC_INDEX_TEST_H_

#include <cstdio>
#include <cstring>

extern "C" {
#include <CUnit/Basic.h>
}

#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_

0 comments on commit 6086f5a

Please sign in to comment.