Commits

masklinn committed bead3db

Add tests for a few literals, implement dict literals

Comments (0)

Files changed (2)

                 }
             }
             return false;
+        },
+        toJSON: function () {
+            return this.values;
         }
     });
     exports.list = exports.tuple;
+    exports.dict = create(exports.object, {
+        toJSON: function () {
+            return this.values;
+        }
+    });
 
     exports.parse = function (toks) {
         var index = 0;
                     list_exprs[k], context));
             }
             return create(exports.list, {values: list_values});
+        case '{':
+            var dict_exprs = expr.first, dict_values = {};
+            for(var l=0; l<dict_exprs.length; ++l) {
+                dict_values[exports.evaluate(dict_exprs[l][0], context)] =
+                    exports.evaluate(dict_exprs[l][1], context);
+            }
+            return create(exports.dict, {values: dict_values});
         case '.':
             if (expr.second.id !== '(name)') {
                 throw new Error('SyntaxError: ' + expr);
 var py = require('../lib/py.js'),
     assert = require('assert');
 
+// Literals
+assert.strictEqual(py.eval('1'), 1);
+assert.strictEqual(py.eval('None'), null);
+assert.strictEqual(py.eval('False'), false);
+assert.strictEqual(py.eval('True'), true);
+assert.strictEqual(py.eval('"somestring"'), 'somestring');
+assert.strictEqual(py.eval("'somestring'"), 'somestring');
+assert.deepEqual(py.eval("()").toJSON(), []);
+assert.deepEqual(py.eval("[]").toJSON(), []);
+assert.deepEqual(py.eval("{}").toJSON(), {});
+assert.deepEqual(py.eval("(None, True, False, 0, 1, 'foo')").toJSON(),
+                 [null, true, false, 0, 1, 'foo']);
+assert.deepEqual(py.eval("[None, True, False, 0, 1, 'foo']").toJSON(),
+                 [null, true, false, 0, 1, 'foo']);
+assert.deepEqual(py.eval("{'foo': 1, foo: 2}", {foo: 'bar'}).toJSON(),
+                 {foo: 1, bar: 2});
+
 // Equality tests
 assert.ok(py.eval(
     "foo == 'foo'", {foo: 'foo'}));