Commits

masklinn  committed b2ec3f3

Implement conversion of floats to string

also move float-related tests in their own file

  • Participants
  • Parent commits be5482a

Comments (0)

Files changed (4)

             }
             throw new Error('TypeError: float() argument must be a string or a number');
         },
+        __str__: function () {
+            return py.str.fromJSON(String(this._value));
+        },
         __eq__: function (other) {
             return this._value === other._value ? py.True : py.False;
         },
             var args = py.PY_parseArgs(arguments, [['value', placeholder]]);
             var s = args.value;
             if (s === placeholder) { this._value = ''; return; }
-            if (py.PY_call(py.isinstance, [s, py.str]) === py.True) {
-                this._value = s._value;
-                return;
-            }
             var v = s.__str__();
             if (py.PY_call(py.isinstance, [v, py.str]) === py.True) {
                 this._value = v._value;
         __hash__: function () {
             return '\1\0\1' + this._value;
         },
+        __str__: function () {
+            return this;
+        },
         __eq__: function (other) {
             if (py.PY_call(py.isinstance, [other, py.str]) === py.True
                     && this._value === other._value) {

File test/builtins/float.js

+var py = require('../../lib/py.js'),
+    expect = require('expect.js');
+var ev = function (str, context) {
+    return py.evaluate(py.parse(py.tokenize(str)), context);
+};
+
+describe('py.float', function () {
+    describe('literal syntax', function () {
+        it('should have the right type', function () {
+            expect(ev('1').__class__).to.be(py.float);
+        });
+        it('should yield the corresponding JS value', function () {
+            expect(py.eval('1')).to.be(1);
+            expect(py.eval('42')).to.be(42);
+            expect(py.eval('9999')).to.be(9999);
+        });
+        it('should correctly handle negative literals', function () {
+            expect(py.eval('-1')).to.be(-1);
+            expect(py.eval('-42')).to.be(-42);
+            expect(py.eval('-9999')).to.be(-9999);
+        });
+        it('should correctly handle float literals', function () {
+            expect(py.eval('.42')).to.be(0.42);
+            expect(py.eval('1.2')).to.be(1.2);
+        });
+    });
+    it('should roundrip', function () {
+        expect(py.float.fromJSON(2.42).toJSON())
+            .to.be(2.42);
+    });
+    it('should stringify', function () {
+        expect(py.eval('str(3.42)'))
+            .to.be('3.42');
+    });
+    describe('numerical protocol', 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);
+        });
+    });
+});

File test/builtins/str.js

     });
 });
 describe('Convert other py types', function () {
+    it('should be an identity for strings', function () {
+        expect(py.eval('str("foo")'))
+            .to.be("foo");
+    });
     describe('No __str__ defined', function () {
         it("should use object's", function () {
             var t = py.type('t', null, {});

File test/test.js

 };
 
 describe('Literals', function () {
-    describe('Number', function () {
-        it('should have the right type', function () {
-            expect(ev('1').__class__).to.be(py.float);
-        });
-        it('should yield the corresponding JS value', function () {
-            expect(py.eval('1')).to.be(1);
-            expect(py.eval('42')).to.be(42);
-            expect(py.eval('9999')).to.be(9999);
-        });
-        it('should correctly handle negative literals', function () {
-            expect(py.eval('-1')).to.be(-1);
-            expect(py.eval('-42')).to.be(-42);
-            expect(py.eval('-9999')).to.be(-9999);
-        });
-        it('should correctly handle float literals', function () {
-            expect(py.eval('.42')).to.be(0.42);
-            expect(py.eval('1.2')).to.be(1.2);
-        });
-    });
     describe('Booleans', function () {
         it('should have the right type', function () {
             expect(ev('False').__class__).to.be(py.bool);
 });
 
 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 () {