Skip to content

Commit ad7622d

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Two Strings. Load test case data from JSON.
JSON load as Resource: https://khalidabuhakmeh.com/how-to-use-embedded-resources-in-dotnet
1 parent 1286c41 commit ad7622d

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"title": "Example 1",
4+
"s1": "and",
5+
"s2": "art",
6+
"expected": "Yes"
7+
},
8+
{
9+
"title": "Example 2",
10+
"s1": "be",
11+
"s2": "cat",
12+
"expected": "No"
13+
},
14+
{
15+
"title": "Sample Test Case 0",
16+
"s1": "hello",
17+
"s2": "world",
18+
"expected": "Yes"
19+
}
20+
]

algorithm_exercises_csharp_test/algorithm_exercises_csharp_test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@
6161
<ItemGroup>
6262
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json" />
6363
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci_ransom_note.testcases.json" />
64+
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.testcases.json" />
6465
</ItemGroup>
6566
</Project>

algorithm_exercises_csharp_test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/TwoStrings.Test.cs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,31 @@ public class TwoStringsTest
55
{
66
public class TwoStringsTestCase
77
{
8-
public string title = "";
9-
public string s1 = "";
10-
public string s2 = "";
11-
public string expected = "Yes";
8+
public string title { get; set; } = default!;
9+
10+
public string s1 { get; set; } = default!;
11+
12+
public string s2 { get; set; } = default!;
13+
14+
public string expected { get; set; } = default!;
1215
}
1316

14-
private static readonly TwoStringsTestCase[] tests = [
15-
new()
16-
{
17-
title = "Example 1",
18-
s1 = "and",
19-
s2 = "art",
20-
expected = "Yes"
21-
},
22-
new()
23-
{
24-
title = "Example 2",
25-
s1 = "be",
26-
s2 = "cat",
27-
expected = "No"
28-
},
29-
new()
30-
{
31-
title = "Sample Test Case 0",
32-
s1 = "hello",
33-
s2 = "world",
34-
expected = "Yes"
35-
},
36-
];
17+
private List<TwoStringsTestCase> testCases { get; set; } = default!;
18+
19+
[TestInitialize]
20+
public void testInitialize()
21+
{
22+
testCases = JsonLoader.resourceLoad<List<TwoStringsTestCase>>(
23+
"hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.testcases.json"
24+
) ?? [];
25+
}
3726

3827
[TestMethod]
3928
public void testTwoStrings()
4029
{
4130
string result;
4231

43-
foreach (TwoStringsTestCase test in tests)
32+
foreach (TwoStringsTestCase test in testCases)
4433
{
4534
result = TwoStrings.twoStrings(test.s1, test.s2);
4635
Assert.AreEqual(test.expected, result);

0 commit comments

Comments
 (0)