File: FormatterServicesTests.cs

package info (click to toggle)
mono 1.2.2.1-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 142,720 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,327; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (127 lines) | stat: -rw-r--r-- 2,494 bytes parent folder | download | duplicates (3)
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
//
// System.Runtime.Serialization.FormatterServicesTests: NUnit test
//
// Authors: 
// 	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) 2002 Ximian Inc. (http://www.ximian.com)
//

using NUnit.Framework;
using System;
using System.Reflection;
using System.Runtime.Serialization;

namespace MonoTests.System.Runtime.Serialization
{
	public class FormatterServicesTests : TestCase
	{
		public void TestClass1 ()
		{
			DerivedClass1 derived = new DerivedClass1 ();
			derived.anotherInt = 69;
			MemberInfo [] members = FormatterServices.GetSerializableMembers (derived.GetType ());
			Assert ("#01", members != null);
			AssertEquals ("#02", 3, members.Length);

			object [] data = FormatterServices.GetObjectData (derived, members);
			Assert ("#03", data != null);
			AssertEquals ("#04", 3, data.Length);

			DerivedClass1 o = (DerivedClass1) FormatterServices.GetUninitializedObject (derived.GetType ());
			Assert ("#05", o != null);

			o = (DerivedClass1) FormatterServices.PopulateObjectMembers (o, members, data);
			Assert ("#06", o != null);
			AssertEquals ("#07", "hola", o.Hello);
			AssertEquals ("#08", 21, o.IntBase);
			AssertEquals ("#09", 1, o.IntDerived);
			AssertEquals ("#10", 69, o.anotherInt);
			AssertEquals ("#11", "hey", DerivedClass1.hey);
		}
	}

	[Serializable]
	class BaseClass1
	{
		public string hello = "hola";
		static int intBase = 21;

		public override int GetHashCode ()
		{
			return base.GetHashCode ();
		}

		public override bool Equals (object o)
		{
			BaseClass1 bc = o  as BaseClass1;
			if (o == null)
				return false;

			if (hello != "hola")
				return false;

			return (hello == bc.hello);
		}

		public string Hello
		{
			get {
				return hello;
			}
		}

		public int IntBase
		{
			get {
				return intBase;
			}
		}
	}
	
	[Serializable]
	class DerivedClass1 : BaseClass1
	{
		private int intDerived = 1;
		[NonSerialized] public int publicint = 2;
		public int anotherInt = 22;
		public static string hey = "hey";

		public string Name 
		{
			get {
				return "Ahem";
			}
		}

		public void SomeMethod ()
		{
			/* Does nothing */
		}

		public override int GetHashCode ()
		{
			return base.GetHashCode ();
		}

		public override bool Equals (object o)
		{
			DerivedClass1 dc = o  as DerivedClass1;
			if (o == null)
				return false;

			if (anotherInt != 22 || hey != "hey")
				return false;
			
			return (anotherInt == dc.anotherInt);
		}

		public int IntDerived
		{
			get {
				return intDerived;
			}
		}
	}
}