From 739f719ad6f26c007deb6b48134734588538bcbe Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 27 Feb 2012 08:37:21 +0100 Subject: [UP] py.parse 0.2 to py.js 0.4 ~ should probably go through a testing phase (checking its result against that of eval_domain_and_context) for a while, but apart from that it's supposed to work. 'stdlib' stuff (datetime.date.today, datetime.timedelta, time.localtime, time.time, time.strftime) still need to be written. Isn't there also a dateutil.relativedeleta somewhere? Anyway that needs to be done and the various evaluation contexts need to be defined, but the valuator itself should mostly work. bzr revid: xmo@openerp.com-20120227073721-nkgeiqacbzch8xev --- addons/web/static/lib/py.js/test/parser.js | 96 ++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 addons/web/static/lib/py.js/test/parser.js (limited to 'addons/web/static/lib/py.js/test/parser.js') diff --git a/addons/web/static/lib/py.js/test/parser.js b/addons/web/static/lib/py.js/test/parser.js new file mode 100644 index 00000000000..16f333f78ac --- /dev/null +++ b/addons/web/static/lib/py.js/test/parser.js @@ -0,0 +1,96 @@ +var py = require('../lib/py.js'), + expect = require('expect.js'); + +expect.Assertion.prototype.tokens = function (n) { + var length = this.obj.length; + this.assert(length === n + 1, + 'expected ' + this.obj + ' to have ' + n + ' tokens', + 'expected ' + this.obj + ' to not have ' + n + ' tokens'); + this.assert(this.obj[length-1].id === '(end)', + 'expected ' + this.obj + ' to have and end token', + 'expected ' + this.obj + ' to not have an end token'); +}; + +expect.Assertion.prototype.constant = function (value) { + this.assert(this.obj.id === '(constant)', + 'expected ' + this.obj + ' to be a constant token', + 'expected ' + this.obj + ' not to be a constant 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.number = function (value) { + this.assert(this.obj.id === '(number)', + 'expected ' + this.obj + ' to be a number token', + 'expected ' + this.obj + ' not to be a number 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.string = function (value) { + this.assert(this.obj.id === '(string)', + 'expected ' + this.obj + ' to be a string token', + 'expected ' + this.obj + ' not to be a string token'); + this.assert(this.obj.value === value, + 'expected ' + this.obj + ' to have tokenized ' + value, + 'expected ' + this.obj + ' not to have tokenized ' + value); +}; + +describe('Tokenizer', function () { + describe('simple literals', function () { + it('tokenizes numbers', function () { + var toks = py.tokenize('1'); + expect(toks).to.have.tokens(1); + expect(toks[0]).to.be.number(1); + + var toks = py.tokenize('-1'); + expect(toks).to.have.tokens(2); + expect(toks[0].id).to.be('-'); + expect(toks[1]).to.be.number(1); + + var toks = py.tokenize('1.2'); + expect(toks).to.have.tokens(1); + expect(toks[0]).to.be.number(1.2); + + var toks = py.tokenize('.42'); + expect(toks).to.have.tokens(1); + expect(toks[0]).to.be.number(0.42); + }); + it('tokenizes strings', function () { + var toks = py.tokenize('"foo"'); + expect(toks).to.have.tokens(1); + expect(toks[0]).to.be.string('foo'); + + var toks = py.tokenize("'foo'"); + expect(toks).to.have.tokens(1); + expect(toks[0]).to.be.string('foo'); + }); + it('tokenizes bare names', function () { + var toks = py.tokenize('foo'); + expect(toks).to.have.tokens(1); + expect(toks[0].id).to.be('(name)'); + expect(toks[0].value).to.be('foo'); + }); + it('tokenizes constants', function () { + var toks = py.tokenize('None'); + expect(toks).to.have.tokens(1); + expect(toks[0]).to.be.constant('None'); + + var toks = py.tokenize('True'); + expect(toks).to.have.tokens(1); + expect(toks[0]).to.be.constant('True'); + + var toks = py.tokenize('False'); + expect(toks).to.have.tokens(1); + expect(toks[0]).to.be.constant('False'); + }); + }); + describe('collections', function () { + it('tokenizes opening and closing symbols', function () { + var toks = py.tokenize('()'); + expect(toks).to.have.tokens(2); + expect(toks[0].id).to.be('('); + expect(toks[1].id).to.be(')'); + }); + }); +}); \ No newline at end of file -- cgit v1.2.3 From 4a6776d7a11f6c2004358c614411bd662dca2ad6 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 1 Mar 2012 12:18:07 +0100 Subject: [FIX] update py.js, fixes tokenization of expressions with trailing whitespace bzr revid: xmo@openerp.com-20120301111807-f3lthxw4zkytk42f --- addons/web/static/lib/py.js/test/parser.js | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'addons/web/static/lib/py.js/test/parser.js') diff --git a/addons/web/static/lib/py.js/test/parser.js b/addons/web/static/lib/py.js/test/parser.js index 16f333f78ac..ca74d2bebc8 100644 --- a/addons/web/static/lib/py.js/test/parser.js +++ b/addons/web/static/lib/py.js/test/parser.js @@ -84,6 +84,11 @@ describe('Tokenizer', function () { expect(toks).to.have.tokens(1); expect(toks[0]).to.be.constant('False'); }); + it('does not fuck up on trailing spaces', function () { + var toks = py.tokenize('None '); + expect(toks).to.have.tokens(1); + expect(toks[0]).to.be.constant('None'); + }); }); describe('collections', function () { it('tokenizes opening and closing symbols', function () { -- cgit v1.2.3 From f8a93bc2848eba516213142f4f730a1feda5a39e Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 5 Mar 2012 09:55:32 +0100 Subject: [FIX] update py.js for operators &al, implement basic crummy version of relativedelta bzr revid: xmo@openerp.com-20120305085532-0vcz6j0m985gjuz1 --- addons/web/static/lib/py.js/test/parser.js | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'addons/web/static/lib/py.js/test/parser.js') 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 -- cgit v1.2.3