2 new revisions:

Revision: 0c038961bb1c
Author:   Pekka Klärck
Date:     Mon May 16 06:14:32 2011
Log:      Update issue 109...
http://code.google.com/p/robotframework/source/detail?r=0c038961bb1c

Revision: bf1155b804b6
Author:   Pekka Klärck
Date:     Mon May 16 06:26:39 2011
Log: Converted tests to use Template to make them easier to read. Didn't do...
http://code.google.com/p/robotframework/source/detail?r=bf1155b804b6

==============================================================================
Revision: 0c038961bb1c
Author:   Pekka Klärck
Date:     Mon May 16 06:14:32 2011
Log:      Update issue 109
Status: Review
Cc: janne.t.harkonen

Implemented precision support for `Should (Not) Be Equal As Numbers`.
Implementation was trivial now that `Convert To Number` already supported
precision argument.

Writing docs was the biggest task and it would be great if Janne could
review them (incl. `Convert To Number`). Otherwise I consider this
issue done.
http://code.google.com/p/robotframework/source/detail?r=0c038961bb1c

Modified:
 /atest/robot/standard_libraries/builtin/verify.txt
 /atest/testdata/standard_libraries/builtin/verify.txt
 /doc/libraries/BuiltIn.html
 /src/robot/libraries/BuiltIn.py

=======================================
--- /atest/robot/standard_libraries/builtin/verify.txt Thu May 5 16:04:57 2011 +++ /atest/robot/standard_libraries/builtin/verify.txt Mon May 16 06:14:32 2011
@@ -62,10 +62,16 @@
 Should Not Be Equal As Numbers
     ${tc}=  Check test case  ${TESTNAME}
     Verify argument type message  ${tc.kws[0].msgs[0]}  unicode  unicode
+
+Should Not Be Equal As Numbers With Precision
+    Check test case  ${TESTNAME}

 Should Be Equal As Numbers
     ${tc}=  Check test case  ${TESTNAME}
     Verify argument type message  ${tc.kws[0].msgs[0]}  unicode  unicode
+
+Should Be Equal As Numbers With Precision
+    Check test case  ${TESTNAME}

 Should Not Be Equal As Strings
     ${tc}=  Check test case  ${TESTNAME}
=======================================
--- /atest/testdata/standard_libraries/builtin/verify.txt Sat May 7 03:15:37 2011 +++ /atest/testdata/standard_libraries/builtin/verify.txt Mon May 16 06:14:32 2011
@@ -96,11 +96,31 @@
     Should Not Be Equal As Numbers  ${STR1}.${STR1}  1.2
     Should Not Be Equal As Numbers  ${STR1}.${STR1}  1.1  Fails again

+Should Not Be Equal As Numbers With Precision
+    [Documentation]  FAIL  Failing: 1.0 == 1.0
+    [Template]  Should Not Be Equal As Numbers
+    1.123     1.456    precision=1
+    1.123     ${1.1}   precision=2
+    ${1.123}  ${1.12}  precision=3
+    1123      1456     precision=-2
+    112       145      precision=-1
+    1.12      1.45     Failing  precision=0
+
 Should Be Equal As Numbers
     [Documentation]  FAIL Only this message again
     Should Be Equal As Numbers  ${STR1}  ${STR1}.${STR0}${STR0}
Should Be Equal As Numbers ${STR1}.000001 ${STR1}.${STR0}${STR0}${STR1} Only this message again False

+Should Be Equal As Numbers With Precision
+    [Documentation]  FAIL  Failure: 110.0 != 150.0
+    [Template]  Should Be Equal As Numbers
+    1.123     1.456    precision=0
+    1.123     ${1.1}   precision=1
+    ${1.123}  ${1.12}  precision=2
+    1123      1456     precision=-3
+    112       145      precision=-2
+    112       145      Failure  precision=-1
+
 Should Not Be Equal As Strings
     [Documentation]  FAIL These strings most certainly should not be equal
     Should Not Be Equal As Strings  1  ${1.1}
