-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from bolorundurowb/feature/add-short-id-options
feature/add short id options
- Loading branch information
Showing
8 changed files
with
198 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using FluentAssertions; | ||
using shortid.Configuration; | ||
using Xunit; | ||
|
||
namespace shortid.Test.Configuration | ||
{ | ||
public class GenerationOptionsTests | ||
{ | ||
[Fact] | ||
public void ShouldSetDefaultsOnInstantiation() | ||
{ | ||
var options = new GenerationOptions(); | ||
|
||
options | ||
.Length | ||
.Should() | ||
.BeGreaterThan(0); | ||
options | ||
.UseNumbers | ||
.Should() | ||
.BeFalse(); | ||
options | ||
.UseSpecialCharacters | ||
.Should() | ||
.BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldAssignRandomLengthOnInstantiation() | ||
{ | ||
var options = new GenerationOptions(); | ||
|
||
options | ||
.Length | ||
.Should() | ||
.BeGreaterThan(7); | ||
options | ||
.Length | ||
.Should() | ||
.BeLessThan(15); | ||
} | ||
|
||
[Fact] | ||
public void ShouldAllowDefaultsToBeChanged() | ||
{ | ||
const bool useNumbers = true; | ||
const bool useSpecial = false; | ||
const int length = 17; | ||
|
||
var options = new GenerationOptions | ||
{ | ||
UseNumbers = useNumbers, | ||
UseSpecialCharacters = useSpecial, | ||
Length = length | ||
}; | ||
|
||
options | ||
.Length | ||
.Should() | ||
.Be(length); | ||
options | ||
.UseNumbers | ||
.Should() | ||
.Be(useNumbers); | ||
options | ||
.UseSpecialCharacters | ||
.Should() | ||
.Be(useSpecial); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using shortid.Utils; | ||
|
||
namespace shortid.Configuration | ||
{ | ||
public class GenerationOptions | ||
{ | ||
/// <summary> | ||
/// Determines whether numbers are used in generating the id | ||
/// Default: false | ||
/// </summary> | ||
public bool UseNumbers { get; set; } | ||
|
||
/// <summary> | ||
/// Determines whether special characters are used in generating the id | ||
/// Default: true | ||
/// </summary> | ||
public bool UseSpecialCharacters { get; set; } = true; | ||
|
||
/// <summary> | ||
/// Determines the length of the generated id | ||
/// Default: a random length between 7 and 15 | ||
/// </summary> | ||
public int Length { get; set; } = | ||
RandomUtils.GenerateNumberInRange(Constants.MinimumAutoLength, Constants.MaximumAutoLength); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace shortid.Utils | ||
{ | ||
internal static class Constants | ||
{ | ||
public const int MinimumAutoLength = 7; | ||
|
||
public const int MaximumAutoLength = 15; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
namespace shortid.Utils | ||
{ | ||
internal static class RandomUtils | ||
{ | ||
private static readonly Random Random = new Random(); | ||
private static readonly object ThreadLock = new object(); | ||
|
||
public static int GenerateNumberInRange(int min, int max) | ||
{ | ||
lock (ThreadLock) | ||
{ | ||
return Random.Next(min, max); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters