Tests for BString convenience functions, plus a fix to BMessageField for a
bug revealed by those tests. Gotta love those unit tests! git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3327 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
1a3b3db436
commit
18a8d114f5
@ -590,6 +590,13 @@ struct BMessageFieldGetDataPolicy<BDataBuffer>
|
||||
{ return data->Buffer(); }
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
template<>
|
||||
struct BMessageFieldGetDataPolicy<BString>
|
||||
{
|
||||
inline static const void* GetData(const BString* data)
|
||||
{ return data->String(); }
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
} // namespace BPrivate
|
||||
|
||||
|
147
src/tests/kits/app/bmessage/MessageBStringItemTest.h
Normal file
147
src/tests/kits/app/bmessage/MessageBStringItemTest.h
Normal file
@ -0,0 +1,147 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// MessageStringItemTest.h
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#ifndef MESSAGESTRINGITEMTEST_H
|
||||
#define MESSAGESTRINGITEMTEST_H
|
||||
|
||||
// Standard Includes -----------------------------------------------------------
|
||||
|
||||
// System Includes -------------------------------------------------------------
|
||||
#include <Debug.h>
|
||||
#include <String.h>
|
||||
|
||||
// Project Includes ------------------------------------------------------------
|
||||
|
||||
// Local Includes --------------------------------------------------------------
|
||||
#include "MessageItemTest.h"
|
||||
|
||||
// Local Defines ---------------------------------------------------------------
|
||||
|
||||
// Globals ---------------------------------------------------------------------
|
||||
|
||||
struct TBStringFuncPolicy
|
||||
{
|
||||
static status_t Add(BMessage& msg, const char* name, BString& data);
|
||||
static status_t Find(BMessage& msg, const char* name, int32 index,
|
||||
BString* data);
|
||||
static BString QuickFind(BMessage& msg, const char* name, int32 index);
|
||||
static bool Has(BMessage& msg, const char* name, int32 index);
|
||||
static status_t Replace(BMessage& msg, const char* name, int32 index,
|
||||
BString& data);
|
||||
static status_t AddData(BMessage& msg, const char* name, type_code type,
|
||||
const BString* data, ssize_t size);
|
||||
static status_t FindData(BMessage& msg, const char* name, type_code type,
|
||||
int32 index, const void** data, ssize_t* size);
|
||||
|
||||
private:
|
||||
static BString sStr;
|
||||
};
|
||||
BString TBStringFuncPolicy::sStr;
|
||||
//------------------------------------------------------------------------------
|
||||
status_t TBStringFuncPolicy::Add(BMessage& msg, const char* name,
|
||||
BString& data)
|
||||
{
|
||||
return msg.AddString(name, data);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
status_t TBStringFuncPolicy::Find(BMessage& msg, const char* name,
|
||||
int32 index, BString* data)
|
||||
{
|
||||
return msg.FindString(name, index, data);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
BString TBStringFuncPolicy::QuickFind(BMessage& msg, const char* name,
|
||||
int32 index)
|
||||
{
|
||||
BString data;
|
||||
msg.FindString(name, index, &data);
|
||||
return data;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool TBStringFuncPolicy::Has(BMessage& msg, const char* name,
|
||||
int32 index)
|
||||
{
|
||||
return msg.HasString(name, index);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
status_t TBStringFuncPolicy::Replace(BMessage& msg, const char* name,
|
||||
int32 index, BString& data)
|
||||
{
|
||||
return msg.ReplaceString(name, index, data);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
status_t TBStringFuncPolicy::AddData(BMessage& msg, const char* name,
|
||||
type_code type, const BString* data,
|
||||
ssize_t size)
|
||||
{
|
||||
return msg.AddData(name, type, (const void*)data->String(), size,
|
||||
false);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
status_t TBStringFuncPolicy::FindData(BMessage& msg, const char* name,
|
||||
type_code type, int32 index,
|
||||
const void** data, ssize_t* size)
|
||||
{
|
||||
*data = NULL;
|
||||
char* ptr;
|
||||
status_t err = msg.FindData(name, type, index, (const void**)&ptr, size);
|
||||
if (!err)
|
||||
{
|
||||
sStr = ptr;
|
||||
*(BString**)data = &sStr;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
|
||||
struct TBStringInitPolicy : public ArrayTypeBase<BString>
|
||||
{
|
||||
inline static BString Zero() { return BString(); }
|
||||
inline static BString Test1() { return "Test1"; }
|
||||
inline static BString Test2() { return "Supercalafragilistricexpialadocious"; }
|
||||
inline static size_t SizeOf(const BString& s) { return s.Length() + 1; }
|
||||
inline static ArrayType Array()
|
||||
{
|
||||
ArrayType array;
|
||||
array.push_back(Zero());
|
||||
array.push_back(Test1());
|
||||
array.push_back(Test2());
|
||||
return array;
|
||||
}
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
struct TBStringAssertPolicy
|
||||
{
|
||||
inline static BString Zero() { return BString(); }
|
||||
inline static BString Invalid() { return BString(); }
|
||||
static bool Size(size_t size, BString& msg)
|
||||
;//{ return size == msg.FlattenedSize(); }
|
||||
};
|
||||
bool TBStringAssertPolicy::Size(size_t size, BString& data)
|
||||
{
|
||||
return size == data.Length() + 1;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
typedef TMessageItemTest
|
||||
<
|
||||
BString,
|
||||
B_STRING_TYPE,
|
||||
TBStringFuncPolicy,
|
||||
TBStringInitPolicy,
|
||||
TBStringAssertPolicy
|
||||
>
|
||||
TMessageBStringItemTest;
|
||||
|
||||
#endif // MESSAGESTRINGITEMTEST_H
|
||||
|
||||
/*
|
||||
* $Log $
|
||||
*
|
||||
* $Id $
|
||||
*
|
||||
*/
|
||||
|
@ -190,7 +190,7 @@ struct TMessageAssertPolicy
|
||||
static bool Size(size_t size, BMessage& msg)
|
||||
;//{ return size == msg.FlattenedSize(); }
|
||||
};
|
||||
bool TMessageAssertPolicy::Size(size_t size, BMessage& msg)
|
||||
bool TMessageAssertPolicy::Size(size_t size, BMessage& msg)
|
||||
{
|
||||
ssize_t msgSize = msg.FlattenedSize();
|
||||
return size == msgSize;
|
||||
|
@ -14,9 +14,9 @@
|
||||
#include "MessageFloatItemTest.h"
|
||||
#include "MessageDoubleItemTest.h"
|
||||
#include "MessageMessageItemTest.h"
|
||||
//#ifdef TEST_OBOS
|
||||
#include "MessageRefItemTest.h"
|
||||
//#endif
|
||||
#include "MessageBStringItemTest.h"
|
||||
#include "MessageCStringItemTest.h"
|
||||
|
||||
Test* MessageTestSuite()
|
||||
{
|
||||
@ -35,9 +35,9 @@ Test* MessageTestSuite()
|
||||
tests->addTest(TMessageFloatItemTest::Suite());
|
||||
tests->addTest(TMessageDoubleItemTest::Suite());
|
||||
tests->addTest(TMessageMessageItemTest::Suite());
|
||||
//#ifdef TEST_OBOS
|
||||
tests->addTest(TMessageRefItemTest::Suite());
|
||||
//#endif
|
||||
tests->addTest(TMessageBStringItemTest::Suite());
|
||||
tests->addTest(TMessageCStringItemTest::Suite());
|
||||
|
||||
return tests;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user