コード例 #1
0
 public Roulette(IRandom randomizer, IFieldFactory factory)
 {
     _randomizer = randomizer;
     _factory    = factory;
     _fields     = _factory.CreateFields();
     _result     = _fields[0];
 }
コード例 #2
0
 public Roulette(IRouletteNumberGenerator rouletteNumberGenerator, IFieldFactory fieldFactory)
 {
     _rouletteNumberGenerator = rouletteNumberGenerator;
     _fieldFactory            = fieldFactory;
     _fields = _fieldFactory.CreateFields("USA");
     _result = _fields[0];
 }
コード例 #3
0
        public void Spin_ReturnResult_ResultIsCorrect(int number)
        {
            Field        fieldToBeReturned = new Field(1, 2);
            List <Field> fields            = new List <Field>
            {
                new Field(0, Field.Green),
                new Field(1, Field.Red),
                new Field(2, Field.Black),
                new Field(3, Field.Red),
                new Field(4, Field.Black),
                new Field(5, Field.Red),
                new Field(6, Field.Black),
                new Field(7, Field.Red),
                new Field(8, Field.Black),
                new Field(9, Field.Red),
                new Field(10, Field.Black),
                new Field(11, Field.Black),
                new Field(12, Field.Red),
                new Field(13, Field.Black),
                new Field(14, Field.Red),
                new Field(15, Field.Black),
                new Field(16, Field.Red),
                new Field(17, Field.Black),
                new Field(18, Field.Red),
                new Field(19, Field.Red),
                new Field(20, Field.Black),
                new Field(21, Field.Red),
                new Field(22, Field.Black),
                new Field(23, Field.Red),
                new Field(24, Field.Black),
                new Field(25, Field.Red),
                new Field(26, Field.Black),
                new Field(27, Field.Red),
                new Field(28, Field.Black),
                new Field(29, Field.Black),
                new Field(30, Field.Red),
                new Field(31, Field.Black),
                new Field(32, Field.Red),
                new Field(33, Field.Black),
                new Field(34, Field.Red),
                new Field(35, Field.Black),
                new Field(36, Field.Red)
            };


            _fakeFieldFactory.CreateFields(Arg.Any <string>()).Returns(fields);
            _uut = new Roulette(_fakeNumberGen, _fakeFieldFactory);
            _fakeNumberGen.GetNumber().Returns((uint)number);
            _uut.Spin();

            Assert.That(_uut.GetResult(), Is.EqualTo(fields[number]));
        }
コード例 #4
0
        private List <SCField> CreateFields(Guid itemId, SCItem parentItem)
        {
            var destFields = new List <SCField>();

            IEnumerable <Tuple <string, int> > langVersions = parentItem.Fields.Where(f => f.Version != null && f.Language != null).Select(f => new Tuple <string, int>(f.Language, (int)f.Version)).Distinct();
            var languages = parentItem.Fields.Where(f => f.Language != null).Select(f => f.Language).Distinct();

            foreach (var newField in _itemMetadataTemplate.fields.newFields)
            {
                destFields.AddRange(_fieldFactory.CreateFields(newField, itemId, langVersions, languages));
            }

            return(destFields);
        }
