Example #1
0
        public void FromRgb_WithInvalidHex_Throws()
        {
            // arrange
            var values = new[]
            {
                "#afafafah",
                "1234abcef",
                "#afafafa"
            };

            // act & assert
            foreach (var value in values)
            {
                var exThrown = false;
                try
                {
                    var color = ColorSpecification.New().FromArgb(value);
                }
                catch (ArgumentException)
                {
                    exThrown = true;
                }

                if (!exThrown)
                {
                    Assert.Fail($"An argument exception was expected for value = {value}, but not thrown");
                }
            }
        }
Example #2
0
        public void TestFindByColorSizeAndBelowPrice()
        {
            // Arrange
            ColorSpecification      colorSpecification      = new ColorSpecification(ProductColor.Red);
            SizeSpecification       sizeSpecification       = new SizeSpecification(ProductSize.Small);
            BelowPriceSpecification belowPriceSpecification = new BelowPriceSpecification(10.00M);

            AndSpecification firstAndSpecification  = new AndSpecification(colorSpecification, sizeSpecification);
            AndSpecification secondAndSpecification = new AndSpecification(firstAndSpecification, belowPriceSpecification);

            // Act
            ReadOnlyCollection <Product> foundProducts = this._finder.SelectBy(secondAndSpecification);

            // Assert
            Assert.AreEqual(0, foundProducts.Count, "found no small red products below $10.00");

            // Arrange
            colorSpecification      = new ColorSpecification(ProductColor.Red);
            sizeSpecification       = new SizeSpecification(ProductSize.Medium);
            belowPriceSpecification = new BelowPriceSpecification(10.00M);

            firstAndSpecification  = new AndSpecification(colorSpecification, sizeSpecification);
            secondAndSpecification = new AndSpecification(firstAndSpecification, belowPriceSpecification);

            // Act
            foundProducts = this._finder.SelectBy(secondAndSpecification);

            // Assert
            Assert.AreEqual(this._fireTruck, foundProducts[0], "found firetruck when looking for cheap medium red toys");
        }
Example #3
0
        private static void Main()
        {
            Smartphone[] smartphones =
            {
                new Smartphone("white", "xiaomi",     12),
                new Smartphone("white", "samsung",     8),
                new Smartphone("black", "motorolla",   2),
                new Smartphone("red",   "apple",     128),
                new Smartphone("blue",  "xiaomi",      8),
                new Smartphone("blue",  "sony", 8)
            };

            ISpecification <Smartphone> whiteSpec = new ColorSpecification("white");

            Console.WriteLine("Smartphones with color=white");
            PrintResult(whiteSpec, smartphones);

            var appleOrBlackSpec
                = new BrandSpecification("apple").Or(new ColorSpecification("black"));

            Console.WriteLine("Smartphones with brand=apple or color=black=");
            PrintResult(appleOrBlackSpec, smartphones);

            var whiteAndEightCamerasSpec
                = new NumberOfCamerasSpecification(8).And(new ColorSpecification("white"));

            Console.WriteLine("Smartphones with color=white and numberOfCameras=8");
            PrintResult(whiteAndEightCamerasSpec, smartphones);

            var notBlueAndWhiteSpec
                = new ColorSpecification("blue").Or(new ColorSpecification("white")).Not();

            Console.WriteLine("Smartphones with color=!blue and color=!white");
            PrintResult(notBlueAndWhiteSpec, smartphones);
        }
Example #4
0
        public static AndSpecification <Product> And(this Color color, Size size)
        {
            var colosSpec = new ColorSpecification(color);
            var sizeSpec  = new SizeSpecification(size);

            return(new AndSpecification <Product>(colosSpec, sizeSpec));
        }
Example #5
0
        public void TestFindBelowPriceAvoidingAColor()
        {
            // Arrange
            BelowPriceSpecification belowPriceSpecification = new BelowPriceSpecification(9.00M);
            ColorSpecification      colorSpecification      = new ColorSpecification(ProductColor.White);

            AndSpecification andSpecification = new AndSpecification(belowPriceSpecification, new NotSpecification(colorSpecification));

            // Act
            ReadOnlyCollection <Product> foundProducts = this._finder.SelectBy(andSpecification);

            // Assert
            Assert.AreEqual(1, foundProducts.Count, "found 1 non-white product < $9.00");
            Assert.IsTrue(foundProducts.Contains(this._fireTruck), "found fireTruck");

            // Arrange
            belowPriceSpecification = new BelowPriceSpecification(9.00M);
            colorSpecification      = new ColorSpecification(ProductColor.Red);

            andSpecification = new AndSpecification(belowPriceSpecification, new NotSpecification(colorSpecification));

            // Act
            foundProducts = this._finder.SelectBy(andSpecification);

            // Arrange
            Assert.AreEqual(1, foundProducts.Count, "found 1 non-red product < $9.00");
            Assert.IsTrue(foundProducts.Contains(this._baseball), "found baseball");
        }
