Browse Source

refactoring: made unit test of the function isValidAdress() more readable by removing code duplicates.

remotes/origin/backup-for-employees-infos-access
fdai7207 2 years ago
parent
commit
b6ec68c25e
  1. 21
      src/createEmployeeAccount.c
  2. 2
      src/createEmployeeAccount.h
  3. 56
      tests/test_createEmployeeAccount.c

21
src/createEmployeeAccount.c

@ -2,6 +2,7 @@
#include "createEmployeeAccount.h" #include "createEmployeeAccount.h"
bool isValidEmployeeID(const char* employeeId,const int maximumStringLength) bool isValidEmployeeID(const char* employeeId,const int maximumStringLength)
{ {
int employeeIdLength = strlen(employeeId); int employeeIdLength = strlen(employeeId);
@ -169,6 +170,12 @@ void getNewEmployeeCredentials()
char employeeId[maxLength]; char employeeId[maxLength];
char employeePassword[maxLength]; char employeePassword[maxLength];
char passwordVerfication[maxLength]; char passwordVerfication[maxLength];
char street[maximalAdressLength];
char city[maximalAdressLength];
int houseNumber;
char houseNumberString[10];
int postalCode;
char postalCodeString[10];
printf("please enter your wished Id :\n"); printf("please enter your wished Id :\n");
/*Added the regular expression [^\n] so that the string keep on getting read until a newline '\n' is found*/ /*Added the regular expression [^\n] so that the string keep on getting read until a newline '\n' is found*/
@ -194,7 +201,19 @@ void getNewEmployeeCredentials()
if(isValidName(data->firstName,minimumNameLength) && isValidName(data->lastName,minimumNameLength)) if(isValidName(data->firstName,minimumNameLength) && isValidName(data->lastName,minimumNameLength))
{ {
printf("\n\nplease enter your adress\n"); printf("\n\nplease enter your adress\n");
scanf(" %[^\n]s",data->address);
scanf(" %[^\n]s",street);
strcat(data->address,street);
scanf(" %[^\n]s",city);
strcat(data->address,city);
scanf("%d",houseNumber);
sprintf(houseNumberString,"%d",houseNumber);
strcat(data->address,houseNumberString);
scanf("%s",postalCode);
sprintf(postalCodeString,"%d",postalCode);
strcat(data->address,postalCodeString);
printf("\n\nplease enter your Phone number\n"); printf("\n\nplease enter your Phone number\n");
scanf(" %[^\n]s",data->phoneNumber); scanf(" %[^\n]s",data->phoneNumber);

2
src/createEmployeeAccount.h

@ -18,7 +18,7 @@ struct employeesInformations
{ {
char firstName[15]; char firstName[15];
char lastName[15]; char lastName[15];
char address[20];
char address[100];
char phoneNumber[15]; char phoneNumber[15];
}; };
typedef struct employeesInformations employeedata; typedef struct employeesInformations employeedata;

56
tests/test_createEmployeeAccount.c

@ -243,44 +243,48 @@ void test_validPhoneNumber(void)
void test_isValidAdressSuccess(void) void test_isValidAdressSuccess(void)
{ {
/*Arrange*/ /*Arrange*/
char* validStreet[] = {"LeipzigerStrasse","HannauerLandStra","HenirichStrasse","MAgdeburgerStrasse"};
char* validCity[] = {"Hannover","Frankfurt","Berlin","Fulda"};
int validHouseNumber[4] = {112,365,16,998};
int validPostalCode[4] = {36879,36897,12354,9999};
bool validAdress[4];
char* validCityAndStreet[][2] = {
{"LeipzigerStrasse","Hannover"},
{"HannauerLandStra","Frankfurt"},
{"HenirichStrasse","Berlin"},
{"MAgdeburgerStrasse","Fulda"}};
int validHouseNumberAndPostalCode[][2] = {
{112,36879},
{365,36897},
{16,12354},
{998,9999}};
bool expectation = true;
/*Act*/
/*Act and Assert*/
for(int i=0;i<4;i++) for(int i=0;i<4;i++)
{ {
validAdress[i] = isValidAdress(validStreet[i],validCity[i],validHouseNumber[i],validPostalCode[i]);
}
/*Assert*/
for(int j=0;j<4;j++)
{
TEST_ASSERT_TRUE(validAdress[j]);
bool validAdress = isValidAdress(validCityAndStreet[i][0],validCityAndStreet[i][1],validHouseNumberAndPostalCode[i][0],validHouseNumberAndPostalCode[i][1]);
TEST_ASSERT_EQUAL(expectation,validAdress);
} }
} }
void test_isValidAdressFailure(void) void test_isValidAdressFailure(void)
{ {
/*Arrange*/ /*Arrange*/
char* invalidStreet[] = {"LeipzigerStrassehvjhb","HannauerLandStranl","bob",".."};
char* invalidCity[] = {"log","fiz","foo","bar"};
int invalidHouseNumber[4] = {-10,-1,0,99815};
int invalidPostalCode[4] = {-1,10,999,65};
bool invalidAdress[4];
char* invalidCityAndStreet[][2] = {
{"LeipzigerStrassehvjhb","log"},
{"HannauerLandStranl","fiz"},
{"bob","foo"},
{"..","bar"}};
int invalidHouseNumberAndPostalCode[][2] = {
{-10,-1},
{-1,10},
{0,999},
{99815,65}};
bool expectation = false;
/*Act*/
/*Act and Assert*/
for(int i=0;i<4;i++) for(int i=0;i<4;i++)
{ {
invalidAdress[i] = isValidAdress(invalidStreet[i],invalidCity[i],invalidHouseNumber[i],invalidPostalCode[i]);
}
/*Assert*/
for(int j=0;j<4;j++)
{
TEST_ASSERT_FALSE(invalidAdress[j]);
bool invalidAdress = isValidAdress(invalidCityAndStreet[i][0],invalidCityAndStreet[i][1],invalidHouseNumberAndPostalCode[i][0],invalidHouseNumberAndPostalCode[i][1]);
TEST_ASSERT_EQUAL(expectation,invalidAdress);
} }
} }

Loading…
Cancel
Save