Messy commit. Testing suff and some improvements to how data is shown.

This commit is contained in:
eelke 2017-12-09 10:45:13 +01:00
parent bebb3391c3
commit 3a13b7ffb4
59 changed files with 2045 additions and 716 deletions

View file

@ -0,0 +1,13 @@
#include "tst_CsvWriter.h"
#include "tst_expected.h"
#include "tst_PasswordManager.h"
#include "tst_scopeguard.h"
#include "tst_SqlLexer.h"
#include <gtest/gtest.h>
int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View file

@ -0,0 +1,32 @@
include(../gtest_dependency.pri)
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG += thread
CONFIG += qt
QT += core
HEADERS += \
tst_expected.h \
tst_SqlLexer.h \
tst_scopeguard.h \
tst_CsvWriter.h \
tst_PasswordManager.h
SOURCES += main.cpp \
tst_ExplainJsonParser.cpp
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../../core/release/ -lcore
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../../core/debug/ -lcore
INCLUDEPATH += C:\prog\include C:\VSproj\boost_1_63_0
INCLUDEPATH += $$PWD/../../../core
DEPENDPATH += $$PWD/../../../core
LIBS += c:\prog\lib\botand_imp.lib
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../core/release/libcore.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../core/debug/libcore.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../core/release/core.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../core/debug/core.lib

View file

@ -0,0 +1,113 @@
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include "CsvWriter.h"
#include <QTextStream>
#include <QByteArray>
using namespace testing;
TEST(CsvWriter, one_row_two_numbers)
{
QString result;
QTextStream stream(&result);
CsvWriter writer(&stream);
writer.setQuote('"');
writer.setSeperator(',');
writer.writeField("1");
writer.writeField("2");
writer.nextRow();
QString expected = QString::fromUtf8("1,2\n");
ASSERT_THAT(result, Eq(expected));
}
TEST(CsvWriter, one_row_one_number_one_unquoted_string)
{
QString result;
QTextStream stream(&result);
CsvWriter writer(&stream);
writer.setQuote('"');
writer.setSeperator(',');
writer.writeField("1");
writer.writeField("hello");
writer.nextRow();
QString expected = QString::fromUtf8("1,hello\n");
ASSERT_THAT(result, Eq(expected));
}
TEST(CsvWriter, one_row_one_number_one_quoted_string)
{
QString result;
QTextStream stream(&result);
CsvWriter writer(&stream);
writer.setQuote('"');
writer.setSeperator(',');
writer.writeField("1");
writer.writeField("hel,lo");
writer.nextRow();
QString expected = QString::fromUtf8("1,\"hel,lo\"\n");
ASSERT_THAT(result, Eq(expected));
}
TEST(CsvWriter, newline_in_field)
{
QString result;
QTextStream stream(&result);
CsvWriter writer(&stream);
writer.setQuote('"');
writer.setSeperator(',');
writer.writeField("1");
writer.writeField("hel\nlo");
writer.nextRow();
QString expected = QString::fromUtf8("1,\"hel\nlo\"\n");
ASSERT_THAT(result, Eq(expected));
}
TEST(CsvWriter, escape_quote)
{
QString result;
QTextStream stream(&result);
CsvWriter writer(&stream);
writer.setQuote('"');
writer.setSeperator(',');
writer.writeField("1");
writer.writeField("hel\"lo");
writer.nextRow();
QString expected = QString::fromUtf8("1,\"hel\"\"lo\"\n");
ASSERT_THAT(result, Eq(expected));
}
TEST(CsvWriter, non_default_seperator)
{
QString result;
QTextStream stream(&result);
CsvWriter writer(&stream);
writer.setQuote('"');
writer.setSeperator('\t');
writer.writeField("1");
writer.writeField("hel,lo");
writer.nextRow();
QString expected = QString::fromUtf8("1\thel,lo\n");
ASSERT_THAT(result, Eq(expected));
}
TEST(CsvWriter, non_default_quote)
{
QString result;
QTextStream stream(&result);
CsvWriter writer(&stream);
writer.setQuote('*');
writer.setSeperator('\t');
writer.writeField("1");
writer.writeField("hel\tlo");
writer.nextRow();
QString expected = QString::fromUtf8("1\t*hel\tlo*\n");
ASSERT_THAT(result, Eq(expected));
}

