Subversion Repository Public Repository

WOX2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using ClaimPortal;
using ClaimPortal.Requests;
using NUnit.Framework;
using wox.serial.tests.objects;

/// <header>
/// <wox>
///     <version>1.0</version>
///     <author>Carlos R. Jaimez Gonzalez</author>
///     <author>Simon M. Lucas</author>
///     <site>http://woxserializer.sourceforge.net/</site>
/// </wox>
/// <wox>
///     <version>1.5</version>
///     <author>Steven M Lewis</author>
/// </wox>
/// <wox>
///     <version>2.0</version>
///     <author>George A Smith</author>
///     <svn>http://woxserializer.sourceforge.net/</svn>
///     <note>XML form for vs 2 is more compact and therefor incompatible with vs 1</note>
/// </wox>
/// </header>

namespace wox.serial.tests
{
    [TestFixture]
    public class SerializationTests
    {
        private Easy mEasy;

        public class TestData
        {
            private const string TEST_DATA_SUB_DIR = "TestData";

            private static volatile string sDeterminedPath;

            public static string GetPath()
            {
                if ( sDeterminedPath == null )
                {
                    sDeterminedPath = FindTestDataDir();
                }
                return sDeterminedPath;
            }

            private static string FindTestDataDir()
            {
                var zDir = new DirectoryInfo( "." );
                if ( !zDir.Exists )
                {
                    throw new ApplicationException( "No Current Directory" );
                }
                string zOrigPath = zDir.FullName;
                DirectoryInfo zTestDataDir;
                while ( !(zTestDataDir = new DirectoryInfo( zDir.FullName + "/" + TEST_DATA_SUB_DIR )).Exists )
                {
                    if ( null == (zDir = zDir.Parent) )
                    {
                        throw new ApplicationException( TEST_DATA_SUB_DIR + " Not Found off any of the following path: " + zOrigPath );
                    }
                }
                return zTestDataDir.FullName;
            }

            public static void WriteOutLines( string pFileName, string pData )
            {
                var fi = new FileInfo( GetPath() + "/" + pFileName + ".out.txt" );
                StreamWriter writer = fi.CreateText();
                writer.Write( pData.Replace( "\r\n", "\n" ) );
                writer.Flush();
                writer.Close();
            }

            public static string ReadInLines( string pFileName )
            {
                var fi = new FileInfo( GetPath() + "/" + pFileName + ".in.txt" );
                StreamReader writer = fi.OpenText();
                string zData = writer.ReadToEnd();
                writer.Close();
                return zData.Replace( "\r\n", "\n" );
            }
        }

        private class XyzzyTypeMapper : WoxTypeMapper
        {
            private const string NATIVE_PREFIX = "wox.serial.tests.SerializationTests+";
            private const string WOX_PREFIX = "Xyzzy_";

            public string GetNativeTypeFor( string pWoxType )
            {
                return pWoxType.StartsWith( WOX_PREFIX )
                           ? NATIVE_PREFIX + pWoxType.Substring( WOX_PREFIX.Length )
                           : pWoxType;
            }

            public string GetWoxTypeFor( string pNativeType )
            {
                return pNativeType.StartsWith( NATIVE_PREFIX )
                           ? WOX_PREFIX + pNativeType.Substring( NATIVE_PREFIX.Length )
                           : pNativeType;
            }
        }

        private class XyzzyFieldNameMapper : BackingFieldWoxFieldNameMapper
        {
            protected override string GetWoxFieldNameForProperty( string pNativePropertyName )
            {
                return "m" + pNativePropertyName;
            }
        }

        [TestFixtureSetUp]
        protected void TestFixtureSetUp()
        {
            mEasy = new Easy();
            mEasy.Add( new PatientIdentifierStringifier() );
            mEasy.Add( new XyzzyFieldNameMapper() );
            mEasy.Add( new XyzzyTypeMapper() );
        }

        private static void AssertEquivalent( object pExpected, object pActual )
        {
            if ( !ComparisonUtilities.Equivalent( pExpected, pActual ) )
            {
                Type zExpectedType = pExpected.GetType();
                Type zActualType = pActual.GetType();
                string zExpected = "" + zExpectedType + ":" + pExpected;
                string zActual = "" + zActualType + ":" + pActual;
                Assert.AreEqual( zExpected, zActual );
                Assert.AreEqual( "E:" + zExpected, "A:" + zActual );
            }
        }

