Skip to content

Commit

Permalink
fix unit test to account for escape sequences
Browse files Browse the repository at this point in the history
And address reviewer's comments + prettify.
  • Loading branch information
Calin Cascaval committed May 3, 2017
1 parent 5982d04 commit 428a976
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
35 changes: 19 additions & 16 deletions lib/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ limitations under the License.

namespace Util {

// Base class for all exceptions.
// The constructor uses boost::format for the format string, i.e.,
// %1%, %2%, etc (starting at 1, not at 0)
// colors to pretty print messages
constexpr char ANSI_RED[] = "\e[31m";
constexpr char ANSI_BLUE[] = "\e[34m";
constexpr char ANSI_CLR[] = "\e[0m";

/// Base class for all exceptions.
/// The constructor uses boost::format for the format string, i.e.,
/// %1%, %2%, etc (starting at 1, not at 0)
class P4CExceptionBase : public std::exception {
protected:
cstring message;
Expand All @@ -35,47 +40,45 @@ class P4CExceptionBase : public std::exception {
template <typename... T>
P4CExceptionBase(const char* format, T... args) {
boost::format fmt(format);
this->message = ::bug_helper(fmt, "", "", "", std::forward<T>(args)...);
message = ::bug_helper(fmt, "", "", "", std::forward<T>(args)...);
}

const char* what() const noexcept
{ return this->message.c_str(); }
{ return message.c_str(); }
};

// This class indicates a bug in the compiler
/// This class indicates a bug in the compiler
class CompilerBug final : public P4CExceptionBase {
public:
template <typename... T>
CompilerBug(const char* format, T... args)
: P4CExceptionBase(format, args...)
// \e[31m prints the text in red
{ message = "\e[31mCOMPILER BUG\e[0m:\n" + message; }
{ message = cstring(ANSI_RED) + "Compiler Bug" + ANSI_CLR + ":\n" + message; }
template <typename... T>
CompilerBug(const char* file, int line, const char* format, T... args)
: P4CExceptionBase(format, args...)
{ message = cstring("In file: ") + file + ":" + Util::toString(line) + "\n" +
cstring("\e[31mCOMPILER BUG\e[0m: ") + message; }
+ ANSI_RED + "Compiler Bug" + ANSI_CLR + ": " + message; }
};

// This class indicates an unimplemented feature in the compiler
/// This class indicates an unimplemented feature in the compiler
class CompilerUnimplemented final : public P4CExceptionBase {
public:
template <typename... T>
CompilerUnimplemented(const char* format, T... args)
: P4CExceptionBase(format, args...)
// \e[34m prints the text in blue
{ message = "\e[34mUnimplemented compiler support\e[0m:\n" + message; }
{ message = cstring(ANSI_BLUE) +"Unimplemented compiler support"+ ANSI_CLR + ":\n" + message; }
template <typename... T>
CompilerUnimplemented(const char* file, int line, const char* format, T... args)
: P4CExceptionBase(format, args...)
{ message = cstring("In file: ") + file + ":" + Util::toString(line) + "\n" +
cstring("\e[34mUnimplemented compiler support\e[0m: ") + message; }
ANSI_BLUE + "Unimplemented compiler support" + ANSI_CLR + ": " + message; }
};


// This class indicates a compilation error that we do not want to recover from.
// This may be due to a malformed input program.
// TODO: this class is very seldom used, perhaps we can remove it.
/// This class indicates a compilation error that we do not want to recover from.
/// This may be due to a malformed input program.
/// TODO: this class is very seldom used, perhaps we can remove it.
class CompilationError : public P4CExceptionBase {
public:
template <typename... T>
Expand Down
5 changes: 3 additions & 2 deletions test/unittests/exception_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,8 @@ class TestException : public TestBase {
}
catch (std::exception &ex) {
cstring err(ex.what());
ASSERT_EQ(err, "COMPILER BUG:\ntest\n");
cstring expected = cstring(ANSI_RED) + "Compiler Bug" + ANSI_CLR +":\ntest\n";
ASSERT_EQ(err, expected);
}

try {
Expand Down

0 comments on commit 428a976

Please sign in to comment.