Browse Source

implement unit tests for the function createNewEmployee()

remotes/origin/feature/employees-infos-access
fdai7207 2 years ago
parent
commit
affa82bc3d
  1. 40
      build-project.sh
  2. 20
      src/createEmployeeAccount.c
  3. 2
      src/createEmployeeAccount.h
  4. 2
      src/employeeList.txt
  5. 31
      tests/test_createEmployeeAccount.c
  6. 12
      tests/test_employeeLogin.c

40
build-project.sh

@ -2,26 +2,50 @@ trap 'echo "Interrupted";
rm main;
cp employeeLogin.c.bak employeeLogin.c;
cp mainMenu.c.bak mainMenu.c;
cp createEmployeeAccount.c.bak createEmployeeAccount.c;
rm employeeLogin.c.bak;
rm mainMenu.c.bak;
rm createEmployeeAccount.c.bak;
cd ..;
rm -r build; exit' SIGINT
clear
ceedling test:all
cd src/
cp employeeLogin.c employeeLogin.c.bak
cp mainMenu.c mainMenu.c.bak
sed '/John Doe/,$d' employeeList.txt > temp.txt
mv temp.txt employeeList.txt
# backup files
for file in employeeLogin.c mainMenu.c createEmployeeAccount.c; do
cp "$file" "$file.bak"
done
# replace .c with .h in respective files
sed -i 's/employeeLogin.c/employeeLogin.h/g' mainMenu.c
sed -i 's/showGeneralInfoEmployee.c/showGeneralInfoEmployee.h/g' employeeLogin.c
sed -i 's/mainMenu.c/mainMenu.h/g' employeeLogin.c
sed -i 's/src\///g' employeeLogin.c
gcc main.c mainMenu.c employeeLogin.c showGeneralInfoEmployee.c -o main
sed -i 's/employeeLogin.c/employeeLogin.h/g' createEmployeeAccount.c
# remove 'src/'
for file in employeeLogin.c createEmployeeAccount.c; do
sed -i 's/src\///g' "$file"
done
# compile and run program
gcc main.c mainMenu.c employeeLogin.c showGeneralInfoEmployee.c createEmployeeAccount.c -o main
./main
rm main
cp employeeLogin.c.bak employeeLogin.c
cp mainMenu.c.bak mainMenu.c
rm employeeLogin.c.bak
rm mainMenu.c.bak
# restore backups
for file in employeeLogin.c mainMenu.c createEmployeeAccount.c; do
cp "$file.bak" "$file"
done
# remove backups
for file in employeeLogin.c.bak mainMenu.c.bak createEmployeeAccount.c.bak; do
rm "$file"
done
cd ..
rm -r build/

20
src/createEmployeeAccount.c

@ -1,5 +1,5 @@
#include "employeeLogin.c"
#include "employeeLogin.h"
#include "createEmployeeAccount.h"
@ -18,3 +18,21 @@ bool isValidEmployeeID(char* employeeId)
}
}
bool createNewEmployee(char* employeeId, char* employeePassword)
{
FILE* employeesList;
employeesList = fopen("src/employeeList.txt","a");
if(employeesList == NULL)
{
printf("Error: could not open the list of Employees");
return false;
}
fprintf(employeesList,"\n%s %s\n",employeeId,employeePassword);
fclose(employeesList);
return checkEmployeeCredentials(employeeId, employeePassword);
}

2
src/createEmployeeAccount.h

@ -7,4 +7,6 @@
#include<string.h>
bool isValidEmployeeID(char* employee);
bool createNewEmployee(char* employeeId, char* employeePassword);
#endif

2
src/employeeList.txt

@ -11,3 +11,5 @@ Mohamed MDfdai6618
Shivam Schivam007fdlt3781

31
tests/test_createEmployeeAccount.c

@ -59,4 +59,35 @@ void test_isNotValidEmployeeID(void)
}
void test_employeeCreatedSuccessfully(void)
{
/*Arrange*/
char* potentialEmployees[][2] = {
{"John", "Doe"},
{"Fizz", "Buzz"},
{"Jane", "Doe"},
{"Foo", "Bar"},
{"MusterMann", "MusterManPassword"},
{"MusterFrau", "MusterFrauPassword"}
};
bool expected[6] = {true,true,true,true,true,true};
bool result[6];
/*Act*/
for(int i=0; i<6;i++)
{
result[i] = createNewEmployee(potentialEmployees[i][0],potentialEmployees[i][1]);
}
/*Assert*/
for(int i=0; i<6;i++)
{
TEST_ASSERT_EQUAL(expected[i],result[i]);
}
}
#endif // TEST

12
tests/test_employeeLogin.c

@ -84,12 +84,12 @@ void test_MissingLoginEmployee(void)
/*Arrange*/
char* missingEmployeesCredentials[][2] = {
{"John", "doe"},
{"Jane", "Doe"},
{"Foo", "Bar"},
{"Fizz", "Buzz"},
{"Musterman", "Mustermanpassword"},
{"Musterfrau", "Musterfraupassword"}
{"Germany", "Berlin"},
{"Italy", "Rome"},
{"Belgium", "Bruxelle"},
{"Swizerland", "Geneve"},
{"Netherlands", "Amsterdam"},
{"Sweden", "Stockholm"}
};
int expected[] = {0,0,0,0,0,0};

Loading…
Cancel
Save