| OLD | NEW |
| 1 // This should output "PROXY success:80" if all the tests pass. | 1 // This should output "PROXY success:80" if all the tests pass. |
| 2 // Otherwise it will output "PROXY failure:<num-failures>". | 2 // Otherwise it will output "PROXY failure:<num-failures>". |
| 3 // | 3 // |
| 4 // This aims to unit-test the PAC library functions, which are | 4 // This aims to unit-test the PAC library functions, which are |
| 5 // exposed in the PAC's execution environment. (Namely, dnsDomainLevels, | 5 // exposed in the PAC's execution environment. (Namely, dnsDomainLevels, |
| 6 // timeRange, etc.) | 6 // timeRange, etc.) |
| 7 | 7 |
| 8 function FindProxyForURL(url, host) { | 8 function FindProxyForURL(url, host) { |
| 9 var numTestsFailed = 0; | 9 var numTestsFailed = 0; |
| 10 | 10 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 t.expectFalse(dnsDomainIs("google.com", ".co.uk")); | 38 t.expectFalse(dnsDomainIs("google.com", ".co.uk")); |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 Tests.testDnsDomainLevels = function(t) { | 41 Tests.testDnsDomainLevels = function(t) { |
| 42 t.expectEquals(0, dnsDomainLevels("www")); | 42 t.expectEquals(0, dnsDomainLevels("www")); |
| 43 t.expectEquals(2, dnsDomainLevels("www.google.com")); | 43 t.expectEquals(2, dnsDomainLevels("www.google.com")); |
| 44 t.expectEquals(3, dnsDomainLevels("192.168.1.1")); | 44 t.expectEquals(3, dnsDomainLevels("192.168.1.1")); |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 Tests.testIsInNet = function(t) { | 47 Tests.testIsInNet = function(t) { |
| 48 // TODO(eroman): | 48 t.expectTrue( |
| 49 isInNet("192.89.132.25", "192.89.132.25", "255.255.255.255")); |
| 50 t.expectFalse( |
| 51 isInNet("193.89.132.25", "192.89.132.25", "255.255.255.255")); |
| 52 |
| 53 t.expectTrue(isInNet("192.89.132.25", "192.89.0.0", "255.255.0.0")); |
| 54 t.expectFalse(isInNet("193.89.132.25", "192.89.0.0", "255.255.0.0")); |
| 49 | 55 |
| 50 // t.expectTrue( | 56 t.expectFalse( |
| 51 // isInNet("192.89.132.25", "192.89.132.25", "255.255.255.255")); | 57 isInNet("192.89.132.a", "192.89.0.0", "255.255.0.0")); |
| 52 // t.expectFalse( | |
| 53 // isInNet("193.89.132.25", "192.89.132.25", "255.255.255.255")); | |
| 54 // | |
| 55 // t.expectTrue(isInNet("192.89.132.25", "192.89.0.0", "255.255.0.0")); | |
| 56 // t.expectFalse(isInNet("193.89.132.25", "192.89.0.0", "255.255.0.0")); | |
| 57 }; | 58 }; |
| 58 | 59 |
| 59 Tests.testIsPlainHostName = function(t) { | 60 Tests.testIsPlainHostName = function(t) { |
| 60 t.expectTrue(isPlainHostName("google")); | 61 t.expectTrue(isPlainHostName("google")); |
| 61 t.expectFalse(isPlainHostName("google.com")); | 62 t.expectFalse(isPlainHostName("google.com")); |
| 62 }; | 63 }; |
| 63 | 64 |
| 64 Tests.testLocalHostOrDomainIs = function(t) { | 65 Tests.testLocalHostOrDomainIs = function(t) { |
| 65 t.expectTrue(localHostOrDomainIs("www.google.com", "www.google.com")); | 66 t.expectTrue(localHostOrDomainIs("www.google.com", "www.google.com")); |
| 66 t.expectTrue(localHostOrDomainIs("www", "www.google.com")); | 67 t.expectTrue(localHostOrDomainIs("www", "www.google.com")); |
| 67 t.expectFalse(localHostOrDomainIs("maps.google.com", "www.google.com")); | 68 t.expectFalse(localHostOrDomainIs("maps.google.com", "www.google.com")); |
| 68 }; | 69 }; |
| 69 | 70 |
| 70 Tests.testShExpMatch = function(t) { | 71 Tests.testShExpMatch = function(t) { |
| 71 // TODO(eroman): | 72 t.expectTrue(shExpMatch("foo.jpg", "*.jpg")); |
| 72 | 73 t.expectTrue(shExpMatch("foo5.jpg", "*o?.jpg")); |
| 73 //t.expectTrue(shExpMatch("http://maps.google.com/blah/foo/moreblah.jpg", | 74 t.expectFalse(shExpMatch("foo.jpg", ".jpg")); |
| 74 // ".*/foo/.*jpg")); | 75 t.expectFalse(shExpMatch("foo.jpg", "foo")); |
| 75 | |
| 76 //t.expectFalse(shExpMatch("http://maps.google.com/blah/foo/moreblah.jpg", | |
| 77 // ".*/foo/.*.html")); | |
| 78 }; | 76 }; |
| 79 | 77 |
| 80 Tests.testWeekdayRange = function(t) { | 78 Tests.testWeekdayRange = function(t) { |
| 81 // TODO(eroman) | 79 // Test with local time. |
| 80 MockDate.setCurrent("Tue Mar 03 2009"); |
| 81 t.expectEquals(true, weekdayRange("MON", "FRI")); |
| 82 t.expectEquals(true, weekdayRange("TUE", "FRI")); |
| 83 t.expectEquals(true, weekdayRange("TUE", "TUE")); |
| 84 t.expectEquals(true, weekdayRange("TUE")); |
| 85 t.expectEquals(false, weekdayRange("WED", "FRI")); |
| 86 t.expectEquals(false, weekdayRange("SUN", "MON")); |
| 87 t.expectEquals(false, weekdayRange("SAT")); |
| 88 t.expectEquals(false, weekdayRange("FRI", "MON")); |
| 89 |
| 90 // Test with GMT time. |
| 91 MockDate.setCurrent("Tue Mar 03 2009 GMT"); |
| 92 t.expectEquals(true, weekdayRange("MON", "FRI", "GMT")); |
| 93 t.expectEquals(true, weekdayRange("TUE", "FRI", "GMT")); |
| 94 t.expectEquals(true, weekdayRange("TUE", "TUE", "GMT")); |
| 95 t.expectEquals(true, weekdayRange("TUE", "GMT")); |
| 96 t.expectEquals(false, weekdayRange("WED", "FRI", "GMT")); |
| 97 t.expectEquals(false, weekdayRange("SUN", "MON", "GMT")); |
| 98 t.expectEquals(false, weekdayRange("SAT", "GMT")); |
| 82 }; | 99 }; |
| 83 | 100 |
| 84 Tests.testDateRange = function(t) { | 101 Tests.testDateRange = function(t) { |
| 85 // TODO(eroman) | 102 // dateRange(day) |
| 103 MockDate.setCurrent("Mar 03 2009"); |
| 104 t.expectEquals(true, dateRange(3)); |
| 105 t.expectEquals(false, dateRange(1)); |
| 106 |
| 107 // dateRange(day, "GMT") |
| 108 MockDate.setCurrent("Mar 03 2009 GMT"); |
| 109 t.expectEquals(true, dateRange(3, "GMT")); |
| 110 t.expectEquals(false, dateRange(1, "GMT")); |
| 111 |
| 112 // dateRange(day1, day2) |
| 113 MockDate.setCurrent("Mar 03 2009"); |
| 114 t.expectEquals(true, dateRange(1, 4)); |
| 115 t.expectEquals(false, dateRange(4, 20)); |
| 116 |
| 117 // dateRange(day, month) |
| 118 MockDate.setCurrent("Mar 03 2009"); |
| 119 t.expectEquals(true, dateRange(3, "MAR")); |
| 120 MockDate.setCurrent("Mar 03 2014"); |
| 121 t.expectEquals(true, dateRange(3, "MAR")); |
| 122 // TODO(eroman): |
| 123 //t.expectEquals(false, dateRange(2, "MAR")); |
| 124 //t.expectEquals(false, dateRange(3, "JAN")); |
| 125 |
| 126 // dateRange(day, month, year) |
| 127 MockDate.setCurrent("Mar 03 2009"); |
| 128 t.expectEquals(true, dateRange(3, "MAR", 2009)); |
| 129 t.expectEquals(false, dateRange(4, "MAR", 2009)); |
| 130 t.expectEquals(false, dateRange(3, "FEB", 2009)); |
| 131 MockDate.setCurrent("Mar 03 2014"); |
| 132 t.expectEquals(false, dateRange(3, "MAR", 2009)); |
| 133 |
| 134 // dateRange(month1, month2) |
| 135 MockDate.setCurrent("Mar 03 2009"); |
| 136 t.expectEquals(true, dateRange("JAN", "MAR")); |
| 137 t.expectEquals(true, dateRange("MAR", "APR")); |
| 138 t.expectEquals(false, dateRange("MAY", "SEP")); |
| 139 |
| 140 // dateRange(day1, month1, day2, month2) |
| 141 MockDate.setCurrent("Mar 03 2009"); |
| 142 t.expectEquals(true, dateRange(1, "JAN", 3, "MAR")); |
| 143 t.expectEquals(true, dateRange(3, "MAR", 4, "SEP")); |
| 144 t.expectEquals(false, dateRange(4, "MAR", 4, "SEP")); |
| 145 |
| 146 // dateRange(month1, year1, month2, year2) |
| 147 MockDate.setCurrent("Mar 03 2009"); |
| 148 t.expectEquals(true, dateRange("FEB", 2009, "MAR", 2009)); |
| 149 MockDate.setCurrent("Apr 03 2009"); |
| 150 t.expectEquals(true, dateRange("FEB", 2009, "MAR", 2010)); |
| 151 t.expectEquals(false, dateRange("FEB", 2009, "MAR", 2009)); |
| 152 |
| 153 // dateRange(day1, month1, year1, day2, month2, year2) |
| 154 MockDate.setCurrent("Mar 03 2009"); |
| 155 t.expectEquals(true, dateRange(1, "JAN", 2009, 3, "MAR", 2009)); |
| 156 t.expectEquals(true, dateRange(3, "MAR", 2009, 4, "SEP", 2009)); |
| 157 t.expectEquals(true, dateRange(3, "JAN", 2009, 4, "FEB", 2010)); |
| 158 t.expectEquals(false, dateRange(4, "MAR", 2009, 4, "SEP", 2009)); |
| 86 }; | 159 }; |
| 87 | 160 |
| 88 Tests.testTimeRange = function(t) { | 161 Tests.testTimeRange = function(t) { |
| 89 // TODO(eroman) | 162 // timeRange(hour) |
| 163 MockDate.setCurrent("Mar 03, 2009 03:34:01"); |
| 164 t.expectEquals(true, timeRange(3)); |
| 165 t.expectEquals(false, timeRange(2)); |
| 166 |
| 167 // timeRange(hour1, hour2) |
| 168 MockDate.setCurrent("Mar 03, 2009 03:34:01"); |
| 169 t.expectEquals(true, timeRange(2, 3)); |
| 170 t.expectEquals(true, timeRange(2, 4)); |
| 171 t.expectEquals(true, timeRange(3, 5)); |
| 172 t.expectEquals(false, timeRange(1, 2)); |
| 173 t.expectEquals(false, timeRange(11, 12)); |
| 174 |
| 175 // timeRange(hour1, min1, hour2, min2) |
| 176 MockDate.setCurrent("Mar 03, 2009 03:34:01"); |
| 177 t.expectEquals(true, timeRange(1, 0, 3, 34)); |
| 178 t.expectEquals(true, timeRange(1, 0, 3, 35)); |
| 179 t.expectEquals(true, timeRange(3, 34, 5, 0)); |
| 180 t.expectEquals(false, timeRange(1, 0, 3, 0)); |
| 181 t.expectEquals(false, timeRange(11, 0, 16, 0)); |
| 182 |
| 183 // timeRange(hour1, min1, sec1, hour2, min2, sec2) |
| 184 MockDate.setCurrent("Mar 03, 2009 03:34:14"); |
| 185 t.expectEquals(true, timeRange(1, 0, 0, 3, 34, 14)); |
| 186 t.expectEquals(false, timeRange(1, 0, 0, 3, 34, 0)); |
| 187 t.expectEquals(true, timeRange(1, 0, 0, 3, 35, 0)); |
| 188 t.expectEquals(true, timeRange(3, 34, 0, 5, 0, 0)); |
| 189 t.expectEquals(false, timeRange(1, 0, 0, 3, 0, 0)); |
| 190 t.expectEquals(false, timeRange(11, 0, 0, 16, 0, 0)); |
| 90 }; | 191 }; |
| 91 | 192 |
| 92 // -------------------------- | 193 // -------------------------- |
| 93 // Helpers | 194 // TestContext |
| 94 // -------------------------- | 195 // -------------------------- |
| 95 | 196 |
| 96 // |name| is the name of the test being executed, it will be used when logging | 197 // |name| is the name of the test being executed, it will be used when logging |
| 97 // errors. | 198 // errors. |
| 98 function TestContext(name) { | 199 function TestContext(name) { |
| 99 this.numFailures_ = 0; | 200 this.numFailures_ = 0; |
| 100 this.name_ = name; | 201 this.name_ = name; |
| 101 }; | 202 }; |
| 102 | 203 |
| 103 TestContext.prototype.failed = function() { | 204 TestContext.prototype.failed = function() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 121 | 222 |
| 122 TestContext.prototype.log = function(x) { | 223 TestContext.prototype.log = function(x) { |
| 123 // Prefix with the test name that generated the log. | 224 // Prefix with the test name that generated the log. |
| 124 try { | 225 try { |
| 125 alert(this.name_ + ": " + x); | 226 alert(this.name_ + ": " + x); |
| 126 } catch(e) { | 227 } catch(e) { |
| 127 // In case alert() is not defined. | 228 // In case alert() is not defined. |
| 128 } | 229 } |
| 129 }; | 230 }; |
| 130 | 231 |
| 232 // -------------------------- |
| 233 // MockDate |
| 234 // -------------------------- |
| 235 |
| 236 function MockDate() { |
| 237 this.wrappedDate_ = new MockDate.super_(MockDate.currentDateString_); |
| 238 }; |
| 239 |
| 240 // Setup the MockDate so it forwards methods to "this.wrappedDate_" (which is a |
| 241 // real Date object). We can't simply chain the prototypes since Date() doesn't |
| 242 // allow it. |
| 243 MockDate.init = function() { |
| 244 MockDate.super_ = Date; |
| 245 |
| 246 function createProxyMethod(methodName) { |
| 247 return function() { |
| 248 return this.wrappedDate_[methodName] |
| 249 .apply(this.wrappedDate_, arguments); |
| 250 } |
| 251 }; |
| 252 |
| 253 for (i in MockDate.methodNames_) { |
| 254 var methodName = MockDate.methodNames_[i]; |
| 255 // Don't define the closure directly in the loop body, since Javascript's |
| 256 // crazy scoping rules mean |methodName| actually bleeds out of the loop! |
| 257 MockDate.prototype[methodName] = createProxyMethod(methodName); |
| 258 } |
| 259 |
| 260 // Replace the native Date() with our mock. |
| 261 Date = MockDate; |
| 262 }; |
| 263 |
| 264 // Unfortunately Date()'s methods are non-enumerable, therefore list manually. |
| 265 MockDate.methodNames_ = [ |
| 266 "toString", "toDateString", "toTimeString", "toLocaleString", |
| 267 "toLocaleDateString", "toLocaleTimeString", "valueOf", "getTime", |
| 268 "getFullYear", "getUTCFullYear", "getMonth", "getUTCMonth", |
| 269 "getDate", "getUTCDate", "getDay", "getUTCDay", "getHours", "getUTCHours", |
| 270 "getMinutes", "getUTCMinutes", "getSeconds", "getUTCSeconds", |
| 271 "getMilliseconds", "getUTCMilliseconds", "getTimezoneOffset", "setTime", |
| 272 "setMilliseconds", "setUTCMilliseconds", "setSeconds", "setUTCSeconds", |
| 273 "setMinutes", "setUTCMinutes", "setHours", "setUTCHours", "setDate", |
| 274 "setUTCDate", "setMonth", "setUTCMonth", "setFullYear", "setUTCFullYear", |
| 275 "toGMTString", "toUTCString", "getYear", "setYear" |
| 276 ]; |
| 277 |
| 278 MockDate.setCurrent = function(currentDateString) { |
| 279 MockDate.currentDateString_ = currentDateString; |
| 280 } |
| 281 |
| 282 // Bind the methods to proxy requests to the wrapped Date(). |
| 283 MockDate.init(); |
| 284 |
| OLD | NEW |