Revision: a73344efe427
Branch:   default
Author:   Robot Framework Developers ([email protected])
Date:     Mon Nov 25 14:07:31 2013 UTC
Log:      Tuning BuiltIn.Convert To Bytes.

Update issue 1581
Implemented these changes:
- Integers must be separated by whitespace.
- Integers can have 0x, 0o, and 0b prefix.
- Binary and hex can use whitespace freely.
- Binary and hex must pad values with zeros.

In a hurry to leave this machine and code/tests may still need some cleanup.
Also docs must be updated. May also conside adding support for passing input
as a list/iterable directly.
http://code.google.com/p/robotframework/source/detail?r=a73344efe427

Modified:
 /atest/robot/standard_libraries/builtin/convert_to_bytes.txt
 /atest/testdata/standard_libraries/builtin/convert_to_bytes.txt
 /src/robot/libraries/BuiltIn.py

=======================================
--- /atest/robot/standard_libraries/builtin/convert_to_bytes.txt Mon Nov 25 08:30:59 2013 UTC +++ /atest/robot/standard_libraries/builtin/convert_to_bytes.txt Mon Nov 25 14:07:31 2013 UTC
@@ -25,11 +25,14 @@
 Integers
     Check Test Case    ${TESTNAME}

-Integers without separator works
+Any whitespace as integer separator
     Check Test Case    ${TESTNAME}

-Integers without separator requires input to be multiple of 3
+Integers with prefixes
     Check Test Case    ${TESTNAME}
+
+Integers without separators does not work
+    Check Test Case    ${TESTNAME}

 Too big integers fail
     Check Test Case    ${TESTNAME}
@@ -37,35 +40,26 @@
 Invalid integers fail
     Check Test Case    ${TESTNAME}

-Hex
+Hex without whitespace
     Check Test Case    ${TESTNAME}

-Hex without separator works
+Hex with whitespace
     Check Test Case    ${TESTNAME}

-Hex without separator requires even input
-    Check Test Case    ${TESTNAME}
-
-Too big hex fail
+Hex requires even input
     Check Test Case    ${TESTNAME}

 Invalid hex fail
     Check Test Case    ${TESTNAME}

-Binary
-    Check Test Case    ${TESTNAME}
-
-Binary without separator works
+Binary without spaces
     Check Test Case    ${TESTNAME}

-Binary without separator requires input to be multiple of 8
+Binary with whitespace
     Check Test Case    ${TESTNAME}

-Too big binary fail
+Binary requires input to be multiple of 8
     Check Test Case    ${TESTNAME}

 Invalid binary fail
     Check Test Case    ${TESTNAME}
-
-Any whitesapce works as separator
-    Check Test Case    ${TESTNAME}
=======================================
--- /atest/testdata/standard_libraries/builtin/convert_to_bytes.txt Mon Nov 25 08:30:59 2013 UTC +++ /atest/testdata/standard_libraries/builtin/convert_to_bytes.txt Mon Nov 25 14:07:31 2013 UTC
@@ -41,73 +41,72 @@
     104 101 108 108 111    int    104, 101, 108, 108, 111
     0 1 2 3 4              int    0, 1, 2, 3, 4

-Integers without separator works
-    000001007    int    0, 1, 7
-    123255       int    123, 255
+Any whitespace as integer separator
+    0\t255                 int    0, 255
+    0\n1${SPACE*5}2\r33    int    0, 1, 2, 33

-Integers without separator requires input to be multiple of 3
+Integers with prefixes
+    0b111 0B0010           int    7, 2
+    0o111 0O0010           int    73, 8
+    0xF 0X0f               int    15, 15
+    0b10 0o10 10 0x10      int    2, 8, 10, 16
+    0b11111111 0o377 0xff  int    255, 255, 255
+
+Integers without separators does not work
     [Template]    Creating bytes should fail
-    1234       int    Expected input to be multiple of 3.
-    1234457    int    Expected input to be multiple of 3.
+ 001007 int Integer '001007' is too big to be represented as a byte.

 Too big integers fail
     [Template]    Creating bytes should fail
- 1000 1 int Integer '1000' in base 10 is too big to be represented as a byte. - 0 256 int Integer '256' in base 10 is too big to be represented as a byte.
+    256 1      int    Integer '256' is too big to be represented as a byte.
+ 0 0xfff int Integer '0xfff' is too big to be represented as a byte.

 Invalid integers fail
     [Template]    Creating bytes should fail
-    hello 1    int    Cannot convert 'hello' to an integer in base 10.
-    0 1 a      int    Cannot convert 'a' to an integer in base 10.
+ hello 1 int 'hello' cannot be converted to an integer: ValueError: * + 0 0xa 0ba int '0ba' cannot be converted to an integer: ValueError: *

-Hex
-    a                 hex    10
-    aA                hex    170
-    68 65 6c 6C 6f    hex    104, 101, 108, 108, 111
-    0 1 2 ff          hex    0, 1, 2, 255
+Hex without whitespace
+    0a                 hex    10
+    aBbA               hex    171, 186
+    000102ff           hex    0, 1, 2, 255
+    68656c6C6f         hex    104, 101, 108, 108, 111

