From b6ec68c25e8b8a775045a13d3fb74f661dfe2889 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Fri, 10 Feb 2023 04:24:40 +0100 Subject: [PATCH] refactoring: made unit test of the function isValidAdress() more readable by removing code duplicates. --- src/createEmployeeAccount.c | 21 ++++++++++- src/createEmployeeAccount.h | 2 +- tests/test_createEmployeeAccount.c | 56 ++++++++++++++++-------------- 3 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/createEmployeeAccount.c b/src/createEmployeeAccount.c index edd9262..10b8339 100644 --- a/src/createEmployeeAccount.c +++ b/src/createEmployeeAccount.c @@ -2,6 +2,7 @@ #include "createEmployeeAccount.h" + bool isValidEmployeeID(const char* employeeId,const int maximumStringLength) { int employeeIdLength = strlen(employeeId); @@ -169,6 +170,12 @@ void getNewEmployeeCredentials() char employeeId[maxLength]; char employeePassword[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"); /*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)) { 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"); scanf(" %[^\n]s",data->phoneNumber); diff --git a/src/createEmployeeAccount.h b/src/createEmployeeAccount.h index 5b9b6fe..e9321f4 100644 --- a/src/createEmployeeAccount.h +++ b/src/createEmployeeAccount.h @@ -18,7 +18,7 @@ struct employeesInformations { char firstName[15]; char lastName[15]; - char address[20]; + char address[100]; char phoneNumber[15]; }; typedef struct employeesInformations employeedata; diff --git a/tests/test_createEmployeeAccount.c b/tests/test_createEmployeeAccount.c index 8fea023..ad3ab81 100644 --- a/tests/test_createEmployeeAccount.c +++ b/tests/test_createEmployeeAccount.c @@ -243,44 +243,48 @@ void test_validPhoneNumber(void) void test_isValidAdressSuccess(void) { /*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"}}; - /*Act*/ + int validHouseNumberAndPostalCode[][2] = { + {112,36879}, + {365,36897}, + {16,12354}, + {998,9999}}; + bool expectation = true; + + /*Act and Assert*/ 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) { /*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++) { - 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); } }