1
0
Fork 0
forked from 0x2620/oxjs

add test browser

This commit is contained in:
rolux 2011-05-07 19:25:34 +02:00
commit a58350a29e
15 changed files with 160 additions and 1751 deletions

View file

@ -318,7 +318,7 @@ Ox.filter = function(obj, fn) {
/*@
Ox.find <f> Returns array elements that match a string, leading matches first
> Ox.find(['foo', 'bar', 'foobar', 'barfoo'], 'foo')
[['foo", 'foobar'], ['barfoo']]
[['foo', 'foobar'], ['barfoo']]
@*/
Ox.find = function(arr, str) {
/*
@ -352,15 +352,15 @@ Ox.forEach <f> forEach loop
callback <f> Callback function
value <*> Value
key <n|s> Key
@*/
/*
> Ox.test.string = "";
> Ox.forEach(["f", "o", "o"], function(v, i) { Ox.test.string += i; })
> Ox.forEach({a: "f", b: "o", c: "o"}, function(v, k) { Ox.test.string += k; })
> Ox.forEach("foo", function(v) { Ox.test.string += v; })
<script>
Ox.test.string = "";
Ox.forEach(["f", "o", "o"], function(v, i) { Ox.test.string += i; });
Ox.forEach({a: "f", b: "o", c: "o"}, function(v, k) { Ox.test.string += k; });
Ox.forEach("foo", function(v) { Ox.test.string += v; });
</script>
> Ox.test.string
"012abcfoo"
*/
@*/
Ox.forEach = function(obj, fn) {
var isObject = Ox.isObject(obj), key;
for (key in obj) {
@ -516,7 +516,9 @@ Ox.keys = function(obj) {
Ox.last <f> Gets or sets the last element of an array
Unlike <code>foobarbaz[foobarbaz.length - 1]</code>,
<code>Ox.last(foobarbaz)</code> is short.
> Ox.test.array = [1, 2, 3]
<script>
Ox.test.array = [1, 2, 3];
</script>
> Ox.last(Ox.test.array)
3
> Ox.last(Ox.test.array, 4)
@ -636,7 +638,7 @@ Ox.makeObject = function(obj) {
return ret;
};
/*
/*@
Ox.map <f> Transforms the values of an array, object or string
Unlike <code>[].map()</code>, <code>Ox.map()</code> works for arrays,
objects and strings. Returning <code>null</code> from the iterator
@ -651,7 +653,7 @@ Ox.map <f> Transforms the values of an array, object or string
[true, true, false]
> Ox.map([,], function(v, i) { return i; })
[0]
*/
@*/
Ox.map = function(obj, fn) {
// fixme: return null to filter out is a bit esoteric
@ -1104,6 +1106,7 @@ Ox.SHORT_WEEKDAYS = Ox.WEEKDAYS.map(function(val) {
//@ Date -----------------------------------------------------------------------
//@ Ox.getDate <f> Get the day of a date, optionally UTC
// see Ox.setSeconds for source code
/*@
Ox.getDateInWeek <f> Get the date that falls on a given weekday in the same week
@ -1138,6 +1141,7 @@ Ox.getDateInWeek = function(date, weekday, utc) {
}
//@ Ox.getDay <f> Get the weekday of a date, optionally UTC
// see Ox.setSeconds for source code
/*@
Ox.getDayOfTheYear <f> Get the day of the year for a given date
@ -1212,8 +1216,9 @@ Ox.getFirstDayOfTheYear = function(date, utc) {
};
//@ Ox.getFullYear <f> Get the year of a date, optionally UTC
// see Ox.setSeconds for source code
//@ Ox.getHours <f> Get the hours of a date, optionally UTC
// see Ox.setSeconds for source code
/*@
Ox.getISODate <f> Get the ISO date string for a given date
@ -1277,9 +1282,13 @@ Ox.getISOYear = function(date, utc) {
};
//@ Ox.getMilliseconds <f> Get the milliseconds of a date
// see Ox.setSeconds for source code
//@ Ox.getMinutes <f> Get the minutes of a date, optionally UTC
// see Ox.setSeconds for source code
//@ Ox.getMonth <f> Get the month of a date, optionally UTC
// see Ox.setSeconds for source code
//@ Ox.getSeconds <f> Get the seconds of a date
// see Ox.setSeconds for source code
//@ Ox.getTime <f> Alias for <code>+new Date()</code> (deprecated)
Ox.getTime = function() {
@ -1365,12 +1374,19 @@ Ox.makeYear = function(date, utc) {
};
//@ Ox.setDate <f> Set the day of a date, optionally UTC
// see Ox.setSeconds for source code
//@ Ox.setDay <f> Set the weekday of a date, optionally UTC
// see Ox.setSeconds for source code
//@ Ox.setFullYear <f> Set the year of a date, optionally UTC
// see Ox.setSeconds for source code
//@ Ox.setHours <f> Set the hours of a date, optionally UTC
// see Ox.setSeconds for source code
//@ Ox.setMilliseconds <f> Set the milliseconds of a date
// see Ox.setSeconds for source code
//@ Ox.setMinutes <f> Set the minutes of a date, optionally UTC
// see Ox.setSeconds for source code
//@ Ox.setMonth <f> Set the month of a date, optionally UTC
// see Ox.setSeconds for source code
//@ Ox.setSeconds <f> Set the seconds of a date
[
@ -1773,7 +1789,7 @@ Ox.element = function(str) {
'&#x00E4;b&#x00E7;d&#x00EA;'
@*/
Ox.encodeHTML = function(str) {
return Ox.map(str, function(v) {
return Ox.map(str.toString(), function(v) {
var code = v.charCodeAt(0);
return code < 128 ? (v in Ox.HTML_ENTITIES ? Ox.HTML_ENTITIES[v] : v) :
'&#x' + Ox.pad(code.toString(16).toUpperCase(), 4) + ';';
@ -1946,11 +1962,7 @@ Ox.element = function(str) {
})();
/*
================================================================================
Format functions
================================================================================
*/
//@ Format ---------------------------------------------------------------------
/*@
Ox.formatArea <f> Formats a number of meters as square kilometers
@ -1984,7 +1996,9 @@ Ox.formatDate <f> Formats a date according to a format string
See
<a href="http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/strftime.3.html">strftime</a>
and <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a>.
> Ox.test.date = new Date('2005-01-02 00:03:04')
<script>
Ox.test.date = new Date('2005-01-02 00:03:04');
</script>
> Ox.formatDate(Ox.test.date, '%A') // Full weekday
'Sunday'
> Ox.formatDate(Ox.test.date, '%a') // Abbreviated weekday
@ -2256,7 +2270,7 @@ Ox.formatResolution = function(arr, str) {
/*@
Ox.formatString <f> Basic string formatting
> Ox.formatString('{0}{1}', ['foo', 'bar'])
foobar'
'foobar'
> Ox.formatString('{a}{b}', {a: 'foo', b: 'bar'})
'foobar'
@*/
@ -2298,7 +2312,7 @@ Ox.formatUnit = function(num, str) {
return num + ' ' + str;
};
//* Geo ------------------------------------------------------------------------
//@ Geo ------------------------------------------------------------------------
(function() {
@ -2677,7 +2691,9 @@ Ox.parseHTML = (function() {
Ox.parseURL <f> Takes a URL, returns its components
(url) -> <o> URL components
url <s> URL
> Ox.test.object = Ox.parseURL('http://www.foo.com:8080/bar/index.html?a=0&b=1#c')
<script>
Ox.test.object = Ox.parseURL('http://www.foo.com:8080/bar/index.html?a=0&b=1#c');
</script>
> Ox.test.object.hash
'#c'
> Ox.test.object.host
@ -3053,29 +3069,28 @@ Ox.minify = function(source) {
/*@
Ox.test <f> Takes JavaScript, runs inline tests, returns results
@*/
Ox.test = function(source) {
var tests = {
success: [],
failure: []
};
Ox.doc(source).forEach(function(item) {
item.examples && item.examples.forEach(function(example) {
Ox.print(example)
var actual = eval(example.statement);
if (example.result) {
tests[
Ox.isEqual(eval('Ox.test.result = ' + example.result), actual) ?
'success' : 'failure'
].push({
test: example.statement,
expected: example.result,
actual: actual
});
}
Ox.test = function(file, callback) {
Ox.doc(file, function(items) {
var tests = [];
items.forEach(function(item) {
item.examples && item.examples.forEach(function(example) {
Ox.print(example)
var actual = eval(example.statement);
if (example.result) {
tests.push({
actual: actual,
expected: example.result,
name: item.name,
section: item.section,
statement: example.statement,
success: Ox.isEqual(eval('Ox.test.result = ' + example.result), actual)
});
}
});
});
callback(tests);
});
return tests;
}
};
/*@
Ox.tokenize <f> Tokenizes JavaScript