Lot of code for generating code. Working on unit tests.
This commit is contained in:
parent
da45929b12
commit
8f4845d4d2
42 changed files with 1089 additions and 267 deletions
231
tests/pglabtests/tst_CodeBuilder.cpp
Normal file
231
tests/pglabtests/tst_CodeBuilder.cpp
Normal file
|
|
@ -0,0 +1,231 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include <gmock/gmock-matchers.h>
|
||||
#include <QRegularExpression>
|
||||
#include "PgType.h"
|
||||
#include "PgTypeContainer.h"
|
||||
#include "PrintTo_Qt.h"
|
||||
#include "codebuilder/IndentationConfig.h"
|
||||
#include "codebuilder/TypeMappings.h"
|
||||
#include "Pgsql_oids.h"
|
||||
|
||||
using namespace testing;
|
||||
|
||||
|
||||
|
||||
// Test to verify my understanding of the documentation
|
||||
TEST(QRegularExpression, doIUnderstandItCorrectly)
|
||||
{
|
||||
QString text("abcdefghi");
|
||||
QRegularExpression re("c(def)g");
|
||||
QRegularExpressionMatch match;
|
||||
int pos = text.indexOf(re, 0, &match);
|
||||
|
||||
ASSERT_THAT(pos, Eq(2));
|
||||
ASSERT_THAT(match.capturedStart(), Eq(2));
|
||||
ASSERT_THAT(match.capturedEnd(), Eq(7));
|
||||
ASSERT_THAT(match.capturedStart(1), Eq(3));
|
||||
ASSERT_THAT(match.capturedEnd(1), Eq(6));
|
||||
}
|
||||
|
||||
TEST(QRegularExpression, doIUnderstandItCorrectly2)
|
||||
{
|
||||
QString text("abc/%def%/ghi");
|
||||
QRegularExpression re("(?:[^\\\\]|^)(\\/%([a-zA-Z0-9_-]+)%\\/)");
|
||||
QRegularExpressionMatch match;
|
||||
int pos = text.indexOf(re, 0, &match);
|
||||
|
||||
ASSERT_THAT(pos, Eq(2));
|
||||
ASSERT_THAT(match.capturedStart(1), Eq(3));
|
||||
ASSERT_THAT(match.capturedLength(1), Eq(7));
|
||||
ASSERT_THAT(match.capturedStart(2), Eq(5));
|
||||
ASSERT_THAT(match.capturedLength(2), Eq(3));
|
||||
}
|
||||
|
||||
#include <QTextStream>
|
||||
|
||||
void FormatToStream(QTextStream &stream, QString format, std::function<void(QTextStream &, QString)> field_callback);
|
||||
|
||||
TEST(FormatToStream, EmptyFormat)
|
||||
{
|
||||
QString result;
|
||||
QTextStream stream(&result);
|
||||
FormatToStream(stream, "", nullptr);
|
||||
stream.flush();
|
||||
|
||||
ASSERT_THAT(result, Eq(QString()));
|
||||
}
|
||||
|
||||
TEST(FormatToStream, OnlyVar)
|
||||
{
|
||||
QString result;
|
||||
QTextStream stream(&result);
|
||||
FormatToStream(stream, "/%var%/",
|
||||
[](QTextStream &stream, QString varname)
|
||||
{
|
||||
if (varname == "var")
|
||||
stream << "abc";
|
||||
});
|
||||
stream.flush();
|
||||
|
||||
ASSERT_EQ(result, QString("abc"));
|
||||
}
|
||||
|
||||
TEST(FormatToStream, StartWithVar)
|
||||
{
|
||||
QString result;
|
||||
QTextStream stream(&result);
|
||||
FormatToStream(stream, "/%var%/def",
|
||||
[](QTextStream &stream, QString varname)
|
||||
{
|
||||
if (varname == "var")
|
||||
stream << "abc";
|
||||
});
|
||||
stream.flush();
|
||||
|
||||
ASSERT_EQ(result, QString("abcdef"));
|
||||
}
|
||||
|
||||
TEST(FormatToStream, EndWithVar)
|
||||
{
|
||||
QString result;
|
||||
QTextStream stream(&result);
|
||||
FormatToStream(stream, "def/%var%/",
|
||||
[](QTextStream &stream, QString varname)
|
||||
{
|
||||
if (varname == "var")
|
||||
stream << "abc";
|
||||
});
|
||||
stream.flush();
|
||||
|
||||
ASSERT_EQ(result, QString("defabc"));
|
||||
}
|
||||
|
||||
TEST(FormatToStream, TwoVarsTogether)
|
||||
{
|
||||
QString result;
|
||||
QTextStream stream(&result);
|
||||
FormatToStream(stream, "/%var1%//%var2%/",
|
||||
[](QTextStream &stream, QString varname)
|
||||
{
|
||||
if (varname == "var1")
|
||||
stream << "abc";
|
||||
else if (varname == "var2")
|
||||
stream << "def";
|
||||
});
|
||||
stream.flush();
|
||||
|
||||
ASSERT_EQ(result, QString("abcdef"));
|
||||
}
|
||||
|
||||
TEST(FormatToStream, TwoVarsWithTextInBetween)
|
||||
{
|
||||
QString result;
|
||||
QTextStream stream(&result);
|
||||
FormatToStream(stream, "1/%var1%/2/%var2%/3",
|
||||
[](QTextStream &stream, QString varname)
|
||||
{
|
||||
if (varname == "var1")
|
||||
stream << "abc";
|
||||
else if (varname == "var2")
|
||||
stream << "def";
|
||||
});
|
||||
stream.flush();
|
||||
|
||||
ASSERT_EQ(result, QString("1abc2def3"));
|
||||
}
|
||||
|
||||
|
||||
TEST(IndentationConfig, tab8indent4true_level1)
|
||||
{
|
||||
IndentationConfig icfg(8, 4, true);
|
||||
QString result = icfg.getIndentString(1);
|
||||
ASSERT_EQ(result, " ");
|
||||
}
|
||||
|
||||
TEST(IndentationConfig, tab8indent4true_level2)
|
||||
{
|
||||
IndentationConfig icfg(8, 4, true);
|
||||
QString result = icfg.getIndentString(2);
|
||||
ASSERT_EQ(result, "\t");
|
||||
}
|
||||
|
||||
TEST(IndentationConfig, tab8indent4true_level3)
|
||||
{
|
||||
IndentationConfig icfg(8, 4, true);
|
||||
QString result = icfg.getIndentString(3);
|
||||
ASSERT_EQ(result, "\t ");
|
||||
}
|
||||
|
||||
TEST(IndentationConfig, tab8indent4false_level1)
|
||||
{
|
||||
IndentationConfig icfg(8, 4, false);
|
||||
QString result = icfg.getIndentString(1);
|
||||
ASSERT_EQ(result, " ");
|
||||
}
|
||||
|
||||
TEST(IndentationConfig, tab8indent4false_level2)
|
||||
{
|
||||
IndentationConfig icfg(8, 4, false);
|
||||
QString result = icfg.getIndentString(2);
|
||||
ASSERT_EQ(result, " ");
|
||||
}
|
||||
|
||||
|
||||
class TypeMappingsTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override
|
||||
{
|
||||
def_str_type = "std::string";
|
||||
int4str = "int";
|
||||
int8str = "long long";
|
||||
tm = TypeMappings( {
|
||||
{ Pgsql::int4_oid, int4str },
|
||||
{ Pgsql::int8_oid, int8str }
|
||||
});
|
||||
tm.setDefaultStringType(def_str_type);
|
||||
tm.setDefaultContainerType("std::vector<%1>");
|
||||
}
|
||||
|
||||
// void TearDown() override {}
|
||||
|
||||
QString def_str_type;
|
||||
QString int4str;
|
||||
QString int8str;
|
||||
TypeMappings tm;
|
||||
};
|
||||
|
||||
TEST_F(TypeMappingsTest, defStringType)
|
||||
{
|
||||
QString result = tm.getTypeForOid(Pgsql::float4_oid);
|
||||
ASSERT_EQ(result, def_str_type);
|
||||
}
|
||||
|
||||
TEST_F(TypeMappingsTest, int4Type)
|
||||
{
|
||||
QString result = tm.getTypeForOid(Pgsql::int4_oid);
|
||||
ASSERT_EQ(result, int4str);
|
||||
}
|
||||
|
||||
TEST_F(TypeMappingsTest, int4overideType)
|
||||
{
|
||||
tm.set(Pgsql::int4_oid, "QString");
|
||||
QString result = tm.getTypeForOid(Pgsql::int4_oid);
|
||||
ASSERT_EQ(result, "QString");
|
||||
}
|
||||
|
||||
// Need catalogue for the next test
|
||||
// Maybe we should mock this !?
|
||||
TEST_F(TypeMappingsTest, int4arrayType)
|
||||
{
|
||||
auto types= std::make_shared<PgTypeContainer>();
|
||||
PgType int4arr;
|
||||
int4arr.oid = Pgsql::int4_array_oid;
|
||||
int4arr.elem = Pgsql::int4_oid;
|
||||
types->add(int4arr);
|
||||
|
||||
tm.setTypes(types);
|
||||
|
||||
QString result = tm.getTypeForOid(Pgsql::int4_array_oid);
|
||||
ASSERT_EQ(result, "std::vector<int>");
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue