Skip to content

Commit 3fb28b2

Browse files
committed
Merge pull request s9tpepper#1 from newtriks/null_fixes
Null fixes
2 parents c5d40fc + d895d41 commit 3fb28b2

File tree

7 files changed

+171
-8
lines changed

7 files changed

+171
-8
lines changed

.gitignore

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
1-
.project
1+
#OS X
22
.DS_Store
3-
.settings
3+
Icon?
4+
5+
# Thumbnails
6+
._*
7+
8+
# Files that might appear on external disk
9+
.Spotlight-V100
10+
.Trashes
11+
12+
#Flex/Flash
13+
14+
# Build Dir
15+
bin/
16+
bin-release/
17+
bin-debug/
18+
out/
19+
20+
# Project property files
21+
.actionScriptProperties
22+
.flexProperties
23+
.settings/
24+
.project
25+
26+
#IntelliJ IDEA
27+
*.iml
28+
*.ipr
29+
*.iws
30+
.idea/
431

32+
#TextMate
33+
*.tmproj
34+
*.tmproject
35+
tmtags

src/ab/fl/utils/json/JSON.as

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ package ab.fl.utils.json
382382
for each (key in sourceKeys)
383383
{
384384
value = source[key];
385-
if (!value)
385+
if (value==null)
386386
{
387387
target[key] = null;
388388
continue;
@@ -393,15 +393,15 @@ package ab.fl.utils.json
393393
switch (true)
394394
{
395395
case (value is String):
396-
target[key] = value;
396+
target[key] = (value.length) ? value : "";
397397
break;
398398
case (value is Boolean):
399-
target[key] = (value !== null) ? value : null;
399+
target[key] = (value !== null) ? value : false;
400400
break;
401401
case (value is Number):
402402
case (value is int):
403403
case (value is uint):
404-
target[key] = (value) ? value : null;
404+
target[key] = (value !== null) ? value : null;
405405
break;
406406

407407
case (className == "Object"):

src_test/EncodeToTypedTest.as

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package {
2+
3+
import ab.fl.utils.json.JSON;
4+
5+
import helpers.vo.TypedVO;
6+
7+
import org.flexunit.Assert;
8+
9+
public class EncodeToTypedTest {
10+
11+
private var strongTypedVO:TypedVO;
12+
13+
[Before]
14+
public function setup():void {
15+
strongTypedVO = typedVO;
16+
ab.fl.utils.json.JSON.registerClass("TypedVO", TypedVO);
17+
}
18+
19+
[After]
20+
public function tearDown():void {
21+
strongTypedVO = null;
22+
}
23+
24+
[Test]
25+
public function int_value_of_1_should_encode_to_1():void {
26+
var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO);
27+
var index:int = json.search(/aInt/i) + 6;
28+
Assert.assertEquals(1, json.substr(index, 1));
29+
}
30+
31+
[Test]
32+
public function int_value_of_0_should_encode_to_0():void {
33+
strongTypedVO.aInt = 0;
34+
var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO);
35+
var index:int = json.search(/aInt/i) + 6;
36+
Assert.assertEquals(0, json.substr(index, 1));
37+
}
38+
39+
[Test]
40+
public function uint_value_of_1_should_encode_to_1():void {
41+
var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO);
42+
var index:int = json.search(/aUint/i) + 7;
43+
Assert.assertEquals(1, json.substr(index, 1));
44+
}
45+
46+
[Test]
47+
public function uint_value_of_0_should_encode_to_0():void {
48+
strongTypedVO.aUint = 0;
49+
var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO);
50+
var index:int = json.search(/aUint/i) + 7;
51+
Assert.assertEquals(0, json.substr(index, 1));
52+
}
53+
54+
[Test]
55+
public function number_value_of_1_should_encode_to_1():void {
56+
var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO);
57+
var index:int = json.search(/aNumber/i) + 9;
58+
Assert.assertEquals(1, json.substr(index, 1));
59+
}
60+
61+
[Test]
62+
public function number_value_of_0_should_encode_to_0():void {
63+
strongTypedVO.aNumber = 0;
64+
var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO);
65+
var index:int = json.search(/aNumber/i) + 9;
66+
Assert.assertEquals(0, json.substr(index, 1));
67+
}
68+
69+
[Test]
70+
public function string_value_of_a_should_encode_to_a():void {
71+
var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO);
72+
var index:int = json.search(/aString/i) + 10;
73+
Assert.assertEquals("a", json.substr(index, 1));
74+
}
75+
76+
[Test]
77+
public function empty_string_value_should_encode_to_empty_string():void {
78+
strongTypedVO.aString = "";
79+
var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO);
80+
var index:int = json.search(/aString/i) + 10;
81+
Assert.assertEquals('"', json.substr(index, 1));
82+
}
83+
84+
[Test]
85+
public function boolean_value_of_true_should_encode_to_true():void {
86+
var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO);
87+
var index:int = json.search(/aBoolean/i) + 10;
88+
Assert.assertEquals("true", json.substr(index, 4));
89+
}
90+
91+
[Test]
92+
public function boolean_value_of_false_should_encode_to_false():void {
93+
strongTypedVO.aBoolean = false;
94+
var json:String = ab.fl.utils.json.JSON.encodeToTyped(strongTypedVO);
95+
var index:int = json.search(/aBoolean/i) + 10;
96+
Assert.assertEquals("false", json.substr(index, 5));
97+
}
98+
99+
private function get typedVO():TypedVO {
100+
var _typedVO:TypedVO = new TypedVO();
101+
_typedVO.aInt = 1;
102+
_typedVO.aUint = 1;
103+
_typedVO.aNumber = 1;
104+
_typedVO.aString = "a";
105+
_typedVO.aBoolean = true;
106+
return _typedVO;
107+
}
108+
}
109+
}

src_test/FlexTestSuite.as

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package
2+
{
3+
[Suite]
4+
[RunWith("org.flexunit.runners.Suite")]
5+
public class FlexTestSuite
6+
{
7+
public var simpleTest:SimpleTest;
8+
public var encodeToTypedIntTest:EncodeToTypedTest;
9+
}
10+
}

src_test/FlexUnitRunner.as

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ package {
2828
//You can also pass a Request Object, which allows you to sort, filter and subselect.
2929
//var request:Request = Request.methods( someClass, ["method1", "method2", "method3"] ).sortWith( someSorter ).filterWith( someFilter );
3030
//core.run( request );
31-
core.run(SimpleTest);
31+
core.run(FlexTestSuite);
3232
}
3333
}
3434
}

src_test/SimpleTest.as

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package {
55

66
[Test]
77
public function testMe() : void {
8-
Assert.assertTrue("Was not true", false);
8+
Assert.assertTrue("Was not true", true);
99
}
1010
}
1111
}

src_test/helpers/vo/TypedVO.as

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package helpers.vo
2+
{
3+
[RemoteClass(alias="TypedVO")]
4+
public class TypedVO
5+
{
6+
public var aInt:int;
7+
public var aUint:uint;
8+
public var aNumber:Number;
9+
public var aString:String;
10+
public var aBoolean:Boolean;
11+
public var _explicitType:String;
12+
}
13+
}

0 commit comments

Comments
 (0)