HelloAurora/Tests/Public/7. Hello Parse/Main.cpp
2023-07-28 17:31:59 +01:00

301 lines
8.3 KiB
C++

/***
Copyright (C) 2020-2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Main.cpp
Date: 2020-11-14
Author: Reece
Note: Yes, this test is from gen 1, 2020. feelsoldman
***/
#include <AuroraRuntime.hpp>
#include <gtest/gtest.h>
TEST(Parse, EncapsulatedString)
{
AuLogInfo("Testing encapsulated string");
AuParse::ParseObject exampleStructure =
{
AuParse::ParseBit(AuParse::ParsableTag::kParseUInt, "Alpha"),
AuParse::ParseBit(AuParse::ParsableTag::kParseNumber, "Bravo"),
AuParse::ParseBit(AuParse::ParsableTag::kParseString, "String"),
};
AuString str = "4 5.3 \"hello\"";
AuParse::ParseResult res;
ASSERT_TRUE(AuParse::Parse(res, exampleStructure, str));
const auto &array = res.result;
ASSERT_EQ(array.size(), 3);
auto ia = array[0].value.single.primitive.uint;
auto ib = array[1].value.single.primitive.number;
auto str2 = array[2].value.single.string;
AuLogDbg("Got: a: {}, b: {}, c: {}", ia, ib, str2);
ASSERT_EQ(ia, 4);
ASSERT_EQ(ib, 5.3);
ASSERT_EQ(str2, "hello");
}
TEST(Parse, BrokenString)
{
AuLogWarn("Testing broken string (IGNORE WARNINGS)");
AuParse::ParseObject exampleStructure =
{
AuParse::ParseBit(AuParse::ParsableTag::kParseUInt, "Alpha"),
AuParse::ParseBit(AuParse::ParsableTag::kParseNumber, "Bravo"),
AuParse::ParseBit(AuParse::ParsableTag::kParseString, "String"),
};
AuString str = "4 5.3 \"hello";
AuMach offset = 0;
AuParse::ParseResult res;
ASSERT_FALSE(AuParse::Parse(res, exampleStructure, str));
}
TEST(Parse, BrokenIntegerLiteral)
{
AuLogInfo("Testing bad integer literal failure (IGNORE WARNINGS)");
AuParse::ParseObject exampleStructure =
{
AuParse::ParseBit(AuParse::ParsableTag::kParseUInt, "Alpha")
};
AuString str = "hello";
AuParse::ParseResult res;
ASSERT_FALSE(AuParse::Parse(res, exampleStructure, str));
}
TEST(Parse, Vararg)
{
AuLogInfo("Testing variable arrays [string]");
AuParse::ParseObject exampleStructure =
{
AuParse::ParseBit(AuParse::ParsableTag::kParseUInt, "Alpha"),
AuParse::ParseBit(AuParse::ParsableTag::kParseNumber, "Bravo"),
AuParse::ParseBit(AuParse::ParsableTag::kParseString, "Charlie", false, false, true)
};
AuString str = "4 5.3 \"re eee\" hello world";
AuMach offset = 0;
AuParse::ParseResult res;
ASSERT_TRUE(AuParse::Parse(res, exampleStructure, str));
ASSERT_EQ(res.result.size(), 3);
const auto & result = res.result[2];
const auto & array = result.value.array;
ASSERT_EQ(array.size(), result.count);
ASSERT_EQ(result.count, 3);
ASSERT_EQ(array[0].string, "re eee");
ASSERT_EQ(array[1].string, "hello");
ASSERT_EQ(array[2].string, "world");
}
TEST(Parse, Arrays)
{
AuLogInfo("Testing length prefixed arrays");
AuParse::ParseObject exampleStructure =
{
AuParse::ParseBit(AuParse::ParsableTag::kParseUInt, "Int Alpha"),
AuParse::ParseBit(AuParse::ParsableTag::kParseString, "String Bravo", true, false, false)
};
AuString str = "4 3 \"re eee\" hello world";
AuMach offset = 0;
AuParse::ParseResult res;
ASSERT_TRUE(AuParse::Parse(res, exampleStructure, str));
ASSERT_EQ(res.result.size(), 2);
const auto &result = res.result[1];
const auto &array = result.value.array;
ASSERT_EQ(array.size(), result.count);
ASSERT_EQ(result.count, 3);
ASSERT_EQ(array[0].string, "re eee");
ASSERT_EQ(array[1].string, "hello");
ASSERT_EQ(array[2].string, "world");
AuString reserialized = "";
str = "4 3 \"re eee\" \"hello\" \"world\"";
AuParse::Serialize(res.result, reserialized);
ASSERT_EQ(reserialized, str);
}
TEST(Parse, ArraysReserialize)
{
AuLogInfo("Testing length prefixed arrays [reserialization]");
AuParse::ParseObject exampleStructure =
{
AuParse::ParseBit(AuParse::ParsableTag::kParseUInt, "Int Alpha"),
AuParse::ParseBit(AuParse::ParsableTag::kParseString, "String Bravo", true, false, false)
};
AuString reserialized;
AuString str = "4 3 \"re eee\" hello world";
AuMach offset = 0;
AuParse::ParseResult res;
ASSERT_TRUE(AuParse::Parse(res, exampleStructure, str));
str = "4 3 \"re eee\" \"hello\" \"world\"";
AuParse::Serialize(res.result, reserialized);
ASSERT_EQ(reserialized, str);
}
TEST(Parse, Optionals)
{
AuLogInfo("Testing optionals");
AuParse::ParseObject exampleStructure =
{
AuParse::ParseBit(AuParse::ParsableTag::kParseUInt, "Alpha"),
AuParse::ParseBit(AuParse::ParsableTag::kParseNumber, "Bravo", false, true, false)
};
AuString str_1 = "4 5.3";
AuParse::ParseResult res_1;
ASSERT_TRUE(AuParse::Parse(res_1, exampleStructure, str_1));
ASSERT_EQ(res_1.result.size(), 2);
AuString str_2 = "4";
AuParse::ParseResult res_2;
ASSERT_TRUE(AuParse::Parse(res_2, exampleStructure, str_2));
ASSERT_EQ(res_2.result.size(), 1);
}
TEST(Parse, VarStrings)
{
AuLogInfo("Testing var strings");
AuParse::ParseObject exampleStructure =
{
AuParse::ParseBit(AuParse::ParsableTag::kParseUInt, "Alpha"),
AuParse::ParseBit(AuParse::ParsableTag::kParseStringVararg, "String")
};
AuString magic = "\"re eee\" hello world";
AuString str = "4 " + magic;
AuMach offset = 0;
AuParse::ParseResult res;
ASSERT_TRUE(AuParse::Parse(res, exampleStructure, str));
ASSERT_EQ(res.result.size(), 2);
const auto &result = res.result[1];
const auto &value = result.value.single;
ASSERT_EQ(result.count, 1);
ASSERT_EQ(value.string, magic);
}
TEST(Parse, NewLine)
{
AuLogInfo("Testing newline");
AuParse::ParseObject exampleStructure =
{
AuParse::ParseBit(AuParse::ParsableTag::kParseUInt, "Alpha"),
AuParse::ParseBit(AuParse::ParsableTag::kParseString, "Bravo"),
AuParse::ParseBit(AuParse::ParsableTag::kParseStringVararg, "Charlie")
};
AuString magicStringEscaped = "\"world\nhello\"";
AuString magicStringRaw = "world\nhello";
AuString magicEscaped = "\"re eee\" hello world\\\nhello";
AuString magicRaw = "\"re eee\" hello world\nhello";
AuString str = "4 " + magicStringEscaped + " " + magicEscaped;
AuMach offset = 0;
AuParse::ParseResult res;
ASSERT_TRUE(AuParse::Parse(res, exampleStructure, str));
ASSERT_EQ(res.result.size(), 3);
const auto &resultString = res.result[1];
const auto &valueString = resultString.value.single;
ASSERT_EQ(resultString.count, 1);
ASSERT_EQ(valueString.string, magicStringRaw);
const auto &resultVararg = res.result[2];
const auto &valueVararg = resultVararg.value.single;
ASSERT_EQ(resultVararg.count, 1);
ASSERT_EQ(valueVararg.string, magicRaw);
}
TEST(Parse, Escapes)
{
AuLogInfo("Testing escapes");
AuParse::ParseObject exampleStructure =
{
AuParse::ParseBit(AuParse::ParsableTag::kParseUInt, "Alpha"),
AuParse::ParseBit(AuParse::ParsableTag::kParseString, "Bravo"),
};
AuString magicStringEscaped = "\"world \\\"hello\"";
AuString magicStringRaw = "world \"hello";
AuString str = "4 " + magicStringEscaped;
AuParse::ParseResult res;
ASSERT_TRUE(AuParse::Parse(res, exampleStructure, str));
ASSERT_EQ(res.result.size(), 2);
const auto& resultString = res.result[1];
const auto& valueString = resultString.value.single;
ASSERT_EQ(resultString.count, 1);
ASSERT_EQ(valueString.string, magicStringRaw);
AuString reserialized;
AuParse::Serialize(res.result, reserialized);
ASSERT_EQ(reserialized, str);
}
TEST(Parse, EscapesReserialize)
{
AuLogInfo("Testing escapes [reserialization]");
AuParse::ParseObject exampleStructure =
{
AuParse::ParseBit(AuParse::ParsableTag::kParseUInt, "Alpha"),
AuParse::ParseBit(AuParse::ParsableTag::kParseString, "Bravo"),
};
AuString reserialized;
AuString str = "3 \"world \\\"hello\"";
AuParse::ParseResult res;
ASSERT_TRUE(AuParse::Parse(res, exampleStructure, str));
AuParse::Serialize(res.result, reserialized);
ASSERT_EQ(reserialized, str);
}
void RunTests()
{
Aurora::RuntimeStartInfo info;
info.console.fio.bEnableLogging = false;
Aurora::RuntimeStart(info);
}