Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Dec 26, 2015
1 parent 85277a7 commit 4b49450
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
64 changes: 64 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,67 @@ from undefined import Undefined

I tend to be torn between lowercase, for simplicity, and Uppercase.


# Why not `None`, difference with `None`

`undefined` is likely slower, and as it is a regular Python object there are a few on purpose (or not difference).

### Unlike `None`, you can assign to it

```
>>> None = 3
SyntaxError: can't assign to keyword
```

```
>>> undefined = 3
>>> undefned
3
```

### Unlike `None`, `undefined` is mutable

```
>>> undefined.value = 42
>>> undefined.value
42
```

(might be able to fix that with `__get_attr__`

### Unlike `None`, `undefined` is neither true not false.

If you test for boolean value of `undefind` if will raise.
That is to say: the following will fail:

```
value = undefined
if value:
pass # will raise before reaching here.
```

You have to check for identity:

```
value = undefined
other = 1
if value is undefined:
pass # will execute
```

for info, undefined is not `True`,`False`, not undefined with respect to identity

```
>>> undefined is True
False
>>> undefined is False
False
>>>: undefined is None
False
```

### String form

`str(undefined)` raises. `repr(undefined)` is the unicode string `'Undefined'`
5 changes: 3 additions & 2 deletions undefined/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def __repr__(self):
return self.__class__.__name__

def __bool__(self):
return False
raise NotImplementedError('Undefined is not defined, neither True, nor False.')

__str__ = __repr__
def __str__(self):
raise NotImplementedError("Cannot represent undefined !")


Undefined.__name__ = 'Undefined'
Expand Down
17 changes: 17 additions & 0 deletions undefined/tests/test_undefined.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import undefined as ud
import undefined
from undefined import Undefined as uc


def test_undefined():
assert ud is uc



import unittest

class TestUndefined(unittest.TestCase):


def test_bool(self):
with self.assertRaises(NotImplementedError):
if undefined:
pass

def test_str(self):
with self.assertRaises(NotImplementedError):
'%s' % undefined

0 comments on commit 4b49450

Please sign in to comment.