=======================================
--- /doc/libraries/BuiltIn.html Fri May  6 00:54:42 2011
+++ /doc/libraries/BuiltIn.html Mon May 16 06:14:32 2011
@@ -367,11 +367,13 @@
   <td class="arg">item, base=None</td>
   <td class="doc">Converts the given item to an integer number.<br />
 <br />
-If the item is a string, it is by default expected to be an integer in base 10. Starting from Robot Framework 2.6 there are two ways to convert from other bases:<br /> +If the given item is a string, it is by default expected to be an integer in base 10. Starting from Robot Framework 2.6 there are two ways to convert from other bases:<br />
 <br />
1) Give base explicitly to the keyword as <span class="name">base</span> argument.<br />
 <br />
-2) Prefix the given string with the base so that <span class="name">0b</span> means base 2 (binary), <span class="name">0o</span> means base 8 (octal), and <span class="name">0x</span> means base 16 (hex). In this case possible <span class="name">base</span> argument is ignored.<br /> +2) Prefix the given string with the base so that <span class="name">0b</span> means base 2 (binary), <span class="name">0o</span> means base 8 (octal), and <span class="name">0x</span> means base 16 (hex). The prefix is considered only when <span class="name">base</span> argument is not given.<br />
+<br />
+The syntax is case-insensitive and possible spaces are ignored.<br />
 <br />
 Examples:<br />
 <table border="1" class="doc">
@@ -383,6 +385,13 @@
 <td># Result is 100</td>
 </tr>
 <tr>
+<td>${result} =</td>
+<td>Convert To Integer</td>
+<td>FF AA</td>
+<td>16</td>
+<td># Result is 65450</td>
+</tr>
+<tr>
 <td>${result} =</td>
 <td>Convert To Integer</td>
 <td>100</td>
@@ -411,12 +420,51 @@
 <td># Result is 256</td>
 </tr>
 </table>
-</td>
+<br />
+If you need a floating point number, use <a href="#Convert To Number" class="name">Convert To Number</a> instead.</td>
 </tr>
 <tr>
   <td class="kw"><a name="Convert To Number"></a>Convert To Number</td>
-  <td class="arg">item</td>
-  <td class="doc">Converts the given item to a floating point number.</td>
+  <td class="arg">item, precision=None</td>
+  <td class="doc">Converts the given item to a floating point number.<br />
+<br />
+If the optional <span class="name">precision</span> is positive or zero, the returned number is rounded to that number of decimal digits. Negative precision means that the number is rounded to the closest multiple of 10 to the power of the absolute precision. The support for precision was added in Robot Framework 2.6.<br />
+<br />
+Examples:<br />
+<table border="1" class="doc">
+<tr>
+<td>${result} =</td>
+<td>Convert To Number</td>
+<td>42.512</td>
+<td></td>
+<td># Result is 42.512</td>
+</tr>
+<tr>
+<td>${result} =</td>
+<td>Convert To Number</td>
+<td>42.512</td>
+<td>1</td>
+<td># Result is 42.5</td>
+</tr>
+<tr>
+<td>${result} =</td>
+<td>Convert To Number</td>
+<td>42.512</td>
+<td>0</td>
+<td># Result is 43.0</td>
+</tr>
+<tr>
+<td>${result} =</td>
+<td>Convert To Number</td>
+<td>42.512</td>
+<td>-1</td>
+<td># Result is 40.0</td>
+</tr>
+</table>
+<br />
+Notice that machines generally cannot store floating point numbers accurately. This may cause surprises with these numbers in general and also when they are rounded. For more information see, for example, this floating point arithmetic tutorial: <a href="http://docs.python.org/tutorial/floatingpoint.html";>http://docs.python.org/tutorial/floatingpoint.html</a><br />
+<br />
+If you need an integer number, use <a href="#Convert To Integer" class="name">Convert To Integer</a> instead.</td>
 </tr>
 <tr>
   <td class="kw"><a name="Convert To String"></a>Convert To String</td>
@@ -1491,12 +1539,46 @@
 </tr>
 <tr>
<td class="kw"><a name="Should Be Equal As Numbers"></a>Should Be Equal As Numbers</td>
-  <td class="arg">first, second, msg=None, values=True</td>
+  <td class="arg">first, second, msg=None, values=True, precision=6</td>
<td class="doc">Fails if objects are unequal after converting them to real numbers.<br />
 <br />