View file

@ -0,0 +1,80 @@
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include "ExplainTreeModelItem.h"
#include "json/json.h"
using namespace testing;
TEST(ExplainParser, verbose)
{
std::string input = R"__json__("[
{
"Plan": {
"Node Type": "Limit",
"Parallel Aware": false,
"Startup Cost": 0.42,
"Total Cost": 0.42,
"Plan Rows": 10,
"Plan Width": 830,
"Output": ["fr1.callstack_crc_2", "fr1.id", "fr1.program", "fr1.version", "fr1.lic_bedrijf", "fr1.lic_plaats", "fr1.lic_number", "fr1.callstack_crc_1", "fr1.callstack_crc_3", "fr1.exception_class", "fr1.exception_message", "fr1.full_report", "fr1.screenshot_filename", "fr1.ts", "fr1.duplicate_of", "fr1.actief", "fr1.commentaar", "fr1.bugzilla", "fr1.attachment_filename", "fr1.version_according_to_datetime", "fr1.program_according_to_full_report", "fr1.date", "fr2.id", "fr2.program", "fr2.version", "fr2.lic_bedrijf", "fr2.lic_plaats", "fr2.lic_number", "fr2.callstack_crc_1", "fr2.callstack_crc_3", "fr2.exception_class", "fr2.exception_message", "fr2.full_report", "fr2.screenshot_filename", "fr2.ts", "fr2.duplicate_of", "fr2.actief", "fr2.commentaar", "fr2.bugzilla", "fr2.attachment_filename", "fr2.version_according_to_datetime", "fr2.program_according_to_full_report", "fr2.date"],
"Plans": [
{
"Node Type": "Nested Loop",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Join Type": "Inner",
"Startup Cost": 0.42,
"Total Cost": 103148.97,
"Plan Rows": 493512899,
"Plan Width": 830,
"Output": ["fr1.callstack_crc_2", "fr1.id", "fr1.program", "fr1.version", "fr1.lic_bedrijf", "fr1.lic_plaats", "fr1.lic_number", "fr1.callstack_crc_1", "fr1.callstack_crc_3", "fr1.exception_class", "fr1.exception_message", "fr1.full_report", "fr1.screenshot_filename", "fr1.ts", "fr1.duplicate_of", "fr1.actief", "fr1.commentaar", "fr1.bugzilla", "fr1.attachment_filename", "fr1.version_according_to_datetime", "fr1.program_according_to_full_report", "fr1.date", "fr2.id", "fr2.program", "fr2.version", "fr2.lic_bedrijf", "fr2.lic_plaats", "fr2.lic_number", "fr2.callstack_crc_1", "fr2.callstack_crc_3", "fr2.exception_class", "fr2.exception_message", "fr2.full_report", "fr2.screenshot_filename", "fr2.ts", "fr2.duplicate_of", "fr2.actief", "fr2.commentaar", "fr2.bugzilla", "fr2.attachment_filename", "fr2.version_according_to_datetime", "fr2.program_according_to_full_report", "fr2.date"],
"Plans": [
{
"Node Type": "Seq Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Relation Name": "foutrapport",
"Schema": "public",
"Alias": "fr1",
"Startup Cost": 0.00,
"Total Cost": 6048.89,
"Plan Rows": 105489,
"Plan Width": 419,
"Output": ["fr1.id", "fr1.program", "fr1.version", "fr1.lic_bedrijf", "fr1.lic_plaats", "fr1.lic_number", "fr1.callstack_crc_1", "fr1.callstack_crc_2", "fr1.callstack_crc_3", "fr1.exception_class", "fr1.exception_message", "fr1.full_report", "fr1.screenshot_filename", "fr1.ts", "fr1.duplicate_of", "fr1.actief", "fr1.commentaar", "fr1.bugzilla", "fr1.attachment_filename", "fr1.version_according_to_datetime", "fr1.program_according_to_full_report", "fr1.date"]
},
{
"Node Type": "Index Scan",
"Parent Relationship": "Inner",
"Parallel Aware": false,
"Scan Direction": "Forward",
"Index Name": "foutrapport_callstack_crc_2_idx",
"Relation Name": "foutrapport",
"Schema": "public",
"Alias": "fr2",
"Startup Cost": 0.42,
"Total Cost": 0.81,
"Plan Rows": 11,
"Plan Width": 419,
"Output": ["fr2.id", "fr2.program", "fr2.version", "fr2.lic_bedrijf", "fr2.lic_plaats", "fr2.lic_number", "fr2.callstack_crc_1", "fr2.callstack_crc_2", "fr2.callstack_crc_3", "fr2.exception_class", "fr2.exception_message", "fr2.full_report", "fr2.screenshot_filename", "fr2.ts", "fr2.duplicate_of", "fr2.actief", "fr2.commentaar", "fr2.bugzilla", "fr2.attachment_filename", "fr2.version_according_to_datetime", "fr2.program_according_to_full_report", "fr2.date"],
"Index Cond": "(fr2.callstack_crc_2 = fr1.callstack_crc_2)"
}
]
}
]
}
}
]")__json__";
Json::Value root; // will contains the root value after parsing.
Json::Reader reader;
bool parsingSuccessful = reader.parse(input, root);
ASSERT_TRUE(parsingSuccessful);
auto explain = ExplainRoot::createFromJson(root);
ASSERT_TRUE(explain != nullptr);
}

