Skip to content

Commit

Permalink
Added unit tests, updated model to match Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
pmachapman committed Aug 29, 2021
1 parent 2db060e commit 412596b
Show file tree
Hide file tree
Showing 20 changed files with 883 additions and 17 deletions.
22 changes: 11 additions & 11 deletions GoToBible.Model/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,18 @@ public static PassageReference AsPassageReference(this string passage, int defau
}

// Sanitise the passage reference for retrieving the book and verse
string sanitisedPassage = SanitisePassageReference(passage);
string sanitisedPassage = passage.SanitisePassageReference();

// Get the book
string book = GetBook(sanitisedPassage);
string book = sanitisedPassage.GetBook();
if (!string.IsNullOrWhiteSpace(book) && BookLengths[book] is int[] chapters)
{
if (chapters.Length == 1)
{
sanitisedPassage = NormaliseSingleChapterReference(sanitisedPassage);
sanitisedPassage = sanitisedPassage.NormaliseSingleChapterReference();
}

string[] ranges = GetRanges(sanitisedPassage);
string[] ranges = sanitisedPassage.GetRanges();
foreach (string range in ranges)
{
if (!int.TryParse(range.Split(':')[0], out int chapter))
Expand Down Expand Up @@ -496,7 +496,7 @@ public static string RenderCss(this RenderingParameters parameters)
/// <returns>
/// The book from the passage reference.
/// </returns>
private static string GetBook(string passage)
internal static string GetBook(this string passage)
{
// Prepare the passage reference for the regex
passage = passage.Replace(":", string.Empty);
Expand Down Expand Up @@ -534,7 +534,7 @@ private static string GetBook(string passage)
/// </summary>
/// <param name="passage">The passage.</param>
/// <returns>The ranges.</returns>
private static string[] GetRanges(string passage)
internal static string[] GetRanges(this string passage)
{
// Validate input
if (string.IsNullOrWhiteSpace(passage) || passage.Length == 1)
Expand All @@ -545,7 +545,7 @@ private static string[] GetRanges(string passage)
passage = passage[1..];
Regex rangePartRegex = new Regex(@"\d", RegexOptions.Compiled);
string rangePart = passage[rangePartRegex.Match(passage).Index..];
rangePart = NormaliseCommas(rangePart);
rangePart = rangePart.NormaliseCommas();
string[] semiParts = rangePart.Split(';');
rangePart = semiParts[0];
semiParts = semiParts.Skip(1).ToArray();
Expand Down Expand Up @@ -584,7 +584,7 @@ private static string[] GetRanges(string passage)
}
}

ranges.AddRange(GetRanges("Book" + semiParts[i]));
ranges.AddRange($"Book{semiParts[i]}".GetRanges());
}

return ranges.ToArray();
Expand All @@ -597,7 +597,7 @@ private static string[] GetRanges(string passage)
/// <returns>
/// The range part with commas normalised.
/// </returns>
private static string NormaliseCommas(string rangePart)
internal static string NormaliseCommas(this string rangePart)
{
string[] parts = rangePart.Split(',');

Expand Down Expand Up @@ -630,7 +630,7 @@ private static string NormaliseCommas(string rangePart)
/// <returns>
/// The single chapter reference normalised.
/// </returns>
private static string NormaliseSingleChapterReference(string passage)
internal static string NormaliseSingleChapterReference(this string passage)
{
string[] semiParts = passage.Split(';');
if (!semiParts[0].Contains(":"))
Expand All @@ -652,7 +652,7 @@ private static string NormaliseSingleChapterReference(string passage)
/// <returns>
/// The passage reference ready for cleaning.
/// </returns>
private static string SanitisePassageReference(string passage)
internal static string SanitisePassageReference(this string passage)
{
Regex dashRegex = new Regex(@"[‐‑‒–—-]", RegexOptions.Compiled);
return dashRegex.Replace(passage.Replace(" ", string.Empty).Replace('.', ':').ToLowerInvariant(), "-");
Expand Down
13 changes: 10 additions & 3 deletions GoToBible.Model/GoToBible.Model.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>GoToBible.Model.snk</AssemblyOriginatorKeyFile>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<Nullable>enable</Nullable>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
Expand All @@ -25,8 +26,8 @@
<ApplicationIcon>App.ico</ApplicationIcon>
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Bug fixes and improvements from the port to Swift</PackageReleaseNotes>
<Version>1.1.3</Version>
<PackageReleaseNotes>Bug fixes and improvements found when porting the unit tests from Swift</PackageReleaseNotes>
<Version>1.1.4</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down Expand Up @@ -56,4 +57,10 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>GoToBible.Tests.Model, PublicKey=0024000004800000940000000602000000240000525341310004000001000100451405b438b9725f449dd5b93d547517f8e31d58f90db632f8dae6d329245b7d7fea4ccec586e0285e6067e14030379d2c813efb46abd5db044ee781fef435fb38c6cacfa007d739424291880fbfc1b0fe5171fb5e78f5d6d8b00fb80b371ac2f278d26c2feab7b54a545c976839a5502c811689a5b2528e89d890d5358ccce5</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

</Project>
25 changes: 24 additions & 1 deletion GoToBible.Model/PassageReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
namespace GoToBible.Model
{
using System;
using System.Linq;

/// <summary>
/// A passage reference.
/// </summary>
public record PassageReference
public record PassageReference : IEquatable<PassageReference>
{
/// <summary>
/// Gets or sets the chapter reference.
Expand Down Expand Up @@ -47,5 +48,27 @@ public record PassageReference
/// We only require the chapter reference. This method assumes you used <c>AsPassageReference()</c> to generate this object.
/// </remarks>
public bool IsValid => this.ChapterReference.IsValid;

/// <inheritdoc/>
public virtual bool Equals(PassageReference? other)
=> other is not null
&& this.ChapterReference == other.ChapterReference
&& this.Display == other.Display
&& this.HighlightedVerses.SequenceEqual(other.HighlightedVerses)
&& this.IsValid == other.IsValid;

/// <inheritdoc/>
public override int GetHashCode()
{
HashCode hashCode = default;
hashCode.Add(this.ChapterReference);
hashCode.Add(this.Display);
foreach (int highlightedVerse in this.HighlightedVerses)
{
hashCode.Add(highlightedVerse);
}

return hashCode.ToHashCode();
}
}
}
45 changes: 45 additions & 0 deletions GoToBible.Tests.Model/BookTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// -----------------------------------------------------------------------
// <copyright file="BookTests.cs" company="Conglomo">
// Copyright 2020-2021 Conglomo Limited. Please see LICENSE for license details.
// </copyright>
// -----------------------------------------------------------------------

namespace GoToBible.Tests.Model
{
using GoToBible.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
/// Tests the <see cref="Book"/> class.
/// </summary>
[TestClass]
public class BookTests
{
/// <summary>
/// Tests the empty object.
/// </summary>
[TestMethod]
public void TestEmpty()
{
Book book = new Book();
Assert.AreEqual(book.Name, string.Empty);
Assert.AreEqual(book.Chapters.Count, 0);
Assert.AreEqual(book.ToString(), string.Empty);
}

/// <summary>
/// Tests the <see cref="Book.Name"/> property.
/// </summary>
[TestMethod]
public void TestName()
{
Book book = new Book()
{
Name = "Genesis",
};
Assert.AreEqual(book.Name, "Genesis");
Assert.AreEqual(book.Chapters.Count, 0);
Assert.AreEqual(book.ToString(), "Genesis");
}
}
}
122 changes: 122 additions & 0 deletions GoToBible.Tests.Model/ChapterReferenceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// -----------------------------------------------------------------------
// <copyright file="ChapterReferenceTests.cs" company="Conglomo">
// Copyright 2020-2021 Conglomo Limited. Please see LICENSE for license details.
// </copyright>
// -----------------------------------------------------------------------

namespace GoToBible.Tests.Model
{
using GoToBible.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
/// Tests the <see cref="ChapterReference"/> class.
/// </summary>
[TestClass]
public class ChapterReferenceTests
{
/// <summary>
/// Tests the empty constructor.
/// </summary>
[TestMethod]
public void TestEmpty()
{
ChapterReference chapterReference = new ChapterReference();
Assert.AreEqual(chapterReference.Book, string.Empty);
Assert.AreEqual(chapterReference.ChapterNumber, 0);
Assert.AreEqual(chapterReference.ToString(), string.Empty);
Assert.IsFalse(chapterReference.IsValid);
}

/// <summary>
/// Tests an invalid constructor.
/// </summary>
[TestMethod]
public void TestBookAndChapterInvalidConstructor()
{
ChapterReference chapterReference = new ChapterReference(string.Empty);
Assert.AreEqual(chapterReference.Book, string.Empty);
Assert.AreEqual(chapterReference.ChapterNumber, 0);
Assert.AreEqual(chapterReference.ToString(), string.Empty);
Assert.IsFalse(chapterReference.IsValid);
}

/// <summary>
/// Tests an invalid chapter constructor.
/// </summary>
[TestMethod]
public void TestBookAndChapterInvalidChapterConstructor()
{
ChapterReference chapterReference = new ChapterReference("Genesis Fifty");
Assert.AreEqual(chapterReference.Book, "Genesis");
Assert.AreEqual(chapterReference.ChapterNumber, 0);
Assert.AreEqual(chapterReference.ToString(), "Genesis 0");
Assert.IsTrue(chapterReference.IsValid);
}

/// <summary>
/// Tests a missing chapter constructor.
/// </summary>
[TestMethod]
public void TestBookAndChapterMissingChapterConstructor()
{
ChapterReference chapterReference = new ChapterReference("Genesis");
Assert.AreEqual(chapterReference.Book, "Genesis");
Assert.AreEqual(chapterReference.ChapterNumber, 0);
Assert.AreEqual(chapterReference.ToString(), "Genesis 0");
Assert.IsTrue(chapterReference.IsValid);
}

/// <summary>
/// Tests a valid chapter constructor.
/// </summary>
[TestMethod]
public void TestBookAndChapterValidChapterConstructor()
{
ChapterReference chapterReference = new ChapterReference("Genesis 50");
Assert.AreEqual(chapterReference.Book, "Genesis");
Assert.AreEqual(chapterReference.ChapterNumber, 50);
Assert.AreEqual(chapterReference.ToString(), "Genesis 50");
Assert.IsTrue(chapterReference.IsValid);
}

/// <summary>
/// Tests an invalid two parameter constructor.
/// </summary>
[TestMethod]
public void TestTwoParameterInvalidConstructor()
{
ChapterReference chapterReference = new ChapterReference(string.Empty, 0);
Assert.AreEqual(chapterReference.Book, string.Empty);
Assert.AreEqual(chapterReference.ChapterNumber, 0);
Assert.AreEqual(chapterReference.ToString(), string.Empty);
Assert.IsFalse(chapterReference.IsValid);
}

/// <summary>
/// Tests an invalid two parameter constructor for Psalm 151.
/// </summary>
[TestMethod]
public void TestTwoParameterPsalm151Constructor()
{
ChapterReference chapterReference = new ChapterReference("Psalm 151", 0);
Assert.AreEqual(chapterReference.Book, "Psalm");
Assert.AreEqual(chapterReference.ChapterNumber, 151);
Assert.AreEqual(chapterReference.ToString(), "Psalm 151");
Assert.IsTrue(chapterReference.IsValid);
}

/// <summary>
/// Tests a valid two parameter constructor.
/// </summary>
[TestMethod]
public void TestTwoParameterValidConstructor()
{
ChapterReference chapterReference = new ChapterReference("Genesis", 50);
Assert.AreEqual(chapterReference.Book, "Genesis");
Assert.AreEqual(chapterReference.ChapterNumber, 50);
Assert.AreEqual(chapterReference.ToString(), "Genesis 50");
Assert.IsTrue(chapterReference.IsValid);
}
}
}
35 changes: 35 additions & 0 deletions GoToBible.Tests.Model/ChapterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// -----------------------------------------------------------------------
// <copyright file="ChapterTests.cs" company="Conglomo">
// Copyright 2020-2021 Conglomo Limited. Please see LICENSE for license details.
// </copyright>
// -----------------------------------------------------------------------

namespace GoToBible.Tests.Model
{
using GoToBible.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
/// Tests the <see cref="Chapter"/> class.
/// </summary>
[TestClass]
public class ChapterTests
{
/// <summary>
/// Tests the empty object.
/// </summary>
[TestMethod]
public void TestEmpty()
{
Chapter chapter = new Chapter();
Assert.AreEqual(chapter.Book, string.Empty);
Assert.AreEqual(chapter.ChapterNumber, 0);
Assert.AreEqual(chapter.Copyright, string.Empty);
Assert.AreEqual(chapter.NextChapterReference.ToString(), string.Empty);
Assert.AreEqual(chapter.PreviousChapterReference.ToString(), string.Empty);
Assert.IsFalse(chapter.SupportsItalics);
Assert.AreEqual(chapter.Text, string.Empty);
Assert.AreEqual(chapter.Translation, string.Empty);
}
}
}
Loading

0 comments on commit 412596b

Please sign in to comment.