-The check for equality is done using six decimal places.<br />
+The conversion is done with <a href="#Convert To Number" class="name">Convert To Number</a> keyword using the given <span class="name">precision</span>. The support for giving precision was added in Robot Framework 2.6, in earlier versions it was hard-coded to 6.<br />
 <br />
-See <a href="#Should Be Equal" class="name">Should Be Equal</a> for an explanation on how to override the default error message with <span class="name">msg</span> and <span class="name">values</span>.</td>
+Examples:<br />
+<table border="1" class="doc">
+<tr>
+<td>Should Be Equal As Numbers</td>
+<td>${x}</td>
+<td>1.1</td>
+<td></td>
+<td># Passes if ${x} is 1.1</td>
+</tr>
+<tr>
+<td>Should Be Equal As Numbers</td>
+<td>1.123</td>
+<td>1.1</td>
+<td>precision=1</td>
+<td># Passes</td>
+</tr>
+<tr>
+<td>Should Be Equal As Numbers</td>
+<td>1.123</td>
+<td>1.4</td>
+<td>precision=0</td>
+<td># Passes</td>
+</tr>
+<tr>
+<td>Should Be Equal As Numbers</td>
+<td>112.3</td>
+<td>75</td>
+<td>precision=-2</td>
+<td># Passes</td>
+</tr>
+</table>
+<br />
+As discussed in the documentation of <a href="#Convert To Number" class="name">Convert To Number</a>, machines generally cannot store floating point numbers accurately. Because of this limitation, comparing floats for equality is also problematic and a correct approach to use depends on the context. This keyword uses a very naive approach of rounding the numbers before comparing them which is both prone to rounding errors and does not work very well if numbers are really big or small. For more information about comparing floats, and ideas on how to implement your own context specific comparison algorithm, see this great article: <a href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm";>http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm</a><br />
+<br />
+See <a href="#Should Not Be Equal As Numbers" class="name">Should Not Be Equal As Numbers</a> for a negative version of this keyword and <a href="#Should Be Equal" class="name">Should Be Equal</a> for an explanation on how to override the default error message with <span class="name">msg</span> and <span class="name">values</span>.</td>
 </tr>
 <tr>
<td class="kw"><a name="Should Be Equal As Strings"></a>Should Be Equal As Strings</td>
@@ -1691,12 +1773,12 @@
 </tr>
 <tr>
<td class="kw"><a name="Should Not Be Equal As Numbers"></a>Should Not Be Equal As Numbers</td>
-  <td class="arg">first, second, msg=None, values=True</td>
+  <td class="arg">first, second, msg=None, values=True, precision=6</td>
<td class="doc">Fails if objects are equal after converting them to real numbers.<br />
 <br />
-The check for equality is done using six decimal places.<br />
+The conversion is done with <a href="#Convert To Number" class="name">Convert To Number</a> keyword using the given <span class="name">precision</span>. The support for giving precision was added in Robot Framework 2.6, in earlier versions it was hard-coded to 6.<br />
 <br />
-See <a href="#Should Be Equal" class="name">Should Be Equal</a> for an explanation on how to override the default error message with <span class="name">msg</span> and <span class="name">values</span>.</td> +See <a href="#Should Be Equal As Numbers" class="name">Should Be Equal As Numbers</a> for examples on how to use <span class="name">precision</span> and why it does not always work as expected. See also <a href="#Should Be Equal" class="name">Should Be Equal</a> for an explanation on how to override the default error message with <span class="name">msg</span> and <span class="name">values</span>.</td>
 </tr>
 <tr>
<td class="kw"><a name="Should Not Be Equal As Strings"></a>Should Not Be Equal As Strings</td>
@@ -1848,7 +1930,7 @@
 <p id="footer">
 Altogether 79 keywords.<br />
Generated by <a href="http://code.google.com/p/robotframework/wiki/LibraryDocumentationTool";>libdoc.py</a>
-on 2011-05-06 10:51:05.
+on 2011-05-16 16:09:10.
 </p>
 </body>
 </html>