View file

@ -0,0 +1,73 @@
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include "PasswordManager.h"
using namespace testing;
TEST(PasswordManager, initial_changeMasterPassword_returns_true)
{
PasswordManager pwm;
auto res = pwm.changeMasterPassword("", "my test passphrase");
ASSERT_NO_THROW( res.get() );
ASSERT_THAT( res.get(), Eq(true) );
}
TEST(PasswordManager, unlock_succeeds)
{
PasswordManager pwm;
std::string passphrase = "my test passphrase";
auto res = pwm.changeMasterPassword("", passphrase);
ASSERT_NO_THROW( res.get() );
ASSERT_THAT( res.get(), Eq(true) );
auto res2 = pwm.unlock(passphrase);
ASSERT_NO_THROW( res2.get() );
ASSERT_THAT( res2.get(), Eq(true) );
}
TEST(PasswordManager, unlock_fails)
{
PasswordManager pwm;
std::string passphrase = "my test passphrase";
auto res = pwm.changeMasterPassword("", passphrase);
ASSERT_NO_THROW( res.get() );
ASSERT_THAT( res.get(), Eq(true) );
auto res2 = pwm.unlock(passphrase + "2");
ASSERT_NO_THROW( res2.get() );
ASSERT_THAT( res2.get(), Eq(false) );
}
TEST(PasswordManager, test_save_get)
{
PasswordManager pwm;
std::string passphrase = "my test passphrase";
auto res = pwm.changeMasterPassword("", passphrase);
ASSERT_NO_THROW( res.get() );
ASSERT_THAT( res.get(), Eq(true) );
// auto res2 = pwm.unlock(passphrase + "2");
// ASSERT_NO_THROW( res2.get() );
// ASSERT_THAT( res2.get(), Eq(false) );
const std::string password = "password123";
const std::string key = "abc";
auto res2 = pwm.savePassword(key, password);
ASSERT_THAT( res2.valid(), Eq(true) );
std::string result;
auto res3 = pwm.getPassword(key, result);
ASSERT_THAT( res3.valid(), Eq(true) );
ASSERT_THAT( res3.get(), Eq(true) );
ASSERT_THAT( result, Eq(password) );
}

View file

@ -0,0 +1,38 @@
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include "SqlLexer.h"
using namespace testing;
TEST(SqlLexer, lexer)
{
QString input = " SELECT ";
SqlLexer lexer(input, LexerState::Null);
int startpos, length;
BasicTokenType tokentype;
QString out;
lexer.nextBasicToken(startpos, length, tokentype, out);
ASSERT_THAT(startpos, Eq(1));
ASSERT_THAT(length, Eq(6));
ASSERT_THAT(tokentype, Eq(BasicTokenType::Symbol));
ASSERT_THAT( out, Eq(QString("SELECT")) );
}
TEST(SqlLexer, lexer_quote_in_string)
{
QString input = " 'abc''def' ";
SqlLexer lexer(input, LexerState::Null);
int startpos, length;
BasicTokenType tokentype;
QString out;
lexer.nextBasicToken(startpos, length, tokentype, out);
ASSERT_THAT(startpos, Eq(1));
ASSERT_THAT(length, Eq(10));
ASSERT_THAT(tokentype, Eq(BasicTokenType::QuotedString));
}

