/
test_js_argument_checker.html
75 lines (60 loc) · 3.29 KB
/
test_js_argument_checker.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
<!DOCTYPE html>
<html>
<head>
<title>Test JavaScript Argument Checker (domutils.js)</title>
<script type="text/javascript" src="../bridge/client/domutils.js"></script>
<script type="text/javascript" src="./test_utils.js"></script>
<script type="text/javascript">
function CustomConstructor() {
}
window.onload = function () {
log("Start");
var basicTestInput = {
"object": "{}",
"number": "1.0",
"string": "\"foo\"",
"boolean": "true",
"function": "function () {}",
"dictionary": "{}"
};
log("Basic tests")
for (var typeName in basicTestInput)
runBasicTests(typeName, basicTestInput[typeName]);
log("More checkArguments() tests");
shouldNotThrow('checkArguments("test", "object | number", 1, [{}]);');
shouldNotThrow('checkArguments("test", "object | number", 1, [1.0]);');
shouldThrow('checkArguments("test", "object | number", 1, [function () {}]);');
shouldThrow('checkArguments("test", "object", 1, ["bad value"]);');
shouldThrow('checkArguments("test", "object", 0, ["bad optional value"]);');
shouldThrow('checkArguments("test", "number", 1, [NaN]);');
shouldThrow('checkArguments("test", "number", 1, [Infinity]);');
shouldThrow('checkArguments("test", "number", 1, [-Infinity]);');
shouldNotThrow('checkArguments("test", "CustomConstructor", 1, [new CustomConstructor()]);');
shouldThrow('checkArguments("test", "CustomConstructor", 1, [{}]);');
shouldThrow('checkArguments("test", "CustomUnknownConstructor", 1, [new CustomConstructor()]);');
shouldNotThrow('checkArguments("test", "dictionary", 1, [null]);');
shouldNotThrow('checkArguments("test", "dictionary", 1, [undefined]);');
shouldNotThrow('checkArguments("test", "dictionary", 1, [{}]);');
log("Test checkDictionary()");
shouldNotThrow('checkDictionary("test", {}, { "b": "boolean", "n": "number", "o": "object"});');
shouldNotThrow('checkDictionary("test", {"b": true}, { "b": "boolean", "n": "number", "o": "object"});');
shouldThrow('checkDictionary("test", {"n": "not a number"}, { "b": "boolean", "n": "number", "o": "object"});');
shouldNotThrow('checkDictionary("test", {"b": true, "n": 1.0}, { "b": "boolean", "n": "number", "o": "object"});');
shouldNotThrow('checkDictionary("test", {"not-a-member": true}, { "b": "boolean", "n": "number", "o": "object"});');
shouldNotThrow('checkDictionary("test", {"o": {}, "not-a-member": true}, { "b": "boolean", "n": "number", "o": "object"});');
log("Done with " + getErrorCount() + " errors");
}
function runBasicTests(typeName, strValue) {
shouldThrow('checkArguments("test", "' + typeName + '", 1, []);');
shouldNotThrow('checkArguments("test", "' + typeName + '", 0, []);');
shouldNotThrow('checkArguments("test", "' + typeName + '", 0, [' + strValue + ']);');
shouldNotThrow('checkArguments("test", "' + typeName + '", 1, [' + strValue + ']);');
shouldNotThrow('checkArguments("test", "' + typeName + '", 1, [' + strValue + ', "extra"]);');
shouldNotThrow('checkArguments("test", "' + typeName + ', ' + typeName + '", 2, [' + strValue + ', ' + strValue + ']);');
shouldNotThrow('checkArguments("test", "' + typeName + ', ' + typeName + '", 1, [' + strValue + ', ' + strValue + ']);');
}
</script>
</head>
<body>
</body>
</html>