Introduction

DataGenerator is a set of strongly-typed random data generators that can be used for testing. All generators support generating a single value using Generate() or multiple values using Generate( count As Integer ).

Supplied Generators
  • Boolean Generator
   'Return True of False randomly.
   Dim gen As New BooleanGenerator()
   Console.WriteLine( gen.Generate() )

  • Choice Generator
   'Return either "Hello" or "World" randomly. Any data type is supported.
   Dim choices As New List(Of String)
   choices.Add( "Hello" )
   choices.Add( "World" )

   Dim gen As New ChoiceGenerator(Of String)( choices )
   Console.WriteLine( gen.Generate() )

  • Constant Generator
   'Always return the string "Hello World". Any data type is supported.
   Dim gen As New ConstantGenerator(Of String)( "Hello World" )
   Console.WriteLine( gen.Generate() )

  • DateTime Generator
   'Generate a random date between DateTime.MinValue and DateTime.MaxValue.
   Dim gen As New DateTimeGenerator()
   Console.WriteLine( gen.Generate() )


   'Generate a date in the past.
   Dim gen As New DateTimeGenerator( DateTimeGenerator.TemporalDirection.Past )
   Console.WriteLine( gen.Generate() )


   'Generate a date in the future.
   Dim gen As New DateTimeGenerator( DateTimeGenerator.TemporalDirection.Future )
   Console.WriteLine( gen.Generate() )

  • First Name Generator
   'Generates a random first name using the data from:
   'http://www.census.gov/genealogy/names/names_files.html
   Dim gen As New FirstNameGenerator()
   Console.WriteLine( gen.Generate() )


   'Only generate male names.
   Dim gen As New FirstNameGenerator( FirstNameGenerator.Genders.Male )
   Console.WriteLine( gen.Generate() )


   'Only generate female names.
   Dim gen As New FirstNameGenerator( FirstNameGenerator.Genders.Female )
   Console.WriteLine( gen.Generate() )

  • Guid Generator
   'Returns a globally unique identifier (Guid).
   Dim gen As New GuidGenerator()
   Console.WriteLine( gen.Generate() )

  • Integer Generator
   'Returns a random integer between 0 and Integer.MaxValue.
   Dim gen As New IntegerGenerator()
   Console.WriteLine( gen.Generate() )


   'Returns a random integer between -5 and 5.
   Dim gen As New IntegerGenerator( -5, 5 )
   Console.WriteLine( gen.Generate() )

  • Integer Sequence Generator
   'Returns 1, 2, 3, 4, etc.
   Dim gen As New IntegerSequenceGenerator()
   Console.WriteLine( gen.Generate() )


   'Returns 5, 10, 15, 20, etc.
   Dim gen As New IntegerSequenceGenerator( 0, 5 )
   Console.WriteLine( gen.Generate() )

  • Last Name Generator
   'Generates a random last name using the data from:
   'http://www.census.gov/genealogy/names/names_files.html
   Dim gen As New LastNameGenerator()
   Console.WriteLine( gen.Generate() )


Special Generators

These generators do not generate values directly but can be used to tailor the output from other generators.
  • Format Generator
   'Generate a list of first names prefixed with a row number.
   Dim rowNumberGenerator As New IntegerSequenceGenerator()
   Dim firstNameGenerator As New FirstNameGenerator()
   Dim gen As New FormatGenerator( "{0}. {1}", rowNumberGenerator, firstNameGenerator )
   Console.WriteLine( gen.Generate() )

  • Nullable Generator
   'Generate a random nullable-integer.
   Dim integerGenerator As New IntegerGenerator()
   Dim gen As New NullableGenerator(Of Integer)( integerGenerator )
   
   Dim value As Nullable(Of Integer)
   value = gen.Generate()

   If value.HasValue Then
      Console.WriteLine( value.Value )
   Else
      Console.WriteLine( "(NULL)" )
   End If


   'Generate a random nullable-integer with a 25% probability of NULLs.
   Dim integerGenerator As New IntegerGenerator()
   Dim gen As New NullableGenerator(Of Integer)( integerGenerator, 0.25 )
   
   Dim value As Nullable(Of Integer)
   value = gen.Generate()

   If value.HasValue Then
      Console.WriteLine( value.Value )
   Else
      Console.WriteLine( "(NULL)" )
   End If


Creating Your Own Generator

Simply implement IDataGenerator(Of T) or inherit from DataGeneratorBase(Of T). It is preferred to inherit from DataGeneratorBase(Of T) as it provides a default implementation of Generate( count As Integer) requiring you to only provide an implementation of Generate().

Last edited Nov 29, 2006 at 9:17 AM by wbarthol, version 12