View file

@ -0,0 +1,203 @@
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include "Expected.h"
using namespace testing;
Expected<int> getAnswerToEverything() { return 42; }
TEST(expected, valid_when_valid_returns_true)
{
Expected<int> v = getAnswerToEverything();
ASSERT_THAT(v.valid(), Eq(true));
}
TEST(expected, get_when_valid_returns_value)
{
Expected<int> v = getAnswerToEverything();
ASSERT_THAT(v.get(), Eq(42));
}
TEST(expected, hasException_when_valid_returns_false)
{
Expected<int> v = getAnswerToEverything();
ASSERT_THAT(v.hasException<std::exception>(), Eq(false));
}
TEST(expected, T_fromException_is_not_valid)
{
auto e = Expected<int>::fromException(std::runtime_error("hello"));
ASSERT_THAT(e.valid(), Eq(false));
}
TEST(expected, T_fromException_get_thows)
{
auto e = Expected<int>::fromException(std::runtime_error("hello"));
ASSERT_THROW (e.get(), std::runtime_error);
}
TEST(expected, T_fromException_has_exception_true)
{
auto e = Expected<int>::fromException(std::runtime_error("hello"));
ASSERT_THAT(e.hasException<std::runtime_error>(), Eq(true));
}
TEST(expected, T_fromException_has_exception_false)
{
auto e = Expected<int>::fromException(std::runtime_error("hello"));
ASSERT_THAT(e.hasException<std::logic_error>(), Eq(false));
}
TEST(expected, T_fromException_has_derived_exception)
{
auto e = Expected<int>::fromException(std::runtime_error("hello"));
ASSERT_THAT(e.hasException<std::exception>(), Eq(true));
}
TEST(expected, T_fromCode_is_valid)
{
auto e = Expected<int>::fromCode([]() -> int { return 42; });
ASSERT_THAT(e.valid(), Eq(true));
}
TEST(expected, T_fromCode_get)
{
auto e = Expected<int>::fromCode([]() -> int { return 42; });
ASSERT_THAT(e.get(), Eq(42));
}
TEST(expected, T_fromCode_E_is_not_valid)
{
auto e = Expected<int>::fromCode([]() -> int { throw std::runtime_error("hello"); });
ASSERT_THAT(e.valid(), Eq(false));
}
TEST(expected, T_fromCode_E_get_thows)
{
auto e = Expected<int>::fromCode([]() -> int { throw std::runtime_error("hello"); });
ASSERT_THROW (e.get(), std::runtime_error);
}
TEST(expected, T_fromCode_E_has_exception_true)
{
auto e = Expected<int>::fromCode([]() -> int { throw std::runtime_error("hello"); });
ASSERT_THAT(e.hasException<std::runtime_error>(), Eq(true));
}
TEST(expected, T_fromCode_E_has_exception_false)
{
auto e = Expected<int>::fromCode([]() -> int { throw std::runtime_error("hello"); });
ASSERT_THAT(e.hasException<std::logic_error>(), Eq(false));
}
TEST(expected, T_fromCode_E_has_derived_exception)
{
auto e = Expected<int>::fromCode([]() -> int { throw std::runtime_error("hello"); });
ASSERT_THAT(e.hasException<std::exception>(), Eq(true));
}
//Expected<int> getIntWithStdRuntimeError() { return Expected<void>(); }
Expected<void> getNothing() { return Expected<void>(); }
TEST(expected_void, valid_when_valid_returns_true)
{
Expected<void> v = getNothing();
ASSERT_THAT(v.valid(), Eq(true));
}
TEST(expected_void, get_when_valid_returns_value)
{
Expected<void> v = getNothing();
ASSERT_NO_THROW(v.get());
}
TEST(expected_void, hasException_when_valid_returns_false)
{
Expected<void> v = getNothing();
ASSERT_THAT(v.hasException<std::exception>(), Eq(false));
}
TEST(expected_void, void_fromException_is_not_valid)
{
auto e = Expected<void>::fromException(std::runtime_error("hello"));
ASSERT_THAT(e.valid(), Eq(false));
}
TEST(expected_void, void_fromException_get_thows)
{
auto e = Expected<void>::fromException(std::runtime_error("hello"));
ASSERT_THROW (e.get(), std::runtime_error);
}
TEST(expected_void, void_fromException_has_exception_true)
{
auto e = Expected<void>::fromException(std::runtime_error("hello"));
ASSERT_THAT(e.hasException<std::runtime_error>(), Eq(true));
}
TEST(expected_void, void_fromException_has_exception_false)
{
auto e = Expected<void>::fromException(std::runtime_error("hello"));
ASSERT_THAT(e.hasException<std::logic_error>(), Eq(false));
}
TEST(expected_void, void_fromException_has_derived_exception)
{
auto e = Expected<void>::fromException(std::runtime_error("hello"));
ASSERT_THAT(e.hasException<std::exception>(), Eq(true));
}
TEST(expected_void, void_fromCode_is_valid)
{
auto e = Expected<void>::fromCode([]() -> void { });
ASSERT_THAT(e.valid(), Eq(true));
}
TEST(expected_void, void_fromCode_get)
{
auto e = Expected<void>::fromCode([]() -> void { });
ASSERT_NO_THROW(e.get());
}
void expected_void_throws_func()
{
throw std::runtime_error("hello");
}
TEST(expected_void, void_fromCode_E_is_not_valid)
{
auto e = Expected<void>::fromCode(expected_void_throws_func);
ASSERT_THAT(e.valid(), Eq(false));
}
TEST(expected_void, void_fromCode_E_get_thows)
{
auto e = Expected<void>::fromCode(expected_void_throws_func);
ASSERT_THROW (e.get(), std::runtime_error);
}
TEST(expected_void, void_fromCode_E_has_exception_true)
{
auto e = Expected<void>::fromCode(expected_void_throws_func);
ASSERT_THAT(e.hasException<std::runtime_error>(), Eq(true));
}
TEST(expected_void, void_fromCode_E_has_exception_false)
{
auto e = Expected<void>::fromCode(expected_void_throws_func);
ASSERT_THAT(e.hasException<std::logic_error>(), Eq(false));
}
TEST(expected_void, void_fromCode_E_has_derived_exception)
{
auto e = Expected<void>::fromCode(expected_void_throws_func);
ASSERT_THAT(e.hasException<std::exception>(), Eq(true));
}

