File: SignatureTest.cs

package info (click to toggle)
dbus-sharp 0.8.1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 868 kB
  • sloc: cs: 7,690; sh: 494; makefile: 181
file content (219 lines) | stat: -rw-r--r-- 5,234 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
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
// Copyright 2009 Alp Toker <alp@atoker.com>
// This software is made available under the MIT License
// See COPYING for details

using System;
using System.Linq;
using System.Collections.Generic;

using NUnit.Framework;
using DBus;
using DBus.Protocol;

namespace DBus.Tests
{
	[TestFixture]
	public class SignatureTest
	{
		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void Parse_NullString ()
		{
			new Signature ((string) null);
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void Parse_NullArray ()
		{
			new Signature ((DType []) null);
		}
		
		[Test]
		public void Parse_Empty ()
		{
			var x = new Signature ("");
			Assert.AreEqual (Signature.Empty, x, "#1");
		}
		
		[Test]
		public void ParseStruct ()
		{
			var sig = new Signature ("(iu)");
			Assert.IsTrue (sig.IsStruct, "#1");
			
			var elements = sig.GetFieldSignatures ().ToArray ();
			Assert.AreEqual (2, elements.Length, "#2");
			Assert.AreEqual (Signature.Int32Sig, elements [0], "#3");
			Assert.AreEqual (Signature.UInt32Sig, elements [1], "#4");
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void ParseInvalid_TypeCode ()
		{
			// Use an invalid type code
			new Signature ("z");
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void ParseInvalid_MissingClosingBrace ()
		{
			// Use an invalid type code
			new Signature ("(i");
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void ParseInvalid_MissingOpeningBrace ()
		{
			// Use an invalid type code
			new Signature ("i)");
		}
		
		[Test]
		public void Parse_ArrayOfString ()
		{
			string sigText = "as";
			Signature sig = new Signature (sigText);

			Assert.IsTrue (sig.IsArray);
			Assert.IsFalse (sig.IsDict);
			Assert.IsFalse (sig.IsPrimitive);
		}

		[Test]
		public void Equality ()
		{
			string sigText = "as";
			Signature a = new Signature (sigText);
			Signature b = new Signature (sigText);

			Assert.IsTrue (a == b);
			Assert.IsTrue (a.GetElementSignature () == Signature.StringSig);

			Assert.AreEqual (Signature.ArraySig + Signature.StringSig + Signature.Empty, new Signature ("as"));
		}

		[Test]
		public void FixedSize ()
		{
			Signature sig;

			sig = new Signature ("s");
			Assert.IsFalse (sig.IsFixedSize);

			sig = new Signature ("as");
			Assert.IsFalse (sig.IsFixedSize);

			sig = new Signature ("u");
			Assert.IsTrue (sig.IsFixedSize);

			sig = new Signature ("u(ub)");
			Assert.IsTrue (sig.IsFixedSize);

			sig = new Signature ("u(uvb)");
			Assert.IsFalse (sig.IsFixedSize);
		}
		
		[Test]
		public void CombineSignatures ()
		{
			var x = Signature.ByteSig + Signature.StringSig;
			Assert.AreEqual ("ys", x.Value, "#1");
		}
		
		[Test]
		public void MakeArray ()
		{
			var x = Signature.MakeArray (Signature.Int32Sig);
			Assert.AreEqual ("ai", x.Value, "#1");
		}
		
		[Test]
		public void MakeArrayOfStruct ()
		{
			var type = Signature.MakeStruct (Signature.Int32Sig + Signature.Int32Sig);
			var x = Signature.MakeArray (type);
			Assert.AreEqual ("a(ii)", x.Value, "#1");
		}
		
		[Test]
		public void MakeArrayOfArray ()
		{
			var x = Signature.MakeArray (Signature.Int32Sig);
			x = Signature.MakeArray (x);
			Assert.AreEqual ("aai", x.Value, "#1");
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void MakeArray_NotSingleCompleteType ()
		{
			Signature.MakeArray (Signature.Int32Sig + Signature.UInt16Sig);
		}
		
		[Test]
		public void MakeStruct ()
		{
			// 'r' isn't used, just brackets.
			var x = Signature.MakeStruct (Signature.ByteSig + Signature.StringSig);
			Assert.AreEqual ("(ys)", x.Value, "#1");
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void MakeStruct_Empty ()
		{
			Signature.MakeStruct (Signature.Empty);
		}
		
		[Test]
		public void MakeDictionaryEntry ()
		{
			// Make a valid dictionary entry, should appear as an array of dict_entries
			var x = Signature.MakeDictEntry (Signature.StringSig, Signature.Int32Sig);
			Assert.AreEqual ("{si}", x.Value, "#1");
		}
		
		[Test]
		public void MakeDictionary ()
		{
			// 'r' isn't used, just brackets.
			var x = Signature.MakeDict (Signature.StringSig, Signature.Int32Sig);
			Assert.AreEqual ("a{si}", x.Value, "#1");
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void MakeDictionary_TwoCompleteTypes_Key ()
		{
			// They key is not a single complete type
			 Signature.MakeDictEntry (Signature.StringSig + Signature.Int32Sig, Signature.Int32Sig);
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void MakeDictionary_TwoCompleteTypes_Value ()
		{
			// They value is not a single complete type
			Signature.MakeDictEntry (Signature.StringSig, Signature.Int32Sig + Signature.Int32Sig);
		}

		[Test]
		public void ComplexSignatureIsSingleTypeTest ()
		{
			string sig = "(ssa{i(ss)})";
			Assert.IsTrue (new Signature (sig).IsSingleCompleteType);
		}

		[Test]
		public void AssertComplexTypeToTypeTest ()
		{
			var sig = new Signature ("(ssa{i(ss)})");
			Assert.AreEqual (typeof (DBusStruct<string, string, Dictionary<int, DBusStruct<string, string>>>),
			                 sig.ToType ());
		}
	}
}