Skip to content

Instantly share code, notes, and snippets.

@anekos
Created December 22, 2009 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anekos/261760 to your computer and use it in GitHub Desktop.
Save anekos/261760 to your computer and use it in GitHub Desktop.
Vimperator のコードテスト用スクリプトテンプレ
(function(){
// util function
// {{{
let echo = function (str) {
setTimeout(function () liberator.echo(str), 100);
};
let log = liberator.log;
let dlog = function (delay, func) {
setTimeout(function () log(func()), delay * 1000);
};
let time = function (name, func) {
let [a, r, b] = [new Date(), func(), new Date()];
log(name + ": " + ((b.getTime() - a.getTime()) / 1000) + "msec");
return r;
}
let logFailForError = function (name, e) {
log("Error: " + name + ": " + e);
};
let assertEqual = function (name, except, func) {
let result = func();
log((except === result) ? ("OK: " + name + ":")
: ("Fail: " + name + ": except = " + except + ", but was " + result));
};
let assert = function (name, func) {
try {
let result = func();
log(result ? ("OK: " + name + ":")
: ("Fail: " + name + ": except = " + true + ", but was " + result));
} catch (e) {
logFailForError(name, e);
}
};
let assertNot = function (name, func) {
try {
let result = func();
log(!result ? ("OK: " + name + ":")
: ("Fail: " + name + ": except = " + false + ", but was " + result));
} catch (e) {
logFailForError(name, e);
}
};
function assertError (name, func) {
try {
func();
log('Fail: ' + name + ': no error');
} catch (e) {
log('OK: ' + name + ': ' + e);
}
};
function s2b (s,d) (!/^(\d+|false)$/i.test(s)|s*1|!!d*2)&1<<!s;
function toBool (s,d) [(!/^(\d+|false)$/i.test(s)|s*1|!!d*2)&1<<!s+'',d][(s==''.U)*1];
function toBoolean (value, def) {
switch (typeof value) {
case 'undefined':
return def;
case 'number':
return !!value;
case 'string':
return !/^(\d+|false)$/i.test(value) || parseInt(value);
default:
return !!value;
}
}
function around (obj, name, func) {
let next = obj[name];
obj[name] = function ()
let (self = this, args = arguments)
func.call(self,
function () next.apply(self, args),
args);
}
// }}}
//
if (0) { // {{{
} // }}}
// get relative path
if (1) { // {{{
function makeFile (path) {
return io.File(path);
let file = Components.classes['@mozilla.org/file/local;1']
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(path);
return file;
}
function getRelativePath (target, base) {
return makeFile(target).getRelativeDescriptor(makeFile(base));
}
let f = getRelativePath;
assertEqual(
1,
'../foo/bar',
function () f('c:\\home\\anekos\\foo\\bar', 'c:\\home\\anekos\\hoge')
);
assertEqual(
2,
'../../hoge',
function () f('c:\\home\\anekos\\hoge', 'c:\\home\\anekos\\foo\\bar')
);
assertEqual(
3,
'../../hoge',
function () f('c:\\home\\anekos\\hoge', 'c:\\home\\anekos\\foo\\bar')
);
assertEqual(
4,
'hoge',
function () f('c:\\home\\anekos\\hoge', 'c:\\home\\anekos\\')
);
assertEqual(
5,
'../foo',
function () f('~/foo', '~/var')
);
assertEqual(
6,
'../hoge/moge',
function () f('~/hoge/moge', '~/var')
);
assertEqual(
7,
'../../var',
function () f('~/var', '~/hoge/moge')
);
} // }}}
})();
// vim:sw=2 ts=2 et si fdm=marker:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment