summaryrefslogtreecommitdiff
path: root/addons/web/static/lib/py.js/test
diff options
context:
space:
mode:
authorXavier Morel <xmo@openerp.com>2012-03-05 09:55:32 +0100
committerXavier Morel <xmo@openerp.com>2012-03-05 09:55:32 +0100
commitf8a93bc2848eba516213142f4f730a1feda5a39e (patch)
treeb8381b7f63589accc03d20885928068b66d0272a /addons/web/static/lib/py.js/test
parent12358af73bc0df054881258d690c91536e92b368 (diff)
[FIX] update py.js for operators &al, implement basic crummy version of relativedelta
bzr revid: xmo@openerp.com-20120305085532-0vcz6j0m985gjuz1
Diffstat (limited to 'addons/web/static/lib/py.js/test')
-rw-r--r--addons/web/static/lib/py.js/test/parser.js31
-rw-r--r--addons/web/static/lib/py.js/test/test.js67
2 files changed, 96 insertions, 2 deletions
diff --git a/addons/web/static/lib/py.js/test/parser.js b/addons/web/static/lib/py.js/test/parser.js
index ca74d2bebc8..bbbd5d7663d 100644
--- a/addons/web/static/lib/py.js/test/parser.js
+++ b/addons/web/static/lib/py.js/test/parser.js
@@ -11,6 +11,14 @@ expect.Assertion.prototype.tokens = function (n) {
'expected ' + this.obj + ' to not have an end token');
};
+expect.Assertion.prototype.named = function (value) {
+ this.assert(this.obj.id === '(name)',
+ 'expected ' + this.obj + ' to be a name token',
+ 'expected ' + this.obj + ' not to be a name token');
+ this.assert(this.obj.value === value,
+ 'expected ' + this.obj + ' to have tokenized ' + value,
+ 'expected ' + this.obj + ' not to have tokenized ' + value);
+};
expect.Assertion.prototype.constant = function (value) {
this.assert(this.obj.id === '(constant)',
'expected ' + this.obj + ' to be a constant token',
@@ -98,4 +106,27 @@ describe('Tokenizer', function () {
expect(toks[1].id).to.be(')');
});
});
+ describe('functions', function () {
+ it('tokenizes kwargs', function () {
+ var toks = py.tokenize('foo(bar=3, qux=4)');
+ expect(toks).to.have.tokens(10);
+ });
+ });
+});
+
+describe('Parser', function () {
+ describe('functions', function () {
+ var ast = py.parse(py.tokenize('foo(bar=3, qux=4)'));
+ expect(ast.id).to.be('(');
+ expect(ast.first).to.be.named('foo');
+
+ args = ast.second;
+ expect(args[0].id).to.be('=');
+ expect(args[0].first).to.be.named('bar');
+ expect(args[0].second).to.be.number(3);
+
+ expect(args[1].id).to.be('=');
+ expect(args[1].first).to.be.named('qux');
+ expect(args[1].second).to.be.number(4);
+ });
}); \ No newline at end of file
diff --git a/addons/web/static/lib/py.js/test/test.js b/addons/web/static/lib/py.js/test/test.js
index 6c53bcfad85..31fccc23240 100644
--- a/addons/web/static/lib/py.js/test/test.js
+++ b/addons/web/static/lib/py.js/test/test.js
@@ -262,6 +262,15 @@ describe('Attribute access', function () {
});
expect(py.eval('foo.bar()', {foo: o})).to.be('ok');
});
+ it('should not convert function attributes into methods', function () {
+ var o = new py.object();
+ o.bar = new py.type(function bar() {});
+ o.bar.__getattribute__ = function () {
+ return o.bar.baz;
+ }
+ o.bar.baz = py.True;
+ expect(py.eval('foo.bar.baz', {foo: o})).to.be(true);
+ });
it('should work on instance attributes', function () {
var typ = py.type(function MyType() {
this.attr = new py.float(3);
@@ -296,16 +305,37 @@ describe('Callables', function () {
});
expect(py.eval('MyType()', {MyType: typ})).to.be(true);
});
+ it('should accept kwargs', function () {
+ expect(py.eval('foo(ok=True)', {
+ foo: function foo() { return py.True; }
+ })).to.be(true);
+ });
+ it('should be able to get its kwargs', function () {
+ expect(py.eval('foo(ok=True)', {
+ foo: function foo(args, kwargs) { return kwargs.ok; }
+ })).to.be(true);
+ });
+ it('should be able to have both args and kwargs', function () {
+ expect(py.eval('foo(1, 2, 3, ok=True, nok=False)', {
+ foo: function (args, kwargs) {
+ expect(args).to.have.length(3);
+ expect(args[0].toJSON()).to.be(1);
+ expect(kwargs).to.only.have.keys('ok', 'nok')
+ expect(kwargs.nok.toJSON()).to.be(false);
+ return kwargs.ok;
+ }
+ })).to.be(true);
+ });
});
describe('issubclass', function () {
it('should say a type is its own subclass', function () {
- expect(py.issubclass.__call__(py.dict, py.dict).toJSON())
+ expect(py.issubclass.__call__([py.dict, py.dict]).toJSON())
.to.be(true);
expect(py.eval('issubclass(dict, dict)'))
.to.be(true);
});
it('should work with subtypes', function () {
- expect(py.issubclass.__call__(py.bool, py.object).toJSON())
+ expect(py.issubclass.__call__([py.bool, py.object]).toJSON())
.to.be(true);
});
});
@@ -313,4 +343,37 @@ describe('builtins', function () {
it('should aways be available', function () {
expect(py.eval('bool("foo")')).to.be(true);
});
+});
+
+describe('numerical protocols', function () {
+ describe('True numbers (float)', function () {
+ describe('Basic arithmetic', function () {
+ it('can be added', function () {
+ expect(py.eval('1 + 1')).to.be(2);
+ expect(py.eval('1.5 + 2')).to.be(3.5);
+ expect(py.eval('1 + -1')).to.be(0);
+ });
+ it('can be subtracted', function () {
+ expect(py.eval('1 - 1')).to.be(0);
+ expect(py.eval('1.5 - 2')).to.be(-0.5);
+ expect(py.eval('2 - 1.5')).to.be(0.5);
+ });
+ it('can be multiplied', function () {
+ expect(py.eval('1 * 3')).to.be(3);
+ expect(py.eval('0 * 5')).to.be(0);
+ expect(py.eval('42 * -2')).to.be(-84);
+ });
+ it('can be divided', function () {
+ expect(py.eval('1 / 2')).to.be(0.5);
+ expect(py.eval('2 / 1')).to.be(2);
+ });
+ });
+ });
+ describe('Strings', function () {
+ describe('Basic arithmetics operators', function () {
+ it('can be added (concatenation)', function () {
+ expect(py.eval('"foo" + "bar"')).to.be('foobar');
+ });
+ });
+ });
}); \ No newline at end of file