View file

@ -0,0 +1,63 @@
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include "ScopeGuard.h"
using namespace testing;
TEST(ScopeGuard, normal_run_fun_on_destruction_1)
{
bool result = false;
auto sg = scopeGuard([&result]() { result = true; });
ASSERT_THAT(result, Eq(false));
}
TEST(ScopeGuard, normal_run_fun_on_destruction_2)
{
bool result = false;
{
auto sg = scopeGuard([&result]() { result = true; });
}
ASSERT_EQ(result, true);
}
TEST(ScopeGuard, dismiss)
{
bool result = false;
{
auto sg = scopeGuard([&result]() { result = true; });
sg.dismiss();
}
ASSERT_THAT(result, Eq(false));
}
TEST(ScopeGuard, SCOPE_EXIT_macro_1)
{
bool result = false;
{
SCOPE_EXIT { result = true; };
ASSERT_THAT(result, Eq(false)); // prove previous statement hasn't run yet
}
}
TEST(ScopeGuard, SCOPE_EXIT_macro_2)
{
bool result = false;
{
SCOPE_EXIT { result = true; };
}
ASSERT_THAT(result, Eq(true));
}
//TEST(expected, get_when_valid_returns_value)
//{
// Expected<int> v = getAnswerToEverything();
// ASSERT_THAT(v.get(), Eq(42));
//}