Example #6
0
        public void OrderBySize()
        {
            var sizeSpecification = new ColorSpecification("2");

            var filteredList = filterManager.Apply(productList, sizeSpecification);

            foreach (var product in filteredList)
            {
                Assert.AreEqual("2", product.Color());
            }
        }
        public void TestColorFilter()
        {
            Color color = Color.Red;

            var colorSpecification = new ColorSpecification(color);
            IList<IProduct> filterList = filter.Filter(_productList, colorSpecification);
            foreach (var product in filterList)
            {
                Assert.AreEqual(product.Color, color);
            }
        }
Example #8
0
        public void Build_FromRgbWithBytes_Succeeds()
        {
            // arrange
            var spec = ColorSpecification.New().FromArgb(255, 66, 134, 244);

            // act
            var color = spec.Build(contextMock.Object);

            // assert
            Assert.IsNotNull(color);
            Assert.AreEqual("FF4286F4", color.Argb);
        }
        public void TestColorFilter()
        {
            Color color = Color.Red;

            var colorSpecification      = new ColorSpecification(color);
            IList <IProduct> filterList = filter.Filter(_productList, colorSpecification);

            foreach (var product in filterList)
            {
                Assert.AreEqual(product.Color, color);
            }
        }
Example #10
0
        public void TestFindByColor()
        {
            // Arrange
            ColorSpecification colorSpecification = new ColorSpecification(ProductColor.Red);

            // Act
            ReadOnlyCollection <Product> foundProducts = this._finder.SelectBy(colorSpecification);

            // Assert
            Assert.AreEqual(2, foundProducts.Count, "found 2 red products");
            Assert.IsTrue(foundProducts.Contains(this._fireTruck), "found fireTruck");
            Assert.IsTrue(foundProducts.Contains(this._toyConvertible), "found Toy Porsche Convertible");
        }
        public void FindProductsByColorAndSizeTest()
        {
            ColorSpecification colorSpecification = new ColorSpecification(ProductColor.GREEN);

            SizeSpecification sizeSpecification = new SizeSpecification(ProductSize.SMALL);

            Specification colorAndSizeSpecification =
                new AndSpecification(colorSpecification, sizeSpecification);

            IList <Product> filteredProducts =
                _productRepositoryWithSpecification.FindProducts(colorAndSizeSpecification);

            Assert.AreEqual(0, filteredProducts.Count);
        }
        public void FindProductsByColorAndBelowPriceTest()
        {
            ColorSpecification colorSpecification = new ColorSpecification(ProductColor.GREEN);

            BelowPriceSpecification belowPriceSpecification = new BelowPriceSpecification(10);

            Specification colorAndBelowPriceSpecification =
                new AndSpecification(colorSpecification, belowPriceSpecification);

            IList <Product> filteredProducts =
                _productRepositoryWithSpecification.FindProducts(colorAndBelowPriceSpecification);

            Assert.AreEqual(1, filteredProducts.Count);
            Assert.AreEqual("Frisbee", filteredProducts.First().Description);
        }
        public void TestOrFilter()
        {
            Size size = Size.Large;
            Color color = Color.Red;
            var spec1 = new SizeSpecification(size);
            var spec2 = new ColorSpecification(color);
            var orSizeSpecification = new OrSpecification(spec1, spec2);

            IList<IProduct> filterList = filter.Filter(_productList, orSizeSpecification);
            foreach (var product in filterList)
            {
                bool matched = (product.Color == color) || (product.Size == size);
                Assert.IsTrue(matched);
            }
        }
 public Lego(ColorSpecification color, Vector3 position, LegoType type, bool isBound)
 {
     this.color    = color;
     this.position = position;
     this.type     = type;
     this.isBound  = isBound;
     this.dimen    = new Vector2(1, 1);
     gposList      = new List <Vector3> ();
     up            = new List <Vector3>();
     down          = new List <Vector3>();
     left          = new List <Vector3>();
     right         = new List <Vector3>();
     front         = new List <Vector3>();
     back          = new List <Vector3>();
 }
        public void TestColorAndSizeFilter()
        {
            Size  size  = Size.Large;
            Color color = Color.Red;
            var   spec1 = new SizeSpecification(size);
            var   spec2 = new ColorSpecification(color);
            var   andSizeSpecification = new AndSpecification(spec1, spec2);

            IList <IProduct> filterList = filter.Filter(_productList, andSizeSpecification);

            foreach (var product in filterList)
            {
                bool matched = (product.Color == color) && (product.Size == size);
                Assert.IsTrue(matched);
            }
        }
        public void FindProductsByColorOrSizeTest()
        {
            ColorSpecification colorSpecification = new ColorSpecification(ProductColor.GREEN);

            SizeSpecification sizeSpecification = new SizeSpecification(ProductSize.SMALL);

            Specification colorOrSizeSpecification =
                new OrSpecification(colorSpecification, sizeSpecification);

            IList <Product> filteredProducts =
                _productRepositoryWithSpecification.FindProducts(colorOrSizeSpecification);

            Assert.AreEqual(2, filteredProducts.Count);
            Assert.AreEqual("Barbie Classic", filteredProducts.First().Description);
            Assert.AreEqual("Frisbee", filteredProducts.Last().Description);
        }
