Browse Source

implement unit tests for the function isValidEmployeeID()

remotes/origin/feature/employees-infos-access
fdai7207 2 years ago
parent
commit
906ee12708
  1. 18
      build-project.sh
  2. 20
      src/createEmployeeAccount.c
  3. 10
      src/createEmployeeAccount.h
  4. 1
      src/employeeList.txt
  5. 9
      src/employeeLogin.c
  6. 7
      src/employeeLogin.h
  7. 2
      src/main.c
  8. 5
      src/mainMenu.c
  9. 5
      src/showGeneralInfoEmployee.c
  10. 3
      src/showGeneralInfoEmployee.h
  11. 62
      tests/test_createEmployeeAccount.c
  12. 26
      tests/test_showGeneralInfoEmployee.c

18
build-project.sh

@ -1,13 +1,27 @@
trap 'echo "Interrupted"; rm main; cp employeeLogin.c.bak employeeLogin.c; rm employeeLogin.c.bak; cd ..; rm -r build/; exit' SIGINT
trap 'echo "Interrupted";
rm main;
cp employeeLogin.c.bak employeeLogin.c;
cp mainMenu.c.bak mainMenu.c;
rm employeeLogin.c.bak;
rm mainMenu.c.bak;
cd ..;
rm -r build; exit' SIGINT
clear clear
ceedling test:all ceedling test:all
cd src/ cd src/
cp employeeLogin.c employeeLogin.c.bak cp employeeLogin.c employeeLogin.c.bak
cp mainMenu.c mainMenu.c.bak
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 sed -i 's/src\///g' employeeLogin.c
gcc main.c mainMenu.c -o main
gcc main.c mainMenu.c employeeLogin.c showGeneralInfoEmployee.c -o main
./main ./main
rm main rm main
cp employeeLogin.c.bak employeeLogin.c cp employeeLogin.c.bak employeeLogin.c
cp mainMenu.c.bak mainMenu.c
rm employeeLogin.c.bak rm employeeLogin.c.bak
rm mainMenu.c.bak
cd .. cd ..
rm -r build/ rm -r build/

20
src/createEmployeeAccount.c

@ -0,0 +1,20 @@
#include "employeeLogin.c"
#include "employeeLogin.h"
#include "createEmployeeAccount.h"
bool isValidEmployeeID(char* employeeId)
{
int characterLimit = 20;
if(strlen(employeeId)>characterLimit || strchr(employeeId,' '))
{
return false;
}
else
{
return true;
}
}

10
src/createEmployeeAccount.h

@ -0,0 +1,10 @@
#ifndef CREATEEMPLOYEEACCOUNT_H_
#define CREATEEMPLOYEEACCOUNT_H_
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
bool isValidEmployeeID(char* employee);
#endif

1
src/employeeList.txt

@ -10,3 +10,4 @@ Mohamed MDfdai6618
Shivam Schivam007fdlt3781 Shivam Schivam007fdlt3781

9
src/employeeLogin.c

