Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

Commit

Permalink
feat: Reformat print of messages
Browse files Browse the repository at this point in the history
Part of #70
  • Loading branch information
Gashmob committed Dec 2, 2023
1 parent 2a86bc3 commit 90018eb
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ your code.
Dev warning are show like this :

```
DEV WARNING <code> <message>
<filename>
<line> |<code>
| ^
DEV WARNING[<code>]: <message>
--> <filename>
<line> | <code>
| ^
```

| Dev code | Meaning | File |
Expand Down
6 changes: 5 additions & 1 deletion src/lib/message/DevWarning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ namespace filc::message {
DevWarning::DevWarning(unsigned int code, filc::utils::AbstractPosition *position, std::string content)
: Message(ERROR, std::move(content)), _code(code), _position(position) {}

auto DevWarning::getCode() const -> unsigned int {
return _code;
}

auto DevWarning::print(std::ostream &out) -> std::ostream & {
if (_printed) {
return out;
}

out << "\033[1;36mDEV WARNING:\033[0m " << "\033[1;46m " << _code << " \033[0m " << _content << std::endl;
out << "\033[1;36mDEV WARNING[" << _code << "]\033[0m\033[1m: " << _content << std::endl;
out << _position->dump("\033[1;36m");

_printed = true;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/message/DevWarning.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace filc::message {
public:
DevWarning(unsigned int code, filc::utils::AbstractPosition *position, std::string content);

[[nodiscard]] auto getCode() const -> unsigned int;

auto print(std::ostream &out) -> std::ostream & override;

private:
Expand Down
6 changes: 4 additions & 2 deletions src/lib/message/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace filc::message {
return out;
}

out << "\033[1;31mERROR:\033[0m " << _content;
out << "\033[1;31mERROR\033[0m\033[1m: " << _content << "\033[0m";
_printed = true;

return out;
Expand All @@ -44,9 +44,11 @@ namespace filc::message {
return out;
}

out << "\033[1;31mERROR:\033[0m " << _content << '\n';
out << "\033[1;31mERROR\033[0m\033[1m: " << _content << "\033[0m\n";
out << _position->dump("\033[1;31m");

_printed = true;

return out;
}
}
3 changes: 2 additions & 1 deletion src/lib/message/Warning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace filc::message {
return out;
}

out << "\033[1;33mWARNING:\033[0m " << _content;
out << "\033[1;33mWARNING\033[0m\033[1m: " << _content << "\033[0m";

_printed = true;

return out;
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ add_executable(tests
# === Message ===
unit/message/MessageTest.cpp
unit/message/MessageCollectorTest.cpp
unit/message/WarningTest.cpp
unit/message/ErrorTest.cpp
unit/message/DevWarningTest.cpp
# === Grammar ===
unit/grammar/ParserTest.cpp
# === AST ===
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/message/DevWarningTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "DevWarning.h"
#include "test_tools.h"

TEST(DevWarning, constructor) {
auto dev_warning = filc::message::DevWarning(5, new filc::utils::SimplePosition(
FIXTURES_PATH "/utils/position.txt", 12, 3
), "My dev warning");
ASSERT_EQ(filc::message::ERROR, dev_warning.getLevel());
ASSERT_EQ(5, dev_warning.getCode());
}

TEST(DevWarning, print) {
auto dev_warning = filc::message::DevWarning(5, new filc::utils::SimplePosition(
FIXTURES_PATH "/utils/position.txt", 12, 3
), "My dev warning");
auto expected =
"\033[1;36mDEV WARNING[5]\033[0m\033[1m: My dev warning\n"
" \033[1;34m--> \033[0m../../tests/unit/Fixtures/utils/position.txt:12:3\n"
"\033[1;34m 12 | \033[0m12;abcd\n"
"\033[1;34m | \033[0m \033[1;36m^\033[0m\n";
ASSERT_MESSAGE_CONTENT(expected, dev_warning);
}
54 changes: 54 additions & 0 deletions tests/unit/message/ErrorTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "Error.h"
#include "test_tools.h"

TEST(BasicError, constructor) {
auto basic_error = filc::message::BasicError(filc::message::ERROR, "My error");
ASSERT_EQ(filc::message::ERROR, basic_error.getLevel());
}

TEST(BasicError, print) {
auto basic_error = filc::message::BasicError(filc::message::ERROR, "My error");
ASSERT_MESSAGE_CONTENT("\033[1;31mERROR\033[0m\033[1m: My error\033[0m", basic_error);
ASSERT_MESSAGE_CONTENT("", basic_error);
}

TEST(Error, constructor) {
auto error = filc::message::Error(filc::message::ERROR, "My error",
new filc::utils::SimplePosition(FIXTURES_PATH "/utils/position.txt", 5, 2));
ASSERT_EQ(filc::message::ERROR, error.getLevel());
}

TEST(Error, print) {
auto error = filc::message::Error(filc::message::ERROR, "My error",
new filc::utils::SimplePosition(FIXTURES_PATH "/utils/position.txt", 5, 2));
auto expected =
"\033[1;31mERROR\033[0m\033[1m: My error\033[0m\n"
" \033[1;34m--> \033[0m../../tests/unit/Fixtures/utils/position.txt:5:2\n"
"\033[1;34m 5 | \033[0m05;abcd\n"
"\033[1;34m | \033[0m \033[1;31m^\033[0m\n";
ASSERT_MESSAGE_CONTENT(expected, error);
ASSERT_MESSAGE_CONTENT("", error);
}
22 changes: 6 additions & 16 deletions tests/unit/message/MessageTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,22 @@
*/
#include "Message.h"
#include "test_tools.h"
#include <sstream>

auto getMessageContent(filc::message::Message &message) -> std::string {
std::stringstream stream;
stream << message;

std::string result(std::istreambuf_iterator<char>(stream), {});

return result;
}

TEST(Message, constructor) {
std::string expected = "My message";
auto expected = "My message";
auto message = filc::message::Message(filc::message::WARNING, expected);
ASSERT_EQ(filc::message::WARNING, message.getLevel());
ASSERT_STREQ(expected.c_str(), getMessageContent(message).c_str());
ASSERT_MESSAGE_CONTENT(expected, message);

expected = "My message 2";
message = filc::message::Message((filc::message::LEVEL) 9, expected);
ASSERT_EQ(5, message.getLevel());
ASSERT_STREQ(expected.c_str(), getMessageContent(message).c_str());
ASSERT_MESSAGE_CONTENT(expected, message);
}

TEST(Message, print) {
std::string expected = "My message";
auto expected = "My message";
auto message = filc::message::Message(filc::message::WARNING, expected);
ASSERT_STREQ(expected.c_str(), getMessageContent(message).c_str());
ASSERT_STREQ("", getMessageContent(message).c_str());
ASSERT_MESSAGE_CONTENT(expected, message);
ASSERT_MESSAGE_CONTENT("", message);
}
36 changes: 36 additions & 0 deletions tests/unit/message/WarningTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "Warning.h"
#include "test_tools.h"

TEST(BasicWarning, constructor) {
auto basic_warning = filc::message::BasicWarning(filc::message::WARNING, "My warning");
ASSERT_EQ(filc::message::WARNING, basic_warning.getLevel());
}

TEST(BasicWarning, print) {
auto basic_warning = filc::message::BasicWarning(filc::message::WARNING, "My warning");
ASSERT_MESSAGE_CONTENT("\033[1;33mWARNING\033[0m\033[1m: My warning\033[0m", basic_warning);
ASSERT_MESSAGE_CONTENT("", basic_warning);
}
8 changes: 8 additions & 0 deletions tests/unit/test_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "MessageCollector.h"
#include <sstream>

using namespace ::testing;

Expand All @@ -48,3 +49,10 @@ using namespace ::testing;
ASSERT_IDENTIFIER(name, var_##variable->getIdentifier()); \
ASSERT_TYPE(type, var_##variable->getType()); \
ASSERT_LITERAL(value, literal, var_##variable->getAssignation())

#define ASSERT_MESSAGE_CONTENT(expected, message) { \
std::stringstream stream; \
message.print(stream); \
std::string result(std::istreambuf_iterator<char>(stream), {}); \
ASSERT_STREQ(expected, result.c_str()); \
}

0 comments on commit 90018eb

Please sign in to comment.