1- /**
2- This Source Code Form is subject to the terms of the Mozilla Public
3- License, v. 2.0. If a copy of the MPL was not distributed with this
4- file, You can obtain one at http://mozilla.org/MPL/2.0/.
5- **/
6-
7- using System ;
8- using System . Collections ;
9- using System . Collections . Generic ;
10- using System . Text . Json ;
11- using Microsoft . VisualStudio . TestTools . UnitTesting ;
12- using PhpSerializerNET ;
13-
14- namespace PhpSerializerNET . Test {
15- [ TestClass ]
16- public class DeserializeListOptions {
17-
18- [ TestMethod ]
19- public void ListOptionNever ( ) {
20- var test = PhpSerialization . Deserialize (
21- "a:2:{i:0;s:1:\" a\" ;i:1;s:1:\" b\" ;}" ,
22- new PhpDeserializationOptions ( ) {
23- UseLists = ListOptions . Never
24- }
25- ) ;
26-
27- var dictionary = test as Dictionary < object , object > ;
28- Assert . IsNotNull ( dictionary ) ;
29- Assert . AreEqual ( 2 , dictionary . Count ) ;
30- Assert . AreEqual ( "a" , dictionary [ ( long ) 0 ] ) ;
31- Assert . AreEqual ( "b" , dictionary [ ( long ) 1 ] ) ;
32- }
33-
34- [ TestMethod ]
35- public void ListOptionDefault ( ) {
36- var test = PhpSerialization . Deserialize (
37- "a:2:{i:0;s:1:\" a\" ;i:1;s:1:\" b\" ;}" ,
38- new PhpDeserializationOptions ( ) {
39- UseLists = ListOptions . Default
40- }
41- ) ;
42-
43- var list = test as List < object > ;
44- Assert . IsNotNull ( list ) ;
45- Assert . AreEqual ( 2 , list . Count ) ;
46- Assert . AreEqual ( "a" , list [ 0 ] ) ;
47- Assert . AreEqual ( "b" , list [ 1 ] ) ;
48-
49- // Same option, non-consecutive integer keys:
50- test = PhpSerialization . Deserialize (
51- "a:2:{i:2;s:1:\" a\" ;i:4;s:1:\" b\" ;}" ,
52- new PhpDeserializationOptions ( ) {
53- UseLists = ListOptions . Default
54- }
55- ) ;
56-
57- var dictionary = test as Dictionary < object , object > ;
58- Assert . IsNotNull ( dictionary ) ;
59- Assert . AreEqual ( 2 , dictionary . Count ) ;
60- Assert . AreEqual ( "a" , dictionary [ ( long ) 2 ] ) ;
61- Assert . AreEqual ( "b" , dictionary [ ( long ) 4 ] ) ;
62- }
63-
64- [ TestMethod ]
65- public void ListOptionAllIntegers ( ) {
66- var test = PhpSerialization . Deserialize (
67- "a:2:{i:0;s:1:\" a\" ;i:1;s:1:\" b\" ;}" ,
68- new PhpDeserializationOptions ( ) {
69- UseLists = ListOptions . OnAllIntegerKeys
70- }
71- ) ;
72-
73- var list = test as List < object > ;
74- Assert . IsNotNull ( list ) ;
75- Assert . AreEqual ( 2 , list . Count ) ;
76- Assert . AreEqual ( "a" , list [ 0 ] ) ;
77- Assert . AreEqual ( "b" , list [ 1 ] ) ;
78-
79- // Same option, non-consecutive integer keys:
80- test = PhpSerialization . Deserialize (
81- "a:2:{i:2;s:1:\" a\" ;i:4;s:1:\" b\" ;}" ,
82- new PhpDeserializationOptions ( ) {
83- UseLists = ListOptions . OnAllIntegerKeys
84- }
85- ) ;
86-
87- list = test as List < object > ;
88- Assert . IsNotNull ( list ) ;
89- Assert . AreEqual ( 2 , list . Count ) ;
90- Assert . AreEqual ( "a" , list [ 0 ] ) ;
91- Assert . AreEqual ( "b" , list [ 1 ] ) ;
92- }
93- }
94- }
1+ /**
2+ This Source Code Form is subject to the terms of the Mozilla Public
3+ License, v. 2.0. If a copy of the MPL was not distributed with this
4+ file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+ **/
6+
7+ using System . Collections . Generic ;
8+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
9+
10+ namespace PhpSerializerNET . Test {
11+ [ TestClass ]
12+ public class UseListsTest {
13+ [ TestMethod ]
14+ public void Option_Never ( ) {
15+ var test = PhpSerialization . Deserialize (
16+ "a:2:{i:0;s:1:\" a\" ;i:1;s:1:\" b\" ;}" ,
17+ new PhpDeserializationOptions ( ) {
18+ UseLists = ListOptions . Never
19+ }
20+ ) ;
21+
22+ var dictionary = test as Dictionary < object , object > ;
23+ Assert . IsNotNull ( dictionary ) ;
24+ Assert . AreEqual ( 2 , dictionary . Count ) ;
25+ Assert . AreEqual ( "a" , dictionary [ ( long ) 0 ] ) ;
26+ Assert . AreEqual ( "b" , dictionary [ ( long ) 1 ] ) ;
27+ }
28+
29+ [ TestMethod ]
30+ public void Option_Default ( ) {
31+ var result = PhpSerialization . Deserialize (
32+ "a:2:{i:0;s:1:\" a\" ;i:1;s:1:\" b\" ;}" ,
33+ new PhpDeserializationOptions ( ) {
34+ UseLists = ListOptions . Default
35+ }
36+ ) ;
37+
38+ var list = result as List < object > ;
39+ Assert . IsNotNull ( list ) ;
40+ Assert . AreEqual ( 2 , list . Count ) ;
41+ Assert . AreEqual ( "a" , list [ 0 ] ) ;
42+ Assert . AreEqual ( "b" , list [ 1 ] ) ;
43+ }
44+
45+ [ TestMethod ]
46+ public void Option_Default_NonConsequetive ( ) {
47+ // Same option, non-consecutive integer keys:
48+ var result = PhpSerialization . Deserialize (
49+ "a:2:{i:2;s:1:\" a\" ;i:4;s:1:\" b\" ;}" ,
50+ new PhpDeserializationOptions ( ) {
51+ UseLists = ListOptions . Default
52+ }
53+ ) ;
54+
55+ var dictionary = result as Dictionary < object , object > ;
56+ Assert . IsNotNull ( dictionary ) ;
57+ Assert . AreEqual ( 2 , dictionary . Count ) ;
58+ Assert . AreEqual ( "a" , dictionary [ ( long ) 2 ] ) ;
59+ Assert . AreEqual ( "b" , dictionary [ ( long ) 4 ] ) ;
60+ }
61+
62+ [ TestMethod ]
63+ public void Option_OnAllIntegerKeys ( ) {
64+ var test = PhpSerialization . Deserialize (
65+ "a:2:{i:0;s:1:\" a\" ;i:1;s:1:\" b\" ;}" ,
66+ new PhpDeserializationOptions ( ) {
67+ UseLists = ListOptions . OnAllIntegerKeys
68+ }
69+ ) ;
70+
71+ var list = test as List < object > ;
72+ Assert . IsNotNull ( list ) ;
73+ Assert . AreEqual ( 2 , list . Count ) ;
74+ Assert . AreEqual ( "a" , list [ 0 ] ) ;
75+ Assert . AreEqual ( "b" , list [ 1 ] ) ;
76+
77+ }
78+
79+ [ TestMethod ]
80+ public void Option_OnAllIntegerKeys_NonConsequetive ( ) {
81+ // Same option, non-consecutive integer keys:
82+ var result = PhpSerialization . Deserialize (
83+ "a:2:{i:2;s:1:\" a\" ;i:4;s:1:\" b\" ;}" ,
84+ new PhpDeserializationOptions ( ) {
85+ UseLists = ListOptions . OnAllIntegerKeys
86+ }
87+ ) ;
88+
89+ var list = result as List < object > ;
90+ Assert . IsNotNull ( list ) ;
91+ Assert . AreEqual ( 2 , list . Count ) ;
92+ Assert . AreEqual ( "a" , list [ 0 ] ) ;
93+ Assert . AreEqual ( "b" , list [ 1 ] ) ;
94+ }
95+ }
96+ }
0 commit comments