| 1 |
<html> |
|---|
| 2 |
<head> |
|---|
| 3 |
<script src="../lib/OpenLayers.js"></script> |
|---|
| 4 |
<script type="text/javascript"> |
|---|
| 5 |
|
|---|
| 6 |
function test_01_String_startsWith(t) { |
|---|
| 7 |
t.plan(3); |
|---|
| 8 |
|
|---|
| 9 |
var str = "chickenHead"; |
|---|
| 10 |
|
|---|
| 11 |
var test1 = "chicken"; |
|---|
| 12 |
var test2 = "beet"; |
|---|
| 13 |
|
|---|
| 14 |
t.ok(OpenLayers.String.startsWith(str, "chicken"), |
|---|
| 15 |
"'chickenHead' starts with 'chicken'"); |
|---|
| 16 |
t.ok(!OpenLayers.String.startsWith(str, "Head"), |
|---|
| 17 |
"'chickenHead' does not start with 'Head'"); |
|---|
| 18 |
t.ok(!OpenLayers.String.startsWith(str, "beet"), |
|---|
| 19 |
"'chickenHead' doesnt start with 'beet'"); |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
function test_02_String_contains(t) { |
|---|
| 23 |
t.plan(4); |
|---|
| 24 |
|
|---|
| 25 |
var str = "chickenHead"; |
|---|
| 26 |
|
|---|
| 27 |
t.ok(OpenLayers.String.contains(str, "chicken"), |
|---|
| 28 |
"(beginning) 'chickenHead' contains with 'chicken'"); |
|---|
| 29 |
t.ok(OpenLayers.String.contains(str, "ick"), |
|---|
| 30 |
"(middle) 'chickenHead' contains with 'ick'"); |
|---|
| 31 |
t.ok(OpenLayers.String.contains(str, "Head"), |
|---|
| 32 |
"(end) 'chickenHead' contains with 'Head'"); |
|---|
| 33 |
t.ok(!OpenLayers.String.startsWith(str, "beet"), |
|---|
| 34 |
"'chickenHead' doesnt start with 'beet'"); |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
function test_03_String_trim(t) { |
|---|
| 38 |
t.plan(5); |
|---|
| 39 |
|
|---|
| 40 |
var str = "chickenHead"; |
|---|
| 41 |
t.eq(OpenLayers.String.trim(str), |
|---|
| 42 |
"chickenHead", "string with no extra whitespace is left alone"); |
|---|
| 43 |
|
|---|
| 44 |
str = " chickenHead"; |
|---|
| 45 |
t.eq(OpenLayers.String.trim(str), |
|---|
| 46 |
"chickenHead", "string with extra whitespace at beginning is trimmed correctly"); |
|---|
| 47 |
|
|---|
| 48 |
str = "chickenHead "; |
|---|
| 49 |
t.eq(OpenLayers.String.trim(str), |
|---|
| 50 |
"chickenHead", "string with extra whitespace at end is trimmed correctly"); |
|---|
| 51 |
|
|---|
| 52 |
str = " chickenHead "; |
|---|
| 53 |
t.eq(OpenLayers.String.trim(str), |
|---|
| 54 |
"chickenHead", "string with extra whitespace at beginning and end is trimmed correctly"); |
|---|
| 55 |
|
|---|
| 56 |
str = " "; |
|---|
| 57 |
t.eq(OpenLayers.String.trim(str), "", "whitespace string is trimmed correctly"); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
function test_05_String_camelize(t) { |
|---|
| 61 |
t.plan(7); |
|---|
| 62 |
|
|---|
| 63 |
var str = "chickenhead"; |
|---|
| 64 |
t.eq(OpenLayers.String.camelize(str), "chickenhead", "string with no hyphens is left alone"); |
|---|
| 65 |
|
|---|
| 66 |
str = "chicken-head"; |
|---|
| 67 |
t.eq(OpenLayers.String.camelize(str), "chickenHead", "string with one middle hyphen is camelized correctly"); |
|---|
| 68 |
|
|---|
| 69 |
str = "chicken-head-man"; |
|---|
| 70 |
t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens is camelized correctly"); |
|---|
| 71 |
|
|---|
| 72 |
str = "-chickenhead"; |
|---|
| 73 |
t.eq(OpenLayers.String.camelize(str), "Chickenhead", "string with starting hyphen is camelized correctly (capitalized)"); |
|---|
| 74 |
|
|---|
| 75 |
str = "-chicken-head-man"; |
|---|
| 76 |
t.eq(OpenLayers.String.camelize(str), "ChickenHeadMan", "string with starting hypen and multiple middle hyphens is camelized correctly"); |
|---|
| 77 |
|
|---|
| 78 |
str = "chicken-"; |
|---|
| 79 |
t.eq(OpenLayers.String.camelize(str), "chicken", "string ending in hyphen is camelized correctly (hyphen dropped)"); |
|---|
| 80 |
|
|---|
| 81 |
str = "chicken-head-man-"; |
|---|
| 82 |
t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens and end hyphen is camelized correctly (end hyphen dropped)"); |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
function test_06_Number_limitSigDigs(t) { |
|---|
| 88 |
t.plan(9); |
|---|
| 89 |
|
|---|
| 90 |
var num = 123456789; |
|---|
| 91 |
t.eq(OpenLayers.Number.limitSigDigs(num), 0, "passing 'null' as sig returns 0"); |
|---|
| 92 |
t.eq(OpenLayers.Number.limitSigDigs(num, -1), 0, "passing -1 as sig returns 0"); |
|---|
| 93 |
t.eq(OpenLayers.Number.limitSigDigs(num, 0), 0, "passing 0 as sig returns 0"); |
|---|
| 94 |
|
|---|
| 95 |
t.eq(OpenLayers.Number.limitSigDigs(num, 15), 123456789, "passing sig greater than num digits in number returns number unmodified"); |
|---|
| 96 |
|
|---|
| 97 |
t.eq(OpenLayers.Number.limitSigDigs(num, 1), 100000000, "passing sig 1 works"); |
|---|
| 98 |
t.eq(OpenLayers.Number.limitSigDigs(num, 3), 123000000, "passing middle sig works (rounds down)"); |
|---|
| 99 |
t.eq(OpenLayers.Number.limitSigDigs(num, 5), 123460000, "passing middle sig works (rounds up)"); |
|---|
| 100 |
t.eq(OpenLayers.Number.limitSigDigs(num, 9), 123456789, "passing sig equal to num digits in number works"); |
|---|
| 101 |
|
|---|
| 102 |
num = 1234.56789; |
|---|
| 103 |
t.eq(OpenLayers.Number.limitSigDigs(num, 5), 1234.6, "running limSigDig() on a floating point number works fine"); |
|---|
| 104 |
|
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
function test_07_Function_bind(t) { |
|---|
| 108 |
t.plan(12); |
|---|
| 109 |
|
|---|
| 110 |
g_obj = {}; |
|---|
| 111 |
g_Arg1 = {}; |
|---|
| 112 |
g_Arg2 = {}; |
|---|
| 113 |
g_Arg3 = {}; |
|---|
| 114 |
g_Arg4 = {}; |
|---|
| 115 |
var foo = function(x,y,z,a) { |
|---|
| 116 |
t.ok(this == g_obj, "context correctly set"); |
|---|
| 117 |
t.ok(x == g_Arg1, "arg1 passed correctly"); |
|---|
| 118 |
t.ok(y == g_Arg2, "arg2 passed correctly"); |
|---|
| 119 |
t.ok(z == g_Arg3, "arg3 passed correctly"); |
|---|
| 120 |
t.ok(a == g_Arg4, "arg4 passed correctly"); |
|---|
| 121 |
t.eq(arguments.length, 4, "correct number of arguments ((regression test for #876))"); |
|---|
| 122 |
}; |
|---|
| 123 |
|
|---|
| 124 |
var newFoo = OpenLayers.Function.bind(foo, g_obj, g_Arg1, g_Arg2); |
|---|
| 125 |
|
|---|
| 126 |
newFoo(g_Arg3, g_Arg4); |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
newFoo(g_Arg3, g_Arg4); |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
function test_08_Function_bindAsEventListener(t) { |
|---|
| 133 |
t.plan(4); |
|---|
| 134 |
|
|---|
| 135 |
g_obj = {}; |
|---|
| 136 |
g_Event = {}; |
|---|
| 137 |
g_WindowEvent = {}; |
|---|
| 138 |
|
|---|
| 139 |
var foo = function(x) { |
|---|
| 140 |
t.ok(this == g_obj, "context correctly set"); |
|---|
| 141 |
g_X = x; |
|---|
| 142 |
}; |
|---|
| 143 |
|
|---|
| 144 |
var newFoo = OpenLayers.Function.bindAsEventListener(foo, g_obj); |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
g_X = null; |
|---|
| 148 |
newFoo(g_Event); |
|---|
| 149 |
t.ok(g_X == g_Event, "event properly passed as first argument when event specified"); |
|---|
| 150 |
|
|---|
| 151 |
g_X = null; |
|---|
| 152 |
newFoo(); |
|---|
| 153 |
t.ok(g_X == window.event, "window.event properly passed as first argument when nothing specified"); |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
</script> |
|---|
| 159 |
</head> |
|---|
| 160 |
<body> |
|---|
| 161 |
</body> |
|---|
| 162 |
</html> |
|---|