-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoor-mans-jasmine.html
82 lines (71 loc) · 3.18 KB
/
poor-mans-jasmine.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<meta name="robots" content="noindex">
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Home-made Jasmine-like JS Unit Testing</title>
<link href="poor-mans-jasmine.css" rel="stylesheet" />
<script src="poor-mans-jasmine.js"></script>
</head>
<body>
<div id="output"></div>
<script>
function getDisplayTime(param_){
var weDontHaveAParam = typeof param_ === "null" || typeof param_ === "undefined" || param_ === null;
if (weDontHaveAParam){
return '';
}
var currentTime = function(hourAsNumberParam_){
var self = this;
self.hour = hourAsNumberParam_;
var _isNoon = self.hour === 12;
var _isAfternoon = self.hour > 12;
var _ampm = self.hour >= 12 ? 'PM' : 'AM';
var _isMidnight = self.hour === 0;
return {
hour: self.hour,
isMidnight: _isMidnight,
isNoon: _isNoon,
isAfternoon: _isAfternoon,
amPm: _ampm
};
};
var paramIsLegit = param_.toString().includes('~') &&
parseInt(param_.split('~')[0], 10) >= 0 &&
parseInt(param_.split('~')[0], 10) < 24;
if (paramIsLegit){
var theHourPiece = param_.split('~')[0];
var theHourAsNumber = parseInt(theHourPiece, 10);
var theHourIsLegit = isNaN(theHourAsNumber) === false;
var theHourSegmentToDisplay = '';
if (theHourIsLegit){
var time = currentTime(theHourAsNumber);
theHourSegmentToDisplay = time.hour.toString();
if (time.isAfternoon){
theHourSegmentToDisplay = (time.hour % 12).toString(); // so we can have things like 3 PM
}
if (time.isMidnight){
theHourSegmentToDisplay = '12'; // so we can have 12 AM
}
return theHourSegmentToDisplay + ':00 ' + time.amPm;
}
}
return '';
}
var ourTests = [
{ testParam: '3~2~1', expected: '3:00 AM', functionToExecute: getDisplayTime, description: getDisplayTime.name + ' returns correct value for legitimate AM value' },
{ testParam: '13~2~1', expected: '1:00 PM', functionToExecute: getDisplayTime, description: getDisplayTime.name + ' returns correct value for legitimate PM value' },
{ testParam: '12~0~0', expected: '12:00 PM', functionToExecute: getDisplayTime, description: getDisplayTime.name + ' returns correct value for noon' },
{ testParam: null, expected: '', functionToExecute: getDisplayTime, description: getDisplayTime.name + ' returns empty string for null parameter' },
{ testParam: '100~0~0', expected: '', functionToExecute: getDisplayTime, description: getDisplayTime.name + ' returns empty string for bogusly huge hour value/number' },
{ testParam: '0~0~0', expected: '12:00 AM', functionToExecute: getDisplayTime, description: getDisplayTime.name + ' returns correct value for midnight' },
{ testParam: '10~0~0', expected: '10:00 AM', functionToExecute: getDisplayTime, description: getDisplayTime.name + ' returns correct value for legitimate AM value' },
{ testParam: '2 pm', expected: '', functionToExecute: getDisplayTime, description: getDisplayTime.name + ' returns correct value for non-legit parameter' },
];
ourTests.forEach(thisTestCase => {
RunTestFor(thisTestCase);
});
</script>
</body>
</html>