Skip to content

Commit 2369303

Browse files
Closes #11: Merge branch '#11-missing-config' into main
2 parents 4ee873d + d7cc253 commit 2369303

File tree

7 files changed

+156
-8
lines changed

7 files changed

+156
-8
lines changed

src/.idea/.idea.Stravaig.ShortCode/.idea/riderModule.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Stravaig.ShortCode.Benchmarks/EncoderBenchmarks.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public Config()
1717
}
1818

1919
private ulong[] _codes = new ulong[1];
20-
private IEncoder _defaultEncoder;
2120

2221
[Params(3, 5, 7, 9)]
2322
public int FixedLength { get; set; }

src/Stravaig.ShortCode.Benchmarks/EncoderVariantBenchmarks.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace Stravaig.ShortCode.Benchmarks
55
{
66
public class EncoderVariantBenchmarks
77
{
8-
private ulong[] _codes = new ulong[1024];
9-
private int _index = 0;
8+
private readonly ulong[] _codes = new ulong[1024];
9+
private int _index;
1010
private Encoder _baselineEncoder = new Encoder(NamedCharacterSpaces.ReducedAmbiguity);
1111
private Encoder1 _encoder1 = new Encoder1(NamedCharacterSpaces.ReducedAmbiguity);
1212
private Encoder2 _encoder2 = new Encoder2(NamedCharacterSpaces.ReducedAmbiguity);
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using NUnit.Framework;
3+
using Shouldly;
4+
5+
namespace Stravaig.ShortCode.Tests
6+
{
7+
[TestFixture]
8+
public class ShortCodeOptionsTests
9+
{
10+
[Test]
11+
public void Ctor_ProducesReasonableValues()
12+
{
13+
ShortCodeOptions opts = new ShortCodeOptions();
14+
opts.Generator.ShouldNotBeNull();
15+
opts.Generator.ShouldBeEmpty();
16+
opts.CharacterSpace.ShouldBe(NamedCharacterSpaces.ReducedAmbiguity);
17+
opts.FixedLength.ShouldBeNull();
18+
opts.MaxLength.ShouldBe(64);
19+
}
20+
21+
[Test]
22+
[TestCase(null)]
23+
[TestCase("")]
24+
[TestCase(" ")]
25+
[TestCase("A")]
26+
public void SetCharacterSpace_InvalidValues_ThrowsException(string value)
27+
{
28+
Should.Throw<ArgumentException>(() =>
29+
new ShortCodeOptions()
30+
{
31+
CharacterSpace = value,
32+
}).ParamName.ShouldBe("CharacterSpace");
33+
}
34+
35+
[Test]
36+
[TestCase("AB")]
37+
[TestCase(NamedCharacterSpaces.LettersAndDigits)]
38+
public void SetCharacterSpace_ValidValues_GetsSameValue(string value)
39+
{
40+
var opts = new ShortCodeOptions()
41+
{
42+
CharacterSpace = value,
43+
};
44+
opts.CharacterSpace.ShouldBe(value);
45+
}
46+
47+
[Test]
48+
[TestCase(-1)]
49+
[TestCase(0)]
50+
[TestCase(65)]
51+
public void SetFixedLength_InvalidValues_ThrowsException(int? value)
52+
{
53+
Should.Throw<ArgumentException>(() =>
54+
new ShortCodeOptions()
55+
{
56+
FixedLength = value,
57+
}).ParamName.ShouldBe("FixedLength");
58+
}
59+
60+
[Test]
61+
[TestCase(null)]
62+
[TestCase(1)]
63+
[TestCase(64)]
64+
public void SetFixedLength_ValidValues_GetsSameValue(int? value)
65+
{
66+
var opts = new ShortCodeOptions()
67+
{
68+
FixedLength = value,
69+
};
70+
opts.FixedLength.ShouldBe(value);
71+
}
72+
73+
[Test]
74+
[TestCase(-1)]
75+
[TestCase(0)]
76+
[TestCase(65)]
77+
public void SetMaxLength_InvalidValues_ThrowsException(int value)
78+
{
79+
Should.Throw<ArgumentException>(() =>
80+
new ShortCodeOptions
81+
{
82+
MaxLength = value,
83+
}).ParamName.ShouldBe("MaxLength");
84+
}
85+
86+
[Test]
87+
[TestCase(1)]
88+
[TestCase(10)]
89+
[TestCase(64)]
90+
public void SetMaxLength_ValidValues_GetsSameValue(int value)
91+
{
92+
var opts = new ShortCodeOptions
93+
{
94+
MaxLength = value,
95+
};
96+
opts.MaxLength.ShouldBe(value);
97+
}
98+
99+
}
100+
}

src/Stravaig.ShortCode.sln

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "__Build_and_Deploy", "__Bui
1616
..\.stravaig\list-contributor-akas.json = ..\.stravaig\list-contributor-akas.json
1717
..\Run-Benchmarks.ps1 = ..\Run-Benchmarks.ps1
1818
..\build-release-notes.ps1 = ..\build-release-notes.ps1
19+
..\version.txt = ..\version.txt
20+
..\Set-Version.ps1 = ..\Set-Version.ps1
1921
EndProjectSection
2022
EndProject
2123
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "__Documenation", "__Documenation", "{9342FEEB-81FD-48BE-BA72-773C679EB0D1}"

src/Stravaig.ShortCode/SequentialCodeGenerator.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
using System.Linq;
31
using Microsoft.Extensions.Options;
42
using Stravaig.ShortCode.Internal;
53

src/Stravaig.ShortCode/ShortCodeOptions.cs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,60 @@
1+
using System;
12
using System.Collections.Generic;
23

34
namespace Stravaig.ShortCode
45
{
56
public class ShortCodeOptions
67
{
7-
public string CharacterSpace { get; set; } = Stravaig.ShortCode.NamedCharacterSpaces.ReducedAmbiguity;
8-
public int MaxLength { get; set; } = int.MaxValue;
9-
public int? FixedLength { get; set; }
8+
private const int AbsoluteMinLength = 1;
9+
private const int AbsoluteMaxLength = 64;
10+
private const int CharacterSpaceMinLength = 2;
11+
12+
private int _maxLength = AbsoluteMaxLength;
13+
private int? _fixedLength;
14+
private string _characterSpace = NamedCharacterSpaces.ReducedAmbiguity;
15+
16+
public string CharacterSpace
17+
{
18+
get => _characterSpace;
19+
set
20+
{
21+
if (string.IsNullOrWhiteSpace(value))
22+
throw new ArgumentException(
23+
"The value cannot be, null, Empty or contain whitespace.",
24+
nameof(CharacterSpace));
25+
if (value.Length < CharacterSpaceMinLength)
26+
throw new ArgumentException(
27+
$"The value must contain at least {CharacterSpaceMinLength} characters.",
28+
nameof(CharacterSpace));
29+
_characterSpace = value;
30+
}
31+
}
32+
33+
public int MaxLength
34+
{
35+
get => _maxLength;
36+
set
37+
{
38+
if (value < AbsoluteMinLength || value > AbsoluteMaxLength)
39+
throw new ArgumentOutOfRangeException(
40+
nameof(MaxLength),
41+
$"The value must be between {AbsoluteMinLength} and {AbsoluteMaxLength}.");
42+
_maxLength = value;
43+
}
44+
}
45+
46+
public int? FixedLength
47+
{
48+
get => _fixedLength;
49+
set
50+
{
51+
if (value.HasValue && (value.Value < AbsoluteMinLength || value.Value > AbsoluteMaxLength))
52+
throw new ArgumentOutOfRangeException(
53+
nameof(FixedLength),
54+
$"The value, if present, must be between {AbsoluteMinLength} and {AbsoluteMaxLength}");
55+
_fixedLength = value;
56+
}
57+
}
1058

1159
public Dictionary<string, object> Generator { get; set; } = new Dictionary<string, object>();
1260
}

0 commit comments

Comments
 (0)