From aadd59d005bdabbb801f622dc1a0e3b12a5e5286 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Mon, 23 Mar 2020 15:29:49 -0600 Subject: [PATCH] error test --- src/libutil/error.cc | 36 +++++++++++++++++++++++++----------- src/libutil/error.hh | 2 -- tests/errors/main.cc | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 tests/errors/main.cc diff --git a/src/libutil/error.cc b/src/libutil/error.cc index 4af4691d4..71f422622 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -8,7 +8,7 @@ using std::cout; using std::endl; // return basic_format? -string showErrLine(ErrLine &errLine) +string showErrLine(ErrLine &errLine) { if (errLine.columnRange.has_value()) { @@ -22,15 +22,26 @@ string showErrLine(ErrLine &errLine) void print_code_lines(string &prefix, NixCode &nix_code) { + if (nix_code.errLine.has_value()) { // previous line of code. if (nix_code.errLine->prevLineOfCode.has_value()) { - cout << format("%1% %2$+5d%| %3%") % prefix % (nix_code.errLine->lineNumber - 1) % *nix_code.errLine->prevLineOfCode; + cout << format("%1% %|2$5d|| %3%") + % prefix + % (nix_code.errLine->lineNumber - 1) + % *nix_code.errLine->prevLineOfCode + << endl; } - // line of code containing the error. - cout << format("%1% %2$+5d%| %3%") % prefix % (nix_code.errLine->lineNumber) % nix_code.errLine->errLineOfCode; + // line of code containing the error.%2$+5d% + cout << format("%1% %|2$5d|| %3%") + % prefix + % (nix_code.errLine->lineNumber) + % nix_code.errLine->errLineOfCode + << endl; + + // error arrows for the column range. if (nix_code.errLine->columnRange.has_value()) @@ -49,19 +60,21 @@ void print_code_lines(string &prefix, NixCode &nix_code) arrows.append("^"); } - cout << format("%1% |%2%%3%") % prefix % spaces % arrows; + cout << format("%1% |%2%%3%") % prefix % spaces % arrows; } + // next line of code. if (nix_code.errLine->nextLineOfCode.has_value()) { - cout << format("%1% %2$+5d%| %3%") % prefix % (nix_code.errLine->lineNumber + 1) % *nix_code.errLine->nextLineOfCode; + cout << format("%1% %|2$5d|| %3%") + % prefix + % (nix_code.errLine->lineNumber + 1) + % *nix_code.errLine->nextLineOfCode + << endl; } } - else - { - } } @@ -99,7 +112,8 @@ void print_error(ErrorInfo &einfo) % "---" % einfo.errName % dashes - % einfo.toolName; + % einfo.toolName + << endl; // filename. if (einfo.nixCode.has_value()) @@ -110,7 +124,7 @@ void print_error(ErrorInfo &einfo) ? string(" ") + showErrLine(*einfo.nixCode->errLine) : ""; - cout << format("%1%in file: %2%%3%") % prefix % *einfo.nixCode->nixFile % eline; + cout << format("%1%in file: %2%%3%") % prefix % *einfo.nixCode->nixFile % eline << endl; cout << prefix << endl; } else diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 0bc1f4e24..8dac1508e 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -3,11 +3,9 @@ #include "types.hh" #include #include -// #include using std::string; using std::optional; -// using boost::format; namespace nix { diff --git a/tests/errors/main.cc b/tests/errors/main.cc new file mode 100644 index 000000000..1c998103e --- /dev/null +++ b/tests/errors/main.cc @@ -0,0 +1,39 @@ +#include "error.hh" + +#include +#include + +using std::optional; + +int main() { + +using namespace nix; + + ColumnRange columnRange; + columnRange.start = 24; + columnRange.len = 14; + + ErrLine errline; + errline.lineNumber = 7; + errline.columnRange = optional(columnRange); + errline.errLineOfCode = "line of code where the error occurred"; + + NixCode nixcode; + nixcode.nixFile = optional("myfile.nix"); + nixcode.errLine = errline; + + ErrorInfo generic; + generic.level = elError; + generic.errName = "error name"; + generic.description = "general error description"; + generic.toolName = "nixtool.exe"; + generic.nixCode = nixcode; + + print_error(generic); + + return 0; +} + + + +