=======================================
--- /src/robot/libraries/BuiltIn.py     Mon May 16 05:12:47 2011
+++ /src/robot/libraries/BuiltIn.py     Mon May 16 06:14:32 2011
@@ -94,7 +94,8 @@
If the optional `precision` is positive or zero, the returned number is rounded to that number of decimal digits. Negative precision means that the number is rounded to the closest multiple of 10 to the power
-        of the absolute precision.
+        of the absolute precision. The support for precision was added in
+        Robot Framework 2.6.

         Examples:
| ${result} = | Convert To Number | 42.512 | | # Result is 42.512 |
@@ -321,30 +322,56 @@
                               self._convert_to_integer(second, base),
                               msg, values)

- def should_not_be_equal_as_numbers(self, first, second, msg=None, values=True):
+    def should_not_be_equal_as_numbers(self, first, second, msg=None,
+                                       values=True, precision=6):
"""Fails if objects are equal after converting them to real numbers.

-        The check for equality is done using six decimal places.
-
- See `Should Be Equal` for an explanation on how to override the default
+        The conversion is done with `Convert To Number` keyword using the
+        given `precision`. The support for giving precision was added in
+        Robot Framework 2.6, in earlier versions it was hard-coded to 6.
+
+        See `Should Be Equal As Numbers` for examples on how to use
+        `precision` and why it does not always work as expected. See also
+        `Should Be Equal` for an explanation on how to override the default
         error message with `msg` and `values`.
         """
         self._log_types(first, second)
-        first = round(self._convert_to_number(first), 6)
-        second = round(self._convert_to_number(second), 6)
+        first = self._convert_to_number(first, precision)
+        second = self._convert_to_number(second, precision)
         self._should_not_be_equal(first, second, msg, values)

- def should_be_equal_as_numbers(self, first, second, msg=None, values=True): + def should_be_equal_as_numbers(self, first, second, msg=None, values=True,
+                                   precision=6):
"""Fails if objects are unequal after converting them to real numbers.

-        The check for equality is done using six decimal places.
-
- See `Should Be Equal` for an explanation on how to override the default
-        error message with `msg` and `values`.
+        The conversion is done with `Convert To Number` keyword using the
+        given `precision`. The support for giving precision was added in
+        Robot Framework 2.6, in earlier versions it was hard-coded to 6.
+
+        Examples:
+ | Should Be Equal As Numbers | ${x} | 1.1 | | # Passes if ${x} is 1.1 | + | Should Be Equal As Numbers | 1.123 | 1.1 | precision=1 | # Passes | + | Should Be Equal As Numbers | 1.123 | 1.4 | precision=0 | # Passes | + | Should Be Equal As Numbers | 112.3 | 75 | precision=-2 | # Passes |
+
+        As discussed in the documentation of `Convert To Number`, machines
+ generally cannot store floating point numbers accurately. Because of
+        this limitation, comparing floats for equality is problematic and
+        a correct approach to use depends on the context. This keyword uses
+ a very naive approach of rounding the numbers before comparing them, + which is both prone to rounding errors and does not work very well if + numbers are really big or small. For more information about comparing
+        floats, and ideas on how to implement your own context specific
+        comparison algorithm, see this great article:
+ http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
+
+        See `Should Not Be Equal As Numbers` for a negative version of this
+        keyword and `Should Be Equal` for an explanation on how to override
+        the default error message with `msg` and `values`.
         """
         self._log_types(first, second)
-        first = round(self._convert_to_number(first), 6)
-        second = round(self._convert_to_number(second), 6)
+        first = self._convert_to_number(first, precision)
+        second = self._convert_to_number(second, precision)
         self._should_be_equal(first, second, msg, values)

def should_not_be_equal_as_strings(self, first, second, msg=None, values=True):

==============================================================================
Revision: bf1155b804b6
Author:   Pekka Klärck
Date:     Mon May 16 06:26:39 2011
Log: Converted tests to use Template to make them easier to read. Didn't do it for all tests in this suite because got bored.
http://code.google.com/p/robotframework/source/detail?r=bf1155b804b6

