Skip to content

Commit

Permalink
Support constructing long LDAP expressions using concise C++
Browse files Browse the repository at this point in the history
Fixes #246

Signed-off-by: The Mathworks Inc <Roy.Lurie@mathworks.com>
  • Loading branch information
jeffdiclemente committed Oct 26, 2017
1 parent af81428 commit 7f878f1
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 6 deletions.
52 changes: 49 additions & 3 deletions framework/include/cppmicroservices/LDAPProp.h
Expand Up @@ -37,7 +37,7 @@ namespace cppmicroservices {
class US_Framework_EXPORT LDAPPropExpr
{
public:

LDAPPropExpr();
explicit LDAPPropExpr(const std::string& expr);

LDAPPropExpr& operator!();
Expand All @@ -46,10 +46,56 @@ class US_Framework_EXPORT LDAPPropExpr

bool IsNull() const;

private:

LDAPPropExpr& operator=(const LDAPPropExpr&);

/**
* Convenience operator for LDAP logical or '|'.
*
* Writing either
* \code
* LDAPPropExpr expr(LDAPProp("key1") == "value1");
* expr = expr || LDAPProp("key2") == "value2";
* \endcode
* or
* \code
* LDAPPropExpr expr(LDAPProp("key1") == "value1");
* expr |= LDAPProp("key2") == "value2";
* \endcode
* leads to the same string "(|(key1=value1) (key2=value2))".
*
* @param right A LDAP expression object.
* @return A LDAP expression object.
*
* @{
*/
LDAPPropExpr& operator|=(const LDAPPropExpr& right);
/// @}

/**
* Convenience operator for LDAP logical and '&'.
*
* Writing either
* \code
* LDAPPropExpr expr(LDAPProp("key1") == "value1");
* expr = expr && LDAPProp("key2") == "value2";
* \endcode
* or
* \code
* LDAPPropExpr expr(LDAPProp("key1") == "value1");
* expr &= LDAPProp("key2") == "value2";
* \endcode
* leads to the same string "(&(key1=value1) (key2=value2))".
*
* @param right A LDAP expression object.
* @return A LDAP expression object.
*
* @{
*/
LDAPPropExpr& operator&=(const LDAPPropExpr& right);
/// @}

private:

std::string m_ldapExpr;
};
/// \endcond
Expand Down
25 changes: 25 additions & 0 deletions framework/src/util/LDAPProp.cpp
Expand Up @@ -26,6 +26,11 @@

namespace cppmicroservices {

LDAPPropExpr::LDAPPropExpr()
: m_ldapExpr()
{
}

LDAPPropExpr::LDAPPropExpr(const std::string& expr)
: m_ldapExpr(expr)
{}
Expand All @@ -48,6 +53,26 @@ bool LDAPPropExpr::IsNull() const
return m_ldapExpr.empty();
}

LDAPPropExpr& LDAPPropExpr::operator=(const LDAPPropExpr& expr)
{
if(this != &expr)
{
m_ldapExpr = expr.m_ldapExpr;
}
return *this;
}

LDAPPropExpr& LDAPPropExpr::operator|=(const LDAPPropExpr& right)
{
m_ldapExpr = (*this || right).m_ldapExpr;
return *this;
}

LDAPPropExpr& LDAPPropExpr::operator&=(const LDAPPropExpr& right)
{
m_ldapExpr = (*this && right).m_ldapExpr;
return *this;
}

LDAPProp::LDAPProp(const std::string& property)
: m_property(property)
Expand Down
54 changes: 51 additions & 3 deletions framework/test/gtest/LDAPExprTest.cpp
Expand Up @@ -20,11 +20,12 @@ limitations under the License.
=============================================================================*/

#include "cppmicroservices/LDAPFilter.h"
#include "cppmicroservices/FrameworkFactory.h"
#include "cppmicroservices/Framework.h"
#include "cppmicroservices/Bundle.h"
#include "cppmicroservices/BundleContext.h"
#include "cppmicroservices/FrameworkFactory.h"
#include "cppmicroservices/Framework.h"
#include "cppmicroservices/LDAPFilter.h"
#include "cppmicroservices/LDAPProp.h"
#include "cppmicroservices/ServiceEvent.h"
#include "cppmicroservices/ServiceTracker.h"

Expand Down Expand Up @@ -242,3 +243,50 @@ TEST(LDAPExprTest, ParseExceptions)
// Testing '\\' case in LDAPExpr::ParseState::getAttributeValue()
ASSERT_EQ(LDAPFilter("(name=ab\\a)"), LDAPFilter("(name=aba)"));
}

TEST(LDAPExprTest, BitWiseOperatorOr)
{
LDAPPropExpr checkedExpr((LDAPProp("key1") == "value1") || (LDAPProp("key2") == "value2"));

LDAPPropExpr expr(LDAPProp("key1") == "value1");

expr |= LDAPProp("key2") == "value2";
ASSERT_EQ(expr.operator std::string(), checkedExpr.operator std::string());

}

TEST(LDAPExprTest, BitWiseOperatorAnd)
{
LDAPPropExpr checkedExpr((LDAPProp("key1") == "value1") && (LDAPProp("key2") == "value2"));

LDAPPropExpr expr(LDAPProp("key1") == "value1");

expr &= LDAPProp("key2") == "value2";

ASSERT_EQ(expr.operator std::string(), checkedExpr.operator std::string());
}

TEST(LDAPExprTest, OperatorAssignment)
{
LDAPPropExpr checkedExpr((LDAPProp("key1") == "value1") || (LDAPProp("key2") == "value2"));
LDAPPropExpr expr(LDAPProp("key1") == "value1");

expr = expr || LDAPProp("key2") == "value2";

ASSERT_EQ(expr.operator std::string(), checkedExpr.operator std::string());
}

TEST(LDAPExprTest, AssignToDefaultConstructed)
{
LDAPPropExpr checkedExpr((LDAPProp("key2") == "value2"));

LDAPPropExpr defaultConstructed;
ASSERT_TRUE(defaultConstructed.IsNull());

defaultConstructed |= LDAPProp("key2") == "value2";
ASSERT_EQ(defaultConstructed.operator std::string(), checkedExpr.operator std::string());

LDAPPropExpr expr(LDAPProp("key2") == "value2");
expr |= LDAPPropExpr();
ASSERT_EQ(expr.operator std::string(), checkedExpr.operator std::string());
}

0 comments on commit 7f878f1

Please sign in to comment.