public void Translator_AffectNext_Import_Test()
        {
            // SETUP the SUT
            List<string> val = new List<string> { "First Value,    1  ,N", "Second Value, 16","Third Item, 1" };
            StreamReader testBed = TestHelper.prepareTestDouble(val);
            NormalItemFactory normalItemFactory = new NormalItemFactory(testBed);
            ItemParser itemParser = new ItemParser();
            itemParser.setFactory(normalItemFactory);

            List<Item> expected = new List<Item>();
            var affectorItem = new ItemValueAffectedNextItemLength() { Name = "First Value", Length = 1, Value = "01" };
            var affectedItem = new RegularItem() { Name = "Second Value", Length = 2, Value = "0403" };
            affectorItem.setAffectedItem(affectedItem);
            expected.Add(affectorItem);
            expected.Add(affectedItem);
            expected.Add(new RegularItem() { Name = "Third Item", Length = 1, Value = "02" });

            // Exercise
            StringTranslator ST = new StringTranslator();
            itemParser.setTranslator(ST);
            ST.setValue("02040302");
            ST.Import();

            // Verify
            TestHelper.Compare(expected, itemParser.Items);
        }
示例#2
0
        public void Test_ItemDecorator_GetBase1Level()
        {
            RegularItem RI = new RegularItem();
            RegularItem RI2 = new RegularItem();
            OtherItemNotNull_Decorator decWithRegularItem = new OtherItemNotNull_Decorator(RI, RI2);

            Assert.IsInstanceOfType(decWithRegularItem.getBaseClass(), typeof(RegularItem));

            ItemComposite itemComposite = new ItemComposite();
            OtherItemNotNull_Decorator decWithComposite = new OtherItemNotNull_Decorator(itemComposite, RI2);
            Assert.IsInstanceOfType(decWithComposite.getBaseClass(), typeof(ItemComposite));
        }
        public void Test_AffectNextItem_Normal()
        {
            RegularItem Reg = new RegularItem();
            Reg.Name = "test";
            Reg.Value = "002222";
            Reg.Length = 3;
            ItemValueAffectedNextItemLength affectNext = new ItemValueAffectedNextItemLength();
            affectNext.setAffectedItem(Reg);
            affectNext.Name = "Length";

            Assert.AreEqual("03", affectNext.Value );
        }
示例#4
0
        public void Test_Display_otherItemNotNull_FindItemZero()
        {
            List<Item> listOfItems = new List<Item>();
            RegularItem reg = new RegularItem();
            reg.Name = "affect";
            reg.Value = "00";
            listOfItems.Add(reg);

            RegularItem regDepend = new RegularItem();
            regDepend.Name = "depend";
            regDepend.Value = "0000";

            OtherItemNotNull_Decorator dec = new OtherItemNotNull_Decorator(regDepend, reg);
            listOfItems.Add(dec);

            // verify that the value empty since the affect value is 00
            Assert.AreEqual(string.Empty, dec.Value);
        }
