Skip to content

Commit

Permalink
Adds methods to check whether a range has the same column or row.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sydney du Plooy committed May 19, 2018
1 parent 2f159c7 commit bc2a9b8
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Celloc.Tests/CellIndexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public void It_should_throw_an_exception_when_the_cell_is_not_in_the_correct_for
}

[Test]
public void It_should_translate_the_cell_to_an_integer_tuple()
public void It_should_translate_the_cell_to_a_tuple()
{
Assert.AreEqual((1, 1), CellIndex.Translate("A1"));
Assert.AreEqual((16384, 1048576), CellIndex.Translate("XFD1048576"));
}

[Test]
public void It_should_translate_the_cell_to_a_zero_based_integer_tuple()
public void It_should_translate_the_cell_to_a_zero_based_tuple()
{
Assert.AreEqual((0, 0), CellIndex.Translate("A1", Offset.ZeroBased));
Assert.AreEqual((16383, 1048575), CellIndex.Translate("XFD1048576", Offset.ZeroBased));
Expand Down
78 changes: 74 additions & 4 deletions Celloc.Tests/CellRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public void It_should_throw_an_exception_when_the_range_is_not_in_the_correct_fo
}

[Test]
public void It_should_convert_the_range_to_an_integer_tuple()
public void It_should_convert_the_range_to_a_tuple()
{
Assert.AreEqual(((1, 1), (1, 1)), CellRange.Translate("A1:A1"));
Assert.AreEqual(((1, 1), (16384, 1048576)), CellRange.Translate("A1:XFD1048576"));
}

[Test]
public void It_should_convert_the_range_to_a_zero_based_integer_tuple()
public void It_should_convert_the_range_to_a_zero_based_tuple()
{
Assert.AreEqual(((0, 0), (0, 0)), CellRange.Translate("A1:A1", Offset.ZeroBased));
Assert.AreEqual(((0, 0), (16383, 1048575)), CellRange.Translate("A1:XFD1048576", Offset.ZeroBased));
Expand All @@ -42,17 +42,87 @@ public void It_should_throw_an_exception_when_the_range_parameter_is_empty()
}

[Test]
public void It_should_convert_the_range_integer_tuple_to_a_string()
public void It_should_convert_the_range_tuple_to_a_string()
{
Assert.AreEqual("A1:A1", CellRange.Translate(((1, 1), (1, 1))));
Assert.AreEqual("A1:XFD1048576", CellRange.Translate(((1, 1), (16384, 1048576))));
}

[Test]
public void It_should_convert_the_zero_based_range_integer_tuple_to_a_string()
public void It_should_convert_the_zero_based_range_tuple_to_a_string()
{
Assert.AreEqual("A1:A1", CellRange.Translate(((0, 0), (0, 0)), Offset.ZeroBased));
Assert.AreEqual("A1:XFD1048576", CellRange.Translate(((0, 0), (16383, 1048575)), Offset.ZeroBased));
}
}

[TestFixture]
public class When_calling_is_same_row_on_cell_range
{
[Test]
public void It_should_throw_an_exception_when_the_range_parameter_is_null()
{
var exception = Assert.Throws<ArgumentNullException>(() => CellRange.IsSameRow(null));
Assert.AreEqual($"Value cannot be null.{Environment.NewLine}Parameter name: range", exception.Message);
}

[Test]
public void It_should_return_true_when_the_range_is_for_the_same_row()
{
Assert.IsTrue(CellRange.IsSameRow("A1:D1"));
}

[Test]
public void It_should_return_false_when_the_range_is_not_for_the_same_row()
{
Assert.IsFalse(CellRange.IsSameRow("A1:A20"));
}

[Test]
public void It_should_return_true_when_the_range_tuple_is_for_the_same_row()
{
Assert.IsTrue(CellRange.IsSameRow(((0, 0), (20, 0))));
}

[Test]
public void It_should_return_false_when_the_range_tuple_is_not_for_the_same_row()
{
Assert.IsFalse(CellRange.IsSameRow(((0, 0), (0, 20))));
}
}