        private void SaveType( String pFileNameExpectedText, Object pObjectToSaveType )
        {
            pFileNameExpectedText += "Type";

            String zPrettySerializedForm = mEasy.FormatType( pObjectToSaveType );

            String zExpected;
            try
            {
                zExpected = TestData.ReadInLines( pFileNameExpectedText ).Trim();
            }
            catch ( FileNotFoundException )
            {
                TestData.WriteOutLines( pFileNameExpectedText, zPrettySerializedForm );
                throw;
            }
            if ( !zExpected.Equals( zPrettySerializedForm ) )
            {
                TestData.WriteOutLines( pFileNameExpectedText, zPrettySerializedForm );
                Assert.AreEqual( zExpected, zPrettySerializedForm );
            }
        }

        public object FileRoundTripObjectEquivalent( string pFileName, object pInObject )
        {
            string zPrettySerializedForm = mEasy.Format( pInObject, true );

            string zExpected;
            try
            {
                zExpected = TestData.ReadInLines( pFileName ).Trim();
            }
            catch ( FileNotFoundException )
            {
                TestData.WriteOutLines( pFileName, zPrettySerializedForm );
                throw;
            }
            if ( !zExpected.Equals( zPrettySerializedForm ) )
            {
                TestData.WriteOutLines( pFileName, zPrettySerializedForm );
                Assert.AreEqual( zExpected, zPrettySerializedForm );
            }

            object zDeserializedInstance = mEasy.Parse( zPrettySerializedForm );

            AssertEquivalent( pInObject, zDeserializedInstance );

            string zCompactSerializedForm = mEasy.Format( pInObject );
            zDeserializedInstance = mEasy.Parse( zCompactSerializedForm );
            AssertEquivalent( pInObject, zDeserializedInstance );

            return zDeserializedInstance;
        }

        public void FileRoundTripSerializedForm( string pFileName )
        {
            string zInXML = TestData.ReadInLines( pFileName ).Trim();

            Object zDeserializedInstance = mEasy.Parse( zInXML );

            string zOutXML = mEasy.Format( zDeserializedInstance, true );

            if ( !zInXML.Equals( zOutXML ) )
            {
                TestData.WriteOutLines( pFileName, zOutXML );
                Assert.AreEqual( zInXML, zOutXML );
            }
        }

        public void FileRoundTripSerializedFormNonPretty( string pFileNameNonPretty, string pFileNamePretty )
        {
            string zInXML = TestData.ReadInLines( pFileNameNonPretty ).Trim();

            Object zDeserializedInstance = mEasy.Parse( zInXML );

            string zOutXML = mEasy.Format( zDeserializedInstance, true );

            zInXML = TestData.ReadInLines( pFileNamePretty ).Trim();

            if ( !zInXML.Equals( zOutXML ) )
            {
                TestData.WriteOutLines( pFileNamePretty, zOutXML );
                Assert.AreEqual( zInXML, zOutXML );
            }
        }

        public String Serialize( Object o )
        {
            return mEasy.Format( o );
        }

        public Object Deserialize( String o )
        {
            return mEasy.Parse( o );
        }

        public void RoundTripObjectEquivalent( Object test )
        {
            object item = RoundTrip( test );
            if ( !ComparisonUtilities.Equivalent( test, item ) )
            {
                Type t_item = item.GetType();
                Type t_test = test.GetType();
                Assert.AreEqual( "" + t_test + ":" + test, "" + t_item + ":" + item );
            }
        }

        public object RoundTrip( object test )
        {
            String data = Serialize( test );
            return Deserialize( data );
        }

        /// <summary>
        /// Deserialize - then reserialize
        /// </summary>
        /// <param name="test">Serialized form</param>
        /// <returns>Serialization of Deserialization - should be identical</returns>
        public String RoundTripString( String test )
        {
            Object data = Deserialize( test );
            return Serialize( data );
        }

        public void ArrayTest()
        {
            var testPrimitiveArrays = new PrimitiveArrays( new[] {'e', 't', 'r', 'g', 'w'},
                                                           new[] {23, 56, 78, 33, 69},
                                                           new[] {true, false, true, false, false} );

            RoundTripObjectEquivalent( testPrimitiveArrays );
        }

        public static readonly String[] SS_NUMBERS = {
                                                         "123-48-2906",
                                                         "123-49-2906",
                                                         "123-50-2906",
                                                     };

        public static readonly String[] PROVIDER_NUMBERS = {
                                                               "1111-48-2906",
                                                               "222-49-2906",
                                                               "333-50-2906",
                                                           };