@ -1,7 +1,8 @@
#include "mainMenu.h" #include "mainMenu.h"
#include "employeeLogin.h"
#include"employeeLogin.h"
#include "showGeneralInfoEmployee.c" #include "showGeneralInfoEmployee.c"
bool employeesAccess(char* employeesAccessCode) bool employeesAccess(char* employeesAccessCode)
{ {
@ -60,6 +61,7 @@ void getEmployeeAccessCode()
{ {
scanf("%s",accessCode); scanf("%s",accessCode);
if(employeesAccess(accessCode)) if(employeesAccess(accessCode))
{ {
printf("\n\nAccess granted!\n\n"); printf("\n\nAccess granted!\n\n");
@ -69,7 +71,7 @@ void getEmployeeAccessCode()
} }
else
else if(!(employeesAccess(accessCode)))
{ {
printf("\n\nAccess key didnt match! try again !\n\n"); printf("\n\nAccess key didnt match! try again !\n\n");
--remainingAttempts; --remainingAttempts;
@ -87,6 +89,7 @@ void getEmployeeCredentials(char* inputUsername,char* inputPassword)
{ {
printf("Enter username: "); printf("Enter username: ");
scanf("%s", inputUsername); scanf("%s", inputUsername);
printf("Enter password: "); printf("Enter password: ");
scanf("%s", inputPassword); scanf("%s", inputPassword);
} }
@ -132,3 +135,5 @@ void loginAsEmployee()
free(password); free(password);
} }

7
src/employeeLogin.h

@ -1,5 +1,5 @@
#ifndef LOGINEMPLOYEE_H_
#define LOGINEMPLOYEE_H_
#ifndef EMPLOYEELOGIN_H_
#define EMPLOYEELOGIN_H_
#define employeeAccessKey "DF9E9A8B5E" #define employeeAccessKey "DF9E9A8B5E"
#define credentialLength 20 #define credentialLength 20
@ -7,12 +7,13 @@
#include<stdbool.h> #include<stdbool.h>
bool employeesAccess(char* employeesAccessCode); bool employeesAccess(char* employeesAccessCode);
int checkEmployeeCredentials(char* username , char* password); int checkEmployeeCredentials(char* username , char* password);
void getEmployeeAccessCode();
void getEmployeeCredentials(char* username, char* password); void getEmployeeCredentials(char* username, char* password);
void loginAsEmployee(); void loginAsEmployee();
#endif #endif

2
src/main.c

@ -1,4 +1,6 @@
#include "mainMenu.h" #include "mainMenu.h"
#include "employeeLogin.h"
#include"showGeneralInfoEmployee.h"
int main() int main()
{ {

5
src/mainMenu.c

@ -2,6 +2,8 @@
#include "employeeLogin.c" #include "employeeLogin.c"
bool agePermission(int age) bool agePermission(int age)
{ {
@ -131,3 +133,6 @@ void showMenu()
} }

5
src/showGeneralInfoEmployee.c

@ -85,7 +85,7 @@ void showGeneralInfoEmployee(char id[length], char password[length])
if(checkUser(id, password) == 5) if(checkUser(id, password) == 5)
{ {
printf(" Welcome Mohamed\n"); printf(" Welcome Mohamed\n");
printf(" Clearance: 3\n");
printf(" Clearance: 1\n");
printf("\n"); printf("\n");
printf("\n"); printf("\n");
printf(" ->Review new customer applications.\n"); printf(" ->Review new customer applications.\n");
@ -96,7 +96,7 @@ void showGeneralInfoEmployee(char id[length], char password[length])
if(checkUser(id, password) == 6) if(checkUser(id, password) == 6)
{ {
printf(" Welcome Shivam\n"); printf(" Welcome Shivam\n");
printf(" Clearance: 3\n");
printf(" Clearance: 1\n");
printf("\n"); printf("\n");
printf("\n"); printf("\n");
printf(" ->Review new customer applications.\n"); printf(" ->Review new customer applications.\n");
@ -105,3 +105,4 @@ void showGeneralInfoEmployee(char id[length], char password[length])
} }
} }

3
src/showGeneralInfoEmployee.h

@ -5,7 +5,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
const int length = 100;
#define length 20
int checkUser(char username[length], char password[length]); int checkUser(char username[length], char password[length]);
void showGeneralInfoEmployee(char id[length], char password[length]); void showGeneralInfoEmployee(char id[length], char password[length]);
#endif // SHOWGENERALINFOEMPLOYEE_H #endif // SHOWGENERALINFOEMPLOYEE_H

62
tests/test_createEmployeeAccount.c

@ -0,0 +1,62 @@
#ifdef TEST
#include "unity.h"
#include "createEmployeeAccount.h"
void setUp(void)
{
}
void tearDown(void)
{
}
void test_isValidEmployeeID(void)
{
/*Arrange*/
char* validEmployeeId [] = {"Atharva","Can","Haytham","Julius","Mohamed","Shivam","Fizz","Buzz","JohnDoe","Foobar","waz","Objectoriented","INSTITUTIONALISATIOL","Intercommunicational","1234","1.6"};
bool validEmployeeIdResult[15];
/*Act*/
for(int i=0; i<15; i++)
{
validEmployeeIdResult[i] = isValidEmployeeID(validEmployeeId[i]);
}
/*Assert*/
for(int i =0; i<15;i++)
{
TEST_ASSERT_TRUE(validEmployeeIdResult[i]);
}
}
void test_isNotValidEmployeeID(void)
{
/*Arrange*/
char* invalidEmployeeId [] = {"Atha rva","Ca n","Geschwindigkeitsbegrenzungen","1234 15","John Doe","fizz Fuzz"};
bool invalidEmployeeIdResult[6];
/*Act*/
for(int i=0; i<6; i++)
{
invalidEmployeeIdResult[i] = isValidEmployeeID(invalidEmployeeId[i]);
}
/*Assert*/
for(int i =0; i<6;i++)
{
TEST_ASSERT_FALSE(invalidEmployeeIdResult[i]);
}
}
#endif // TEST

26
tests/test_showGeneralInfoEmployee.c

@ -2,7 +2,7 @@
#include "unity.h" #include "unity.h"
#include "showGeneralInfoEmployee.c"
#include "showGeneralInfoEmployee.h"
void setUp(void) void setUp(void)
{ {
@ -15,43 +15,43 @@ void tearDown(void)
void test1_showGeneralInfoEmployee(void) void test1_showGeneralInfoEmployee(void)
{ {
char employeeName[] = "Atharva"; //Arrange
char password[] = "Atharvafdai7514";
char employeeName[20] = "Atharva"; //Arrange
char password[20] = "Atharvafdai7514";
int ergebnis = checkUser(employeeName, password); //Act int ergebnis = checkUser(employeeName, password); //Act
TEST_ASSERT_EQUAL_INT(1, ergebnis); //Assert TEST_ASSERT_EQUAL_INT(1, ergebnis); //Assert
} }
void test2_showGeneralInfoEmployee(void) void test2_showGeneralInfoEmployee(void)
{ {
char employeeName[] = "Can"; //Arrange
char password[] = "BlooMaskfdlt3817";
char employeeName[20] = "Can"; //Arrange
char password[20] = "BlooMaskfdlt3817";
int ergebnis = checkUser(employeeName, password); //Act int ergebnis = checkUser(employeeName, password); //Act
TEST_ASSERT_EQUAL_INT(2, ergebnis); //Assert TEST_ASSERT_EQUAL_INT(2, ergebnis); //Assert
} }
void test3_showGeneralInfoEmployee(void) void test3_showGeneralInfoEmployee(void)
{ {
char employeeName[] = "Haytham"; //Arrange
char password[] = "TimoDLfdai7207";
char employeeName[20] = "Haytham"; //Arrange
char password[20] = "TimoDLfdai7207";
int ergebnis = checkUser(employeeName, password); //Act int ergebnis = checkUser(employeeName, password); //Act
TEST_ASSERT_EQUAL_INT(3, ergebnis); //Assert TEST_ASSERT_EQUAL_INT(3, ergebnis); //Assert
} }
void test4_showGeneralInfoEmployee(void) void test4_showGeneralInfoEmployee(void)
{ {
char employeeName[] = "Julius"; //Arrange
char password[] = "Insertcatfdai7057";
char employeeName[20] = "Julius"; //Arrange
char password[20] = "Insertcatfdai7057";
int ergebnis = checkUser(employeeName, password); //Act int ergebnis = checkUser(employeeName, password); //Act
TEST_ASSERT_EQUAL_INT(4, ergebnis); //Assert TEST_ASSERT_EQUAL_INT(4, ergebnis); //Assert
} }
void test5_showGeneralInfoEmployee(void) void test5_showGeneralInfoEmployee(void)
{ {
char employeeName[] = "Mohamed"; //Arrange
char password[] = "MDfdai6618";
char employeeName[20] = "Mohamed"; //Arrange
char password[20] = "MDfdai6618";
int ergebnis = checkUser(employeeName, password); //Act int ergebnis = checkUser(employeeName, password); //Act
TEST_ASSERT_EQUAL_INT(5, ergebnis); //Assert TEST_ASSERT_EQUAL_INT(5, ergebnis); //Assert
} }
void test6_showGeneralInfoEmployee(void) void test6_showGeneralInfoEmployee(void)
{ {
char employeeName[] = "Shivam"; //Arrange
char password[] = "Schivam007fdlt3781";
char employeeName[20] = "Shivam"; //Arrange
char password[20] = "Schivam007fdlt3781";
int ergebnis = checkUser(employeeName, password); //Act int ergebnis = checkUser(employeeName, password); //Act
TEST_ASSERT_EQUAL_INT(6, ergebnis); //Assert TEST_ASSERT_EQUAL_INT(6, ergebnis); //Assert
} }

Loading…
Cancel
Save