How to test the addition of the element in list C#? NUnit



  • Good day!

    I can't figure out how to test the next block in the code. NUnit:

    public static class List  //Абстрактный класс работы со списком на основе двух типовых элементов
    {
        public static void Add(ref SimpleElm firstElm, string value)  //Добавить в список простой элемент
        {
            if (value == "") return;
            SimpleElm newElm = new SimpleElm(value);
            if (firstElm == null)
            {
                firstElm = newElm;
                return;
            }
            SimpleElm elm = firstElm;
            while (elm.next != null)
                elm = elm.next;
            elm.next = newElm;
        }
    }
    

    In fact, it is. SimpleElm below:

    public class SimpleElm // Простой класс
    {
        public string Value
        {
            get;
            set;
        }
        public SimpleElm next;
        public SimpleElm(string value = "")
        {
            Value = value;
            next = null;
        }
        public SimpleElm this[int index]
        {
            get
            {
                if (index < 0) throw new IndexOutOfRangeException();
                var item = this;
                for (int i = 0; i < index; i++)
                    if (item.next != null) 
                        item = item.next;
                return item;
            }
            set
            {
                if (index < 0) throw new IndexOutOfRangeException();
                var item = this;
                for (int i = 0; i < index; i++)
                    if (item.next != null)
                        item = item.next;
                item = value;
            }
        }
    }
    


  • In this case, since SimpleElm has some behavior in the indexer, it is likely to start with modular tests for the SimpleElm class. And here, before moving on, I'd like to know what you'd like to do in the indexer, because if we're going to put this test on and run it with your code, it'll be successful:

    using System;
    using NUnit.Framework;
    

    namespace so.list.test
    {
    [TestFixture]
    public class SimpleElmTest
    {
    private SimpleElm list;

        [SetUp]
        public void setUp()
        {
            this.list = new SimpleElm();
        }
    
        [Test]
        public void after_construction_test()
        {
            Assert.IsNull(this.list.Value);
            Assert.IsNull(this.list.Next);
        }
    
        [Test]
        public void set_Next_with_accessor_test()
        {
            SimpleElm next = new SimpleElm("Second");
            this.list.Next = next;
    
            Assert.AreEqual(next, this.list.Next);
            Assert.AreEqual(next.Value, this.list.Next.Value);
        }
    
        [Test]
        public void set_first_Next_with_indexer_test()
        {
            SimpleElm next = new SimpleElm("Second");
            this.list[1] = next;
    
            Assert.IsNull(this.list.Next);
        }
    
        [Test]
        public void set_third_Next_with_indexer_test()
        {
            SimpleElm next = new SimpleElm("Second");
            this.list[3] = next;
    
            Assert.IsNull(this.list.Next);
        }
    
    }
    

    }

    And the question here, did you really think it would work that way?




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2