Example #17
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var apple = new Product("Apple", Color.Green, Size.Small);
            var tree  = new Product("Tree", Color.Green, Size.Large);
            var house = new Product("House", Color.Blue, Size.Large);

            Product[] products = { apple, tree, house };

            var fs = new FilterService();

            Console.WriteLine("Green products (new):");
            var specification = new ColorSpecification(Color.Green);
            var filtered      = fs.Filter(products, specification);

            foreach (var p in filtered)
            {
                Console.WriteLine($" - {p.Name} is green");
            }

            Console.WriteLine("Large products");
            foreach (var p in fs.Filter(products, new SizeSpecification(Size.Large)))
            {
                Console.WriteLine($" - {p.Name} is large");
            }

            Console.WriteLine("Medium Red products");
            var redSpec           = new ColorSpecification(Color.Red);
            var mediumSpec        = new SizeSpecification(Size.Medium);
            var andSpecification  = new AndSpecification <Product>(redSpec, mediumSpec);
            var mediumRedFiltered = fs.Filter(products, andSpecification);

            foreach (var p in mediumRedFiltered)
            {
                Console.WriteLine($" - {p.Name} is large");
            }

            Console.WriteLine("Large blue items");
            foreach (var p in fs.Filter(products,
                                        new AndSpecification <Product>(
                                            new ColorSpecification(Color.Blue),
                                            new SizeSpecification(Size.Large)))
                     )
            {
                Console.WriteLine($" - {p.Name} is big and blue");
            }
        }
Example #18
0
        public void ProductFilterColorBlue_IsSuccess()
        {
            var productFilter = new ProductFilter();
            var colorSpecify  = new ColorSpecification(Color.Blue);
            var data          = new List <ProductManage>()
            {
                new ProductManage("P1", Color.Blue, Size.Large, 1200),
                new ProductManage("P2", Color.Yellow, Size.Large, 1200)
            };

            var listDatas = productFilter.Filter(data, colorSpecify);

            Assert.True(listDatas.Count() > 0);
            foreach (var checkData in listDatas)
            {
                Assert.Equal(Color.Blue, checkData.color);
            }
        }
Example #19
0
        public void SOLID_OCP_Test2()
        {
            var betterFilter = new BetterFilter();

            var filteredProductNamesByColorAndSize = new List <string>();

            var smallGreenSpec = new ColorSpecification(Color.Green) & new SizeSpecification(Size.Small);

            foreach (var item in betterFilter.Filter(products, smallGreenSpec))
            {
                filteredProductNamesByColorAndSize.Add(item.Name);
            }

            IEnumerable <string> expectedProductNames = products.Where(q => q.Color == Color.Green && q.Size == Size.Small)
                                                        .Select(q => q.Name);

            Assert.IsTrue(HelperTest.AreEquals(expectedProductNames, filteredProductNamesByColorAndSize.ToArray(), order: true));
        }
        public ColorSpecification nearestColor()
        {
            Vector3            tempColor = new Vector3(color.r * 255, color.g * 255, color.b * 255);
            float              min_dist  = float.MaxValue;
            ColorSpecification result    = ColorSpecification.BLACK;
            int i = 0;

            foreach (Vector3 cvector in colorsControl)
            {
                float temp_dis = Vector3.Distance(cvector, tempColor);
                if (temp_dis < min_dist)
                {
                    min_dist = temp_dis;
                    result   = (ColorSpecification)i;
                }
                i++;
            }
            return(result);
        }