Modified:
 /atest/testdata/standard_libraries/builtin/verify.txt

=======================================
--- /atest/testdata/standard_libraries/builtin/verify.txt Mon May 16 06:14:32 2011 +++ /atest/testdata/standard_libraries/builtin/verify.txt Mon May 16 06:26:39 2011
@@ -50,15 +50,17 @@

 Should Not Be Equal
     [Documentation]  FAIL 1 == 1
-    Should Not Be Equal  ${STR1}  Not ${STR1}
-    Should Not Be Equal  ${STR1}  ${INT1}
-    Should Not Be Equal  ${STR1}  1
+    [Template]  Should Not Be Equal
+    ${STR1}  Not ${STR1}
+    ${STR1}  ${INT1}
+    ${STR1}  1

 Should Be Equal
     [Documentation]  FAIL Error message: A != B
-    Should Be Equal  ${STR1}  1
-    Should Be Equal  ${INT1}  ${1}
-    Should Be Equal  A  B  Error message
+    [Template]  Should Be Equal
+    ${STR1}  1
+    ${INT1}  ${1}
+    A  B  Error message

 Should Be Equal When Types Differ But String Representations Are Same
     [Documentation]  FAIL 1 (string) != 1 (number)
@@ -66,35 +68,42 @@

 Should Not Be Equal As Integers
     [Documentation]  FAIL This message only
-    Should Not Be Equal As Integers  1  ${STR0}
-    Should Not Be Equal As Integers  0  ${INT1}
- Should Not Be Equal As Integers 1 ${INT1} This message only No values
+    [Template]  Should Not Be Equal As Integers
+    1  ${STR0}
+    0  ${INT1}
+    1  ${INT1}  This message only  No values

 Should Not Be Equal As Integers With Base
[Documentation] Conversion functionality tested with `Convert To Integer`
-    Should Not Be Equal As Integers  ABC   DEF  base=16
-    Should Not Be Equal As Integers  0b10  10
-    Should Not Be Equal As Integers  0x10  0o10
+    [Template]  Should Not Be Equal As Integers
+    ABC   DEF  base=16
+    0b10  10
+    0x10  0o10

 Should Be Equal As Integers
     [Documentation]  FAIL -1 != 1
- Should Be Equal As Integers ${STR1}${STR0}${STR0} ${STR1}${STR0}${STR0}
-    Should Be Equal As Integers  -${STR1}  ${STR1}
+    [Template]  Should Be Equal As Integers
+    ${STR1}${STR0}${STR0}  ${STR1}${STR0}${STR0}
+    ${STR1}${STR0}${STR0}  ${100.0}
+    -${STR1}  ${STR1}

 Should Be Equal As Integers With Base
[Documentation] Conversion functionality tested with `Convert To Integer`
-    Should Be Equal As Integers  ABCD  abcd   base=16
-    Should Be Equal As Integers  0b10  2
-    Should Be Equal As Integers  0xFF  0o377
-    Should Be Equal As Integers  0b0   0
-    Should Be Equal As Integers  0x0   0o0
-    Should Be Equal As Integers  0b1   1
-    Should Be Equal As Integers  0x1   0o1
+    [Template]  Should Be Equal As Integers
+    ABCD  abcd   base=16
+    0b10  2
+    0xFF  0o377
+    0b0   0
+    0x0   0o0
+    0b1   1
+    0x1   0o1

 Should Not Be Equal As Numbers
     [Documentation]  FAIL Fails again: 1.1 == 1.1
-    Should Not Be Equal As Numbers  ${STR1}.${STR1}  1.2
-    Should Not Be Equal As Numbers  ${STR1}.${STR1}  1.1  Fails again
+    [Template]  Should Not Be Equal As Numbers
+    ${STR1}.${STR1}  1.2
+    ${STR1}.${STR1}  ${1.2}
+    ${STR1}.${STR1}  1.1  Fails again

 Should Not Be Equal As Numbers With Precision
     [Documentation]  FAIL  Failing: 1.0 == 1.0
@@ -107,9 +116,10 @@
     1.12      1.45     Failing  precision=0

 Should Be Equal As Numbers
