Prevent self-comparison, added more tests, fixed some expression checks

This commit is contained in:
rbock
2016-04-07 21:47:17 +02:00
parent 3e008a2b04
commit a77f26ffe3
8 changed files with 105 additions and 65 deletions

View File

@@ -142,6 +142,28 @@ namespace test
};
using _traits = sqlpp::make_traits<sqlpp::time_point, sqlpp::tag::can_be_null>;
};
struct OtherString
{
struct _alias_t
{
static constexpr const char _literal[] = "other_string";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T otherString;
T& operator()()
{
return otherString;
}
const T& operator()() const
{
return otherString;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::varchar, sqlpp::tag::can_be_null>;
};
struct OtherInt
{
struct _alias_t
@@ -261,6 +283,7 @@ namespace test
TabAllTypes_::SomeBool,
TabAllTypes_::SomeDayPoint,
TabAllTypes_::SomeTimePoint,
TabAllTypes_::OtherString,
TabAllTypes_::OtherInt,
TabAllTypes_::OtherFloat,
TabAllTypes_::OtherBool,

View File

@@ -23,7 +23,7 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
function(test_compile name)
set(target sqlpp11_${name})
set(target sqlpp11_assert_${name})
add_executable(${target} ${name}.cpp)
target_link_libraries(${target} PRIVATE sqlpp11 sqlpp11_testing)
endfunction()

View File

@@ -44,7 +44,7 @@ namespace
}
template <typename Assert, typename Operand>
void static_check_comparison(const Operand& operand)
void static_check_self_compare(const Operand& operand)
{
using CheckResult = sqlpp::check_rhs_comparison_operand_t<Operand, Operand>;
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
@@ -63,12 +63,23 @@ namespace
void disallowed_self_comparison()
{
// static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someString);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.someString);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.someInt);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.someFloat);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.someBool);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.someDayPoint);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.someTimePoint);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.otherString);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.otherInt);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.otherFloat);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.otherBool);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.otherDayPoint);
static_check_self_compare<sqlpp::assert_comparison_lhs_rhs_differ_t>(t.otherTimePoint);
}
}
int main(int, char* [])
{
t.someString == t.someString;
// disallowed_self_comparison();
// t.someString == t.someString;
disallowed_self_comparison();
}

View File

@@ -66,9 +66,9 @@ namespace
void allowed_comparands()
{
static_check_comparison<sqlpp::consistent_t>("");
static_check_comparison<sqlpp::consistent_t>('d');
// static_check_comparison<sqlpp::consistent_t>('d'); // not today
static_check_comparison<sqlpp::consistent_t>(std::string(""));
static_check_comparison<sqlpp::consistent_t>(t.otherText);
static_check_comparison<sqlpp::consistent_t>(t.otherString);
}
void disallowed_comparands()
@@ -82,12 +82,32 @@ namespace
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someDayPoint);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someTimePoint);
}
template <typename Expected, typename Expression>
void static_check_type()
{
static_assert(std::is_same<Expected, Expression>::value, "Unexpected check result");
}
auto check_expressions() -> void
{
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString + 1)>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString - 1)>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString - "")>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString * 1)>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString * "")>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString / 1)>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString / "")>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString % 1)>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(t.someString % "")>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(-t.someString)>();
static_check_type<sqlpp::bad_expression<sqlpp::text>, decltype(+t.someString)>();
}
}
int main(int, char* [])
{
#error : This must fail:
t.someString + 1;
allowed_comparands();
disallowed_comparands();
check_expressions();
}