Example #21
0
        static void Main(string[] args)
        {
            #region OCP
            var apple = new Product("Jabłko", Color.Green, Size.Small);
            var tree  = new Product("Drzewo", Color.Green, Size.Large);

            var       house    = new Product("Dom", Color.Blue, Size.Large);
            Product[] products = { apple, tree, house };

            var bf = new BetterFilter();

            var largeGreenSpec = new ColorSpecification(Color.Green)
                                 & new SizeSpecification(Size.Large);

            foreach (var p in bf.Filter(products, largeGreenSpec))
            {
                Console.WriteLine($" - {p.Name} jest zielony");
            }
            #endregion
        }
Example #22
0
        private static void FilterList(IEnumerable <Product> products, string searchBy, string searchKey)
        {
            ISpecification <Product> spec = null;
            var productFilter             = new ProductFilter();

            switch (searchBy.ToLower())
            {
            case "category":
                spec = new CategorySpecification(searchKey);
                break;

            case "color":
                spec = new ColorSpecification(searchKey);
                break;
            }
            Console.WriteLine("\n---------Filtered List------------\n");

            foreach (var item in productFilter.Filter(products, spec))
            {
                Console.WriteLine($"Product: {item.Name}. Category: {item.Category}. Color: {item.Color}");
            }

            Console.WriteLine("\n---------End of List--------------");
        }
Example #23
0
        public void Build_FromRgbWithValidHex_Succeeds()
        {
            // arrange
            var values = new[]
            {
                // with #
                "#111a0f1F",
                "#AFAFAFAF",
                "#11008080",
                // only values
                "FF1AFFa1",
                "1fcbbEba",
                "11794044"
            };

            // act & assert
            foreach (var value in values)
            {
                var color = ColorSpecification.New().FromArgb(value).Build(contextMock.Object);

                Assert.IsNotNull(color);
                Assert.AreEqual(value.Replace("#", ""), color.Argb);
            }
        }
        public static Color getColor(ColorSpecification index)
        {
            Vector3 cv = colorsControl [(int)index];

            return(new Color(cv.x / 255.0f, cv.y / 255.0f, cv.z / 255.0f, 1));
        }
Example #25
0
 public AndSpecification(ColorSpecification colorSpecification, SizeSpecification sizeSpecification)
 {
     _colorSpecification = colorSpecification;
     _sizeSpecification  = sizeSpecification;
 }
 public void changeColor(ColorSpecification color)
 {
     this.color = color;
 }
Example #27
0
        private static void SolidPrinciples()
        {
            var journal = new Journal();

            journal.AddEntry("I Started today Udemy.");
            journal.AddEntry("I started with Solid.");
            Console.WriteLine(journal);

            var persistence = new Persistence();
            var fileName    = @"C:\dev\designPatterns\journal.txt";

            persistence.SaveToFile(journal, fileName, true);

            var apple  = new Product("apple", Enums.Color.Green, Enums.Size.Small);
            var orange = new Product("orange", Enums.Color.Red, Enums.Size.Small);
            var tree   = new Product("tree", Enums.Color.Blue, Enums.Size.Large);
            var house  = new Product("house", Enums.Color.Blue, Enums.Size.Large);

            Product[] products = { apple, tree, house, orange };

            var productFilter = new ProductFilter();

            Console.WriteLine("Blue products (old): ");
            foreach (var p in productFilter.FilterByColor(products, Enums.Color.Blue))
            {
                Console.WriteLine($" - {p.Name} is Blue");
            }

            var betterFilter = new BetterFilter();
            var colorSpec    = new ColorSpecification(Enums.Color.Blue);

            Console.WriteLine("Blue products (new): ");
            foreach (var p in betterFilter.Filter(products, colorSpec))
            {
                Console.WriteLine($" - {p.Name} is Blue");
            }
            var sizeSpec   = new SizeSpecification(Enums.Size.Large);
            var doubleSpec = new AndSpecification <Product>(colorSpec, sizeSpec);

            Console.WriteLine("Blue and Large products (new): ");
            foreach (var p in betterFilter.Filter(products, doubleSpec))
            {
                Console.WriteLine($" - {p.Name} is Blue and large");
            }

            var rectangle = new Rectangle(2, 3);

            Console.WriteLine($"{rectangle} has area {Area(rectangle)}");

            Rectangle square = new Square();

            square.Width = 3;
            Console.WriteLine($"{square} has area {Area(square)}");

            var parent = new PersonDependencyInversion {
                Name = "Otman"
            };
            var child1 = new PersonDependencyInversion {
                Name = "Loreto"
            };
            var child2 = new PersonDependencyInversion {
                Name = "Emilio"
            };

            rS.AddParentChild(parent, child1);
        }