コード例 #5
0
        public Roulette(IFieldFactory factory, IRandomizer random)
        {
            _factory = factory;
            _random  = random;
            _fields  = _factory.CreateFields();
            //_fields = new List<Field>
            //{
            //    new Field(0, Field.Green),
            //    new Field(1, Field.Red),
            //    new Field(2, Field.Black),
            //    new Field(3, Field.Red),
            //    new Field(4, Field.Black),
            //    new Field(5, Field.Red),
            //    new Field(6, Field.Black),
            //    new Field(7, Field.Red),
            //    new Field(8, Field.Black),
            //    new Field(9, Field.Red),
            //    new Field(10, Field.Black),
            //    new Field(11, Field.Black),
            //    new Field(12, Field.Red),
            //    new Field(13, Field.Black),
            //    new Field(14, Field.Red),
            //    new Field(15, Field.Black),
            //    new Field(16, Field.Red),
            //    new Field(17, Field.Black),
            //    new Field(18, Field.Red),
            //    new Field(19, Field.Red),
            //    new Field(20, Field.Black),
            //    new Field(21, Field.Red),
            //    new Field(22, Field.Black),
            //    new Field(23, Field.Red),
            //    new Field(24, Field.Black),
            //    new Field(25, Field.Red),
            //    new Field(26, Field.Black),
            //    new Field(27, Field.Red),
            //    new Field(28, Field.Black),
            //    new Field(29, Field.Black),
            //    new Field(30, Field.Red),
            //    new Field(31, Field.Black),
            //    new Field(32, Field.Red),
            //    new Field(33, Field.Black),
            //    new Field(34, Field.Red),
            //    new Field(35, Field.Black),
            //    new Field(36, Field.Red)
            //};

            _result = _fields[0];
        }
コード例 #6
0
        private List <SCField> CreateFields(Guid itemId, SCItem parentItem)
        {
            var destFields = new List <SCField>();

            IEnumerable <Tuple <string, int> > langVersions = parentItem.Fields.Where(f => f.Version != null && f.Language != null).Select(f => new Tuple <string, int>(f.Language, (int)f.Version)).Distinct();
            var languages = parentItem.Fields.Where(f => f.Language != null).Select(f => f.Language).Distinct();

            var fieldLangVersions = _itemMetadataTemplate?.fields?.newFields?.Where(f => f.values != null).SelectMany(f => f.values.Select(v => v.Key).Distinct()).Distinct();

            if (fieldLangVersions != null && fieldLangVersions.Any())
            {
                langVersions = fieldLangVersions;
                languages    = fieldLangVersions.Select(lv => lv.Item1).Distinct();
            }

            foreach (var newField in _itemMetadataTemplate.fields.newFields)
            {
                destFields.AddRange(_fieldFactory.CreateFields(newField, itemId, langVersions, languages));
            }

            return(destFields);
        }
コード例 #7
0
        public void FixtureSetup()
        {
            fakeList = new List <IField>
            {
                Substitute.For <IField>(),
                Substitute.For <IField>(),
                Substitute.For <IField>()
            };

            fakeList[0].Number.Returns(0U);
            fakeList[1].Number.Returns(1U);
            fakeList[2].Number.Returns(6U);

            fakeList[0].Color.Returns(FieldColor.Green);
            fakeList[1].Color.Returns(FieldColor.Red);
            fakeList[2].Color.Returns(FieldColor.Black);

            fakeList[0].Parity.Returns(Parity.Neither);
            fakeList[1].Parity.Returns(Parity.Odd);
            fakeList[2].Parity.Returns(Parity.Even);

            fakeFieldFactory = Substitute.For <IFieldFactory>();
            fakeFieldFactory.CreateFields().Returns(fakeList);
        }
コード例 #8
0
ファイル: Roulette.cs プロジェクト: sskieller/ikt-sem4
 public Roulette(IFieldFactory fieldFactory, IRandomizer randomizer)
 {
     Fields      = fieldFactory.CreateFields();
     _result     = null;
     _randomizer = randomizer;
 }
コード例 #9
0
ファイル: Roulette.cs プロジェクト: FrejThorsen34/Semester4
 public Roulette(IFieldFactory fieldFactory, IRandom randomizer)
 {
     _fields = fieldFactory.CreateFields();
     _result = _fields[0];
     _random = randomizer;
 }