        public static readonly String[] FIRST_NAMES = {
                                                          "Dave",
                                                          "Bill",
                                                          "Jane",
                                                      };

        public static readonly String[] LAST_NAMES = {
                                                         "Smith",
                                                         "Jones",
                                                         "Ng",
                                                     };

        public static readonly String[] DRUG_NAMES = {
                                                         "Leveprol",
                                                         "Prenopril",
                                                         "Calrex",
                                                     };

        public static readonly String[] STREET_NAMES = {
                                                           "Elm",
                                                           "Main",
                                                           "Broad",
                                                       };

        public static readonly String[] CITY_NAMES = {
                                                         "Seattle",
                                                         "Portland",
                                                         "Las Vegas",
                                                     };

        public static readonly String[] STATE_NAMES = {
                                                          "WA",
                                                          "OR",
                                                          "NV",
                                                      };

        public static readonly String[] ZIP_CODES = {
                                                        "11111",
                                                        "22222",
                                                        "33333",
                                                    };

        public static readonly List<Patient> PATIENTS = new List<Patient>();


        public static Patient[] BuildPatients()
        {
            IList<Patient> holder = new List<Patient>();

            for ( int i = 0; i < SS_NUMBERS.Length; i++ )
            {
                holder.Add( BuildPatient( i ) );
            }
            Patient[] contents = holder.ToArray();
            return contents;
        }

        protected static Patient BuildPatient( int index )
        {
            var zPatient = new Patient();
            zPatient.SSNumber = SS_NUMBERS[index];
            zPatient.FirstName = FIRST_NAMES[index];
            zPatient.LastName = LAST_NAMES[index];

            var zAddress = new Address();
            zAddress.Street = "123 " + STREET_NAMES[index];
            zAddress.City = CITY_NAMES[index];
            zAddress.State = STATE_NAMES[index];
            zAddress.Zip = ZIP_CODES[index];
            zPatient.AddAddress( zAddress );

            for ( int i = 0; i < DRUG_NAMES.Length; i++ )
            {
                var pr = new Perscription();
                pr.Drug = DRUG_NAMES[i];
                pr.Dose = "10 tsp";
                pr.EndDate = new DateTime( 2009, 11, 05 );
                pr.ProviderId = PROVIDER_NUMBERS[i];

                zPatient.AddPerscription( pr );
            }

            return zPatient;
        }

        public class ObjectWithByteFields
        {
            private byte m_B1;
            private byte m_B2;

            public byte B1
            {
                get { return m_B1; }
                set { m_B1 = value; }
            }

            public byte B2
            {
                get { return m_B2; }
                set { m_B2 = value; }
            }
        }

        public class ObjectWithByteObjectFields
        {
            private Byte? m_B1;
            private Byte? m_B2;

            public byte B1
            {
                get { return m_B1.Value; }
                set { m_B1 = value; }
            }

            public byte B2
            {
                get { return m_B2.Value; }
                set { m_B2 = value; }
            }
        }

        [Test]
        public void TestObjectWithByte()
        {
            ObjectWithByteFields o1 = new ObjectWithByteFields();
            o1.B1 = 12;
            o1.B2 = 99;
            FileRoundTripObjectEquivalent( "ObjectWithByteFields", o1 );
            SaveType( "ObjectWithByteFields", o1 );

            ObjectWithByteObjectFields o2 = new ObjectWithByteObjectFields();
            o2.B1 = 12;
            o2.B2 = 99;
            FileRoundTripObjectEquivalent( "ObjectWithByteObjectFields", o2 );
            SaveType( "ObjectWithByteObjectFields", o2 );
        }

        private class ComplexString
        {
            public string mTest = "\r\n\x000C \"Plus Some Text";
        }

        [Test]
        public void TestComplexString()
        {
            ComplexString test = new ComplexString();
            String data = mEasy.Format( test );
            Assert.False( data.Contains( "\r" ) );
            Assert.False( data.Contains( "\n" ) );
            Assert.False( data.Contains( "\x000C" ) );
            Assert.AreEqual( test.mTest, ((ComplexString) mEasy.Parse( data )).mTest );
        }

        [Test]
        public void TestGuid()
        {
            Guid zGuid = new Guid();
            FileRoundTripObjectEquivalent( "Guid", zGuid );
        }

        public class Enums
        {
            public PatientIDType mPatientIDType;
            public ServiceProviderIDType mProviderIDType;

