* (bug 25571) Xml::encodeJsVar now passes floats natively instead of converting to...
authorBrion Vibber <brion@users.mediawiki.org>
Mon, 14 Feb 2011 00:54:40 +0000 (00:54 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Mon, 14 Feb 2011 00:54:40 +0000 (00:54 +0000)
Added unit test cases for int, float, and strings that look like int or float.

includes/Xml.php
tests/phpunit/includes/XmlTest.php

index b293b18..412f041 100644 (file)
@@ -583,8 +583,8 @@ class Xml {
                        $s = $value ? 'true' : 'false';
                } elseif ( is_null( $value ) ) {
                        $s = 'null';
-               } elseif ( is_int( $value ) ) {
-                       $s = $value;
+               } elseif ( is_int( $value ) || is_float( $value ) ) {
+                       $s = strval($value);
                } elseif ( is_array( $value ) && // Make sure it's not associative.
                                        array_keys($value) === range( 0, count($value) - 1 ) ||
                                        count($value) == 0
index 4086ce6..d9647e5 100644 (file)
@@ -248,4 +248,36 @@ class XmlTest extends MediaWikiTestCase {
                        'encodeJsVar() with object'
                );
        }
+
+       function testEncodeJsVarInt() {
+               $this->assertEquals(
+                       '123456',
+                       Xml::encodeJsVar( 123456 ),
+                       'encodeJsVar() with int'
+               );
+       }
+
+       function testEncodeJsVarFloat() {
+               $this->assertEquals(
+                       '1.23456',
+                       Xml::encodeJsVar( 1.23456 ),
+                       'encodeJsVar() with float'
+               );
+       }
+
+       function testEncodeJsVarIntString() {
+               $this->assertEquals(
+                       '"123456"',
+                       Xml::encodeJsVar( '123456' ),
+                       'encodeJsVar() with int-like string'
+               );
+       }
+
+       function testEncodeJsVarFloatString() {
+               $this->assertEquals(
+                       '"1.23456"',
+                       Xml::encodeJsVar( '1.23456' ),
+                       'encodeJsVar() with float-like string'
+               );
+       }
 }