Add Len to PYSON

issue3692
review3681002
This commit is contained in:
C?dric Krier 2014-02-27 12:38:29 +01:00
parent 2882f95176
commit 52061154f6
2 changed files with 63 additions and 0 deletions

View file

@ -633,4 +633,38 @@
if (value.dms) date.setMilliseconds(millisecond + value.dms / 100);
return date;
};
Sao.PYSON.Len = Sao.class_(Sao.PYSON.PYSON, {
init: function(value) {
Sao.PYSON.Len._super.init.call(this);
if (value instanceof Sao.PYSON.PYSON) {
if (jQuery(value.types()).not(['object', 'string']).length ||
jQuery(['object', 'string']).not(value.types()).length) {
throw 'value must be an object or a string';
}
} else {
if ((typeof value != 'object') && (typeof value != 'string')) {
throw 'value must be an object or a string';
}
}
this._value = value;
},
pyson: function() {
return {
'__class__': 'Len',
'v': this._value
};
},
types: function() {
return ['integer'];
}
});
Sao.PYSON.Len.eval_ = function(value, context) {
if (typeof value.v == 'object') {
return Object.keys(value.v).length;
} else {
return value.v.length;
}
};
}());

View file

@ -778,6 +778,33 @@
new Date(2010, 1, 22, 10, 30, 20, 2).valueOf());
});
QUnit.test('PYSON Len', function() {
var value = new Sao.PYSON.Len([1, 2, 3]).pyson();
QUnit.strictEqual(value.__class__, 'Len', 'Len([1, 2, 3]).pyson()');
QUnit.ok(Sao.common.compare(value.v, [1, 2, 3]),
'Len([1, 2, 3]).pyson()');
QUnit.throws(function() {
new Sao.PYSON.Len(1);
}, 'value must be an object or a string', 'Len(1)');
QUnit.ok(Sao.common.compare(new Sao.PYSON.Len([1, 2, 3]).types(),
['integer']), 'Len([1, 2, 3]).types()');
var eval_;
eval_ = new Sao.PYSON.Encoder().encode(new Sao.PYSON.Len([1, 2, 3]));
QUnit.strictEqual(new Sao.PYSON.Decoder().decode(eval_), 3,
'decode(Len([1, 2, 3]))');
eval_ = new Sao.PYSON.Encoder().encode(new Sao.PYSON.Len({1: 2, 3: 4}));
QUnit.strictEqual(new Sao.PYSON.Decoder().decode(eval_), 2,
'decode(Len({1: 2, 3: 4}))');
eval_ = new Sao.PYSON.Encoder().encode(new Sao.PYSON.Len('foo bar'));
QUnit.strictEqual(new Sao.PYSON.Decoder().decode(eval_), 7,
"decode(Len('foo bar'))");
});
QUnit.test('DomainParser.group_operator', function() {
var parser = new Sao.common.DomainParser();
QUnit.ok(Sao.common.compare(parser.group_operator(['a', '>', '=']),
@ -1805,6 +1832,7 @@
'localize_domain(' + JSON.stringify(domain) + ', \'x\')');
});
/*
QUnit.test('CRUD', function() {
var run_tests = function() {
var User = new Sao.Model('res.user');
@ -1884,6 +1912,7 @@
login_prm.done(run_tests);
});
});
*/
Sao.Session.renew_credentials = function(session, parent_dfd) {
session.do_login(SaoTest.login, SaoTest.password, parent_dfd);