            public Enums Init()
            {
                mPatientIDType = PatientIDType.Ssn;
                mProviderIDType = ServiceProviderIDType.StateIssued;
                return this;
            }
        }

        [Test]
        public void TestEnum()
        {
            var zEnums = new Enums();
            FileRoundTripObjectEquivalent( "Enums", zEnums.Init() );
            SaveType( "Enums", zEnums );
        }

        public class ArrayLists
        {
            public ArrayList mArrayList1;
            public ArrayList mArrayList2;
            public ArrayList mArrayList3;

            public ArrayLists Init()
            {
                mArrayList1 = new ArrayList();
                mArrayList1.Add( "Test" );
                mArrayList1.Add( null );
                mArrayList1.Add( 1 );

                mArrayList2 = new ArrayList();

                return this;
            }
        }

        [Test]
        public void TestArrayLists()
        {
            var zArrayLists = new ArrayLists();
            FileRoundTripObjectEquivalent( "ArrayLists", zArrayLists.Init() );
            SaveType( "ArrayLists", zArrayLists );
        }

        public class Hashtables
        {
            public Hashtable mHashtable1;
            public Hashtable mHashtable2;
            public Hashtable mHashtable3;

            public Hashtables Init()
            {
                mHashtable1 = new Hashtable();
                mHashtable1.Add( "A", "Test" );
                mHashtable1.Add( "C", null );
                mHashtable1.Add( "B", 1 );

                mHashtable2 = new Hashtable();

                return this;
            }
        }

        [Test]
        public void TestHashtables()
        {
            var zHashtables = new Hashtables();
            FileRoundTripObjectEquivalent( "Hashtables", zHashtables.Init() );
            SaveType( "Hashtables", zHashtables );
        }

        public class GenericLists
        {
            public List<string> mGenericList1;
            public List<int> mGenericList2;
            public List<PatientIdentifier> mGenericList3;
            public List<string> mGenericList4;
            public List<string> mGenericList5;

            public GenericLists Init()
            {
                mGenericList1 = new List<string>();
                mGenericList1.Add( "Test" );
                mGenericList1.Add( null );

                mGenericList2 = new List<int>();
                mGenericList2.Add( 1 );

                mGenericList3 = new List<PatientIdentifier>();
                mGenericList3.Add( new PatientIdentifier( PatientIDType.DriversLicense, "XYZZY1234" ) );

                mGenericList4 = new List<string>();

                return this;
            }
        }

        [Test]
        public void TestGenericLists()
        {
            var zGenericLists = new GenericLists();
            FileRoundTripObjectEquivalent( "GenericLists", zGenericLists.Init() );
            SaveType( "GenericListsCSharp", zGenericLists );
        }

        public class GenericDictionarys
        {
            public Dictionary<string, string> mGenericDictionary1;
            public Dictionary<int, string> mGenericDictionary2;
            public Dictionary<string, PatientIdentifier> mGenericDictionary3;
            public Dictionary<string, string> mGenericDictionary4;
            public Dictionary<string, string> mGenericDictionary5;
            public Dictionary<string, Dictionary<string, string>> mGenGen;

            public GenericDictionarys Init()
            {
                mGenericDictionary1 = new Dictionary<string, string>();
                mGenericDictionary1.Add( "A1", "Test" );
                mGenericDictionary1.Add( "B2", null );

                mGenericDictionary2 = new Dictionary<int, string>();
                mGenericDictionary2.Add( 1, "Test2" );

                mGenericDictionary3 = new Dictionary<string, PatientIdentifier>();
                mGenericDictionary3.Add( "4", new PatientIdentifier( PatientIDType.DriversLicense, "XYZZY1234" ) );

                mGenericDictionary4 = new Dictionary<string, string>();

                mGenGen = new Dictionary<string, Dictionary<string, string>>();

                return this;
            }
        }

        [Test]
        public void TestGenericDictionarys()
        {
            var zGenericDictionarys = new GenericDictionarys();
            FileRoundTripObjectEquivalent( "GenericDictionarys", zGenericDictionarys.Init() );
            SaveType( "GenericDictionarysCSharp", zGenericDictionarys );
        }

        public class ArraySamples
        {
            public int[] mArray1;
            public string[] mArray2;
            public string[] mArray3;
            public string[] mArray4;
            public PatientIdentifier[] mPIs;
            public int[][] mIntOfInts;

