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

Commit

Permalink
Add Message
Browse files Browse the repository at this point in the history
  • Loading branch information
Gashmob committed Jul 12, 2023
1 parent da73b15 commit af6e8cb
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ add_library(compiler_lib
# === Main ===
FilCompiler.cpp
# === Utils ===
utils/OptionsParser.cpp)
target_include_directories(compiler_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} utils ../../lib)
utils/OptionsParser.cpp
# === Message ===
message/Message.cpp)
target_include_directories(compiler_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} utils message ../../lib)
target_link_libraries(compiler_lib PUBLIC coverage_config)

target_compile_options(compiler_lib PUBLIC -Wall)
52 changes: 52 additions & 0 deletions src/lib/message/Message.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* 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 "Message.h"

#include <utility>

constexpr uint MAX_LEVEL = 5;

namespace filc::message {
Message::Message(uint level, std::string content)
: _level(level <= MAX_LEVEL ? level : MAX_LEVEL), _content(std::move(content)), _printed(false) {}

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

out << _content;
_printed = true;

return out;
}

auto Message::getLevel() const -> uint {
return _level;
}
}

auto operator<<(std::ostream &out, filc::message::Message &message) -> std::ostream & {
return message.print(out);
}
48 changes: 48 additions & 0 deletions src/lib/message/Message.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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.
*/
#ifndef FILC_MESSAGE_H
#define FILC_MESSAGE_H

#include <string>
#include <iostream>

namespace filc::message {
class Message {
public:
Message(uint level, std::string content);

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

auto getLevel() const -> uint;

private:
uint _level;
std::string _content;
bool _printed;
};
}

auto operator<<(std::ostream &out, filc::message::Message &message) -> std::ostream &;

#endif //FILC_MESSAGE_H
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

add_executable(tests
unit/utils/OptionsParserTest.cpp)
unit/utils/OptionsParserTest.cpp unit/message/MessageTest.cpp)

target_link_libraries(tests PRIVATE GTest::gtest_main compiler_lib)

Expand Down
54 changes: 54 additions & 0 deletions tests/unit/message/MessageTest.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 "Message.h"
#include <gtest/gtest.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 message = filc::message::Message(4, expected);
ASSERT_EQ(4, message.getLevel());
ASSERT_STREQ(expected.c_str(), getMessageContent(message).c_str());

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

TEST(Message, print) {
std::string expected = "My message";
auto message = filc::message::Message(4, expected);
ASSERT_STREQ(expected.c_str(), getMessageContent(message).c_str());
ASSERT_STREQ("", getMessageContent(message).c_str());
}

0 comments on commit af6e8cb

Please sign in to comment.