-    [Documentation]  FAIL Only this message again
-    Should Be Equal As Numbers  ${STR1}  ${STR1}.${STR0}${STR0}
- Should Be Equal As Numbers ${STR1}.000001 ${STR1}.${STR0}${STR0}${STR1} Only this message again False
+    [Documentation]  FAIL Only this message
+    [Template]  Should Be Equal As Numbers
+    ${STR1}  ${STR1}.${STR0}${STR0}
+    ${STR1}.000001  ${STR1}.${STR0}${STR0}${STR1}  Only this message  False

 Should Be Equal As Numbers With Precision
     [Documentation]  FAIL  Failure: 110.0 != 150.0
@@ -123,40 +133,50 @@

 Should Not Be Equal As Strings
     [Documentation]  FAIL These strings most certainly should not be equal
-    Should Not Be Equal As Strings  1  ${1.1}
-    Should Not Be Equal As Strings  False  ${True}
- Should Not Be Equal As Strings bar bar These strings most certainly should not be equal False
+    [Template]  Should Not Be Equal As Strings
+    1  ${1.1}
+    False  ${True}
+    bar  bar  These strings most certainly should not be equal  False

 Should Be Equal As Strings
     [Documentation]  FAIL foo != bar
-    Should Be Equal As Strings  ${1}  1
-    Should Be Equal As Strings  ${None}  None
-    Should Be Equal As Strings  foo  bar
+    [Template]  Should Be Equal As Strings
+    ${1}  1
+    ${None}  None
+    foo  bar

 Should Not Start With
     [Documentation]  FAIL 'Hello, world!' starts with 'Hello'
-    Should Not Start With  Hello, world!  Hi
-    Should Not Start With  Hello, world!  Hello
+    [Template]  Should Not Start With
+    Hello, world!  Hi
+    Hello, world!  HELLO
+    Hello, world!  Hello

 Should Start With
[Documentation] FAIL My message: 'This is a big longer sentence and it even has a friend here. This is the friend of the previous sentence -- also this is quite long, actually even longer than its friend.' does not start with 'Whatever'
-    Should Start With  ${STR0}${STR0}7  00
- Should Start With This is a big longer sentence and it even has a friend here. This is the friend of the previous sentence -- also this is quite long, actually even longer than its friend. Whatever My message
+    [Template]  Should Start With
+    Hello, world!  Hello
+    Hello, world!  Hello, world!
+ This is a big longer sentence and it even has a friend here. This is the friend of the previous sentence -- also this is quite long, actually even longer than its friend. Whatever My message

 Should Not End With
     [Documentation]  FAIL Message only
-    Should Not End With  Hello!  Hello
-    Should Not End With  Hillo!  !  Message only  No Values
+    [Template]  Should Not End With
+    Hello!  Hello
+    Hillo!  !  Message only  No Values

 Should End With
-    [Documentation]  FAIL '007' does not end with '00'
-    Should End With  ${STR0}${STR0}7  007
-    Should End With  ${STR0}${STR0}7  00
+    [Documentation]  FAIL 'Hello, world!' does not end with '?'
+    [Template]  Should End With
+    Hello, world!  !
+    Hello, world!  Hello, world!
+    Hello, world!  ?

 Should Not Contain
     [Documentation]  FAIL 'Hello yet again' contains 'yet'
-    Should Not Contain  Hello again  yet
-    Should Not Contain  Hello yet again  yet
+    [Template]  Should Not Contain
+    Hello again  yet
+    Hello yet again  yet

 Should Not Contain With Non-String Values
     [Documentation]  FAIL '(1, 2)' contains '1'
@@ -168,8 +188,10 @@

 Should Contain
     [Documentation]  FAIL Message: 'abcdefg' does not contain 'ABCDEFG'
-    Should Contain  abcdefg  cd  Massage
-    Should Contain  abcdefg  ABCDEFG  Message
+    [Template]  Should Contain
+    abcdefg  cd
+    abcdefg  abcdefg  Massage
+    abcdefg  ABCDEFG  Message

 Should Contain With Non-String Values
     [Documentation]  FAIL '{'a': 1}' does not contain 'c'

Reply via email to