            public ArraySamples Init()
            {
                mArray1 = new[] {1, 2, 3, 4};
                mArray2 = new string[0];
                mArray3 = new[] {"Test", "of", "the", "american", "broadcasting system"};
                mPIs = new[] {new PatientIdentifier( PatientIDType.DriversLicense, "XYZZY1234" ),};
                mIntOfInts = new[] {new int[0], new[] {5, 6, 7}};

                return this;
            }
        }

        [Test]
        public void TestArraySamples()
        {
            var zArraySamples = new ArraySamples();
            FileRoundTripObjectEquivalent( "ArraySamples", zArraySamples.Init() );
            SaveType( "ArraySamples", zArraySamples );
        }

        public class Decimals
        {
            public decimal mDec1;
            public decimal mDec2;
            public decimal mDec3;

            public Decimals Init()
            {
                mDec3 = 42.7m;
                mDec2 = 0.5m;
                mDec1 = 42;
                return this;
            }
        }

        [Test]
        public void TestDecimal()
        {
            var zDecimals = new Decimals();
            FileRoundTripObjectEquivalent( "Decimals", zDecimals.Init() );
            SaveType( "DecimalsCSharp", zDecimals );
        }

        public class Primatives
        {
            public bool mBoolean1;
            public Boolean? mBoolean2;
            public char mChar1;
            public Char? mChar2;
            public byte mByte1;
            public Byte? mByte2;
            public short mShort1;
            public Int16? mShort2;
            public int mInt1;
            public Int32? mInt2;
            public long mLong1;
            public Int64? mLong2;
            public float mSingle1;
            public Single? mSingle2;
            public double mDouble1;
            public Double? mDouble2;

            public Primatives Init()
            {
                mBoolean1 = true;
                mBoolean2 = true;
                mChar1 = 'Z';
                mChar2 = 'Z';
                mByte1 = 42;
                mByte2 = 42;
                mShort1 = 42;
                mShort2 = 42;
                mInt1 = 42;
                mInt2 = 42;
                mLong1 = 42;
                mLong2 = 42;
                mSingle1 = -34.56F;
                mSingle2 = -34.56F;
                mDouble1 = 34.56;
                mDouble2 = 34.56;
                return this;
            }
        }

        [Test]
        public void TestPrimatives()
        {
            var zPrimative = new Primatives().Init();
            FileRoundTripObjectEquivalent( "Primatives", zPrimative );
            SaveType( "Primatives", zPrimative );

            AssertPrimative( zPrimative.mBoolean1, typeof (bool) );
            AssertPrimative( zPrimative.mBoolean2, typeof (Boolean) );
            AssertPrimative( zPrimative.mChar1, typeof (char) );
            AssertPrimative( zPrimative.mChar2, typeof (Char) );
            AssertPrimative( zPrimative.mShort1, typeof (short) );
            AssertPrimative( zPrimative.mShort2, typeof (Int16) );
            AssertPrimative( zPrimative.mInt1, typeof (int) );
            AssertPrimative( zPrimative.mInt2, typeof (Int32) );
            AssertPrimative( zPrimative.mLong1, typeof (long) );
            AssertPrimative( zPrimative.mLong2, typeof (Int64) );
            AssertPrimative( zPrimative.mSingle1, typeof (float) );
            AssertPrimative( zPrimative.mSingle2, typeof (Single) );
            AssertPrimative( zPrimative.mDouble1, typeof (double) );
            AssertPrimative( zPrimative.mDouble2, typeof (Double) );
        }

        private static void AssertPrimative( object pWhat, Type pType )
        {
            Assert.True( pWhat is ValueType );
            Type zType = pWhat.GetType();
            Assert.AreEqual( pType, zType );
            Assert.True( zType.IsPrimitive );
        }

        private static void AssertValueTypeNonPrimative( object pWhat, Type pType )
        {
            Assert.True( pWhat is ValueType );
            Type zType = pWhat.GetType();
            Assert.AreEqual( pType, zType );
            Assert.False( zType.IsPrimitive );
        }

        private static void AssertReferenceType( object pWhat, Type pType )
        {
            Assert.False( pWhat is ValueType );
            Type zType = pWhat.GetType();
            Assert.AreEqual( pType, zType );
            Assert.False( zType.IsPrimitive );
        }

        public class WishWerePrimatives
        {
            public decimal mDecimal1;
            public Decimal mDecimal2;

            public string mString1;
            public String mString2;

            public WishWerePrimatives Init()
            {
                mDecimal1 = 34.56m;
                mDecimal2 = 34.56m;
                mString1 = "Fee Fie Foe Fum";
                mString2 = "Fee Fie Foe Fum";
                return this;
            }
        }

