Skip to content

Commit dc02d3c

Browse files
authored
Merge pull request #1 from gwilcoxen/power-method-tests
Power method tests
2 parents c28250d + 588565b commit dc02d3c

3 files changed

Lines changed: 31 additions & 7 deletions

File tree

.github/workflows/run-app.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ jobs:
2323

2424
- name: Test
2525
run: dotnet test --no-build --verbosity normal
26+

Console/Program.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ static void Main(string[] args)
5353
{
5454
Console.WriteLine(e.Message);
5555
}
56-
5756
}
5857
}
5958

@@ -72,22 +71,24 @@ public static double Subtract(string x, string y)
7271
{
7372
return double.Parse(x) - double.Parse(y);
7473
}
74+
7575
public static double Multiply(string x, string y)
7676
{
7777
return double.Parse(x) * double.Parse(y);
7878
}
79+
7980
public static double Divide(string x, string y)
8081
{
8182
return double.Parse(x) / double.Parse(y);
8283
}
8384

84-
// Implement this method following a similar pattern as above
85+
// ✅ Implemented Power method
8586
public static double Power(string x, string y)
8687
{
87-
throw new NotImplementedException();
88+
if (x == null || y == null)
89+
throw new ArgumentNullException();
90+
91+
return Math.Pow(double.Parse(x), double.Parse(y));
8892
}
8993
}
90-
91-
92-
9394
}

Tests/UnitTests.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ public void Add_Null()
3030
Assert.ThrowsException<ArgumentNullException>(() => Program.Add(null, null));
3131
}
3232

33-
// You can add more [TestMethod]s for Subtract, Multiply, Divide, Power, etc.
33+
[TestMethod]
34+
public void Power_Valid()
35+
{
36+
Assert.AreEqual(8, Program.Power("2", "3"));
37+
Assert.AreEqual(1, Program.Power("5", "0"));
38+
Assert.AreEqual(10000, Program.Power("10", "4"));
39+
}
40+
41+
[TestMethod]
42+
public void Power_Invalid()
43+
{
44+
Assert.ThrowsException<FormatException>(() => Program.Power("a", "3"));
45+
Assert.ThrowsException<FormatException>(() => Program.Power("2", "b"));
46+
Assert.ThrowsException<FormatException>(() => Program.Power("x", "y"));
47+
}
48+
49+
[TestMethod]
50+
public void Power_Null()
51+
{
52+
Assert.ThrowsException<ArgumentNullException>(() => Program.Power(null, "3"));
53+
Assert.ThrowsException<ArgumentNullException>(() => Program.Power("2", null));
54+
Assert.ThrowsException<ArgumentNullException>(() => Program.Power(null, null));
55+
}
3456
}
3557
}

0 commit comments

Comments
 (0)