-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPassingByValueVsPassingByReferenceUnitTest.cs
More file actions
88 lines (71 loc) · 2.35 KB
/
PassingByValueVsPassingByReferenceUnitTest.cs
File metadata and controls
88 lines (71 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PassingByValueVsPassingByReference.Test
{
[TestClass]
public class PassingByReferenceVsPassByValueUnitTest
{
[TestMethod]
public void GivenDoubleNumber_WhenPassedToUpdatePriceByPercentMethod_ThenNoChangeWillAffectTheNumber()
{
// Given
double number = 30;
int discountRate = 50;
// When
Program.UpdatePriceByPercent(number, discountRate);
//Assert
Assert.AreEqual(number, 30);
}
[TestMethod]
public void GivenDoubleNumber_WhenPassedToUpdatePriceByPercentByRefMethod_ThenWillChangeTheNumberValue()
{
// Given
double number = 30;
int discountRate = 50;
// When
Program.UpdatePriceByPercentByRef(ref number, discountRate);
//Assert
Assert.AreEqual(number, 15);
}
[TestMethod]
public void GivenProductObject_WhenPassedToApplyDiscountMethod_ThenWillChangeThePrice()
{
// Given
var product = new Product
{
ProductId = 1,
Price = 60,
Cost = 10
};
int discountRate = 50;
// When
Program.ApplyDiscount(product, discountRate);
//Assert
Assert.AreEqual(product.Price, 30);
}
[TestMethod]
public void GivenArrayOfDoubleNumbers_WhenPassedToUpdatePricesByPercentMethod_ThenWillChangeEveryItemInIt()
{
// Given
double[] originalPrices = [30, 15, 21.5, 50];
double[] prices = [.. originalPrices];
int discountRate = -50;
// When
Program.UpdatePricesByPercent(prices, discountRate);
//Assert
for (int i = 0; i < originalPrices.Length; i++)
{
Assert.AreEqual(prices[i], originalPrices[i] * (1 + discountRate / 100.0));
}
}
[TestMethod]
public void GivenAnUnassignedDoubleVariable_WhenPassedToMakeItZeroMethod_ThenWillHaveZero()
{
// Given
double zero;
// When
Program.MakeItZero(out zero);
//Assert
Assert.AreEqual(zero, 0);
}
}
}