Matthew Holt
11 years ago
2 changed files with 417 additions and 0 deletions
@ -0,0 +1,124 @@ |
|||||||
|
<!DOCTYPE html> |
||||||
|
<html> |
||||||
|
<head> |
||||||
|
<title>jQuery Parse Plugin Tests</title> |
||||||
|
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> |
||||||
|
<script src="jquery.parse.js"></script> |
||||||
|
<script src="tests.js"></script> |
||||||
|
<style> |
||||||
|
body { |
||||||
|
font-family: sans-serif; |
||||||
|
} |
||||||
|
|
||||||
|
#tmp { |
||||||
|
white-space: pre; |
||||||
|
font-family: 'Menlo', 'Monaco', 'Courier New', monospace; |
||||||
|
font-size: 10px; |
||||||
|
} |
||||||
|
|
||||||
|
#results { |
||||||
|
border-collapse: collapse; |
||||||
|
width: 100%; |
||||||
|
} |
||||||
|
|
||||||
|
#results td { |
||||||
|
vertical-align: top; |
||||||
|
padding: 10px; |
||||||
|
border-bottom: 10px solid white; |
||||||
|
} |
||||||
|
|
||||||
|
.count { |
||||||
|
background: #333; |
||||||
|
color: #DDD; |
||||||
|
width: 2em; |
||||||
|
text-align: center; |
||||||
|
} |
||||||
|
|
||||||
|
.input, |
||||||
|
.output { |
||||||
|
width: 25%; |
||||||
|
} |
||||||
|
|
||||||
|
.input { |
||||||
|
background: #DDD; |
||||||
|
} |
||||||
|
|
||||||
|
.config { |
||||||
|
background: #CCC; |
||||||
|
} |
||||||
|
|
||||||
|
.output { |
||||||
|
background: #EEE; |
||||||
|
} |
||||||
|
|
||||||
|
.input code, |
||||||
|
.config code, |
||||||
|
.output code { |
||||||
|
font: 12px/1.5em 'Menlo', 'Monaco', 'Courier New', monospace; |
||||||
|
display: block; |
||||||
|
white-space: pre; |
||||||
|
overflow-x: auto; |
||||||
|
} |
||||||
|
|
||||||
|
.clr-green, |
||||||
|
.passing { |
||||||
|
color: #475B15; |
||||||
|
} |
||||||
|
|
||||||
|
.clr-red, |
||||||
|
.failing { |
||||||
|
color: #AA0000; |
||||||
|
} |
||||||
|
|
||||||
|
.passing { |
||||||
|
background: #ECF9CC; |
||||||
|
color: #475B15; |
||||||
|
} |
||||||
|
|
||||||
|
.failing { |
||||||
|
background: #FFE8E8; |
||||||
|
} |
||||||
|
|
||||||
|
.failing code { |
||||||
|
font-weight: bold; |
||||||
|
} |
||||||
|
|
||||||
|
hr { |
||||||
|
border: 0; |
||||||
|
background: 0; |
||||||
|
clear: both; |
||||||
|
} |
||||||
|
|
||||||
|
.clr-green { |
||||||
|
color: #79A01E; |
||||||
|
} |
||||||
|
|
||||||
|
.clr-red { |
||||||
|
color: #AA0000; |
||||||
|
} |
||||||
|
|
||||||
|
#pass-count, |
||||||
|
#fail-count { |
||||||
|
font-weight: bold; |
||||||
|
} |
||||||
|
</style> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
SUMMARY |
||||||
|
|
||||||
|
<span class="clr-green"><span id="pass-count">-</span> PASS</span> |
||||||
|
|
||||||
|
<span class="clr-red"><span id="fail-count">-</span> FAIL</span> |
||||||
|
<br><br> |
||||||
|
<table id="results"> |
||||||
|
<tr> |
||||||
|
<th></th> |
||||||
|
<th>Input</th> |
||||||
|
<th>Config</th> |
||||||
|
<th>Expected</th> |
||||||
|
<th>Actual</th> |
||||||
|
</tr> |
||||||
|
</table> |
||||||
|
<div id="output"></div> |
||||||
|
</body> |
||||||
|
</html> |
@ -0,0 +1,293 @@ |
|||||||
|
var passCount = 0, failCount = 0; |
||||||
|
var passing = "passing"; |
||||||
|
var failing = "failing" |
||||||
|
|
||||||
|
var resultSet1 = [ |
||||||
|
{ |
||||||
|
config: { delimiter: ",", header: true, dynamicTyping: true }, |
||||||
|
expected: { |
||||||
|
"results": { |
||||||
|
"fields": [ |
||||||
|
"F1", |
||||||
|
"F2", |
||||||
|
"F3" |
||||||
|
], |
||||||
|
"rows": [ |
||||||
|
{ |
||||||
|
"F1": "V1", |
||||||
|
"F2": 2, |
||||||
|
"F3": "V3" |
||||||
|
} |
||||||
|
] |
||||||
|
}, |
||||||
|
"errors": [] |
||||||
|
} |
||||||
|
}, |
||||||
|
{ |
||||||
|
config: { delimiter: ",", header: false, dynamicTyping: true }, |
||||||
|
expected: { |
||||||
|
"results": [ |
||||||
|
[ |
||||||
|
"F1", |
||||||
|
"F2", |
||||||
|
"F3" |
||||||
|
], |
||||||
|
[ |
||||||
|
"V1", |
||||||
|
2, |
||||||
|
"V3" |
||||||
|
] |
||||||
|
], |
||||||
|
"errors": [] |
||||||
|
} |
||||||
|
}, |
||||||
|
{ |
||||||
|
config: { delimiter: ",", header: false, dynamicTyping: false }, |
||||||
|
expected: { |
||||||
|
"results": [ |
||||||
|
[ |
||||||
|
"F1", |
||||||
|
"F2", |
||||||
|
"F3" |
||||||
|
], |
||||||
|
[ |
||||||
|
"V1", |
||||||
|
"2", |
||||||
|
"V3" |
||||||
|
] |
||||||
|
], |
||||||
|
"errors": [] |
||||||
|
} |
||||||
|
}, |
||||||
|
{ |
||||||
|
config: { delimiter: ",", header: true, dynamicTyping: false }, |
||||||
|
expected: { |
||||||
|
"results": { |
||||||
|
"fields": [ |
||||||
|
"F1", |
||||||
|
"F2", |
||||||
|
"F3" |
||||||
|
], |
||||||
|
"rows": [ |
||||||
|
{ |
||||||
|
"F1": "V1", |
||||||
|
"F2": "2", |
||||||
|
"F3": "V3" |
||||||
|
} |
||||||
|
] |
||||||
|
}, |
||||||
|
"errors": [] |
||||||
|
} |
||||||
|
} |
||||||
|
]; |
||||||
|
|
||||||
|
var tests = [ |
||||||
|
{ |
||||||
|
input: "F1,F2,F3\nV1,2,V3", |
||||||
|
cases: resultSet1 |
||||||
|
}, |
||||||
|
{ |
||||||
|
input: "F1,F2,F3\r\nV1,2,V3", |
||||||
|
cases: resultSet1 |
||||||
|
}, |
||||||
|
{ |
||||||
|
input: "F1,\"F2\",F3\r\nV1,2,\"V3\"", |
||||||
|
cases: resultSet1 |
||||||
|
}, |
||||||
|
{ |
||||||
|
input: "F1,F2,F3\n\nV1,2,V3", |
||||||
|
cases: resultSet1 |
||||||
|
}, |
||||||
|
{ |
||||||
|
input: "F1,F2,F3\r\n\r\nV1,2,V3", |
||||||
|
cases: resultSet1 |
||||||
|
}, |
||||||
|
{ |
||||||
|
input: "F1,F2,F3\r\n \r\nV1,2,V3", |
||||||
|
cases: resultSet1 |
||||||
|
}, |
||||||
|
{ |
||||||
|
input: "F1,F2,F3\nV1,2,V3\nV4,V5,V6", |
||||||
|
cases: [ |
||||||
|
{ |
||||||
|
config: { delimiter: ",", header: true, dynamicTyping: true }, |
||||||
|
expected: { |
||||||
|
"results": { |
||||||
|
"fields": [ |
||||||
|
"F1", |
||||||
|
"F2", |
||||||
|
"F3" |
||||||
|
], |
||||||
|
"rows": [ |
||||||
|
{ |
||||||
|
"F1": "V1", |
||||||
|
"F2": 2, |
||||||
|
"F3": "V3" |
||||||
|
}, |
||||||
|
{ |
||||||
|
"F1": "V4", |
||||||
|
"F2": "V5", |
||||||
|
"F3": "V6" |
||||||
|
} |
||||||
|
] |
||||||
|
}, |
||||||
|
"errors": [] |
||||||
|
} |
||||||
|
}, |
||||||
|
{ |
||||||
|
config: { delimiter: ",", header: false, dynamicTyping: true }, |
||||||
|
expected: { |
||||||
|
"results": [ |
||||||
|
[ |
||||||
|
"F1", |
||||||
|
"F2", |
||||||
|
"F3" |
||||||
|
], |
||||||
|
[ |
||||||
|
"V1", |
||||||
|
2, |
||||||
|
"V3" |
||||||
|
], |
||||||
|
[ |
||||||
|
"V4", |
||||||
|
"V5", |
||||||
|
"V6" |
||||||
|
] |
||||||
|
], |
||||||
|
"errors": [] |
||||||
|
} |
||||||
|
}, |
||||||
|
{ |
||||||
|
config: { delimiter: ",", header: false, dynamicTyping: false }, |
||||||
|
expected: { |
||||||
|
"results": [ |
||||||
|
[ |
||||||
|
"F1", |
||||||
|
"F2", |
||||||
|
"F3" |
||||||
|
], |
||||||
|
[ |
||||||
|
"V1", |
||||||
|
"2", |
||||||
|
"V3" |
||||||
|
], |
||||||
|
[ |
||||||
|
"V4", |
||||||
|
"V5", |
||||||
|
"V6" |
||||||
|
] |
||||||
|
], |
||||||
|
"errors": [] |
||||||
|
} |
||||||
|
}, |
||||||
|
{ |
||||||
|
config: { delimiter: ",", header: true, dynamicTyping: false }, |
||||||
|
expected: { |
||||||
|
"results": { |
||||||
|
"fields": [ |
||||||
|
"F1", |
||||||
|
"F2", |
||||||
|
"F3" |
||||||
|
], |
||||||
|
"rows": [ |
||||||
|
{ |
||||||
|
"F1": "V1", |
||||||
|
"F2": "2", |
||||||
|
"F3": "V3" |
||||||
|
}, |
||||||
|
{ |
||||||
|
"F1": "V4", |
||||||
|
"F2": "V5", |
||||||
|
"F3": "V6" |
||||||
|
} |
||||||
|
] |
||||||
|
}, |
||||||
|
"errors": [] |
||||||
|
} |
||||||
|
} |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
input: "F1,F2,F3\n,2,V3\nV4,V5,V6", |
||||||
|
cases: [ |
||||||
|
{ |
||||||
|
config: { delimiter: ",", header: true, dynamicTyping: true }, |
||||||
|
expected: { |
||||||
|
"results": { |
||||||
|
"fields": [ |
||||||
|
"F1", |
||||||
|
"F2", |
||||||
|
"F3" |
||||||
|
], |
||||||
|
"rows": [ |
||||||
|
{ |
||||||
|
"F1": "", |
||||||
|
"F2": 2, |
||||||
|
"F3": "V3" |
||||||
|
}, |
||||||
|
{ |
||||||
|
"F1": "V4", |
||||||
|
"F2": "V5", |
||||||
|
"F3": "V6" |
||||||
|
} |
||||||
|
] |
||||||
|
}, |
||||||
|
"errors": [] |
||||||
|
} |
||||||
|
} |
||||||
|
] |
||||||
|
}, |
||||||
|
]; |
||||||
|
|
||||||
|
$(function() |
||||||
|
{ |
||||||
|
var counter = 0; |
||||||
|
for (var i = 0; i < tests.length; i++) |
||||||
|
{ |
||||||
|
var test = tests[i]; |
||||||
|
var input = test.input; |
||||||
|
for (var j = 0; j < test.cases.length; j++) |
||||||
|
{ |
||||||
|
counter++; |
||||||
|
var testCase = test.cases[j]; |
||||||
|
var actual = doTest(input, testCase.config); |
||||||
|
var status = equal(actual, testCase.expected) ? passing : failing; |
||||||
|
render(input, testCase.expected, actual, testCase.config, counter, status); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
$('#pass-count').text(passCount); |
||||||
|
$('#fail-count').text(failCount); |
||||||
|
}); |
||||||
|
|
||||||
|
function doTest(input, config) |
||||||
|
{ |
||||||
|
return $.parse(input, config); |
||||||
|
} |
||||||
|
|
||||||
|
function render(input, expected, actual, config, count, status) |
||||||
|
{ |
||||||
|
if (status == passing) |
||||||
|
passCount++; |
||||||
|
else |
||||||
|
failCount++; |
||||||
|
|
||||||
|
var html = '<tr>' + |
||||||
|
'<td class="count">'+count+'</td>' + |
||||||
|
'<td class="input"><code>'+string(input)+'</code></td>' + |
||||||
|
'<td class="config"><code>'+string(config)+'</code></td>' + |
||||||
|
'<td class="output"><code>'+string(expected)+'</code></td>' + |
||||||
|
'<td class="output '+status+'"><code>'+string(actual)+'</code></td>' + |
||||||
|
'</tr>'; |
||||||
|
$('#results').append(html); |
||||||
|
} |
||||||
|
|
||||||
|
function string(obj) |
||||||
|
{ |
||||||
|
return typeof obj === "string" ? obj : JSON.stringify(obj, undefined, 2); |
||||||
|
} |
||||||
|
|
||||||
|
function equal(actual, expected) |
||||||
|
{ |
||||||
|
return string(actual) === string(expected); |
||||||
|
} |
Loading…
Reference in new issue