error test

This commit is contained in:
Ben Burdette 2020-03-23 15:29:49 -06:00
parent f694f43d7d
commit aadd59d005
3 changed files with 64 additions and 13 deletions

View file

@ -8,7 +8,7 @@ using std::cout;
using std::endl; using std::endl;
// return basic_format? // return basic_format?
string showErrLine(ErrLine &errLine) string showErrLine(ErrLine &errLine)
{ {
if (errLine.columnRange.has_value()) if (errLine.columnRange.has_value())
{ {
@ -22,15 +22,26 @@ string showErrLine(ErrLine &errLine)
void print_code_lines(string &prefix, NixCode &nix_code) void print_code_lines(string &prefix, NixCode &nix_code)
{ {
if (nix_code.errLine.has_value()) if (nix_code.errLine.has_value())
{ {
// previous line of code. // previous line of code.
if (nix_code.errLine->prevLineOfCode.has_value()) { 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. // line of code containing the error.%2$+5d%
cout << format("%1% %2$+5d%| %3%") % prefix % (nix_code.errLine->lineNumber) % nix_code.errLine->errLineOfCode; cout << format("%1% %|2$5d|| %3%")
% prefix
% (nix_code.errLine->lineNumber)
% nix_code.errLine->errLineOfCode
<< endl;
// error arrows for the column range. // error arrows for the column range.
if (nix_code.errLine->columnRange.has_value()) if (nix_code.errLine->columnRange.has_value())
@ -49,19 +60,21 @@ void print_code_lines(string &prefix, NixCode &nix_code)
arrows.append("^"); arrows.append("^");
} }
cout << format("%1% |%2%%3%") % prefix % spaces % arrows; cout << format("%1% |%2%%3%") % prefix % spaces % arrows;
} }
// next line of code. // next line of code.
if (nix_code.errLine->nextLineOfCode.has_value()) { 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 % einfo.errName
% dashes % dashes
% einfo.toolName; % einfo.toolName
<< endl;
// filename. // filename.
if (einfo.nixCode.has_value()) if (einfo.nixCode.has_value())
@ -110,7 +124,7 @@ void print_error(ErrorInfo &einfo)
? string(" ") + showErrLine(*einfo.nixCode->errLine) ? 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; cout << prefix << endl;
} }
else else

View file

@ -3,11 +3,9 @@
#include "types.hh" #include "types.hh"
#include <string> #include <string>
#include <optional> #include <optional>
// #include <boost/format.hpp>
using std::string; using std::string;
using std::optional; using std::optional;
// using boost::format;
namespace nix { namespace nix {

39
tests/errors/main.cc Normal file
View file

@ -0,0 +1,39 @@
#include "error.hh"
#include <optional>
#include <iostream>
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;
}