Skip to content

Commit

Permalink
Unit tested date formatting and fixed edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcev106 committed Dec 19, 2022
1 parent 1dcdf3c commit 9f438c4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.idea
/.idea
.vscode/launch.json
99 changes: 90 additions & 9 deletions CrossprojectpipingExternalModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,24 +536,27 @@ function runCrossProjectPiping() {

if(!newDate.includes('NaN') && data.length >= 1) {
var dateTimeStr = '';
var dateTimeData = [];
if(dateFormatParams[0] == 'datetime') {
const dataParams = data.split(' ');
data = dataParams[0];

if(dataParams.length <= 1 || dataParams[1].length < 3) {
dateTimeData = ['00', '00', '00'];
} else {
dateTimeData = dataParams[1].split(':');
const dataParams = data.split(' ')
let dateTimeData
if(dataParams.length === 1){
dateTimeData = []
}
else{
dateTimeData = dataParams[1].split(':')
}

while(dateTimeData.length < 3){
dateTimeData.push('00')
}

var dateTimeVal = dateTimeData[0]+':'+dateTimeData[1];
if(dateFormatParams.length > 2) {
var dateTimeVal = dateTimeVal+':'+dateTimeData[2];
}
if(dateTimeVal.length) {
dateTimeStr = ' '+dateTimeVal;
}

}

return newDate + dateTimeStr
Expand All @@ -562,6 +565,84 @@ function runCrossProjectPiping() {
return false
}

// Only run unit tests when on a dev/test server
if(<?=json_encode(($GLOBALS['is_development_server']) === '1')?>){
const tests = [
{
input: '2001-02-03',
outputs: {
date: {
dmy: '03-02-2001',
mdy: '02-03-2001',
ymd: '2001-02-03'
},
time: '00:00',
seconds: '00'
}
},
{
input: '2011-02-03 04:05',
outputs: {
date: {
dmy: '03-02-2011',
mdy: '02-03-2011',
ymd: '2011-02-03'
},
time: '04:05',
seconds: '00'
}
},
{
input: '2021-02-03 06:07:08',
outputs: {
date: {
dmy: '03-02-2021',
mdy: '02-03-2021',
ymd: '2021-02-03'
},
time: '06:07',
seconds: '08'
}
}
]

let testOutput = ''
tests.forEach(test => {
[
'date_dmy',
'date_mdy',
'date_ymd',
'datetime_dmy',
'datetime_mdy',
'datetime_ymd',
'datetime_seconds_dmy',
'datetime_seconds_mdy',
'datetime_seconds_ymd'
].forEach(format => {
const actualOutput = formatDate(test.input, format)
const parts = format.split('_')
const dateFormat = parts.pop()

let expectedOutput = test.outputs.date[dateFormat]
if(parts[0] === 'datetime'){
expectedOutput += ' ' + test.outputs.time
}

if(parts.length === 2){
expectedOutput += ':' + test.outputs.seconds
}

if(actualOutput !== expectedOutput){
testOutput += 'The formateDate() function returned "' + actualOutput + '" instead of "' + expectedOutput + '" for input "' + test.input + '" and format "' + format + '"!<br>'
}
})
})

if(testOutput.length > 0){
simpleDialog(testOutput, 'Cross Project Piping Module - Test Failures', null, 1100)
}
}

var cppAjaxConnections = 0;
var cppFoundField = false;
var cppProcessing = true;
Expand Down

0 comments on commit 9f438c4

Please sign in to comment.