Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Unit tests #347

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@

build/*
.DS_Store
.vscode
22 changes: 21 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ string(REPLACE ";" "|" TEST_PREFIX_PATH "${CMAKE_PREFIX_PATH}")
string(REPLACE ";" "|" TEST_FRAMEWORK_PATH "${CMAKE_FRAMEWORK_PATH}")
string(REPLACE ";" "|" TEST_MODULE_PATH "${CMAKE_MODULE_PATH}")

set(BUILD_TESTS FALSE CACHE BOOL "Build unit tests")
set(BUILD_TESTS FALSE CACHE BOOL "Build unit tests, which should have been integration tests,
but are left here named unit tests, so that the newer correctly-named integration tests,
don't have a name conflict")

if(BUILD_TESTS)
message(STATUS "Building unit tests.")
Expand All @@ -92,3 +94,21 @@ if(BUILD_TESTS)
else()
message(STATUS "Unit tests will not be built. To build unit tests, set BUILD_TESTS to true.")
endif()

set(BUILD_INTEGRATION_TESTS FALSE CACHE BOOL "Build integration tests")

if(BUILD_INTEGRATION_TESTS)
ExternalProject_Add(
integration-tests
SOURCE_DIR ${CMAKE_SOURCE_DIR}/integration-tests
BINARY_DIR ${CMAKE_BINARY_DIR}/integration-tests
CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${EOSIO_CDT_ROOT}/lib/cmake/eosio.cdt/EosioWasmToolchain.cmake
UPDATE_COMMAND ""
PATCH_COMMAND ""
TEST_COMMAND ""
INSTALL_COMMAND ""
BUILD_ALWAYS 1
)
else()
message(STATUS "Integration tests will not be built. To build integration tests, set BUILD_INTEGRATION_TESTS to true.")
endif()
2 changes: 1 addition & 1 deletion contracts/eosio.token/include/eosio.token/eosio.token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ namespace eosio {
using transfer_action = eosio::action_wrapper<"transfer"_n, &token::transfer>;
using open_action = eosio::action_wrapper<"open"_n, &token::open>;
using close_action = eosio::action_wrapper<"close"_n, &token::close>;
private:

struct [[eosio::table]] account {
asset balance;

Expand Down
78 changes: 78 additions & 0 deletions integration-tests/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
BasedOnStyle: LLVM
IndentWidth: 3
UseTab: Never
ColumnLimit: 120

---
Language: Cpp
# always align * and & to the type
DerivePointerAlignment: false
PointerAlignment: Left

# regroup includes to these classes
IncludeCategories:
- Regex: '(<|"(eosio)/)'
Priority: 4
- Regex: '(<|"(boost)/)'
Priority: 3
- Regex: '(<|"(llvm|llvm-c|clang|clang-c)/'
Priority: 3
- Regex: '<[[:alnum:]]+>'
Priority: 2
- Regex: '.*'
Priority: 1

#IncludeBlocks: Regroup

# set indent for public, private and protected
#AccessModifierOffset: 3

# make line continuations twice the normal indent
ContinuationIndentWidth: 6

# add missing namespace comments
FixNamespaceComments: true

# add spaces to braced list i.e. int* foo = { 0, 1, 2 }; instead of int* foo = {0,1,2};
Cpp11BracedListStyle: false
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignOperands: true
AlignTrailingComments: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortBlocksOnASingleLine: true
#AllowShortIfStatementsOnASingleLine: WithoutElse
#AllowShortIfStatementsOnASingleLine: true
#AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakTemplateDeclarations: true

BinPackParameters: true
### use this with clang9
BreakBeforeBraces: Custom
BraceWrapping:
#AfterCaseLabel: true
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false

BreakConstructorInitializers: BeforeColon
CompactNamespaces: true
IndentCaseLabels: true
IndentPPDirectives: AfterHash
NamespaceIndentation: Inner
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true

ReflowComments: false
---
26 changes: 26 additions & 0 deletions integration-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.10)

project(integration-tests)

set(EOSIO_WASM_OLD_BEHAVIOR "off")
find_package(eosio.cdt)

function(add_test TARGET SRC)
add_executable(${TARGET} ${SRC})
target_link_libraries(${TARGET} -ftester -stack-size=65536)
target_compile_options(${TARGET} PUBLIC -ftester -Os)
target_include_directories(${TARGET}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../contracts/eosio.bios/include
${CMAKE_CURRENT_SOURCE_DIR}/../contracts/eosio.msig/include
${CMAKE_CURRENT_SOURCE_DIR}/../contracts/eosio.system/include
${CMAKE_CURRENT_SOURCE_DIR}/../contracts/eosio.token/include
${CMAKE_CURRENT_SOURCE_DIR}/../contracts/eosio.wrap/include
)
set_target_properties(${TARGET}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endfunction()

add_test(eosio.msig-tests eosio.msig-tests.cpp)
add_test(eosio.token-tests eosio.token-tests.cpp)
83 changes: 83 additions & 0 deletions integration-tests/eosio.msig-tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <eosio.bios/eosio.bios.hpp>
#include <eosio.msig/eosio.msig.hpp>
#include <eosio.token/eosio.token.hpp>
#include <eosio/tester.hpp>

#define BOOST_TEST_MAIN
#include <boost/test/included/unit_test.hpp>

using namespace eosio;
using eosiobios::bios;

namespace eosio {

inline bool operator!=(const token::currency_stats& a, const token::currency_stats& b) {
return a.supply != b.supply || a.max_supply != b.max_supply || a.issuer != b.issuer;
}

} // namespace eosio

struct approve_args {
name proposer = {};
name proposal_name = {};
permission_level level = {};

EOSLIB_SERIALIZE(approve_args, (proposer)(proposal_name)(level))
};

struct msig_tester {
test_chain chain;

msig_tester() {
chain.set_code("eosio"_n, "contracts/eosio.bios/eosio.bios.wasm");
chain.create_code_account("eosio.msig"_n, true);
chain.create_account("eosio.stake"_n);
chain.create_account("eosio.ram"_n);
chain.create_account("eosio.ramfee"_n);
chain.create_account("alice"_n);
chain.create_account("bob"_n);
chain.create_account("carol"_n);
chain.set_code("eosio.msig"_n, "contracts/eosio.msig/eosio.msig.wasm");
}

transaction_trace propose(name proposer, name proposal_name, std::vector<permission_level> requested,
transaction trx, const char* expected_except = nullptr) {
return chain.transact({ multisig::propose_action{ "eosio.msig"_n, { proposer, "active"_n } }.to_action(
proposer, proposal_name, std::move(requested), std::move(trx)) },
expected_except);
}

transaction_trace approve(name proposer, name proposal_name, permission_level level,
const char* expected_except = nullptr) {
return chain.transact({ { level, "eosio.msig"_n, "approve"_n, approve_args{ proposer, proposal_name, level } } },
expected_except);
}

transaction_trace exec(name proposer, name proposal_name, name executer,
const char* expected_except = nullptr) {
return chain.transact({ multisig::exec_action{ "eosio.msig"_n, { executer, "active"_n } }.to_action(
proposer, proposal_name, executer) },
expected_except);
}

}; // msig_tester

BOOST_FIXTURE_TEST_CASE(propose_approve_execute, msig_tester) {
propose("alice"_n, "first"_n, { { "alice"_n, "active"_n } },
chain.make_transaction(
{ bios::reqauth_action{ "eosio"_n, { "alice"_n, "active"_n } }.to_action("alice"_n) }));

// fail to execute before approval
exec("alice"_n, "first"_n, "alice"_n, "transaction authorization failed");

// approve and execute
approve("alice"_n, "first"_n, { "alice"_n, "active"_n });
BOOST_TEST(!chain.exec_deferred());
exec("alice"_n, "first"_n, "alice"_n);

auto receipt = chain.exec_deferred();
BOOST_TEST(!chain.exec_deferred());
BOOST_TEST(receipt.has_value());
expect(*receipt);
BOOST_TEST(receipt->action_traces.size() == 1);
} // propose_approve_execute
Loading