Skip to content

Commit 27a8175

Browse files
paulirwinCopilot
andcommitted
Make ShingleBased public and add unit tests, #21 (#55)
This was a regression of #21, which now adds unit tests to ensure it doesn't regress in the future. Fixes #21 --------- Co-authored-by: Copilot <[email protected]>
1 parent e1219e4 commit 27a8175

File tree

5 files changed

+77
-29
lines changed

5 files changed

+77
-29
lines changed

src/F23.StringSimilarity/ShingleBased.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
using System;
2626
using System.Collections.Generic;
27-
using System.Collections.ObjectModel;
2827
using System.Text.RegularExpressions;
2928

3029
namespace F23.StringSimilarity
@@ -43,7 +42,7 @@ public abstract class ShingleBased
4342
/// </summary>
4443
private static readonly Regex SPACE_REG = new Regex("\\s+", RegexOptions.Compiled);
4544

46-
/// <summary>
45+
/// <summary>
4746
/// </summary>
4847
/// <param name="k"></param>
4948
/// <exception cref="ArgumentOutOfRangeException">If k is less than or equal to 0.</exception>
@@ -56,10 +55,10 @@ protected ShingleBased(int k)
5655

5756
this.k = k;
5857
}
59-
58+
6059
protected ShingleBased() : this(DEFAULT_K) { }
6160

62-
protected internal Dictionary<string, int> GetProfile(string s)
61+
public Dictionary<string, int> GetProfile(string s)
6362
{
6463
var shingles = new Dictionary<string, int>();
6564

test/F23.StringSimilarity.Tests/CosineTest.cs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,20 @@
2222
* THE SOFTWARE.
2323
*/
2424

25-
using System;
26-
using System.CodeDom;
2725
using System.Diagnostics;
2826
using System.Diagnostics.CodeAnalysis;
29-
using System.Globalization;
3027
using System.IO;
3128
using System.Reflection;
3229
using System.Threading.Tasks;
3330
using F23.StringSimilarity.Tests.TestUtil;
3431
using Xunit;
35-
using Xunit.Abstractions;
3632

3733
namespace F23.StringSimilarity.Tests
3834
{
3935
[SuppressMessage("ReSharper", "ArgumentsStyleLiteral")]
4036
[SuppressMessage("ReSharper", "ArgumentsStyleNamedExpression")]
4137
public class CosineTest
4238
{
43-
private readonly ITestOutputHelper _testOutputHelper;
44-
45-
public CosineTest(ITestOutputHelper testOutputHelper)
46-
{
47-
_testOutputHelper = testOutputHelper;
48-
}
49-
5039
[Fact]
5140
public void TestSimilarity()
5241
{
@@ -55,8 +44,8 @@ public void TestSimilarity()
5544
var result = instance.Similarity("ABC", "ABCE");
5645

5746
Assert.Equal(
58-
expected: 0.71,
59-
actual: result,
47+
expected: 0.71,
48+
actual: result,
6049
precision: 2 // 0.01
6150
);
6251

@@ -71,8 +60,8 @@ public void TestSmallString()
7160
var result = instance.Similarity("AB", "ABCE");
7261

7362
Assert.Equal(
74-
expected: 0.0,
75-
actual: result,
63+
expected: 0.0,
64+
actual: result,
7665
precision: 5 //0.00001
7766
);
7867
}
@@ -89,7 +78,7 @@ public async Task TestLargeString()
8978
var result = instance.Similarity(string1, string2);
9079

9180
Assert.Equal(
92-
expected: 0.8115,
81+
expected: 0.8115,
9382
actual: result,
9483
precision: 3 //0.001
9584
);
@@ -144,19 +133,34 @@ public void DocumentationExampleTest()
144133
Assert.Equal(0.516185, cosine.Similarity(profile1, profile2), 6);
145134
}
146135

136+
/// <summary>
137+
/// StringSimilarity.NET specific. Ensures that GetProfile is public.
138+
/// </summary>
139+
/// <remarks>
140+
/// https://github.com/feature23/StringSimilarity.NET/issues/21
141+
/// </remarks>
142+
[Fact]
143+
public void GetProfile_IsPublic()
144+
{
145+
var cosine = new Cosine(k: 2);
146+
var profile = cosine.GetProfile("test string");
147+
148+
Assert.NotNull(profile);
149+
}
150+
147151
private static async Task<string> ReadResourceFileAsync(string file)
148152
{
149153
var assembly = Assembly.GetExecutingAssembly();
150154
var resourceName = $"{typeof(CosineTest).Namespace}.{file}";
151155

152-
using (var stream = assembly.GetManifestResourceStream(resourceName))
153-
{
154-
Debug.Assert(stream != null, "stream != null");
155-
using (var reader = new StreamReader(stream))
156-
{
157-
return await reader.ReadToEndAsync();
158-
}
159-
}
156+
// ReSharper disable once UseAwaitUsing - not supported on netstandard2.0
157+
using var stream = assembly.GetManifestResourceStream(resourceName);
158+
159+
Debug.Assert(stream != null, "stream != null");
160+
161+
using var reader = new StreamReader(stream);
162+
163+
return await reader.ReadToEndAsync();
160164
}
161165
}
162166
}

test/F23.StringSimilarity.Tests/JaccardTest.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
* THE SOFTWARE.
2323
*/
24-
24+
2525
using System.Diagnostics.CodeAnalysis;
2626
using F23.StringSimilarity.Tests.TestUtil;
2727
using Xunit;
@@ -59,5 +59,20 @@ public void TestDistance()
5959

6060
NullEmptyTests.TestDistance(instance);
6161
}
62+
63+
/// <summary>
64+
/// StringSimilarity.NET specific. Ensures that GetProfile is public.
65+
/// </summary>
66+
/// <remarks>
67+
/// https://github.com/feature23/StringSimilarity.NET/issues/21
68+
/// </remarks>
69+
[Fact]
70+
public void GetProfile_IsPublic()
71+
{
72+
var jaccard = new Jaccard(k: 2);
73+
var profile = jaccard.GetProfile("test string");
74+
75+
Assert.NotNull(profile);
76+
}
6277
}
6378
}

test/F23.StringSimilarity.Tests/QGramTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,20 @@ public void TestDistance()
6363

6464
NullEmptyTests.AssertArgumentNullExceptions(instance);
6565
}
66+
67+
/// <summary>
68+
/// StringSimilarity.NET specific. Ensures that GetProfile is public.
69+
/// </summary>
70+
/// <remarks>
71+
/// https://github.com/feature23/StringSimilarity.NET/issues/21
72+
/// </remarks>
73+
[Fact]
74+
public void GetProfile_IsPublic()
75+
{
76+
var qgram = new QGram(k: 2);
77+
var profile = qgram.GetProfile("test string");
78+
79+
Assert.NotNull(profile);
80+
}
6681
}
6782
}

test/F23.StringSimilarity.Tests/SorensenDiceTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,20 @@ public void TestDistance()
5858
var instance = new SorensenDice();
5959
NullEmptyTests.TestDistance(instance);
6060
}
61+
62+
/// <summary>
63+
/// StringSimilarity.NET specific. Ensures that GetProfile is public.
64+
/// </summary>
65+
/// <remarks>
66+
/// https://github.com/feature23/StringSimilarity.NET/issues/21
67+
/// </remarks>
68+
[Fact]
69+
public void GetProfile_IsPublic()
70+
{
71+
var dice = new SorensenDice(k: 2);
72+
var profile = dice.GetProfile("test string");
73+
74+
Assert.NotNull(profile);
75+
}
6176
}
6277
}

0 commit comments

Comments
 (0)