|
| 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 | +} |
0 commit comments