From 64024d0fa6a6dc048b2b846738846da597025f56 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 25 Feb 2021 11:16:13 -0500 Subject: Leave statement caret at the start of literals This allows a precise location to be reported for errors within literals, by adding the offset of the error in the literal to the caret. This will be used to report nice errors for things like regular expressions and supported XSD datatypes. --- test/test_reader.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'test/test_reader.c') diff --git a/test/test_reader.c b/test/test_reader.c index dd090d83..b5cefbe1 100644 --- a/test/test_reader.c +++ b/test/test_reader.c @@ -3,12 +3,16 @@ #undef NDEBUG +#include "serd/caret.h" #include "serd/event.h" #include "serd/input_stream.h" +#include "serd/node.h" #include "serd/reader.h" #include "serd/sink.h" +#include "serd/statement.h" #include "serd/status.h" #include "serd/stream.h" +#include "serd/string_view.h" #include "serd/syntax.h" #include "serd/world.h" #include "zix/allocator.h" @@ -21,6 +25,7 @@ #endif #include +#include #include #include @@ -486,6 +491,56 @@ test_read_empty(const char* const path) serd_world_free(world); } +static SerdStatus +check_cursor(void* handle, const SerdEvent* event) +{ + bool* const called = (bool*)handle; + + if (event->type == SERD_STATEMENT) { + const SerdCaret* const caret = + serd_statement_caret(event->statement.statement); + assert(caret); + + assert(!strcmp(serd_node_string(serd_caret_document(caret)), "string")); + assert(serd_caret_line(caret) == 1); + assert(serd_caret_column(caret) == 47); + } + + *called = true; + return SERD_SUCCESS; +} + +static void +test_error_cursor(void) +{ + SerdWorld* world = serd_world_new(); + bool called = false; + SerdSink* sink = serd_sink_new(&called, check_cursor, NULL); + SerdReader* const reader = serd_reader_new(world, SERD_TURTLE, 0, sink); + assert(sink); + assert(reader); + + static const char* const string = + " " + " ."; + + SerdNode* const string_name = serd_new_string(serd_string("string")); + const char* position = string; + SerdInputStream in = serd_open_input_string(&position); + + SerdStatus st = serd_reader_start(reader, &in, string_name, 1); + assert(!st); + assert(serd_reader_read_document(reader) == SERD_SUCCESS); + assert(!serd_reader_finish(reader)); + assert(called); + assert(!serd_close_input(&in)); + + serd_node_free(string_name); + serd_reader_free(reader); + serd_sink_free(sink); + serd_world_free(world); +} + int main(void) { @@ -504,6 +559,7 @@ main(void) test_read_nquads_chunks(nq_path); test_read_turtle_chunks(ttl_path); test_read_empty(ttl_path); + test_error_cursor(); assert(!zix_remove(dir)); -- cgit v1.2.1