        [Test]
        public void TestWishWerePrimatives()
        {
            var zWishWerePrimative = new WishWerePrimatives().Init();
            FileRoundTripObjectEquivalent( "WishWerePrimatives", zWishWerePrimative );
            SaveType( "WishWerePrimativesCSharp", zWishWerePrimative );

            AssertValueTypeNonPrimative( zWishWerePrimative.mDecimal1, typeof (Decimal) );
            AssertValueTypeNonPrimative( zWishWerePrimative.mDecimal2, typeof (Decimal) );

            AssertReferenceType( zWishWerePrimative.mString1, typeof (String) );
            AssertReferenceType( zWishWerePrimative.mString2, typeof (String) );
        }

        public class WrappedPrimitives
        {
            public bool? mBoolean1;
            public char? mChar1;
            public byte? mByte1;
            public short? mShort1;
            public int? mInt1;
            public long? mLong1;
            public float? mSingle1;
            public double? mDouble1;

            public bool? mBoolean2;
            public char? mChar2;
            public byte? mByte2;
            public short? mShort2;
            public int? mInt2;
            public long? mLong2;
            public float? mSingle2;
            public double? mDouble2;

            public WrappedPrimitives Init()
            {
                mBoolean1 = true;
                mChar1 = 'Z';
                mByte1 = 42;
                mShort1 = 42;
                mInt1 = 42;
                mLong1 = 42;
                mSingle1 = -34.56F;
                mDouble1 = 34.56;
                return this;
            }
        }

        [Test]
        public void TestWrappedPrimitives()
        {
            var zWrappedPrimitive = new WrappedPrimitives().Init();
            FileRoundTripObjectEquivalent( "WrappedPrimitives", zWrappedPrimitive );
            SaveType( "WrappedPrimitives", zWrappedPrimitive );

            AssertPrimative( zWrappedPrimitive.mBoolean1, typeof (Boolean) );
            Assert.IsNull( zWrappedPrimitive.mBoolean2 );
            AssertPrimative( zWrappedPrimitive.mChar1, typeof (Char) );
            Assert.IsNull( zWrappedPrimitive.mChar2 );
            AssertPrimative( zWrappedPrimitive.mShort1, typeof (Int16) );
            Assert.IsNull( zWrappedPrimitive.mShort2 );
            AssertPrimative( zWrappedPrimitive.mInt1, typeof (Int32) );
            Assert.IsNull( zWrappedPrimitive.mInt2 );
            AssertPrimative( zWrappedPrimitive.mLong1, typeof (Int64) );
            Assert.IsNull( zWrappedPrimitive.mLong2 );
            AssertPrimative( zWrappedPrimitive.mSingle1, typeof (Single) );
            Assert.IsNull( zWrappedPrimitive.mSingle2 );
            AssertPrimative( zWrappedPrimitive.mDouble1, typeof (Double) );
            Assert.IsNull( zWrappedPrimitive.mDouble2 );
        }

        public class WrappedWishWerePrimitives
        {
            public decimal? mDecimal1;

            public decimal? mDecimal2;

            public WrappedWishWerePrimitives Init()
            {
                mDecimal1 = 34.56m;
                return this;
            }
        }

        [Test]
        public void TestWrappedWishWerePrimitives()
        {
            var zWrappedWishWerePrimitive = new WrappedWishWerePrimitives().Init();
            FileRoundTripObjectEquivalent( "WrappedWishWerePrimitives", zWrappedWishWerePrimitive );
            SaveType( "WrappedWishWerePrimitives", zWrappedWishWerePrimitive );

            AssertValueTypeNonPrimative( zWrappedWishWerePrimitive.mDecimal1, typeof (Decimal) );

            Assert.IsNull( zWrappedWishWerePrimitive.mDecimal2 );
        }

        public class DateTimeCarrier
        {
            private DateTime mDateTime1;

            public DateTimeCarrier Init()
            {
                DateTime zDateTime = DateTime.Parse( "1/24/2010 3:23:36 AM" );
                DateTime2 = mDateTime1 = zDateTime;
                return this;
            }

            public DateTime DateTime1
            {
                get { return mDateTime1; }
                set { mDateTime1 = value; }
            }

            public DateTime DateTime2 { get; set; }

            public DateTime DateTime3 { get; set; }
        }

