Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SectionData"/> class
 /// from a previous instance of <see cref="SectionData"/>.
 /// </summary>
 /// <remarks>
 /// Data is deeply copied
 /// </remarks>
 /// <param name="ori">
 /// The instance of the <see cref="SectionData"/> class 
 /// used to create the new instance.</param>
 public SectionData(SectionData ori)
 {
     _comments = new List<string>(ori._comments);
     _keyDataCollection = new KeyDataCollection(ori._keyDataCollection);
 }
Exemple #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SectionData"/> class
 /// from a previous instance of <see cref="SectionData"/>.
 /// </summary>
 /// <remarks>
 /// Data is deeply copied
 /// </remarks>
 /// <param name="ori">
 /// The instance of the <see cref="SectionData"/> class
 /// used to create the new instance.</param>
 public SectionData(SectionData ori)
 {
     _comments          = new List <string>(ori._comments);
     _keyDataCollection = new KeyDataCollection(ori._keyDataCollection);
 }
        /// <summary>
        ///     EN: Add section if section not exist and select, otherwise only select.
        ///     Add comment to section if comment not exist, otherwise delete old comment and add new.
        /// </summary>
        /// <param name="section">Section name to add</param>
        /// <param name="comment">Section comment</param>
        /// <example>Example: How to add section with comment and select this section</example>
        /// <code>AddAndSelectSection("General", "This is a general section")</code>
        public void addAndSelectSection(string section, string comment)
        {
            if (data != null)
            {
                if (data.Sections.GetSectionData(section) != null)
                {
                    // Get section from data
                    sectionData = data.Sections.GetSectionData(section);
                }
                else
                {
                    // Creating section
                    data.Sections.AddSection(section);
                    // Get section from data
                    sectionData = data.Sections.GetSectionData(section);
                }

                if (comment != null && !comment.Equals(""))
                {
                    if (sectionData.Comments.Count > 0)
                    {
                        sectionData.Comments.Clear();
                    }

                    sectionData.Comments.Add(comment);
                }
            }
        }
        /*
        public IniWriter(IniData data)
        {

            if (data == null)
            {
                this.data = new IniData();
            }
            else
            {
                this.data = data;
            }

            parser = new FileIniDataParser();

        }*/
        /// <summary>
        ///     EN: Add section if section not exist and select, otherwise only select.
        /// </summary>
        /// <param name="section">Section name</param>
        /// <example>Example: How to add and select section</example>
        /// <code>AddAndSelectSection("General")</code>
        public void addAndSelectSection(string section)
        {
            if (data != null)
            {
                if (data.Sections.GetSectionData(section) != null)
                {
                    // Get section from data
                    sectionData = data.Sections.GetSectionData(section);
                }
                else
                {
                    // Creating section
                    data.Sections.AddSection(section);
                    // Get section from data
                    sectionData = data.Sections.GetSectionData(section);
                }
            }
        }
        public void CreateSectionDataProgramaticlyTest()
        {
            string strSectionKeyTest = "MySection";
            string strKeyTest = "Mykey";
            string strValueTest = "My value";

            //Create a section data
            SectionData sd = new SectionData(strSectionKeyTest);

            Assert.That(sd.SectionName == strSectionKeyTest);

            //Assert not null and empty
            Assert.That(sd.Comments, Is.Empty );
            Assert.That(sd.Keys, Is.Empty);

            //Change name
            sd.SectionName = "";
            Assert.That(sd.SectionName == strSectionKeyTest);
            sd.SectionName = strSectionKeyTest + "_new";
            Assert.That(sd.SectionName == strSectionKeyTest+"_new");

            //Add key
            sd.Keys.AddKey(strKeyTest);
            Assert.That(sd.Keys.Count, Is.EqualTo(1));
            Assert.That(sd.Keys.ContainsKey(strKeyTest), Is.True);

            //Assign value
            sd.Keys.GetKeyData(strKeyTest).Value = strValueTest;
            Assert.That(sd.Keys.GetKeyData(strKeyTest).Value, Is.EqualTo(strValueTest));

            //Add repeated key
            sd.Keys.AddKey(strKeyTest);
            Assert.That(sd.Keys.Count, Is.EqualTo(1));

            //Remove non existing key
            sd.Keys.RemoveKey("asfd");
            Assert.That(sd.Keys.Count, Is.EqualTo(1));

            //Remove key
            sd.Keys.RemoveKey(strKeyTest);
            Assert.That(sd.Keys.Count, Is.EqualTo(0));
            Assert.That(sd.Keys.ContainsKey(strKeyTest), Is.False);

            ////Add invalid key
            //try
            //{
            //    sd.Keys.AddKey("invalid key");
            //    Assert.That(false);
            //}
            //catch ( Exception ex )
            //{
            //    Assert.That(ex, Is.TypeOf(typeof(ArgumentException)));
            //}

            ////Add invalid key
            //try
            //{
            //    sd.Keys.AddKey(" invalidKey");
            //    Assert.That(false);
            //}
            //catch ( Exception ex )
            //{
            //    Assert.That(ex, Is.TypeOf(typeof(ArgumentException)));
            //}

            //Access invalid keydata
            Assert.That(sd.Keys["asdf"], Is.Null);
        }