-Hex without separator works
-    68656c6C6f    hex    104, 101, 108, 108, 111
-    000102FF      hex    0, 1, 2, 255
+Hex with whitespace
+    68 65 6c 6C 6f     hex    104, 101, 108, 108, 111
+    6865 6c6C 6f       hex    104, 101, 108, 108, 111
+    6 8 6 5 6 c 6 C    hex    104, 101, 108, 108
+    01\t02\n\r03       hex    1, 2, 3
+    f${SPACE*10}f      hex    255

-Hex without separator requires even input
-    [Template]    Creating bytes should fail
-    abc    hex    Expected input to be multiple of 2.
-
-Too big hex fail
+Hex requires even input
     [Template]    Creating bytes should fail
- ffF f hex Integer 'ffF' in base 16 is too big to be represented as a byte. - 0 100 hex Integer '100' in base 16 is too big to be represented as a byte.
+    abc     hex    Expected input to be multiple of 2.
+    ab c    hex    Expected input to be multiple of 2.

 Invalid hex fail
     [Template]    Creating bytes should fail
-    ff hello    hex     Cannot convert 'hello' to an integer in base 16.
-Binary
-    0                                bin   0
-    0 1 10 11 100 101 110 111        bin   0, 1, 2, 3, 4, 5, 6, 7
-    00000000 00001111 11111111       bin   0, 15, 255
-    00 01 10 11 100                  bin   0, 1, 2, 3, 4
+ ff hello! hex 'he' cannot be converted to an integer: ValueError: *
+
+Binary without spaces
+    00000000                     bin   0
+    0000000000000001             bin   0, 1
+    000000110000111111111111     bin   3, 15, 255

-Binary without separator works
-    000000001111111101111111    bin    0, 255, 127
+Binary with whitespace
+    0000 0000                    bin   0
+    00 00 00 00 00 00 00 01      bin   0, 1
+    0 0 0 0 0 0 1 1 0000 1111    bin   3, 15

-Binary without separator requires input to be multiple of 8
+Binary requires input to be multiple of 8
     [Template]    Creating bytes should fail
     00000000111111110    bin    Expected input to be multiple of 8.
-
-Too big binary fail
-    [Template]    Creating bytes should fail
- 1 100000000 bin Integer '100000000' in base 2 is too big to be represented as a byte.
+    0000 0000 1111       bin    Expected input to be multiple of 8.

 Invalid binary fail
     [Template]    Creating bytes should fail
-    hello    bin    Cannot convert 'hello' to an integer in base 2.
-    0 2      bin    Cannot convert '2' to an integer in base 2.
-
-Any whitesapce works as separator
-    0\t1\n2\r3       int    0, 1, 2, 3
-    a${SPACE*10}b    hex    10, 11
-    0\t\t\t1         bin    0, 1
+ hello!!! bin 'hello!!!' cannot be converted to an integer: ValueError: * + 00000002 bin '00000002' cannot be converted to an integer: ValueError: *

 *** Keywords ***
 Correct bytes should be created
=======================================
--- /src/robot/libraries/BuiltIn.py     Mon Nov 25 08:30:59 2013 UTC
+++ /src/robot/libraries/BuiltIn.py     Mon Nov 25 14:07:31 2013 UTC
@@ -330,7 +330,7 @@
             except AttributeError:
                 raise DataError("Invalid input type '%s'." % input_type)
             return ''.join(chr(o) for o in ordinals(input))
-        except DataError, err:
+        except (DataError, RuntimeError), err:
             raise RuntimeError("Creating bytes failed: %s" % unicode(err))

     def _get_ordinals_from_text(self, input):
@@ -341,32 +341,27 @@
"represented as a byte." % (char, hex(ordinal)))
             yield ordinal

-    def _get_ordinals_from_int(self, input, base=10, token_length=3):
-        for token in self._split_to_tokens(input, token_length):
-            try:
-                ordinal = int(token, base)
-            except ValueError:
- raise DataError("Cannot convert '%s' to an integer in base %d."
-                                % (token, base))
+    def _get_ordinals_from_int(self, input):
+        for token in input.split():
+            ordinal = self._convert_to_integer(token)
             if ordinal > 255:
-                raise DataError("Integer '%s' in base %d is too big to be "
-                                "represented as a byte." % (token, base))
+ raise DataError("Integer '%s' is too big to be represented "
+                                "as a byte." % token)
             yield ordinal

     def _get_ordinals_from_hex(self, input):
-        return self._get_ordinals_from_int(input, base=16, token_length=2)
+        return self._get_ordinals(input, base=16, token_length=2)

     def _get_ordinals_from_bin(self, input):
-        return self._get_ordinals_from_int(input, base=2, token_length=8)
+        return self._get_ordinals(input, base=2, token_length=8)

-    def _split_to_tokens(self, input, token_length):
-        tokens = input.split()
-        length = len(input)
-        if len(tokens) > 1 or length < token_length:
-            return tokens
-        if length % token_length != 0:
+    def _get_ordinals(self, input, base, token_length):
+        input = ''.join(input.split())
+        if len(input) % token_length != 0:
raise DataError('Expected input to be multiple of %d.' % token_length) - return (input[i:i+token_length] for i in xrange(0, length, token_length))
+        for token in (input[i:i+token_length]
+                      for i in xrange(0, len(input), token_length)):
+            yield self._convert_to_integer(token, base)

     def create_list(self, *items):
         """Returns a list containing given items.

--

--- You received this message because you are subscribed to the Google Groups "robotframework-commit" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to