示例#5
0
        public void Test_NormalItemFactory_AffectedNext()
        {
            List<string> val = new List<string> { "two bytes parameter,1 , N", "three bytes parameter ,    3" };

            StreamReader testBed = TestHelper.prepareTestDouble(val);
            NormalItemFactory normalItemFactory = new NormalItemFactory(testBed);
            List<Item> res = normalItemFactory.GetItems();

            List<Item> expected = new List<Item>();
            RegularItem ri = new RegularItem();
            ri.Name = "three bytes parameter";
            ri.Length = 3;
            ItemValueAffectedNextItemLength prev = new ItemValueAffectedNextItemLength();
            prev.setAffectedItem(ri);
            prev.Name = "two bytes parameter";
            prev.Length = 1;
            expected.Add(prev);
            expected.Add(ri);

            // do the checking
            TestHelper.Compare(expected, res);
        }
        public void Translator_AffectNext_Export_Test()
        {
            List<string> val = new List<string> { "First Value,    1  ,N,02", "Second Value, 2,R,0403", "Third Item, 1,R, 02" };

            StreamReader testBed = TestHelper.prepareTestDouble(val);
            NormalItemFactory normalItemFactory = new NormalItemFactory(testBed);
            ItemParser itemParser = new ItemParser();
            itemParser.setFactory(normalItemFactory);

            List<Item> expected = new List<Item>();
            var affectorItem = new ItemValueAffectedNextItemLength() { Name = "First Value", Length = 1, Value = "02" };
            var affectedItem = new RegularItem() { Name = "Second Value", Length = 2, Value = "0403" };
            affectorItem.setAffectedItem(affectedItem);
            expected.Add(affectorItem);
            expected.Add(affectedItem);
            expected.Add(new RegularItem() { Name = "Third Item", Length = 1, Value = "02" });

            StringTranslator ST = new StringTranslator();
            itemParser.setTranslator(ST);
            ST.Export();
            Assert.AreEqual("02040302", ST.getValue());
            Assert.AreEqual(string.Empty, ST.getValue());
        }
 public void setAffectedItem(RegularItem RI)
 {
     countedItem = RI;
 }
        public void Translator_OtherNotNull_Import_Test()
        {
            // SETUP the SUT
            List<string> val = new List<string> {
                "First Value,    1 , R,00",
                "Second Value, 2",
                "Third Item, 1 , RP ,First Value" };
            StreamReader testBed = TestHelper.prepareTestDouble(val);
            NormalItemFactory normalItemFactory = new NormalItemFactory(testBed);
            ItemParser itemParser = new ItemParser();
            itemParser.setFactory(normalItemFactory);

            List<Item> expected = new List<Item>();

            var item1 = new RegularItem() { Name = "First Value", Length = 1, Value = "02" };
            var item2 = new RegularItem() { Name = "Second Value", Length = 2,Value = "0403" };
            var item3 = new OtherItemNotNull_Decorator(
                new RegularItem() { Name = "Third Item", Length = 1,Value= "02" },
                item1
                );
            expected.Add(item1);
            expected.Add(item2);
            expected.Add(item3);

            // Exercise
            StringTranslator ST = new StringTranslator();
            itemParser.setTranslator(ST);
            ST.setValue("02040302");
            ST.Import();

            // Verify
            TestHelper.Compare(expected, itemParser.Items);
        }
        public void Translator_OtherNotNull_Export_Test()
        {
            List<string> val = new List<string> { "First Value,    1  ,R,00", "Second Value, 2,R,0403", "Third Item, 1,RP,First Value,02" };
            StreamReader testBed = TestHelper.prepareTestDouble(val);
            NormalItemFactory normalItemFactory = new NormalItemFactory(testBed);
            ItemParser itemParser = new ItemParser();
            itemParser.setFactory(normalItemFactory);

            List<Item> expected = new List<Item>();
            var item1 = new RegularItem() { Name = "First Value", Length = 1, Value = "00" };
            var item2 = new RegularItem() { Name = "Second Value", Length = 2, Value = "0403" };
            var item3 = new OtherItemNotNull_Decorator(
                new RegularItem() { Name = "Third Item", Length = 1, Value = "02" },
                item1
                );
            expected.Add(item1);
            expected.Add(item2);
            expected.Add(item3);

            StringTranslator ST = new StringTranslator();
            itemParser.setTranslator(ST);
            ST.Export();

            // verify the 00 first value
            Assert.AreEqual("000403", ST.getValue());
            Assert.AreEqual(string.Empty, ST.getValue());

            // verify the 02 first value
            itemParser.Items[0].Value = "02";
            ST.Export();
            Assert.AreEqual("02040302", ST.getValue());
            Assert.AreEqual(string.Empty, ST.getValue());
        }
