Anonymous avatar Anonymous committed ae35800

[svn] * Sync'd number helper, added number unit tests.

Comments (0)

Files changed (3)

 WebHelpers ChangeLog
 
 0.2 (**svn**)
+* Sync'd number helper, added number unit tests.
 * Added markdown.py (python-markdown) for new markdown support in text helper.
 * Added textile.py (PyTextile) for new textilize support in text helper.
 * Brought asset/date/text helpers up to date with revision info.

tests/test_number.py

+from unittest import TestCase
+import unittest
+
+from webhelpers.rails.number import *
+
+class TestTextHelper(TestCase):
+    def test_number_to_phone(self):
+        self.assertEqual("123-555-1234", number_to_phone(1235551234))
+        self.assertEqual("(123) 555-1234", number_to_phone(1235551234, area_code=True))
+        self.assertEqual("123 555 1234", number_to_phone(1235551234, delimiter=" "))
+        self.assertEqual("(123) 555-1234 x 555", number_to_phone(1235551234, area_code=True, extension=555))
+        self.assertEqual("123-555-1234", number_to_phone(1235551234, extension="   "))
+
+    def test_number_to_currency(self):
+        self.assertEqual("$1,234,567,890.50", number_to_currency(1234567890.50))
+        self.assertEqual("$1,234,567,890.51", number_to_currency(1234567890.506))
+        self.assertEqual("$1,234,567,890", number_to_currency(1234567890.50, precision=0))
+        self.assertEqual("$1,234,567,890.5", number_to_currency(1234567890.50, precision=1))
+        self.assertEqual("£1234567890,50", number_to_currency(1234567890.50, unit="£", separator=",", delimiter=""))
+
+    def test_number_to_percentage(self):
+        self.assertEqual("100.000%", number_to_percentage(100))
+        self.assertEqual("100%", number_to_percentage(100, precision=0))
+        self.assertEqual("302.06%", number_to_percentage(302.0574, precision=2))
+
+    def test_number_with_delimiter(self):
+        self.assertEqual("12,345,678", number_with_delimiter(12345678))
+
+    def test_number_to_human_size(self):
+        self.assertEqual('0 Bytes', human_size(0))
+        self.assertEqual('1 Byte', human_size(1))
+        self.assertEqual('3 Bytes', human_size(3.14159265))
+        self.assertEqual('123 Bytes', human_size(123.0))
+        self.assertEqual('123 Bytes', human_size(123))
+        self.assertEqual('1.2 KB', human_size(1234))
+        self.assertEqual('12.1 KB', human_size(12345))
+        self.assertEqual('1.2 MB', human_size(1234567))
+        self.assertEqual('1.1 GB', human_size(1234567890))
+        self.assertEqual('1.1 TB', human_size(1234567890123))
+        self.assertEqual('', human_size('x'))
+        self.assertEqual('', human_size(''))
+
+    def test_number_with_precision(self):
+        self.assertEqual("111.235", number_with_precision(111.2346))

webhelpers/rails/number.py

 """
 Number Helpers
 """
+# Last synced with Rails copy at Revision 4537 on Aug 19th, 2006.
 import re
 
 def number_to_phone(number, area_code=False, delimiter="-", extension=""):
         number = re.sub(r'([0-9]{3})([0-9]{3})([0-9]{4})', r'(\1) \2%s\3' % delimiter, str(number))
     else:
         number = re.sub(r'([0-9]{3})([0-9]{3})([0-9]{4})', r'\1%s\2%s\3' % (delimiter, delimiter), str(number))
-    if extension:
+    if extension and str(extension).strip():
         number += " x %s" % extension
     return number
 
     if precision < 1:
         separator = ""
     parts = number_with_precision(number, precision).split('.')
-    return unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1]
+    num = unit + number_with_delimiter(parts[0], delimiter)
+    if len(parts) > 1:
+        num += separator + parts[1]
+    return num
 
 def number_to_percentage(number, precision=3, separator="."):
     """
         >>> number_to_human_size(1234567890)
         1.1 GB
     """
-    if size < 1024:
+    if size == 1:
+        return "1 Byte"
+    elif size < 1024:
         return "%d Bytes" % size
     elif size < (1024**2):
         return "%.1f KB" % (size / 1024.00)
     else:
         return ""
 
+human_size = number_to_human_size
+
 def number_with_delimiter(number, delimiter=","):
     """
     Formats a ``number`` with a ``delimiter``.
     return formstr % number
 
 __all__ = ['number_to_phone', 'number_to_currency', 'number_to_percentage','number_with_delimiter', 
-           'number_with_precision', 'number_to_human_size']
+           'number_with_precision', 'number_to_human_size', 'human_size']
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.