using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Mocks { public class DataGenerator { public class DataGenerator { private static readonly Random _random = new Random(); private const string words = "the,of,and,a,to,in,is,you,that,it,he,for,was,on,are,as,with,his,they,at,be,this,from,I,have,or,by,one,had,not,but,what,all,were,when,we,there,can,an,your,which,their,said,if,do,will,each,about,how,up,out,them,then,she,many,some,so,these,would,other,into,has,more,her,two,like,him,see,time,could,no,make,than,first,been,its,who,now,people,my,made,over,did,down,only,way,find,use,may,water,long,little,very,after,words,called,just,where,most,know,get,through,back,much,before,go,good,new,write,out,used,me,man,too,any,day,same,right,look,think,also,around,another,came,come,work,three,word,must,because,does,part,even,place,well,such,here,take,why,things,help,put,years,different,away,again,off,went,old,number,great,tell,men,say,small,every,found,still,between,name,should,home,big,give,air,line,set,own,under,read,last,never,us,left,end,along,while,might,next,sound,below,saw,something,thought,both,few,those,always,looked,show,large,often,together,asked,house,don't,world,going,want,school,important,until,form,food,keep,children,feet,land,side,without,boy,once,animals,life,enough,took,sometimes,four,head,above,kind,began,almost,live,page,got,earth,need,far,hand,high,year,mother,light,parts,country,father,let,night,following,picture,being,study,second,eyes,soon,times,story,boys,since,white,days,ever,paper,hard,near,sentence,better,best,across,during,today,others,however,sure,means,knew,its,try,told,young,miles,sun,ways,thing,whole,hear,example,heard,several,change,answer,room,sea,against,top,turned,learn,point,city,play,toward,five,using,himself,usually"; private static readonly string[] WORDS = words.Split(','); private static readonly char[] SENTENCE_TERMINUS = { '.', '!', '?' }; public IEnumerable Items(IEnumerable items, int minNumberOfItems = 1, int maxNumberOfItems = 1) { Assert(minNumberOfItems >= 0, "minNumberOfItems must be greater than zero"); Assert(minNumberOfItems <= maxNumberOfItems, "minNumberOfItems must be lesser or equal than maxNumberOfItems"); int numberOfItems = Next(minNumberOfItems, maxNumberOfItems + 1); IList result = new List(); for (int i = 0; i < numberOfItems; i++) { int index = Next(0, items.Count()); result.Add(items.ElementAt(index)); } return result; } public string Title(int minNumberOfWords = 1, int maxNumberOfWords = 3) { return Words(minNumberOfWords, maxNumberOfWords); } public string Paragraphs(int minNumberOfParagraphes = 1, int maxNumberOfParagraphes = 5, int minNumberOfSentences = 3, int maxNumberOfSentences = 10) { Assert(minNumberOfParagraphes >= 0, "minNumberOfParagraphes must be greater than zero"); Assert(minNumberOfParagraphes <= maxNumberOfParagraphes, "minNumberOfParagraphes must be lesser or equal than maxNumberOfParagraphes"); int numberOfParagraphes = Next(minNumberOfParagraphes, maxNumberOfParagraphes + 1); StringBuilder builder = new StringBuilder(); for (int i = 0; i < numberOfParagraphes; i++) { builder.Append(i > 0 ? " " + Paragraph(minNumberOfSentences, maxNumberOfSentences) : Paragraph(minNumberOfSentences, maxNumberOfSentences)); } return builder.ToString(); } public string Paragraph(int minNumberOfSentences = 3, int maxNumberOfSentences = 15) { StringBuilder builder = new StringBuilder(); builder.Append(Sentences(minNumberOfSentences, maxNumberOfSentences)); builder.Append(Environment.NewLine); return builder.ToString(); } public string Sentences(int minNumberOfSentences = 1, int maxNumberOfSentences = 5, int minNumberOfWords = 5, int maxNumberOfWords = 20) { Assert(minNumberOfSentences >= 0, "minNumberOfSentences must be greater than zero"); Assert(minNumberOfSentences <= maxNumberOfSentences, "minNumberOfSentences must be lesser or equal than maxNumberOfSentences"); int numberOfSentences = Next(minNumberOfSentences, maxNumberOfSentences + 1); StringBuilder builder = new StringBuilder(); for (int i = 0; i < numberOfSentences; i++) { builder.Append(i > 0 ? " " + Sentence(minNumberOfWords, maxNumberOfWords) : Sentence(minNumberOfWords, maxNumberOfWords)); } return builder.ToString(); } public string Sentence(int minNumberOfWords = 5, int maxNumberOfWords = 20) { int terminusIndex = Next(SENTENCE_TERMINUS.Length - 1); StringBuilder builder = new StringBuilder(); builder.Append(Words(minNumberOfWords, maxNumberOfWords)); builder.Append(SENTENCE_TERMINUS[terminusIndex]); return builder.ToString(); } public string Words(int minNumberOfWords, int maxNumberOfWords) { Assert(minNumberOfWords >= 0, "minNumberOfWords must be greater than zero"); Assert(minNumberOfWords <= maxNumberOfWords, "minNumberOfWords must be lesser or equal than maxNumberOfWords"); int numberOfWords = Next(minNumberOfWords, maxNumberOfWords + 1); StringBuilder builder = new StringBuilder(); for (int i = 0; i < numberOfWords; i++) { builder.Append(i > 0 ? " " + Word() : Word()); } return builder.ToString(); } public string Word() { int wordIndex = Next(WORDS.Length); return WORDS[wordIndex]; } public int? IntegerOptional(int minValue, int maxValue) { if (Next(2) == 1) { return Integer(minValue, maxValue); } return null; } public int Integer(int minValue, int maxValue) { Assert(minValue < maxValue, "minValue must be lesser than maxValue"); return Next(minValue, maxValue); } public bool? BooleanOptional() { if (Next(2) == 1) { return Boolean(); } return null; } public bool Boolean() { return Next(2) == 1; } public DateTime? DateOptional(DateTime minValue, DateTime? maxValue = null) { if (Next(2) == 1) { return Date(minValue, maxValue); } return null; } public DateTime Date(DateTime minValue, DateTime? maxValue = null) { if (maxValue == null) { maxValue = DateTime.Now; } Assert(minValue > DateTime.MinValue, "minValue must be greater than DateTime.MinValue"); Assert(minValue < DateTime.MaxValue, "maxValue must be lesser than DateTime.MaxValue"); Assert(minValue < maxValue, "minValue must be lesser than maxValue"); double rand = (maxValue.Value.Ticks * 1.0 - minValue.Ticks * 1.0) * NextDouble() + minValue.Ticks * 1.0; long ticks = Convert.ToInt64(rand); DateTime result = new DateTime(ticks); return result; } protected int Next(int maxValue) { return _random.Next(maxValue); } protected int Next(int minValue, int maxValue) { return _random.Next(minValue, maxValue); } protected double NextDouble() { return _random.NextDouble(); } protected static void Assert(bool value, string message) { if (!value) { throw new InvalidOperationException(message); } } } }