yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
message_test.cc
Go to the documentation of this file.
1#include <gtest/gtest.h>
2
5#include "test/testing.h"
6
7namespace yaze {
8namespace test {
9
10class MessageTest : public ::testing::Test, public SharedRom {
11 protected:
12 void SetUp() override {
13#if defined(__linux__)
14 GTEST_SKIP();
15#endif
16 EXPECT_OK(rom()->LoadFromFile("zelda3.sfc"));
18 }
19 void TearDown() override {}
20
22 std::vector<editor::DictionaryEntry> dictionary_;
23};
24
25TEST_F(MessageTest, ParseSingleMessage_CommandParsing) {
26 std::vector<uint8_t> mock_data = {0x6A, 0x7F, 0x00};
27 int pos = 0;
28
29 auto result = editor::ParseSingleMessage(mock_data, &pos);
30 EXPECT_TRUE(result.ok());
31 const auto message_data = result.value();
32
33 // Verify that the command was recognized and parsed
34 EXPECT_EQ(message_data.ContentsParsed, "[L]");
35 EXPECT_EQ(pos, 2);
36}
37
38TEST_F(MessageTest, ParseSingleMessage_BasicAscii) {
39 // A, B, C, terminator
40 std::vector<uint8_t> mock_data = {0x00, 0x01, 0x02, 0x7F, 0x00};
41 int pos = 0;
42
43 auto result = editor::ParseSingleMessage(mock_data, &pos);
44 ASSERT_TRUE(result.ok());
45 const auto message_data = result.value();
46 EXPECT_EQ(pos, 4); // consumed all 4 bytes
47
48 std::vector<editor::MessageData> message_data_vector = {message_data};
49 auto parsed = editor::ParseMessageData(message_data_vector, dictionary_);
50
51 EXPECT_THAT(parsed, ::testing::ElementsAre("ABC"));
52}
53
54TEST_F(MessageTest, FindMatchingCharacter_Success) {
55 EXPECT_EQ(editor::FindMatchingCharacter('A'), 0x00);
56 EXPECT_EQ(editor::FindMatchingCharacter('Z'), 0x19);
57 EXPECT_EQ(editor::FindMatchingCharacter('a'), 0x1A);
58 EXPECT_EQ(editor::FindMatchingCharacter('z'), 0x33);
59}
60
61TEST_F(MessageTest, FindMatchingCharacter_Failure) {
62 EXPECT_EQ(editor::FindMatchingCharacter('@'), 0xFF);
63 EXPECT_EQ(editor::FindMatchingCharacter('#'), 0xFF);
64}
65
66TEST_F(MessageTest, FindDictionaryEntry_Success) {
67 EXPECT_EQ(editor::FindDictionaryEntry(0x88), 0x00);
68 EXPECT_EQ(editor::FindDictionaryEntry(0x90), 0x08);
69}
70
71TEST_F(MessageTest, FindDictionaryEntry_Failure) {
72 EXPECT_EQ(editor::FindDictionaryEntry(0x00), -1);
73 EXPECT_EQ(editor::FindDictionaryEntry(0xFF), -1);
74}
75
76TEST_F(MessageTest, ParseMessageToData_Basic) {
77 std::string input = "[L][C:01]ABC";
78 auto result = editor::ParseMessageToData(input);
79 std::vector<uint8_t> expected = {0x6A, 0x77, 0x01, 0x00, 0x01, 0x02};
80 EXPECT_EQ(result, expected);
81}
82
83TEST_F(MessageTest, ReplaceAllDictionaryWords_Success) {
84 std::vector<editor::DictionaryEntry> mock_dict = {
85 editor::DictionaryEntry(0x00, "test"),
86 editor::DictionaryEntry(0x01, "message")};
87 std::string input = "This is a test message.";
88 auto result = editor::ReplaceAllDictionaryWords(input, mock_dict);
89 EXPECT_EQ(result, "This is a [D:00] [D:01].");
90}
91
92TEST_F(MessageTest, ReplaceAllDictionaryWords_NoMatch) {
93 std::vector<editor::DictionaryEntry> mock_dict = {
94 editor::DictionaryEntry(0x00, "hello")};
95 std::string input = "No matching words.";
96 auto result = editor::ReplaceAllDictionaryWords(input, mock_dict);
97 EXPECT_EQ(result, "No matching words.");
98}
99
100TEST_F(MessageTest, ParseTextDataByte_Success) {
101 EXPECT_EQ(editor::ParseTextDataByte(0x00), "A");
102 EXPECT_EQ(editor::ParseTextDataByte(0x74), "[1]");
103 EXPECT_EQ(editor::ParseTextDataByte(0x88), "[D:00]");
104}
105
106TEST_F(MessageTest, ParseTextDataByte_Failure) {
107 EXPECT_EQ(editor::ParseTextDataByte(0xFF), "");
108}
109
110TEST_F(MessageTest, ParseSingleMessage_EmptyData) {
111 std::vector<uint8_t> mock_data = {0x7F};
112 int pos = 0;
113
114 auto result = editor::ParseSingleMessage(mock_data, &pos);
115 ASSERT_TRUE(result.ok());
116 const auto message_data = result.value();
117
118 EXPECT_EQ(message_data.ContentsParsed, "");
119 EXPECT_EQ(pos, 1);
120}
121
122TEST_F(MessageTest, OptimizeMessageForDictionary_Basic) {
123 std::vector<editor::DictionaryEntry> mock_dict = {
124 editor::DictionaryEntry(0x00, "Link"),
125 editor::DictionaryEntry(0x01, "Zelda")};
126 std::string input = "[L] rescued Zelda from danger.";
127
128 editor::MessageData message_data;
129 std::string optimized =
130 message_data.OptimizeMessageForDictionary(input, mock_dict);
131
132 EXPECT_EQ(optimized, "[L] rescued [D:01] from danger.");
133}
134
135TEST_F(MessageTest, SetMessage_Success) {
136 std::vector<editor::DictionaryEntry> mock_dict = {
137 editor::DictionaryEntry(0x00, "item")};
138 editor::MessageData message_data;
139 std::string input = "You got an item!";
140
141 message_data.SetMessage(input, mock_dict);
142
143 EXPECT_EQ(message_data.RawString, "You got an item!");
144 EXPECT_EQ(message_data.ContentsParsed, "You got an [D:00]!");
145}
146
147TEST_F(MessageTest, FindMatchingElement_CommandWithArgument) {
148 std::string input = "[W:02]";
150
151 EXPECT_TRUE(result.Active);
152 EXPECT_EQ(result.Parent.Token, "W");
153 EXPECT_EQ(result.Value, 0x02);
154}
155
156TEST_F(MessageTest, FindMatchingElement_InvalidCommand) {
157 std::string input = "[INVALID]";
159
160 EXPECT_FALSE(result.Active);
161}
162
163TEST_F(MessageTest, ImportMessageData_InvalidFile) {
164 auto result = editor::ImportMessageData("nonexistent_file.txt");
165 EXPECT_TRUE(result.empty());
166}
167
168TEST_F(MessageTest, BuildDictionaryEntries_CorrectSize) {
169 auto result = editor::BuildDictionaryEntries(rom());
170 EXPECT_EQ(result.size(), editor::kNumDictionaryEntries);
171 EXPECT_FALSE(result.empty());
172}
173
174} // namespace test
175} // namespace yaze
auto rom()
Definition rom.h:384
SharedRom()=default
void SetUp() override
void TearDown() override
editor::MessageEditor message_editor_
std::vector< editor::DictionaryEntry > dictionary_
uint8_t FindMatchingCharacter(char value)
std::string ParseTextDataByte(uint8_t value)
std::string ReplaceAllDictionaryWords(std::string str, std::vector< DictionaryEntry > dictionary)
constexpr int kNumDictionaryEntries
absl::StatusOr< MessageData > ParseSingleMessage(const std::vector< uint8_t > &rom_data, int *current_pos)
std::vector< std::string > ParseMessageData(std::vector< MessageData > &message_data, const std::vector< DictionaryEntry > &dictionary_entries)
std::vector< DictionaryEntry > BuildDictionaryEntries(Rom *rom)
std::vector< std::string > ImportMessageData(std::string_view filename)
std::vector< uint8_t > ParseMessageToData(std::string str)
ParsedElement FindMatchingElement(const std::string &str)
int8_t FindDictionaryEntry(uint8_t value)
TEST_F(CpuTest, AsmParserTokenizerOk)
Definition cpu_test.cc:44
Main namespace for the application.
Definition controller.cc:18
std::string OptimizeMessageForDictionary(std::string message_string, const std::vector< DictionaryEntry > &dictionary)
void SetMessage(const std::string &message, const std::vector< DictionaryEntry > &dictionary)
#define EXPECT_OK(expr)
Definition testing.h:10