コード例 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:CollectionBase&lt;T&gt;"/> class.
 /// </summary>
 /// <param name="baseCollection">The base collection.</param>
 /// <param name="readOnly">if set to <c>true</c> [read only].</param>
 public CollectionBase(ICollectionBase <T> baseCollection, bool readOnly)
     : this()
 {
     mReadOnly = readOnly;
     //innerList = new System.Collections.ObjectModel.ReadOnlyCollection<T>(baseCollection);
     innerList.AddRange(baseCollection);
 }
コード例 #2
0
        public void create_and_save_test_collection_to_disk()
        {
            int numberOfCollectables        = 2;
            int numberOfItemsPerCollectable = 3;

            foreach (Type collectionType in CollectableBaseFactory.CollectableTypes)
            {
                // Initialize the collection
                string          collectionName = $"Test {collectionType.Name} Collection";
                ICollectionBase testCollection = GetTestCollection(collectionName, collectionType, numberOfCollectables, numberOfItemsPerCollectable);

                // Initialize a repository
                IFileIO fileIO = new FileIO();
                HomeCollectionRepository repo = new HomeCollectionRepository(testCollection, fileIO);

                // Write file to disk
                string fullFilePath = Environment.CurrentDirectory + @"\";
                fullFilePath += collectionName + @"." + HomeCollectionRepository.FILE_EXTENSION;
                try
                {
                    repo.SaveCollection(fullFilePath, true);
                    Assert.IsTrue(true);
                }
                catch (Exception ex)
                {
                    Assert.Fail($"Should not have failed to write test collection file to disk: {collectionType.Name}", ex.Message);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Информация о колонках
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="columns"></param>
        /// <param name="currentColumn"></param>
        /// <param name="columnInfos"></param>
        private static void CollectColumnInfos <T>(ICollectionBase <T> columns, ref int currentColumn,
                                                   List <XamGridColumnInfo> columnInfos, IMeasure measure)
        {
            if (columns == null || !columns.Any() || columnInfos == null)
            {
                return;
            }

            // Заголовок
            foreach (var column in columns)
            {
                var cc = column as GroupColumn;
                if (cc != null)
                {
                    CollectColumnInfos(cc.Columns, ref currentColumn, columnInfos, measure);
                    continue;
                }

                var c = column as Column;
                if (c == null)
                {
                    continue;
                }

                columnInfos.Add(new XamGridColumnInfo
                {
                    Name         = GetHeaderfromColumn(c, measure),
                    Key          = c.Key,
                    Width        = c.Width,
                    ColumnNumber = currentColumn,
                });

                currentColumn++;
            }
        }
コード例 #4
0
        public void converttojson_collection_with_collectables_and_instances_success()
        {
            int N = 3;
            int M = 2;

            foreach (Type collectionType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase collection = GetTestCollection("initial", collectionType, N, M);

                string json = HomeCollectionRepository.ConvertCollectionToJson(collection);

                int countCollectionNameTag = CountOccurences(json, "CollectionName");
                int countCollectionTypeTag = CountOccurences(json, "CollectionType");
                int countDisplayNameTag    = CountOccurences(json, "DisplayName");
                int countDescriptionTag    = CountOccurences(json, "Description");
                int countItemInstanceTag   = CountOccurences(json, "ItemInstances");
                int countItemDetailsTag    = CountOccurences(json, "ItemDetails");
                int countIsFavoriteTag     = CountOccurences(json, "IsFavorite");

                Assert.AreEqual(1, countCollectionNameTag);
                Assert.AreEqual(1, countCollectionTypeTag);
                Assert.AreEqual(N, countDisplayNameTag);
                Assert.AreEqual(N, countDescriptionTag);
                Assert.AreEqual(N, countItemInstanceTag);
                Assert.AreEqual(N * M, countItemDetailsTag);
                Assert.AreEqual(N * M, countIsFavoriteTag);
            }
        }
コード例 #5
0
ファイル: Formatting.cs プロジェクト: thunder176/HeuristicLab
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="stringbuilder"></param>
        /// <param name="rest"></param>
        /// <param name="formatProvider"></param>
        /// <returns>True if collection was shown completely</returns>
        public static bool ShowCollectionValue <T>(ICollectionBase <T> items, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
        {
            string startdelim = "{ ", enddelim = " }";
            bool   showIndexes = false;

            //TODO: do not test here at run time, but select code at compile time
            //      perhaps by delivering the print type to this metod
            if (items is IList <T> )
            {
                startdelim = "[ ";
                enddelim   = " ]";
            }
            else if (items is ICollection <T> )
            {
                startdelim = "{{ ";
                enddelim   = " }}";
            }

            stringbuilder.Append(startdelim);
            rest -= 2 * startdelim.Length;
            bool first    = true;
            bool complete = true;
            int  index    = 0;


            {
                foreach (T x in items)
                {
                    complete = false;
                    if (rest <= 0)
                    {
                        break;
                    }
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        stringbuilder.Append(", ");
                        rest -= 2;
                    }
                    if (showIndexes)
                    {
                        string indexString = string.Format("{0}:", index++);
                        stringbuilder.Append(indexString);
                        rest -= indexString.Length;
                    }
                    complete = Showing.Show(x, stringbuilder, ref rest, formatProvider);
                }
            }
            if (!complete)
            {
                stringbuilder.Append("...");
                rest -= 3;
            }
            stringbuilder.Append(enddelim);
            return(complete);
        }
コード例 #6
0
        public void convertcollectiontojson_throws_exception_when_collection_is_null()
        {
            ICollectionBase collection = null;

            string json = HomeCollectionRepository.ConvertCollectionToJson(collection);

            Assert.Fail("Should have thrown exception when collection is null");
        }
コード例 #7
0
        public void calling_controller_getcollection_returns_current_icollectionbase_instance()
        {
            // controller is initialized with _mockHomeCollection

            ICollectionBase collection = _mockController.GetCollection();

            Assert.AreEqual(_mockHomeCollection.Object, collection);
        }
コード例 #8
0
        public void controller_initialized_with_null_collection_base_fails()
        {
            ICollectionBase nullBase = null;

            _mockController = new HomeCollectionController(nullBase, _mockFileIO.Object);

            Assert.IsFalse(true, "Expected the test to fail when initialized with a null object");
        }
コード例 #9
0
        public void controller_initialized_with_null_fileio_fails()
        {
            ICollectionBase collectionBase = _mockHomeCollection.Object;
            IFileIO         nullFileIO     = null;

            _mockController = new HomeCollectionController(collectionBase, nullFileIO);

            Assert.IsFalse(true, "Expected the test to fail when initialized with a null object");
        }
コード例 #10
0
 public HomeCollectionController(IFileIO fileIO)
 {   // used when loading a collection with the controller
     if (fileIO == null)
     {
         throw new FileIOException("Injected file IO must not be null");
     }
     _fileIO         = fileIO;
     _homeCollection = null;
     _repo           = null;
 }
コード例 #11
0
        public HomeCollectionRepository(IFileIO fileIO)
        {   // we don't have or need an existing collection to call LoadCollection
            if (fileIO == null)
            {
                throw new FileIOException("File IO must not be null");
            }

            _homeCollection = null;
            _fileIO         = fileIO;
        }
コード例 #12
0
        // TODO: merge, import into existing??
        // TODO: more robust JSON parsing - handle some bad characters, bad format, with logging and potentially continue to process
        // TODO: import/export as spreadsheet/CSV
        // TODO: validate fields for special characters - needs to be in other classes, though, not in repo

        // Internal methods
        internal static string ConvertCollectionToJson(ICollectionBase collectionToSerialize)
        {
            if (collectionToSerialize == null)
            {
                throw new CollectionException("Null collection cannot be serialized");
            }
            string jsonCollection = JsonConvert.SerializeObject(collectionToSerialize);

            return(jsonCollection);
        }
コード例 #13
0
        public void loadcollection_fileio_not_found_throws_exception()
        {
            IHomeCollectionRepository repo = new HomeCollectionRepository(_mockFileIO.Object);

            _mockFileIO.Setup(i => i.ReadFile(It.IsAny <string>())).Throws(new CollectionException());
            string fullFilePath = "badfilepath";

            ICollectionBase collection = repo.LoadCollection(fullFilePath);

            Assert.Fail($"Expected exception to be thrown when the file is not found: {fullFilePath}");
        }
コード例 #14
0
 public ICollectionBase LoadCollection(string fullFilePath)
 {   // load the collection from persistent storage via Repository
     try
     {
         _homeCollection = Repository().LoadCollection(fullFilePath);
     }
     catch (Exception ex)
     {
         throw new CollectionException("Unable to load collection", ex);
     }
     return(_homeCollection);
 }
コード例 #15
0
        public void controller_collectiontype_returns_collectable_base_type()
        {
            Type            objType         = typeof(ICollectionBase);
            ICollectionBase collectableBase = _mockHomeCollection.Object;

            _mockHomeCollection.Setup(b => b.CollectionType).Returns(objType);

            _mockController = new HomeCollectionController(collectableBase, _mockFileIO.Object);
            Type objTestType = _mockController.CollectionType;

            Assert.AreEqual(objType, objTestType);
        }
コード例 #16
0
        public void loadcollection_file_invalid_json_content_throws_exception()
        {
            IHomeCollectionRepository repo = new HomeCollectionRepository(_mockFileIO.Object);
            string mockFileContent         = "this is some invalid Json content";

            _mockFileIO.Setup(i => i.ReadFile(It.IsAny <string>())).Returns(mockFileContent);
            string fullFilePath = "filepath";

            ICollectionBase collection = repo.LoadCollection(fullFilePath);

            Assert.Fail($"Expected exception to be thrown when the file content cannot be parsed into a collection: {fullFilePath}");
        }
コード例 #17
0
        public void clearcollection_removes_all_collectables_from_collection()
        {
            int N = 3;

            foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase testCollection = GetMockCollection(N, collectableType);

                testCollection.ClearCollection();

                Assert.AreEqual(0, testCollection.Collectables.Count);
            }
        }
コード例 #18
0
        public void getcollection_returns_count_of_collectables_added_to_collection()
        {
            int N = 3;

            foreach (Type collectionType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase testCollection = GetMockCollection(N, collectionType);

                int count = testCollection.Collectables.Count;

                Assert.AreEqual(N, count);
            }
        }
コード例 #19
0
 public HomeCollectionController(ICollectionBase homeCollection, IFileIO fileIO)
 {
     if (homeCollection == null)
     {
         throw new CollectionException("Collection controller must be initialized with a collection base object");
     }
     _homeCollection = homeCollection;
     if (fileIO == null)
     {
         throw new FileIOException("Injected file IO must not be null");
     }
     _fileIO = fileIO;
     _repo   = null;
 }
コード例 #20
0
        public void initialize_homecollectionrepository_with_null_fails()
        {
            ICollectionBase nullCollection = null;

            try
            {
                IHomeCollectionRepository repo = new HomeCollectionRepository(nullCollection, _mockFileIO.Object);
                Assert.Fail("HomeCollectionRepository initialization was expected to fail when passed a null value");
            }
            catch
            {
                Assert.IsTrue(true);
            }
        }
コード例 #21
0
        public HomeCollectionRepository(ICollectionBase homeCollection, IFileIO fileIO)
        {
            if (fileIO == null)
            {
                throw new FileIOException("File IO must not be null");
            }

            if (homeCollection == null)
            {
                throw new CollectionException("Repository must be initialized with a collection base object");
            }
            _homeCollection = homeCollection;
            _fileIO         = fileIO;
        }
コード例 #22
0
        public void convertjsontocollection_deserializes_to_collection_success()
        {
            string expectedCollectionName = "test";

            string jsonBookCollection = @"{""CollectionName"":""test"",""CollectionType"":""HomeCollector.Models.BookBase, HomeCollector, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"",""Collectables"":[]}";

            ICollectionBase collection = HomeCollectionRepository.ConvertJsonToCollection(jsonBookCollection);

            // This is a very fragile test.  Should look for expected tokens instead.

            Assert.AreEqual(expectedCollectionName, collection.CollectionName);
            Assert.AreEqual(0, collection.Collectables.Count);
            Assert.AreEqual(CollectableBaseFactory.BookType, collection.CollectionType);
        }
コード例 #23
0
        public void load_test_book_collection_from_disk()
        {
            string collectionName = "Star Trek Books - YTH";

            // Initialize a repository
            IFileIO fileIO = new FileIO();
            HomeCollectionRepository repo = new HomeCollectionRepository(fileIO);

            string fullFilePath = Environment.CurrentDirectory + @"\";

            fullFilePath += collectionName + @"." + HomeCollectionRepository.FILE_EXTENSION;

            ICollectionBase books = repo.LoadCollection(fullFilePath);
        }
コード例 #24
0
        public void controller_initialized_with_collection_base_object_returns_controller_instance()
        {
            try
            {
                ICollectionBase collectionBase = _mockHomeCollection.Object;

                _mockController = new HomeCollectionController(collectionBase, _mockFileIO.Object);

                Assert.IsNotNull(_mockController);
            }
            catch
            {
                Assert.IsFalse(true, "Test should not fail when initialized with an object");
            }
        }
コード例 #25
0
        public ICollectionBase LoadCollection(string fullFilePath)
        {
            // read from file system, parse, and initialize
            string jsonCollection = _fileIO.ReadFile(fullFilePath);

            try
            {
                _homeCollection = ConvertJsonToCollection(jsonCollection);
            }
            catch (Exception ex)
            {
                throw new CollectionException($"Unable to load collection: {fullFilePath}", ex);
            }
            return(_homeCollection);
        }
コード例 #26
0
        public void load_test_stamp_collection_from_disk()
        {
            string collectionName = "USA Stamps - Special";
            //string collectionName = "Test StampBase Collection_formatted";

            // Initialize a repository
            IFileIO fileIO = new FileIO();
            HomeCollectionRepository repo = new HomeCollectionRepository(fileIO);

            string fullFilePath = Environment.CurrentDirectory + @"\";

            fullFilePath += collectionName + @"." + HomeCollectionRepository.FILE_EXTENSION;

            ICollectionBase stamps = repo.LoadCollection(fullFilePath);
        }
コード例 #27
0
        public void loadcollection_file_valid_json_content_returns_collection()
        {
            IHomeCollectionRepository repo = new HomeCollectionRepository(_mockFileIO.Object);

            foreach (Type collectionType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase testCollection  = GetTestCollection("test collection", collectionType, 1);
                string          testFileContent = HomeCollectionRepository.ConvertCollectionToJson(testCollection);
                _mockFileIO.Setup(i => i.ReadFile(It.IsAny <string>())).Returns(testFileContent);
                string fullFilePath = "filepath";

                ICollectionBase collection = repo.LoadCollection(fullFilePath);

                Assert.IsNotNull(collection);
            }
        }
コード例 #28
0
        public void loadcollection_with_path_filename_calls_getfullfilepath()
        {
            IHomeCollectionRepository repo = new HomeCollectionRepository(_mockFileIO.Object);

            _mockFileIO.Setup(f => f.GetFullFilePath(It.IsAny <string>(), It.IsAny <string>()))
            .Returns((string p, string f) => p + @"\" + f);

            foreach (Type collectionType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase testCollection  = GetTestCollection("test collection", collectionType, 1);
                string          testFileContent = HomeCollectionRepository.ConvertCollectionToJson(testCollection);
                _mockFileIO.Setup(i => i.ReadFile(It.IsAny <string>())).Returns(testFileContent);
                string path     = "path";
                string filename = "filename";

                ICollectionBase collection = repo.LoadCollection(path, filename);
            }
            _mockFileIO.Verify(r => r.GetFullFilePath(It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(CollectableBaseFactory.CollectableTypes.Count));
        }
コード例 #29
0
        public void collection_serialize_deserialize_success()
        {
            string collectionName              = "test";
            int    numberOfCollectables        = 2;
            int    numberOfItemsPerCollectable = 2;

            foreach (Type collectionType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase testCollection = GetTestCollection(collectionName, collectionType, numberOfCollectables, numberOfItemsPerCollectable);
                string          jsonCollection = HomeCollectionRepository.ConvertCollectionToJson(testCollection);

                ICollectionBase resultCollection = HomeCollectionRepository.ConvertJsonToCollection(jsonCollection);

                Assert.AreEqual(collectionName, resultCollection.CollectionName);
                Assert.AreEqual(collectionType, resultCollection.CollectionType);
                Assert.AreEqual(numberOfCollectables, resultCollection.Collectables.Count);
                Assert.AreEqual(numberOfCollectables * numberOfItemsPerCollectable, resultCollection.Collectables.Count * resultCollection.Collectables[0].ItemInstances.Count);
            }
        }
コード例 #30
0
        public Curso(int id,
                     string nombre,
                     int duracion,
                     Docente docente,
                     ICollectionBase <Alumno> alumnos,
                     ICollectionBase <Unidad> unidades)
        {
            if (nombre == null)
            {
                throw new ArgumentNullException(nameof(nombre));
            }
            if (docente == null)
            {
                throw new ArgumentNullException(nameof(docente));
            }

            Id       = id;
            Nombre   = nombre;
            Duracion = duracion;
            Docente  = docente;
            Alumnos  = alumnos;
            Unidades = unidades;
        }