Home > database >  Problems with Unit Testing in C (Visual studio 2022)
Problems with Unit Testing in C (Visual studio 2022)

Time:06-22

Issues with unit testing

I cant really see what is going wrong with that code, but something doesn't include right relevant files:

MetaData.h

#pragma once
#include <string>
enum ChessColor
{
    White,
    Black
};
enum PieceType
{
   King,
   Queen,
   Rook,
   Bishop,
   Knight,
   Pawn,
   None
};
const int BOARD_SIZE = 8;

std::string getLongNameOfChessType(PieceType* type);
std::string getShortNameOfChessType(PieceType* type);

std::string getLongNameOfChessColor(ChessColor* color);
std::string getShortNameOfChessColor(ChessColor* color);

MetaData.cpp

#include "MetaData.h"

std::string getLongNameOfChessType(PieceType* type)
{
    switch (*type)
    {
    case PieceType::King:
        return "King";
    case PieceType::Queen:
        return "Queen";
    case PieceType::Rook:
        return "Rook";
    case PieceType::Bishop:
        return "Bishop";
    case PieceType::Knight:
        return "Knight";
    case PieceType::Pawn:
        return "Pawn";
    default:
        return "NoType";
    }
}

std::string getShortNameOfChessType(PieceType* type)
{
    switch (*type)
    {
    case PieceType::King:
        return "K";
    case PieceType::Queen:
        return "Q";
    case PieceType::Rook:
        return "R";
    case PieceType::Bishop:
        return "B";
    case PieceType::Knight:
        return "N";
    case PieceType::Pawn:
        return "P";
    default:
        return "NoType";
    }
}
std::string getLongNameOfChessColor(ChessColor* color)
{
    switch (*color)
    {
    case ChessColor::White:
        return "White";
    case ChessColor::Black:
        return "Black";
    default:
        return "NoColor";
    }
}
std::string getShortNameOfChessColor(ChessColor* color)
{
    switch (*color)
    {
    case ChessColor::White:
        return "W";
    case ChessColor::Black:
        return "B";
    default:
        return "NoColor";
    }
}

ChessTest.cpp

The test file.

#include "pch.h"
#include "CppUnitTest.h"
#include <MetaData.h>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace ChessTest
{
    TEST_CLASS(ChessTest)
    {
    public:
        
        TEST_METHOD(TestMethod1)
        {
            ChessColor c = ChessColor::Black;
            std::string actual = "B";
            std::string trying = getShortNameOfChessColor(&c);
            Assert::AreEqual(actual,trying);
        }
    };
}

So the errors i get are these

Error   LNK2019 unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl getShortNameOfChessColor(enum ChessColor *)" (?getShortNameOfChessColor@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEAW4ChessColor@@@Z) referenced in function "public: void __cdecl ChessTest::ChessTest::TestMethod1(void)" (?TestMethod1@ChessTest@1@QEAAXXZ)    ChessTest   C:\Users\censoredUsername\Desktop\prog\Chess\ChessProject\ChessTesting\ChessTest\ChessTest.obj  1

A few warnings

Warning C26812  The enum type 'PieceType' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).    ChessProject    C:\Users\censoredUsername\Desktop\prog\Chess\ChessProject\ChessProject\MetaData.cpp 3
Warning C26812  The enum type 'ChessColor' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).   ChessProject    C:\Users\censoredUsername\Desktop\prog\Chess\ChessProject\ChessProject\MetaData.cpp 44  
Warning C26812  The enum type 'ChessColor' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).   ChessTest   C:\Users\censoredUsername\Desktop\prog\Chess\ChessProject\ChessTesting\ChessTest\ChessTest.cpp  14  

Error 2

Error   LNK1120 1 unresolved externals  ChessTest   C:\Users\censoredUsername\Desktop\prog\Chess\ChessProject\x64\Debug\ChessTest.dll   1   

Some Extra info

I made a project, added some basic files and wanted to do a test driven development.
Thus i added a Native Unit Test Project to my current Project. Described here: MS Description to add unit tests

I coded in a lot of languages before, but have the most experience with java/c# (and as you know, these are very different to cpp)

I appreciate every comment/input for that matter.

CodePudding user response:

I noticed that you used #include <MetaData.h>.

Use Add Existing item to add MetaData .h and .cpp. Or, you need to build static library for MetaData.

  • Related