To clone this project, open your terminal command with in the folder place you want to place this lab and use the following command:
git clone --branch main-en https://github.com/ai4devop/TS-JS-TestWithAI.git
Then install all the needed dependencies :
npm install
The purpose of this exercise is to discover how to use AI tools to assist you in practicing test-driven development.
As a reminder, test-driven development (TDD) involves writing unit tests before the actual code is created. Writing these tests helps define the expected behavior of the functions. Once the code is written, the tests can be run to validate the behavior.
In this exercise, you will find the utils
class, which aims to provide utility functions for processing dates with or without times. You will be required to write tests for the functions we want to implement and then the code for the functions.
- Clone this project.
- Go to the u
utils
file to review the created method skeletons and to utils.test to see the implemented tests. - Realize the differents implementations described
- Launch tests
For the first part of this exercise, we will write functions for processing Date
. In the utils.test
class, a series of unit tests have been written that dictate the expected behavior of 3 functions. You will now need to rely on these unit tests to write the code for these functions.
-
Implement the function
formatDate(LocalDate date)
:- From test
testFormatDate_ValidDate
already written inutils.test
- This function must accept the object
Date
and returnString
with the formatyyyy-mm-dd
.
- From test
-
Implement the function
parseDate(String date)
:- From test
testParseDate_ValidDate
already written inutils.test
- This function must accept
String
with the formatyyyy-mm-dd
and return an objectDate
.
- From test
-
Implement the function
formatDateWithPattern(LocalDate date, String pattern)
:- From test
testFormatDate_WithPattern
already written inutils.test
- This function must accept and object
Date
and aString
which will contains the date pattern expected, ex : 'yyyy-mm-dd' and return aString
with the date with the expected format
- From test
For the second part of the exercise, we will tackle Date
objects. This time, you will need to write tests that will dictate the behavior of the functions, and then proceed with writing the functions.
-
For the function
formatDateTime(Date dateTime)
:- Describe a first test
testFormatDateTime_ValidDateTime
which will verify that for a givenDate
, the functionformatDateTime
return a string, ex : "2024-08-31T08:46:00" - This test will obviously fail cause the function is not yet implemented
- When the test have been made, you can generate the code for the function
formatDateTime(Date dateTime)
- Make sure that the test you wrote now passes
- Describe a first test
-
For the function
parseDateTime(String dateTimeString)
:-
Rédigez un premier test
testParseDateTime_ValidDateTime
qui vérifiera que pour uneString
donnée, au format yyyy-MM-ddThh:mm:ss, la fonctionparseDateTime
retourne bien un objetDate
, ex : "2024-08-31T08:46:00" -
Rédigez un deuxième test
testParseDateTime_InvalidDateTime
qui vérifiera que pour uneString
pas au format yyyy-MM-ddThh:mm:ss, la fonctionformatDateTime
échoue bien avec une exceptionDateTimeParseException
, ex : "invalid-date" -
Ces tests échoueront bien sûr car la fonction appelée n'est pas encore implémentée
-
Une fois les tests rédigés, vous pouvez vous attaquer à la rédaction de fonction
parseDateTime(String dateTimeString)
-
Assurez vous que les tests que vous avez rédigés passent maintenant
-
Describe a first test
testParseDateTime_ValidDateTime
which will verify that for a givenString
, with a given format yyyy-MM-ddThh:mm:ss_, the functionparseDateTime
return an objectDate
, ex : "2024-08-31T08:46:00" -
Describe a second test
testParseDateTime_InvalidDateTime
which will verify that for a givenString
NOT in a format yyyy-MM-ddThh:mm:ss, the functionformatDateTime
will fail with with the exceptionDateTimeParseException
, ex : "invalid-date" -
This test will obviously fail cause the function is not yet implemented
-
When the test have been made, you can generate the code for the function
parseDateTime(String dateTimeString)
-
Make sure that the test you wrote now passes
-
For this final part of the exercise, we will tackle the Testing block, which will allow you to write unit tests based on given code.
- For the function
formatDateTime(Date dateTime, String pattern)
:- The unit test in
testFormatDateTime_WithPattern
will verify that for a givenDate
and a chosen pattern, the functionformatDateTime
return a string with the given format, ex : avec dd/MM/yyyy HH:mm:ss on a "31/08/2024 08:46:00" - The code is already written, use it to generate your test with AI
- The unit test in
- Execute test with :
npm test
- Be sure all tests pass before saving your solution.
- Functions that process
Date
are written and all tests pass.