コード例 #10
0
        private List <SCItem> ConvertFields(SCItem destItem, SCItem lastDescendantItem)
        {
            var destFields = new List <SCField>();
            var destItems  = new List <SCItem>();

            var sourceFields = destItem.Fields;

            var itemId = sourceFields.First().ItemId;

            IEnumerable <Tuple <string, int> > langVersions = sourceFields.Where(f => f.Version != null && f.Language != null).Select(f => new Tuple <string, int>(f.Language, (int)f.Version)).Distinct();
            var languages = sourceFields.Where(f => f.Language != null).Select(f => f.Language).Distinct();

            // Migrate existing fields
            if (_itemMetadataTemplate.fields.existingFields != null)
            {
                var filteredExistingFields = sourceFields.Where(f =>
                                                                _itemMetadataTemplate.fields.existingFields.Select(mf => mf.fieldId).Contains(f.FieldId));

                foreach (var filteredExistingField in filteredExistingFields)
                {
                    var existingField =
                        _itemMetadataTemplate.fields.existingFields.FirstOrDefault(mf => mf.fieldId == filteredExistingField.FieldId);

                    if (existingField != null)
                    {
                        destFields.Add(filteredExistingField);
                    }
                }
            }

            // Convert fields
            if (_itemMetadataTemplate.fields.convertedFields != null)
            {
                // Select only fields that are mapped
                var filteredConvertedFields = sourceFields.Where(f =>
                                                                 _itemMetadataTemplate.fields.convertedFields.Select(mf => mf.sourceFieldId).Contains(f.FieldId));

                foreach (var filteredConvertedField in filteredConvertedFields)
                {
                    var convertedField =
                        _itemMetadataTemplate.fields.convertedFields.FirstOrDefault(mf =>
                                                                                    mf.sourceFieldId == filteredConvertedField.FieldId);

                    if (convertedField != null)
                    {
                        // Process fields that have multiple dest fields
                        if (convertedField.destFields != null && convertedField.destFields.Any())
                        {
                            var valueElements = new List <string>();
                            try
                            {
                                valueElements = XmlHelper.GetXmlElementNames(filteredConvertedField.Value);
                            }
                            catch (Exception ex)
                            {
                                _logger.Log(new LogEntry(LoggingEventType.Error, string.Format("ItemConverter - Failed to parse Xml value for form field item. ItemID = {0} - FieldID = {1} - FieldValue_Decoded = {2}", itemId, filteredConvertedField.Id, filteredConvertedField.Value), ex));
                            }

                            var filteredValueElementsToMany = convertedField.destFields.Where(f =>
                                                                                              valueElements.Contains(f.sourceElementName.ToLower(), StringComparer.InvariantCultureIgnoreCase) && (f.destFieldId == null || f.destFieldId == Guid.Empty));

                            foreach (var valueXmlElementMapping in filteredValueElementsToMany)
                            {
                                // Special case for List Datasource fields
                                if (string.Equals(valueXmlElementMapping.sourceElementName, "Items",
                                                  StringComparison.InvariantCultureIgnoreCase))
                                {
                                    IFieldConverter converter = IoC.CreateConverter(valueXmlElementMapping.fieldConverter);

                                    List <SCField> convertedFields = converter?.ConvertValueElementToFields(filteredConvertedField,
                                                                                                            XmlHelper.GetXmlElementValue(filteredConvertedField.Value, valueXmlElementMapping.sourceElementName));
                                    if (convertedFields != null && convertedFields.Any())
                                    {
                                        destFields.AddRange(convertedFields);
                                    }

                                    // Delete existing list items
                                    var listItemMetadataTemplate = _metadataProvider.GetItemMetadataByTemplateName("ExtendedListItem");
                                    if (lastDescendantItem != null)
                                    {
                                        var listItems =
                                            _destMasterRepository.GetSitecoreChildrenItems(listItemMetadataTemplate.destTemplateId,
                                                                                           lastDescendantItem.ID);

                                        foreach (SCItem listItem in listItems)
                                        {
                                            _destMasterRepository.DeleteSitecoreItem(listItem);
                                        }
                                    }

                                    List <SCItem> convertedItems = converter?.ConvertValueElementToItems(filteredConvertedField,
                                                                                                         XmlHelper.GetXmlElementValue(filteredConvertedField.Value, valueXmlElementMapping.sourceElementName),
                                                                                                         listItemMetadataTemplate, lastDescendantItem ?? destItem);

                                    if (convertedItems != null && convertedItems.Any())
                                    {
                                        destItems.AddRange(convertedItems);
                                    }

                                    // Reporting
                                    if (convertedFields?.Count > 0 && convertedItems?.Count > 0)
                                    {
                                        _conversionReporter.AddUnmappedValueElementSourceField(filteredConvertedField, itemId, valueXmlElementMapping.sourceElementName, XmlHelper.GetXmlElementValue(filteredConvertedField.Value, valueXmlElementMapping.sourceElementName));
                                    }
                                }
                            }

                            var filteredValueElements =
                                convertedField.destFields.Where(f => valueElements.Contains(f.sourceElementName.ToLower(), StringComparer.InvariantCultureIgnoreCase) && (f.destFieldId != null && f.destFieldId != Guid.Empty));

                            foreach (var valueXmlElementMapping in filteredValueElements)
                            {
                                IFieldConverter converter = IoC.CreateConverter(valueXmlElementMapping.fieldConverter);

                                SCField destField = converter?.ConvertValueElement(filteredConvertedField, (Guid)valueXmlElementMapping.destFieldId, XmlHelper.GetXmlElementValue(filteredConvertedField.Value, valueXmlElementMapping.sourceElementName), destItems);

                                if (destField != null && destField.FieldId != Guid.Empty)
                                {
                                    destFields.Add(destField);
                                }
                            }

                            // Reporting
                            var unmappedValueElementSourceFields = valueElements.Where(v =>
                                                                                       !convertedField.destFields.Select(f => f.sourceElementName)
                                                                                       .Contains(v, StringComparer.InvariantCultureIgnoreCase));

                            foreach (var unmappedValueElementSourceField in unmappedValueElementSourceFields)
                            {
                                _conversionReporter.AddUnmappedValueElementSourceField(filteredConvertedField, itemId, unmappedValueElementSourceField, XmlHelper.GetXmlElementValue(filteredConvertedField.Value, unmappedValueElementSourceField));
                            }
                        }
                        // Process fields that have a single dest field
                        else if (convertedField.destFieldId != null && convertedField.destFieldId != Guid.Empty)
                        {
                            IFieldConverter converter = IoC.CreateConverter(convertedField.fieldConverter);
                            SCField         destField = converter?.ConvertField(filteredConvertedField, (Guid)convertedField.destFieldId);

                            if (destField != null && destField.FieldId != Guid.Empty)
                            {
                                destFields.Add(destField);
                            }
                        }
                    }
                }
            }

            if (_itemMetadataTemplate.fields.newFields != null)
            {
                // Create new fields
                foreach (var newField in _itemMetadataTemplate.fields.newFields)
                {
                    destFields.AddRange(_fieldFactory.CreateFields(newField, itemId, langVersions, languages));
                }
            }

            destItem.Fields = destFields;
            destItems.Add(destItem);

            // Merge multi-language List items with same value
            destItems = MergeListItems(destItems);

            // Reporting
            var unmappedSourceFields = sourceFields?.Where(f => (_itemMetadataTemplate.fields.existingFields == null || !_itemMetadataTemplate.fields.existingFields.Select(mf => mf.fieldId).Contains(f.FieldId)) &&
                                                           (_itemMetadataTemplate.fields.convertedFields == null || !_itemMetadataTemplate.fields.convertedFields.Select(mf => mf.sourceFieldId).Contains(f.FieldId)));

            foreach (SCField unmappedSourceField in unmappedSourceFields)
            {
                _conversionReporter.AddUnmappedItemField(unmappedSourceField, itemId);
            }

            return(destItems);
        }