Skip to content

Commit

Permalink
Add tests for MemberRef
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoritzinsky committed Jan 31, 2024
1 parent 9491f51 commit 027ece6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/emit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(SOURCES
moduleref.cpp
methoddef.cpp
standalonesig.cpp
memberref.cpp
)

set(HEADERS emit.hpp)
Expand Down
62 changes: 62 additions & 0 deletions test/emit/memberref.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "emit.hpp"
#include <array>
#include <vector>
#include <gmock/gmock.h>

TEST(MemberRef, Define)
{
dncp::com_ptr<IMetaDataEmit> emit;
ASSERT_NO_FATAL_FAILURE(CreateEmit(emit));
mdMemberRef memberRef;
std::array<uint8_t, 3> signature = {0x01, 0x02, 0x03};
ASSERT_EQ(S_OK, emit->DefineMemberRef(TokenFromRid(1, mdtTypeDef), W("Foo"), signature.data(), (ULONG)signature.size(), &memberRef));
ASSERT_EQ(1, RidFromToken(memberRef));
ASSERT_EQ(mdtMemberRef, TypeFromToken(memberRef));

dncp::com_ptr<IMetaDataImport> import;
ASSERT_EQ(S_OK, emit->QueryInterface(IID_IMetaDataImport, (void**)&import));

mdTypeDef type;
WSTR_string readName;
readName.resize(3);
ULONG readNameLength;
PCCOR_SIGNATURE sigBlob;
ULONG sigBlobLength;
ASSERT_EQ(S_OK, import->GetMemberRefProps(memberRef, &type, readName.data(), (ULONG)readName.capacity(), &readNameLength, &sigBlob, &sigBlobLength));
EXPECT_EQ(W("Foo"), readName.substr(0, readNameLength - 1));
EXPECT_EQ(TokenFromRid(1, mdtTypeDef), type);
EXPECT_THAT(std::vector(sigBlob, sigBlob + sigBlobLength), testing::ContainerEq(std::vector(signature.begin(), signature.end())));
}

TEST(MemberRef, SetParent)
{
dncp::com_ptr<IMetaDataEmit> emit;
ASSERT_NO_FATAL_FAILURE(CreateEmit(emit));
mdMemberRef memberRef;
std::array<uint8_t, 3> signature = {0x01, 0x02, 0x03};
ASSERT_EQ(S_OK, emit->DefineMemberRef(TokenFromRid(1, mdtTypeDef), W("Foo"), signature.data(), (ULONG)signature.size(), &memberRef));
ASSERT_EQ(1, RidFromToken(memberRef));
ASSERT_EQ(mdtMemberRef, TypeFromToken(memberRef));

dncp::com_ptr<IMetaDataImport> import;
ASSERT_EQ(S_OK, emit->QueryInterface(IID_IMetaDataImport, (void**)&import));

mdToken parent;
WSTR_string readName;
readName.resize(3);
ULONG readNameLength;
PCCOR_SIGNATURE sigBlob;
ULONG sigBlobLength;
ASSERT_EQ(S_OK, import->GetMemberRefProps(memberRef, &parent, readName.data(), (ULONG)readName.capacity(), &readNameLength, &sigBlob, &sigBlobLength));
EXPECT_EQ(W("Foo"), readName.substr(0, readNameLength - 1));
EXPECT_EQ(TokenFromRid(1, mdtTypeDef), parent);
EXPECT_THAT(std::vector(sigBlob, sigBlob + sigBlobLength), testing::ContainerEq(std::vector(signature.begin(), signature.end())));

ASSERT_EQ(S_OK, emit->SetParent(memberRef, TokenFromRid(2, mdtTypeRef)));

readName.resize(3);
ASSERT_EQ(S_OK, import->GetMemberRefProps(memberRef, &parent, readName.data(), (ULONG)readName.capacity(), &readNameLength, &sigBlob, &sigBlobLength));
EXPECT_EQ(W("Foo"), readName.substr(0, readNameLength - 1));
EXPECT_EQ(TokenFromRid(2, mdtTypeRef), parent);
EXPECT_THAT(std::vector(sigBlob, sigBlob + sigBlobLength), testing::ContainerEq(std::vector(signature.begin(), signature.end())));
}

0 comments on commit 027ece6

Please sign in to comment.