[TestFixture]
public class When_calling_is_same_column_on_cell_range
{
[Test]
public void It_should_throw_an_exception_when_the_range_parameter_is_null()
{
var exception = Assert.Throws<ArgumentNullException>(() => CellRange.IsSameColumn(null));
Assert.AreEqual($"Value cannot be null.{Environment.NewLine}Parameter name: range", exception.Message);
}

[Test]
public void It_should_return_true_when_the_range_is_for_the_same_column()
{
Assert.IsTrue(CellRange.IsSameColumn("A1:A20"));
}

[Test]
public void It_should_return_false_when_the_range_is_not_for_the_same_column()
{
Assert.IsFalse(CellRange.IsSameColumn("A1:D1"));
}

[Test]
public void It_should_return_true_when_the_range_tuple_is_for_the_same_column()
{
Assert.IsTrue(CellRange.IsSameColumn(((0, 0), (0, 20))));
}

[Test]
public void It_should_return_false_when_the_range_tuple_is_not_for_the_same_column()
{
Assert.IsFalse(CellRange.IsSameColumn(((0, 0), (4, 0))));
}
}
}
5 changes: 3 additions & 2 deletions Celloc.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
<package >
<metadata>
<id>Celloc</id>
<version>2.3.0</version>
<version>2.4.0</version>
<title>Celloc</title>
<authors>Sydney du Plooy</authors>
<licenseUrl>https://github.com/sduplooy/Celloc/blob/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/sduplooy/Celloc</projectUrl>
<iconUrl>https://raw.githubusercontent.com/wiki/sduplooy/Celloc/images/186401-64.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>An Excel cell index converter.</description>
<releaseNotes>Adds translation for a range tuple to string.</releaseNotes>
<releaseNotes>Adds methods to check whether a range has the same column or row.</releaseNotes>
<copyright>Copyright (c) 2018 Sydney du Plooy</copyright>
<tags>Celloc Excel Column Number Converter</tags>
<dependencies>
Expand Down
35 changes: 33 additions & 2 deletions Celloc/CellRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public static class CellRange

public static ((int Column, int Row), (int Column, int Row)) Translate(string range, Offset offset = Offset.None)
{
if (string.IsNullOrEmpty(range))
throw new ArgumentNullException(nameof(range));
GuardAgainstNullRange(range);

var regex = new Regex(RangePattern);
var match = regex.Match(range);
Expand Down Expand Up @@ -41,5 +40,37 @@ public static string Translate(((int Column, int Row), (int Column, int Row)) ra

return $"{fromCell}:{toCell}";
}

public static bool IsSameColumn(((int Column, int Row), (int Column, int Row)) range)
{
return Equals(range.Item1.Column, range.Item2.Column);
}

public static bool IsSameRow(((int Column, int Row), (int Column, int Row)) range)
{
return Equals(range.Item1.Row, range.Item2.Row);
}

public static bool IsSameColumn(string range)
{
GuardAgainstNullRange(range);

var tuple = Translate(range);
return IsSameColumn(tuple);
}

public static bool IsSameRow(string range)
{
GuardAgainstNullRange(range);

var tuple = Translate(range);
return IsSameRow(tuple);
}

private static void GuardAgainstNullRange(string range)
{
if (string.IsNullOrEmpty(range))
throw new ArgumentNullException(nameof(range));
}
}
}
4 changes: 2 additions & 2 deletions Celloc/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.3.0")]
[assembly: AssemblyFileVersion("2.3.0")]
[assembly: AssemblyVersion("2.4.0")]
[assembly: AssemblyFileVersion("2.4.0")]
[assembly: InternalsVisibleTo("Celloc.Tests")]

0 comments on commit bc2a9b8

Please sign in to comment.