masklinn avatar masklinn committed 49911fa

Implement conversion of booleans to string

also move bool-related tests in their own file

Comments (0)

Files changed (4)

             }
             return args.value.__nonzero__() === py.True ? py.True : py.False;
         },
+        __str__: function () {
+            return py.str.fromJSON((this === py.True) ? "True" : "False");
+        },
         __nonzero__: function () { return this; },
+        fromJSON: function (val) { return val ? py.True : py.False },
         toJSON: function () { return this === py.True; }
     });
     py.True = py.PY_call(py.bool);

test/builtins/bool.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.bool', function () {
+    describe('JS conversions', function () {
+        it('should round-trip', function () {
+            expect(py.bool.fromJSON(true).toJSON()).to.be(true);
+            expect(py.bool.fromJSON(false).toJSON()).to.be(false);
+        });
+    });
+    describe('literal syntax', function () {
+        it('should have the right type', function () {
+            expect(ev('False').__class__).to.be(py.bool);
+            expect(ev('True').__class__).to.be(py.bool);
+        });
+        it('should yield the corresponding JS value', function () {
+            expect(py.eval('False')).to.be(false);
+            expect(py.eval('True')).to.be(true);
+        });
+    });
+    describe('conversions from other types', function () {
+        it('should default to true', function () {
+            expect(py.eval('bool(t())', {t: py.type('t', null, {})}))
+                .to.be(true);
+        });
+        it('should use __nonzero__', function () {
+            var t = py.type('t', null, {
+                __nonzero__: function () {
+                    return py.False;
+                }
+            });
+            expect(py.eval('bool(t())', {t: t}))
+                .to.be(false);
+        });
+    });
+    describe('Python protocols', function () {
+        describe('boolean', function () {
+            it('should id', function () {
+                expect(py.eval('bool(True)'))
+                    .to.be(true);
+                expect(py.eval('bool(False)'))
+                    .to.be(false);
+            });
+        });
+        describe('str', function () {
+            it('should return its literal', function () {
+                expect(py.eval('str(True)'))
+                    .to.be("True");
+                expect(py.eval('str(False)'))
+                    .to.be("False");
+            });
+        });
+    });
+});

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, {});
                 /^TypeError: __str__ returned non-string \(type string\)/);
     });
 });
+describe('Python protocols', function () {
+    describe('boolean', function () {
+        it('should be true if non-empty', function () {
+            expect(py.eval('bool("2008")'))
+                .to.be(true);
+        });
+        it('should be false if empty', function () {
+            expect(py.eval('bool("")')) .to.be(false);
+        });
+    });
+    describe('str', function () {
+        it('should be an identity', function () {
+            expect(py.eval('str("foo")'))
+                .to.be("foo");
+        });
+    });
+});
 };
 
 describe('Literals', function () {
-    describe('Booleans', function () {
-        it('should have the right type', function () {
-            expect(ev('False').__class__).to.be(py.bool);
-            expect(ev('True').__class__).to.be(py.bool);
-        });
-        it('should yield the corresponding JS value', function () {
-            expect(py.eval('False')).to.be(false);
-            expect(py.eval('True')).to.be(true);
-        });
-    });
     describe('None', function () {
         it('should have the right type', function () {
             expect(ev('isinstance(None, object)')).to.be(py.True)
         });
     });
 });
-describe('Conversions', function () {
-    describe('to bool', function () {
-        describe('strings', function () {
-            it('should be true if non-empty', function () {
-                expect(py.eval('bool(date_deadline)',
-                               {date_deadline: '2008'}))
-                    .to.be(true);
-            });
-            it('should be false if empty', function () {
-                expect(py.eval('bool(s)', {s: ''})) .to.be(false);
-            });
-        });
-    });
-});
 describe('Attribute access', function () {
     it("should return the attribute's value", function () {
         var o = py.PY_call(py.object);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.