        [Test]
        public void TestDate()
        {
            var zCarrier = new DateTimeCarrier().Init();
            FileRoundTripObjectEquivalent( "DateTimeCarrier", zCarrier );
            SaveType( "DateTimeCarrierCSharp", zCarrier );
        }

        [Test]
        public void TestBooleanAssumptions()
        {
            Assert.AreEqual( "False", Boolean.FalseString );
            Assert.AreEqual( "True", Boolean.TrueString );
        }

        private bool mBool1 = true;
        private bool? mBool2;

        private bool[] mBoolArray1 = new[] {true};
        private bool?[] mBoolArray2 = new bool?[] {true, null};

        [Test]
        public void TestWrappedDiff()
        {
            mBool1 = false;
            mBool2 = false;

            Type zType1 = mBool1.GetType();
            Type zType2 = mBool2.GetType();

            Assert.False( IsNullableValueType( zType1 ) );
            Assert.False( IsNullableValueType( zType2 ) );

            Assert.True( zType1 == zType2 );

            zType1 = typeof (bool);
            zType2 = typeof (bool?);

            Assert.False( IsNullableValueType( zType1 ) );
            Assert.True( IsNullableValueType( zType2 ) );

            Assert.False( zType1 == zType2 );

            Assert.True( zType1.IsAssignableFrom( typeof (bool) ) );
            Assert.False( zType1.IsAssignableFrom( typeof (bool?) ) );

            Assert.True( zType2.IsAssignableFrom( typeof (bool) ) );
            Assert.True( zType2.IsAssignableFrom( typeof (bool?) ) );

            string zFoundName = null;

            PropertyInfo[] zProperties = GetType().GetProperties();

            foreach ( PropertyInfo zInfo in zProperties )
            {
                Type t = zInfo.PropertyType;
                if ( IsNullableValueType( t ) )
                {
                    Assert.IsNull( zFoundName );
                    zFoundName = zInfo.Name;
                }
            }

            FieldInfo[] zFields = GetType().GetFields( BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public );

            foreach ( FieldInfo zInfo in zFields )
            {
                Type t = zInfo.FieldType;
                if ( IsNullableValueType( t ) )
                {
                    Assert.IsNull( zFoundName );
                    zFoundName = zInfo.Name;
                }
            }

            Assert.AreEqual( "mBool2", zFoundName );

            Assert.AreEqual( typeof (bool), mBoolArray1.GetType().GetElementType() );
            Assert.AreEqual( typeof (bool?), mBoolArray2.GetType().GetElementType() );
        }

        private static bool IsNullableValueType( Type pType )
        {
            if ( pType.IsSubclassOf( typeof (ValueType) ) )
            {
                return (pType.IsGenericType && (pType.GetGenericTypeDefinition() == typeof (Nullable<>)));
            }
            return false;
        }

        [Test]
        public void TestArrayResolve()
        {
            int[] zInts = new int[0];
            string zTypeString = zInts.GetType().ToString();

            Type zType = Util.ResolveType( zTypeString );

            Assert.AreEqual( zTypeString, zType.ToString() );
        }

        [Test]
        public void CourseListTest()
        {
            var list = new ArrayList();
            list.Add( new Course( 6756, "XML and Related Technologies", 3 ) );
            list.Add( new Course( 9865, "Object Oriented Programming", 2 ) );
            list.Add( new Course( 1134, "E-Commerce Programming", 2 ) );
            list.Add( new Course( 4598, "Enterprise Component Architecture", 3 ) );

            RoundTripObjectEquivalent( list );
        }

        [Test]
        public void CourseMapTest()
        {
            var map = new Hashtable();
            map.Add( 6756, new Course( 6756, "XML and Related Technologies", 3 ) );
            map.Add( 9865, new Course( 9865, "Object Oriented Programming", 2 ) );
            map.Add( 1134, new Course( 1134, "E-Commerce Programming", 2 ) );
            map.Add( 4598, new Course( 4598, "Enterprise Component Architecture", 3 ) );

            RoundTripObjectEquivalent( map );
        }

        [Test]
        public void DateTest()
        {
            DateTime dt1 = DateTime.Now;
            RoundTripObjectEquivalent( dt1 );

            string zDateAsString = "1957/01/04-13:14:15.016";

            DateTimeConverter zConverter = new DateTimeConverter().AttemptParse( zDateAsString );
            DateTime zDate = zConverter.ToDateTime();
            Assert.AreEqual( zDateAsString, new DateTimeConverter( zDate ).ToString() );
        }

