/*--------------------------------------------------------------------------* DeleteFileTest.cpp This file includes a basic example of fixture use. Uses WinUnit: http://msdn.microsoft.com/en-us/magazine/cc136757.aspx See also: http://www.wintellect.com/cs/blogs/jrobbins/archive/2008/01/17/winunit-an-outstanding-native-c-unit-testing-tool.aspx *--------------------------------------------------------------------------*/ #include "stdafx.h" #include "WinUnit.h" #include <windows.h> #include "p4connection.h" // Fixture must be declared. FIXTURE(P4Fixture); namespace { TCHAR _p4port[] = _T("1666"); TCHAR _p4user[] = _T("bruno"); TCHAR _p4client[] = _T("bruno_ws"); StrBuf errMsg; void InitP4(P4Connection &p4) { p4.SetPort("1666"); p4.SetUser("bruno"); p4.SetClient("bruno_ws"); p4.ParseForms(); } void TraceArray(LPSTR msg, StrArray *sba) { for (int i = 0; i < sba->Count(); i++) { WIN_TRACE("%s: %s\n", msg, sba->Get(i)->Text()); } } void TraceResults(P4Connection &p4) { TraceArray("info", p4.InfoArray()); TraceArray("warning", p4.WarningArray()); TraceArray("error", p4.ErrorArray()); } void ErrorLogger(char * msg) { errMsg = msg; } } // Both SETUP and TEARDOWN must be present. An empty body is allowed // for either function, but use with care. Anything done in SETUP // should be undone in TEARDOWN, so ordinarily you will want code in both. SETUP(P4Fixture) { } // Teardown does the inverse of SETUP, as well as undoing whatever side effects // the tests could have caused. TEARDOWN(P4Fixture) { } BEGIN_TESTF(ConnectFail, P4Fixture) { P4Connection p4; InitP4(p4); p4.SetPort("bad_port.bad_machine:19090"); WIN_ASSERT_NOT_ZERO(p4.Connect(errMsg), _T("Connect fails")); WIN_TRACE("Err: %s\n", errMsg.Text()); TraceResults(p4); Error e; p4.SetErrorLogger(&ErrorLogger); p4.FormatAndOutputError("Test error", &e); WIN_TRACE("Formatted error: %s\n", errMsg.Text()); WIN_ASSERT_STRING_EQUAL("Test error", errMsg.Text(), _T("Check format error")); } END_TESTF BEGIN_TESTF(Connect_Tagged, P4Fixture) { StrBuf errMsg; P4Connection p4; InitP4(p4); p4.ParseForms(); WIN_ASSERT_ZERO(p4.Connect(errMsg), _T("Connect should work")); p4.Run("info", 0, NULL); WIN_ASSERT_TRUE(p4.InfoArray()->Count() > 0, _T("Some info returned.")); TraceResults(p4); { char *args[] = {"-s"}; p4.Run("login", 1, args); TraceResults(p4); } p4.SetInput((LPCSTR) "brunopass"); p4.Run("login", 0, NULL); TraceResults(p4); { WIN_TRACE("Running FSTAT\n"); char *args[] = {"//depot/Jam/MAIN/src/jam.c"}; p4.Run("fstat", 1, args); TraceResults(p4); } { WIN_TRACE("Running SYNC\n"); char *args[] = {"//depot/Jam/MAIN/src/jam.c"}; p4.Run("sync", 1, args); TraceResults(p4); } { WIN_TRACE("Running EDIT\n"); char *args[] = {"//depot/Jam/MAIN/src/jam.c"}; p4.Run("edit", 1, args); TraceResults(p4); } { WIN_TRACE("Submit changelist\n"); // First we fetch the client char *args[] = {"-o"}; p4.Run("change", 1, args); TraceResults(p4); StrArray *sba = p4.GetArrayVar("Files"); TraceArray("Files", sba); delete sba; p4.SetVar("Description", "Dummy submit"); args[0] = "-i"; p4.Run("submit", 1, args); TraceResults(p4); } { WIN_TRACE("Checking client spec manipulation\n"); // First we fetch the client char *args[] = {"-o", "bruno_ws"}; p4.Run("client", 2, args); TraceResults(p4); // Output the View element (which we know to be an array) StrArray *sba = p4.GetArrayVar("View"); TraceArray("View", sba); int len = sba->Count(); // Create a new view and update the client workspace StrBuf new_view("//depot/new/... //bruno_ws/new/..."); sba->Put()->Set(new_view); p4.SetArrayVar("View", sba); delete sba; sba = p4.GetArrayVar("View"); WIN_ASSERT_EQUAL(sba->Count(), len + 1, _T("Check we can add to View")); delete sba; args[0] = "-i"; p4.Run("client", 1, args); TraceResults(p4); // Check that the updated View is correct with what we put in args[0] = "-o"; p4.Run("client", 2, args); TraceResults(p4); sba = p4.GetArrayVar("View"); WIN_ASSERT_EQUAL(sba->Count(), len + 1, _T("Check View is stored")); TraceArray("View", sba); WIN_ASSERT_STRING_EQUAL(sba->Get(sba->Count() - 1)->Text(), new_view.Text(), _T("Check View line is the same")); delete sba; } { WIN_TRACE("Checking bad client\n"); // This test should give some error output char *args[] = {"-o", "bruno_ws"}; p4.Run("client", 2, args); TraceResults(p4); StrArray *sba = p4.GetArrayVar("View"); TraceArray("View", sba); int len = sba->Count(); StrBuf new_view("//depot/new/... //fred_ws/new/..."); sba->Put()->Set(new_view); p4.SetArrayVar("View", sba); delete sba; args[0] = "-i"; p4.Run("client", 1, args); TraceResults(p4); WIN_ASSERT_NOT_ZERO(p4.ErrorArray()->Count(), _T("Check update failed")); } } END_TESTF namespace { }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 7502 | Robert Cowham |
- Updated for 2009.1 API - use static version (/MT rather than /MD) - Fixed SetProg/SetVersion calls - ensures they are called at correct moment. - Add SetProtocol - Added some more tests/examples |
||
#1 | 6468 | Robert Cowham | Draft version. |