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);
        }
        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 );
        }
Example #3
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 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;
        }