        [Test]
        public void DecimalTest()
        {
            decimal t0 = 42;
            RoundTripObjectEquivalent( t0 );
            t0 = 0.5m;
            RoundTripObjectEquivalent( t0 );
            t0 = 42.7m;
            RoundTripObjectEquivalent( t0 );
        }

        [Test]
        public void EnumArrayTest()
        {
            GiantEnum[] t0 = {GiantEnum.Fee, GiantEnum.Foe, GiantEnum.Fee,};
            RoundTripObjectEquivalent( t0 );

            ReviewEnum[] t1 = {ReviewEnum.Glowing, ReviewEnum.Lame, ReviewEnum.Miserable,};
            RoundTripObjectEquivalent( t1 );
        }

        [Test]
        public void EnumTest()
        {
            GiantEnum t0 = GiantEnum.Fee;
            RoundTripObjectEquivalent( t0 );

            ReviewEnum t1 = ReviewEnum.Glowing;
            RoundTripObjectEquivalent( t1 );
        }

        [Test]
        public void MultiArrayTest()
        {
            var test = new MultiArrays( new[]
                                            {
                                                new[] {23, 56, 89, 36, 68},
                                                new[] {87, 64, 88, 32},
                                                new[] {78, 80, 21, 29, 34, 67}
                                            } );

            RoundTripObjectEquivalent( test );
        }

        [Test]
        public void PrimativeArrayTest()
        {
            Int16[] t0 = {42, 43, 67, 88, 0};
            RoundTripObjectEquivalent( t0 );

            Int32[] t1 = {42, 43, 67, 88, 0};
            RoundTripObjectEquivalent( t1 );

            Int64[] t2 = {42, 43, 67, 88, 0};
            RoundTripObjectEquivalent( t2 );

            Double[] f1 = {34.56, 12, 3, 45, 67, 89, 0.34};
            RoundTripObjectEquivalent( f1 );

            String[] t3 = {"Fee Fie Foe Fum", "Hello", "World"};
            RoundTripObjectEquivalent( t3 );

            Single[] f2 = {-34.56F, 12, 3F, 45F, 67F, 89F, 0.34F};
            RoundTripObjectEquivalent( f2 );

            Boolean[] b1 = {true, true, false, false, true};
            RoundTripObjectEquivalent( b1 );

            Char[] c1 = {'Z', 'F', 'g', 't'};
            RoundTripObjectEquivalent( c1 );
        }

        [Test]
        public void PrimativeTest()
        {
            Int16 t0 = 42;
            RoundTripObjectEquivalent( t0 );

            Int32 t1 = 42;
            RoundTripObjectEquivalent( t1 );

            Int64 t2 = 42;
            RoundTripObjectEquivalent( t2 );

            Double f1 = 34.56;
            RoundTripObjectEquivalent( f1 );

            String t3 = "Fee Fie Foe Fum";
            RoundTripObjectEquivalent( t3 );

            Single f2 = -34.56F;
            RoundTripObjectEquivalent( f2 );

            Boolean b1 = true;
            RoundTripObjectEquivalent( b1 );

            Char c1 = 'Z';
            RoundTripObjectEquivalent( c1 );
        }

        [Test]
        public void ReferencesTest()
        {
            var p1 = new Product( "Baked beans", 1.75, 250, true, 'B' );
            var p2 = new Product( "Basmati Rice", 3.89, 750, true, 'R' );
            var p3 = new Product( "White bread", 1.06, 300, false, 'H' );
            var products = new[] {p1, p2, p1, p3, p3, p1};

            RoundTripObjectEquivalent( products );
        }

        [Test]
        ///
        /// PAtients use circular references and generics
        public void TestPatients()
        {
            Patient[] zPatients = BuildPatients();

            RoundTripObjectEquivalent( zPatients );
        }
    }
}

Commits for WOX2/trunk/CSharp/tests/wox/serial/tests/SerializationTests.cs

Diff revisions: vs.
Revision Author Commited Message
14 Diff Diff GeorgeS picture GeorgeS Fri 19 Feb, 2010 19:15:03 +0000
7 Diff Diff GeorgeS picture GeorgeS Fri 05 Feb, 2010 19:14:48 +0000
5 Diff Diff GeorgeS picture GeorgeS Fri 05 Feb, 2010 18:59:04 +0000
4 Diff Diff GeorgeS picture GeorgeS Fri 05 Feb, 2010 18:15:55 +0000
3 GeorgeS picture GeorgeS Thu 04 Feb, 2010 23:53:47 +0000