diff --git a/CourseApp.Tests/CourseApp.Tests.csproj b/CourseApp.Tests/CourseApp.Tests.csproj index 8fb7e4a..e253b4b 100644 --- a/CourseApp.Tests/CourseApp.Tests.csproj +++ b/CourseApp.Tests/CourseApp.Tests.csproj @@ -9,6 +9,7 @@ + diff --git a/CourseApp.Tests/DemoTest.cs b/CourseApp.Tests/DemoTest.cs index fdc46f5..618bfb0 100644 --- a/CourseApp.Tests/DemoTest.cs +++ b/CourseApp.Tests/DemoTest.cs @@ -1,14 +1,53 @@ using System; using Xunit; +using Moq; namespace CourseApp.Tests { public class DemoTest { + [Theory] + [InlineData(5, 5, 10)] + [InlineData(-5, -5, -10)] + [InlineData(-5, 5, 0)] + [InlineData(0, 0, 0)] + public void TestCalc(int x, int y, int exp) + { + Numer a = new Numer(); + a.Value = x; + Numer b = new Numer(); + b.Value = y; + Assert.Equal(exp, Program.Calc(a, b)); + } + + [Fact] + public void TestForOneNull() + { + Numer a = new Numer(); + Numer b = new Numer(); + a.Value = null; + b.Value = 1; + Assert.Throws(() => Program.Calc(a, b)); + } + + [Fact] + public void TestForNull() + { + Numer a = new Numer(); + Numer b = new Numer(); + a.Value = null; + b.Value = null; + Assert.Throws(() => Program.Calc(a, b)); + } + [Fact] - public void Test1() + public void TestFuntionRun() { - Assert.True(true); + var mock = new Mock(); + mock.Setup(b => b.B(It.IsAny())); + var classA = new ClassA(mock.Object); + var exp = classA.B(); + mock.Verify(b => b.B(It.IsAny()), Times.Once()); } } } diff --git a/CourseApp/ClassA.cs b/CourseApp/ClassA.cs new file mode 100644 index 0000000..6da70a5 --- /dev/null +++ b/CourseApp/ClassA.cs @@ -0,0 +1,20 @@ +using System; + +namespace CourseApp +{ + public class ClassA + { + private int a = 0; + private ClassB b = new ClassB(); + + public ClassA(ClassB b) + { + this.b = b; + } + + public int B() + { + return this.b.B(a); + } + } +} \ No newline at end of file diff --git a/CourseApp/ClassB.cs b/CourseApp/ClassB.cs new file mode 100644 index 0000000..8a3bc34 --- /dev/null +++ b/CourseApp/ClassB.cs @@ -0,0 +1,13 @@ +using System; + +namespace CourseApp +{ + public class ClassB + { + public virtual int B(int a) + { + a++; + return a; + } + } +} \ No newline at end of file diff --git a/CourseApp/Numer.cs b/CourseApp/Numer.cs new file mode 100644 index 0000000..8d749db --- /dev/null +++ b/CourseApp/Numer.cs @@ -0,0 +1,9 @@ +using System; + +namespace CourseApp +{ + public class Numer + { + public object Value { get; set; } + } +} diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index 248bbe4..f482ee4 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -4,9 +4,34 @@ namespace CourseApp { public class Program { + public static int Calc(Numer a, Numer b) + { + if ((a.Value == null) || (b.Value == null)) + { + throw new NullReferenceException("a or b is null"); + } + else + { + int c = (int)a.Value + (int)b.Value; + return c; + } + } + + public static void Task2() + { + ClassB b = new ClassB(); + ClassA a = new ClassA(b); + Console.WriteLine(a.B()); + } + public static void Main(string[] args) { - Console.WriteLine("Hello World!"); + Numer a = new Numer(); + Numer b = new Numer(); + a.Value = 5; + b.Value = 5; + Console.WriteLine(Program.Calc(a, b)); + Task2(); Console.ReadLine(); } }