示例#10
0
        public override List<ItemObject.Item> GetItems()
        {
            List<Item> result = new List<Item>();
            Item prev = null;
            Item prevTemp = null;

            string line = " ";
            int defaultValuePosition;
            while (line != null)
            {
                // setting of default value
                defaultValuePosition = 3;
                Item currentItem = null;
                prevTemp = null;

                // do the checking of string
                line = dataSource.ReadLine();
                if (line == null)
                    continue;
                line = line.Trim();

                // check for comment or empty line
                if (line.Length <= 0 || line[0] == '#')
                {
                    continue;
                }

                // chop the string into several string separated by ',' (comma)
                string[] lines = line.Split(',');

                // remove the ending and trailing space
                for(int i = 0;i< lines.Length;i++){
                    lines[i] = lines[i].Trim();
                }

                // if there is a composite item detected at the previous attempt then start adding it's member
                if ((line[0] == '>') && (prev != null) && (prev is ItemComposite))
                {
                    BitItem BI= new BitItem();
                    BI.name = lines[0].Substring(1);
                    BI.location = int.Parse(lines[1]);
                    (prev as ItemComposite).addBitItem(BI);
                    continue;
                }

                // if there is no type provided then create a regular item
                if (lines.Length <= 2)
                {
                    currentItem = new RegularItem();
                }
                else
                {
                     // build the basic type based on first character
                    switch (lines[2].ToUpper()[0])
                    {
                        case 'N':
                            currentItem = new ItemValueAffectedNextItemLength();
                            prevTemp = currentItem;
                            break;
                        case 'C':
                            currentItem = new ItemComposite();
                            prevTemp= (ItemComposite)currentItem;
                            break;
                        case 'R':
                        default :
                            currentItem = new RegularItem();
                            break;
                    }

                    // build the decorator
                    if (lines[2].Length > 1)
                    {
                        switch (lines[2][1])
                        {
                            case 'P' :
                                currentItem = new OtherItemNotNull_Decorator(currentItem,findItem(result, lines[3]));
                                defaultValuePosition++;
                                break;
                            default:
                                break;
                        }
                    }

                }

                // get the default value (in case it exist)
                if (lines.Length >= defaultValuePosition + 1)
                {
                    currentItem.Value = lines[defaultValuePosition];
                }

                // set the item name and length
                currentItem.Name = lines[0];
                currentItem.Length = int.Parse(lines[1]);
                result.Add(currentItem);

                // set the relationship
                if (prev != null)
                {
                    if (prev is ItemValueAffectedNextItemLength)
                    {
                        (prev as ItemValueAffectedNextItemLength).setAffectedItem((RegularItem)currentItem);
                        prevTemp = null;
                    }
                }
                prev = prevTemp;

            }
            return result;
        }
示例#11
0
        public void Test_NormalItemFactory_OtherItemNull()
        {
            List<string> val = new List<string> {
                "two bytes parameter,1",
                "item affected by prev, 3, RP,two bytes parameter  "};

            StreamReader testBed = TestHelper.prepareTestDouble(val);
            NormalItemFactory normalItemFactory = new NormalItemFactory(testBed);
            List<Item> res = normalItemFactory.GetItems();

            List<Item> expected = new List<Item>();
            RegularItem ri = new RegularItem();
            ri.Name = "two bytes parameter";
            ri.Length = 1;
            ri.Value = "00";

            RegularItem ri2= new RegularItem();
            ri2.Name = "item affected by prev";
            ri2.Length = 3;
            ri2.Value = "002233";
            OtherItemNotNull_Decorator decorator = new OtherItemNotNull_Decorator(ri2, ri);

            expected.Add(ri);
            expected.Add(decorator);

               // test the value of decorator is null
            Assert.AreEqual(string.Empty, decorator.Value);

            // test the value of decorator is not null since there is a value on RI
            ri.Value = "01";
            Assert.AreEqual("002233",decorator.Value);
        }