| commit 140: | 56c9a10d4b3b |
| parent 139: | d17419ddb076 |
| branch: | default |
Numeric / String conversion in xobj
8 months ago
Changed (Δ3.8 KB):
raw changeset »
as3/xobjas3-test/src/tests/TestBasics.as (72 lines added, 1 lines removed)
as3/xobjas3-test/src/tests/models/TestableNumericObject.as (32 lines added, 0 lines removed)
as3/xobjas3/src/com/rpath/xobj/XObjXMLDecoder.as (8 lines added, 0 lines removed)
Up to file-list as3/xobjas3-test/src/tests/TestBasics.as:
| … | … | @@ -257,6 +257,77 @@ public class TestBasics extends TestBase |
257 |
257 |
xmlOutput = typedEncoder.encodeObject(o); |
258 |
258 |
assertTrue("encode matches input", compareXML(xmlOutput, xmlInput)); |
259 |
259 |
} |
260 |
} |
|
260 |
||
261 |
public function testStringNumerics():void |
|
262 |
{ |
|
263 |
var obj:TestableNumericObject = new TestableNumericObject(); |
|
264 |
||
265 |
obj.someNumber = 1.1; |
|
266 |
// make sure someNumber is a Number so this is a valid test |
|
267 |
assertTrue(obj.someNumber is Number); |
|
268 |
obj.booleanVar = true; |
|
269 |
var typeMap:* = {obj: TestableNumericObject}; |
|
270 |
||
271 |
var typedEncoder:XObjXMLEncoder = new XObjXMLEncoder(typeMap); |
|
272 |
var xmlOutput:XMLDocument = typedEncoder.encodeObject(obj); |
|
273 |
||
274 |
// neither the Transient nor the xobjTransient vars should be there |
|
275 |
var expectedString:String = |
|
276 |
'<obj>\n'+ |
|
277 |
' <booleanVar>true</booleanVar>\n'+ |
|
278 |
' <someNumber>1.1</someNumber>\n'+ |
|
279 |
'</obj>\n'; |
|
280 |
||
281 |
assertTrue(compareXMLtoString(xmlOutput, expectedString)); |
|
282 |
||
283 |
// now decode it and validate |
|
284 |
var typedDecoder:XObjXMLDecoder = new XObjXMLDecoder(typeMap); |
|
285 |
var xmlInput:XMLDocument = xmlOutput; |
|
286 |
var o:* = typedDecoder.decodeXML(xmlInput); |
|
287 |
assertTrue(o.obj is TestableNumericObject); |
|
288 |
assertTrue(o.obj.someNumber == 1.1); |
|
289 |
assertTrue(o.obj.booleanVar); |
|
290 |
||
291 |
// reencode and check round-trip |
|
292 |
xmlOutput = typedEncoder.encodeObject(o); |
|
293 |
assertTrue("encode matches input", compareXML(xmlOutput, xmlInput)); |
|
294 |
} |
|
295 |
||
296 |
public function testStringNumerics2():void |
|
297 |
{ |
|
298 |
var obj:TestableNumericObject = new TestableNumericObject(); |
|
299 |
||
300 |
obj.someNumber = 0.5; |
|
301 |
// make sure someNumber is a Number so this is a valid test |
|
302 |
assertTrue(obj.someNumber is Number); |
|
303 |
obj.booleanVar = true; |
|
304 |
var typeMap:* = {obj: TestableNumericObject}; |
|
305 |
||
306 |
var typedEncoder:XObjXMLEncoder = new XObjXMLEncoder(typeMap); |
|
307 |
var xmlOutput:XMLDocument = typedEncoder.encodeObject(obj); |
|
308 |
||
309 |
// neither the Transient nor the xobjTransient vars should be there |
|
310 |
var expectedString:String = |
|
311 |
'<obj>\n'+ |
|
312 |
' <booleanVar>true</booleanVar>\n'+ |
|
313 |
' <someNumber>0.5</someNumber>\n'+ |
|
314 |
'</obj>\n'; |
|
315 |
||
316 |
assertTrue(compareXMLtoString(xmlOutput, expectedString)); |
|
317 |
||
318 |
// now decode it and validate |
|
319 |
var typedDecoder:XObjXMLDecoder = new XObjXMLDecoder(typeMap); |
|
320 |
var xmlInput:XMLDocument = xmlOutput; |
|
321 |
var o:* = typedDecoder.decodeXML(xmlInput); |
|
322 |
assertTrue(o.obj is TestableNumericObject); |
|
323 |
assertTrue(o.obj.someNumber == 0.5); |
|
324 |
assertTrue(o.obj.booleanVar); |
|
325 |
||
326 |
// reencode and check round-trip |
|
327 |
xmlOutput = typedEncoder.encodeObject(o); |
|
328 |
assertTrue("encode matches input", compareXML(xmlOutput, xmlInput)); |
|
329 |
} |
|
330 |
||
331 |
} |
|
261 |
332 |
} |
262 |
333 |
Up to file-list as3/xobjas3-test/src/tests/models/TestableNumericObject.as:
1 |
/* |
|
2 |
# |
|
3 |
# Copyright (c) 2009 rPath, Inc. |
|
4 |
# |
|
5 |
# This program is distributed under the terms of the MIT License as found |
|
6 |
# in a file called LICENSE. If it is not present, the license |
|
7 |
# is always available at http://www.opensource.org/licenses/mit-license.php. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, but |
|
10 |
# without any waranty; without even the implied warranty of merchantability |
|
11 |
# or fitness for a particular purpose. See the MIT License for full details. |
|
12 |
*/ |
|
13 |
||
14 |
package tests.models |
|
15 |
{ |
|
16 |
/** |
|
17 |
* A random object for use in validating encoding of objects |
|
18 |
*/ |
|
19 |
public dynamic class TestableNumericObject |
|
20 |
{ |
|
21 |
public var someNumber:Number; |
|
22 |
||
23 |
[Transient] |
|
24 |
public var transientVar:String; |
|
25 |
||
26 |
[xobjTransient] |
|
27 |
public var xobjTransientVar:String; |
|
28 |
||
29 |
public var booleanVar:Boolean; |
|
30 |
||
31 |
} |
|
32 |
} |
Up to file-list as3/xobjas3/src/com/rpath/xobj/XObjXMLDecoder.as:
| … | … | @@ -148,6 +148,14 @@ public class XObjXMLDecoder |
148 |
148 |
{ |
149 |
149 |
result = false; |
150 |
150 |
} |
151 |
else if (resultType == int) |
|
152 |
{ |
|
153 |
result = Number(val); |
|
154 |
} |
|
155 |
else if (resultType == Number) |
|
156 |
{ |
|
157 |
result = Number(val); |
|
158 |
} |
|
151 |
159 |
else if (!isFinite(testNum) || isNaN(testNum) |
152 |
160 |
|| (val.charAt(0) == '0') // starts with a leading zero |
153 |
161 |
|| ((val.charAt(0) == '-') && (val.charAt(1) == '0')) // starts with -0 |
