How to use EnumDataSource class of NUnit.Framework package

Best Nunit code snippet using NUnit.Framework.EnumDataSource

Run Nunit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

RandomAttribute.cs

Source: RandomAttribute.cs Github

copy
1// ***********************************************************************
2// Copyright (c) 2008-2015 Charlie Poole
3//
4// Permission is hereby granted, free of charge, to any person obtaining
5// a copy of this software and associated documentation files (the
6// "Software"), to deal in the Software without restriction, including
7// without limitation the rights to use, copy, modify, merge, publish,
8// distribute, sublicense, and/or sell copies of the Software, and to
9// permit persons to whom the Software is furnished to do so, subject to
10// the following conditions:
11// 
12// The above copyright notice and this permission notice shall be
13// included in all copies or substantial portions of the Software.
14// 
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22// ***********************************************************************
23
24using System;
25using System.Collections;
26using System.Collections.Generic;
27using System.Globalization;
28using System.Reflection;
29using NUnit.Compatibility;
30using NUnit.Framework.Interfaces;
31using NUnit.Framework.Internal;
32
33namespace NUnit.Framework
34{
35    /// <summary>
36    /// RandomAttribute is used to supply a set of random _values
37    /// to a single parameter of a parameterized test.
38    /// </summary>
39    public class RandomAttribute : DataAttribute, IParameterDataSource
40    {
41        private RandomDataSource _source;
42        private int _count;
43
44        #region Constructors
45
46        /// <summary>
47        /// Construct a random set of values appropriate for the Type of the 
48        /// parameter on which the attribute appears, specifying only the count.
49        /// </summary>
50        /// <param name="count"></param>
51        public RandomAttribute(int count)
52        {
53            _count = count;
54        }
55
56        /// <summary>
57        /// Construct a set of ints within a specified range
58        /// </summary>
59        public RandomAttribute(int min, int max, int count)
60        {
61            _source = new IntDataSource(min, max, count);
62        }
63
64        /// <summary>
65        /// Construct a set of unsigned ints within a specified range
66        /// </summary>
67        [CLSCompliant(false)]
68        public RandomAttribute(uint min, uint max, int count)
69        {
70            _source = new UIntDataSource(min, max, count);
71        }
72
73        /// <summary>
74        /// Construct a set of longs within a specified range
75        /// </summary>
76        public RandomAttribute(long min, long max, int count)
77        {
78            _source = new LongDataSource(min, max, count);
79        }
80
81        /// <summary>
82        /// Construct a set of unsigned longs within a specified range
83        /// </summary>
84        [CLSCompliant(false)]
85        public RandomAttribute(ulong min, ulong max, int count)
86        {
87            _source = new ULongDataSource(min, max, count);
88        }
89
90        /// <summary>
91        /// Construct a set of shorts within a specified range
92        /// </summary>
93        public RandomAttribute(short min, short max, int count)
94        {
95            _source = new ShortDataSource(min, max, count);
96        }
97
98        /// <summary>
99        /// Construct a set of unsigned shorts within a specified range
100        /// </summary>
101        [CLSCompliant(false)]
102        public RandomAttribute(ushort min, ushort max, int count)
103        {
104            _source = new UShortDataSource(min, max, count);
105        }
106
107        /// <summary>
108        /// Construct a set of doubles within a specified range
109        /// </summary>
110        public RandomAttribute(double min, double max, int count)
111        {
112            _source = new DoubleDataSource(min, max, count);
113        }
114
115        /// <summary>
116        /// Construct a set of floats within a specified range
117        /// </summary>
118        public RandomAttribute(float min, float max, int count)
119        {
120            _source = new FloatDataSource(min, max, count);
121        }
122
123        /// <summary>
124        /// Construct a set of bytes within a specified range
125        /// </summary>
126        public RandomAttribute(byte min, byte max, int count)
127        {
128            _source = new ByteDataSource(min, max, count);
129        }
130
131        /// <summary>
132        /// Construct a set of sbytes within a specified range
133        /// </summary>
134        [CLSCompliant(false)]
135        public RandomAttribute(sbyte min, sbyte max, int count)
136        {
137            _source = new SByteDataSource(min, max, count);
138        }
139
140        #endregion
141
142        #region IParameterDataSource Interface
143
144        /// <summary>
145        /// Get the collection of _values to be used as arguments.
146        /// </summary>
147        public IEnumerable GetData(IParameterInfo parameter)
148        {
149            // Since a separate Randomizer is used for each parameter,
150            // we can't fill in the data in the constructor of the
151            // attribute. Only now, when GetData is called, do we have
152            // sufficient information to create the values in a 
153            // repeatable manner.
154
155            Type parmType = parameter.ParameterType;
156
157            if (_source == null)
158            {
159                if (parmType == typeof(int))
160                    _source = new IntDataSource(_count);
161                else if (parmType == typeof(uint))
162                    _source = new UIntDataSource(_count);
163                else if (parmType == typeof(long))
164                    _source = new LongDataSource(_count);
165                else if (parmType == typeof(ulong))
166                    _source = new ULongDataSource(_count);
167                else if (parmType == typeof(short))
168                    _source = new ShortDataSource(_count);
169                else if (parmType == typeof(ushort))
170                    _source = new UShortDataSource(_count);
171                else if (parmType == typeof(double))
172                    _source = new DoubleDataSource(_count);
173                else if (parmType == typeof(float))
174                    _source = new FloatDataSource(_count);
175                else if (parmType == typeof(byte))
176                    _source = new ByteDataSource(_count);
177                else if (parmType == typeof(sbyte))
178                    _source = new SByteDataSource(_count);
179                else if (parmType == typeof(decimal))
180                    _source = new DecimalDataSource(_count);
181                else if (parmType.GetTypeInfo().IsEnum)
182                    _source = new EnumDataSource(_count);
183                else // Default
184                    _source = new IntDataSource(_count);
185            }
186            else if (_source.DataType != parmType && WeConvert(_source.DataType, parmType))
187            {
188                _source = new RandomDataConverter(_source);
189            }
190
191            return _source.GetData(parameter);
192
193            //// Copy the random _values into the data array
194            //// and call the base class which may need to
195            //// convert them to another type.
196            //this.data = new object[values.Count];
197            //for (int i = 0; i < values.Count; i++)
198            //    this.data[i] = values[i];
199
200            //return base.GetData(parameter);
201        }
202
203        private bool WeConvert(Type sourceType, Type targetType)
204        {
205            if (targetType == typeof(short) || targetType == typeof(ushort) || targetType == typeof(byte) || targetType == typeof(sbyte))
206                return sourceType == typeof(int);
207
208            if (targetType == typeof(decimal))
209                return sourceType == typeof(int) || sourceType == typeof(double);
210            
211            return false;
212        }
213
214        #endregion
215
216        #region Nested DataSource Classes
217
218        #region RandomDataSource
219
220        abstract class RandomDataSource : IParameterDataSource
221        {
222            public Type DataType { get; protected set; }
223
224            public abstract IEnumerable GetData(IParameterInfo parameter);
225        }
226
227        abstract class RandomDataSource<T> : RandomDataSource
228        {
229            private T _min;
230            private T _max;
231            private int _count;
232            private bool _inRange;
233
234            protected Randomizer _randomizer;
235
236            protected RandomDataSource(int count)
237            {
238                _count = count;
239                _inRange = false;
240
241                DataType = typeof(T);
242            }
243
244            protected RandomDataSource(T min, T max, int count)
245            {
246                _min = min;
247                _max = max;
248                _count = count;
249                _inRange = true;
250
251                DataType = typeof(T);
252            }
253
254            public override IEnumerable GetData(IParameterInfo parameter)
255            {
256                //Guard.ArgumentValid(parameter.ParameterType == typeof(T), "Parameter type must be " + typeof(T).Name, "parameter");
257
258                _randomizer = Randomizer.GetRandomizer(parameter.ParameterInfo);
259
260                for (int i = 0; i < _count; i++)
261                    yield return _inRange
262                        ? GetNext(_min, _max)
263                        : GetNext();
264            }
265
266            protected abstract T GetNext();
267            protected abstract T GetNext(T min, T max);
268        }
269
270        #endregion
271
272        #region RandomDataConverter
273
274        class RandomDataConverter : RandomDataSource
275        {
276            IParameterDataSource _source;
277
278            public RandomDataConverter(IParameterDataSource source)
279            {
280                _source = source;
281            }
282
283            public override IEnumerable GetData(IParameterInfo parameter)
284            {
285                Type parmType = parameter.ParameterType;
286
287                foreach (object obj in _source.GetData(parameter))
288                {
289                    if (obj is int)
290                    {
291                        int ival = (int)obj; // unbox first
292                        if (parmType == typeof(short))
293                            yield return (short)ival;
294                        else if (parmType == typeof(ushort))
295                            yield return (ushort)ival;
296                        else if (parmType == typeof(byte))
297                            yield return (byte)ival;
298                        else if (parmType == typeof(sbyte))
299                            yield return (sbyte)ival;
300                        else if (parmType == typeof(decimal))
301                            yield return (decimal)ival;
302                    }
303                    else if (obj is double)
304                    {
305                        double d = (double)obj; // unbox first
306                        if (parmType == typeof(decimal))
307                            yield return (decimal)d;
308                    }
309                }
310            }
311        }
312
313        #endregion
314
315        #region IntDataSource
316
317        class IntDataSource : RandomDataSource<int>
318        {
319            public IntDataSource(int count) : base(count) { }
320
321            public IntDataSource(int min, int max, int count) : base(min, max, count) { }
322
323            protected override int GetNext()
324            {
325                return _randomizer.Next();
326            }
327
328            protected override int GetNext(int min, int max)
329            {
330                return _randomizer.Next(min, max);
331            }
332        }
333
334        #endregion
335
336        #region UIntDataSource
337
338        class UIntDataSource : RandomDataSource<uint>
339        {
340            public UIntDataSource(int count) : base(count) { }
341
342            public UIntDataSource(uint min, uint max, int count) : base(min, max, count) { }
343
344            protected override uint GetNext()
345            {
346                return _randomizer.NextUInt();
347            }
348
349            protected override uint GetNext(uint min, uint max)
350            {
351                return _randomizer.NextUInt(min, max);
352            }
353        }
354
355        #endregion
356
357        #region LongDataSource
358
359        class LongDataSource : RandomDataSource<long>
360        {
361            public LongDataSource(int count) : base(count) { }
362
363            public LongDataSource(long min, long max, int count) : base(min, max, count) { }
364
365            protected override long GetNext()
366            {
367                return _randomizer.NextLong();
368            }
369
370            protected override long GetNext(long min, long max)
371            {
372                return _randomizer.NextLong(min, max);
373            }
374        }
375
376        #endregion
377
378        #region ULongDataSource
379
380        class ULongDataSource : RandomDataSource<ulong>
381        {
382            public ULongDataSource(int count) : base(count) { }
383
384            public ULongDataSource(ulong min, ulong max, int count) : base(min, max, count) { }
385
386            protected override ulong GetNext()
387            {
388                return _randomizer.NextULong();
389            }
390
391            protected override ulong GetNext(ulong min, ulong max)
392            {
393                return _randomizer.NextULong(min, max);
394            }
395        }
396
397        #endregion
398
399        #region ShortDataSource
400
401        class ShortDataSource : RandomDataSource<short>
402        {
403            public ShortDataSource(int count) : base(count) { }
404
405            public ShortDataSource(short min, short max, int count) : base(min, max, count) { }
406
407            protected override short GetNext()
408            {
409                return _randomizer.NextShort();
410            }
411
412            protected override short GetNext(short min, short max)
413            {
414                return _randomizer.NextShort(min, max);
415            }
416        }
417
418        #endregion
419
420        #region UShortDataSource
421
422        class UShortDataSource : RandomDataSource<ushort>
423        {
424            public UShortDataSource(int count) : base(count) { }
425
426            public UShortDataSource(ushort min, ushort max, int count) : base(min, max, count) { }
427
428            protected override ushort GetNext()
429            {
430                return _randomizer.NextUShort();
431            }
432
433            protected override ushort GetNext(ushort min, ushort max)
434            {
435                return _randomizer.NextUShort(min, max);
436            }
437        }
438
439        #endregion
440
441        #region DoubleDataSource
442
443        class DoubleDataSource : RandomDataSource<double>
444        {
445            public DoubleDataSource(int count) : base(count) { }
446
447            public DoubleDataSource(double min, double max, int count) : base(min, max, count) { }
448
449            protected override double GetNext()
450            {
451                return _randomizer.NextDouble();
452            }
453
454            protected override double GetNext(double min, double max)
455            {
456                return _randomizer.NextDouble(min, max);
457            }
458        }
459
460        #endregion
461
462        #region FloatDataSource
463
464        class FloatDataSource : RandomDataSource<float>
465        {
466            public FloatDataSource(int count) : base(count) { }
467
468            public FloatDataSource(float min, float max, int count) : base(min, max, count) { }
469
470            protected override float GetNext()
471            {
472                return _randomizer.NextFloat();
473            }
474
475            protected override float GetNext(float min, float max)
476            {
477                return _randomizer.NextFloat(min, max);
478            }
479        }
480
481        #endregion
482
483        #region ByteDataSource
484
485        class ByteDataSource : RandomDataSource<byte>
486        {
487            public ByteDataSource(int count) : base(count) { }
488
489            public ByteDataSource(byte min, byte max, int count) : base(min, max, count) { }
490
491            protected override byte GetNext()
492            {
493                return _randomizer.NextByte();
494            }
495
496            protected override byte GetNext(byte min, byte max)
497            {
498                return _randomizer.NextByte(min, max);
499            }
500        }
501
502        #endregion
503
504        #region SByteDataSource
505
506        class SByteDataSource : RandomDataSource<sbyte>
507        {
508            public SByteDataSource(int count) : base(count) { }
509
510            public SByteDataSource(sbyte min, sbyte max, int count) : base(min, max, count) { }
511
512            protected override sbyte GetNext()
513            {
514                return _randomizer.NextSByte();
515            }
516
517            protected override sbyte GetNext(sbyte min, sbyte max)
518            {
519                return _randomizer.NextSByte(min, max);
520            }
521        }
522
523        #endregion
524
525        #region EnumDataSource
526
527        class EnumDataSource : RandomDataSource
528        {
529            private int _count;
530
531            public EnumDataSource(int count)
532            {
533                _count = count;
534                DataType = typeof(Enum);
535            }
536
537            public override IEnumerable GetData(IParameterInfo parameter)
538            {
539                Guard.ArgumentValid(parameter.ParameterType.GetTypeInfo().IsEnum, "EnumDataSource requires an enum parameter", "parameter");
540
541                Randomizer randomizer = Randomizer.GetRandomizer(parameter.ParameterInfo);
542                DataType = parameter.ParameterType;
543
544                for (int i = 0; i < _count; i++ )
545                    yield return randomizer.NextEnum(parameter.ParameterType);
546            }
547        }
548
549        #endregion
550
551        #region DecimalDataSource
552
553        // Currently, Randomizer doesn't implement methods for decimal
554        // so we use random Ulongs and convert them. This doesn't cover
555        // the full range of decimal, so it's temporary.
556        class DecimalDataSource : RandomDataSource<decimal>
557        {
558            public DecimalDataSource(int count) : base(count) { }
559
560            public DecimalDataSource(decimal min, decimal max, int count) : base(min, max, count) { }
561
562            protected override decimal GetNext()
563            {
564                return _randomizer.NextDecimal();
565            }
566
567            protected override decimal GetNext(decimal min, decimal max)
568            {
569                return _randomizer.NextDecimal(min, max);
570            }
571        }
572
573        #endregion
574
575        #endregion
576    }
577}
578
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in EnumDataSource

    No methods found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)