Skip to content

Commit 7547380

Browse files
committed
Throw new Exeptions. Change properties for View. Add unit tests.
1 parent 45bfa0c commit 7547380

6 files changed

Lines changed: 155 additions & 55 deletions

File tree

FiscalDeviceStatusDecoder/Application/MainViewModel.cs

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,53 @@ public List<StatusBit>? StatusDevices
3232
OnPropertyChanged(nameof(StatusDevices));
3333
}
3434
}
35+
public string DisplayHex
36+
{
37+
get => Hex.ToUpper();
38+
set
39+
{
40+
hex = value;
41+
}
42+
}
3543

36-
public string HEX
44+
public string DisplayBytes
3745
{
38-
get => hex.ToUpper();
46+
get => $"Status Bytes {Bytes}";
47+
}
48+
49+
public string Hex
50+
{
51+
get => hex;
3952
set
4053
{
4154
hex = value;
42-
OnPropertyChanged(nameof(HEX));
55+
OnPropertyChanged(nameof(DisplayHex));
4356
}
4457
}
4558

4659
public string Bytes
4760
{
48-
get => $"Status Bytes {bytes}";
61+
get => bytes;
4962
set
5063
{
5164
bytes = value;
52-
OnPropertyChanged(nameof(Bytes));
65+
OnPropertyChanged(nameof(DisplayBytes));
5366
}
5467
}
5568

5669
public ICommand DecodeCommand => new RelayCommand(execute: _ => StatusDevices = InitializeStatusDevice(SelectedDevices, hex), canExecute: _ => hex?.Length > 0);
5770

71+
public string DecodeToByte(ref string hex)
72+
{
73+
Hex hexValue = new(hex);
74+
75+
hexValue.ReduceRange(null, 127);
76+
77+
hex = Regex.Replace(hexValue.ToString(), ".{2}", "$0 ").Trim();
78+
79+
return Regex.Replace(hexValue.ConvertToBinary(), ".{8}", "$0 ").Trim();
80+
}
81+
5882
public void OnPropertyChanged(string prop = "")
5983
{
6084
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
@@ -71,41 +95,37 @@ public void OnPropertyChanged(string prop = "")
7195
{
7296
MessageBox.ShowMessage(ex.Message, "Oops..", MessageBoxButton.OK, MessageBoxImage.Error);
7397

74-
HEX = string.Empty;
98+
Hex = string.Empty;
7599
Bytes = string.Empty;
76100

77101
return null;
78102
}
79103
}
80104

81-
public string DecodeToByte(ref string hex)
105+
public List<string> SetStatusBytesAndHex(IDeviceModels selectedDevices, string hex)
82106
{
83-
Hex hexValue = new(hex);
107+
string bytes = DecodeToByte(ref hex);
84108

85-
hexValue.ReduceRange(null, 127);
109+
List<string> hexArray = hex.Split(' ').ToList();
110+
List<string> bytesArray = bytes.Split(' ').ToList();
86111

87-
hex = Regex.Replace(hexValue.ToString(), ".{2}", "$0 ").Trim();
112+
if (bytesArray.Count < selectedDevices.QuantityStatusByte)
113+
{
114+
throw new ArgumentException($"For this group device quantity of byte must be minimum {selectedDevices.QuantityStatusByte}!");
115+
}
116+
else if (bytesArray.Count > selectedDevices.QuantityStatusByte)
117+
{
118+
bytesArray.RemoveFrom(selectedDevices.QuantityStatusByte);
119+
hexArray.RemoveFrom(selectedDevices.QuantityStatusByte);
120+
}
88121

89-
return Regex.Replace(hexValue.ConvertToBinary(), ".{8}", "$0 ").Trim();
90-
}
122+
Bytes = string.Join(" ", bytesArray);
123+
Hex = string.Join(" ", hexArray);
91124

92-
private static List<IDeviceModels> InitializeDevices() => new List<IDeviceModels>()
93-
{
94-
new DeviceModels(Datecs.Instance, 6 , Country.BG , new string[] { "DP-05", "DP-25", "DP-35", "WP-50", "DP-150" }),
95-
new DeviceModels(Datecs.Instance, 6, Country.BG, new string[] { "FP-800", "FP-2000", "FP-650", "SK1-21F", "SK1-31F", "FMP-10", "FP-700" }),
96-
new DeviceModels(Datecs.Instance, 8, Country.BG, new string[] { "DP-25X", "DP-05C", "WP-500X", "WP-50X", "FP-700X", "FP-700XR", "FMP-350Xv", "FMP-55X" }),
97-
new DeviceModels(Datecs.Instance, 8, Country.RO),
98-
new DeviceModels(Daisy.Instance, 6, Country.BG),
99-
new DeviceModels(Daisy.Instance, 6, Country.KZ),
100-
new DeviceModels(Eltrade.Instance, 6, Country.BG),
101-
new DeviceModels(Tremol.Instance, 7, Country.BG),
102-
new DeviceModels(Tremol.Instance, 6, Country.KE, new string[] { "CU", "M23" }),
103-
new DeviceModels(Incotext.Instance, 4, Country.KE),
104-
new DeviceModels(Port.Instance, 8, Country.RU, new string[] { "150", "600", "1000" }),
105-
new DeviceModels(Port.Instance, 6, Country.KZ, new string[] { "150" }),
106-
};
125+
return bytesArray;
126+
}
107127

108-
private List<StatusBit> SetStatusDevice(IDeviceModels selectedDevices, List<string> bytesArray)
128+
public List<StatusBit> SetStatusDevice(IDeviceModels selectedDevices, List<string> bytesArray)
109129
{
110130
List<StatusBit> status = new();
111131
var manufacturer = selectedDevices.Manufacturer;
@@ -118,29 +138,23 @@ private List<StatusBit> SetStatusDevice(IDeviceModels selectedDevices, List<stri
118138

119139
status.Add(new StatusBit(statusBit, keyValuePair.Value));
120140
}
141+
121142
return status;
122143
}
123144

124-
private List<string> SetStatusBytesAndHex(IDeviceModels selectedDevices, string hex)
125-
{
126-
string bytes = DecodeToByte(ref hex);
127-
128-
List<string> hexArray = hex.Split(' ').ToList();
129-
List<string> bytesArray = bytes.Split(' ').ToList();
130-
131-
if (bytesArray.Count < selectedDevices.QuantityStatusByte)
132-
{
133-
throw new ArgumentException($"For this group device quantity of byte must be minimum {selectedDevices.QuantityStatusByte}!");
134-
}
135-
else if (bytesArray.Count > selectedDevices.QuantityStatusByte)
145+
private static List<IDeviceModels> InitializeDevices() => new List<IDeviceModels>()
136146
{
137-
bytesArray.RemoveFrom(selectedDevices.QuantityStatusByte);
138-
Bytes = string.Join(" ", bytesArray);
139-
140-
hexArray.RemoveFrom(selectedDevices.QuantityStatusByte);
141-
HEX = string.Join(" ", hexArray);
142-
}
143-
144-
return bytesArray;
145-
}
147+
new DeviceModels(Datecs.Instance, 6 , Country.BG , new string[] { "DP-05", "DP-25", "DP-35", "WP-50", "DP-150" }),
148+
new DeviceModels(Datecs.Instance, 6, Country.BG, new string[] { "FP-800", "FP-2000", "FP-650", "SK1-21F", "SK1-31F", "FMP-10", "FP-700" }),
149+
new DeviceModels(Datecs.Instance, 8, Country.BG, new string[] { "DP-25X", "DP-05C", "WP-500X", "WP-50X", "FP-700X", "FP-700XR", "FMP-350Xv", "FMP-55X" }),
150+
new DeviceModels(Datecs.Instance, 8, Country.RO),
151+
new DeviceModels(Daisy.Instance, 6, Country.BG),
152+
new DeviceModels(Daisy.Instance, 6, Country.KZ),
153+
new DeviceModels(Eltrade.Instance, 6, Country.BG),
154+
new DeviceModels(Tremol.Instance, 7, Country.BG),
155+
new DeviceModels(Tremol.Instance, 6, Country.KE, new string[] { "CU", "M23" }),
156+
new DeviceModels(Incotext.Instance, 4, Country.KE),
157+
new DeviceModels(Port.Instance, 8, Country.RU, new string[] { "150", "600", "1000" }),
158+
new DeviceModels(Port.Instance, 6, Country.KZ, new string[] { "150" }),
159+
};
146160
}

FiscalDeviceStatusDecoder/Application/Services/Hex.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public string ConvertToBinary(string separator = "") =>
2222

2323
public void ReduceRange(int? lessThan = null, int? moreThan = null)
2424
{
25+
bool _ = Value.Length % 2 != 0 ? true : throw new ArgumentException($"You must write HEX number!");
26+
2527
List<string> validHexNumbers = new List<string>();
2628

2729
for (int i = 0; i < Value.Length; i += 2)

FiscalDeviceStatusDecoder/Domain/Manufacturer/BaseManufacturer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public abstract class BaseManufacturer
3030
}
3131
}
3232

33-
throw new NotImplementedException();
33+
throw new ArgumentException($"Check that the documentation for this device group is correct.");
3434
}
3535
}
3636

FiscalDeviceStatusDecoder/Presentation/MainWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
VerticalContentAlignment="Center"
5252
Padding="10"
5353
Margin="0,10,10,10"
54-
Text="{Binding HEX , Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}">
54+
Text="{Binding DisplayHex , Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}">
5555
</TextBox>
5656
<Button Grid.Column="2"
5757
Margin="0,10,30,10"
@@ -61,7 +61,7 @@
6161

6262
<GroupBox Grid.Row="2"
6363
FontSize="14"
64-
Header="{Binding Bytes}"
64+
Header="{Binding DisplayBytes}"
6565
Margin="10"
6666
Padding="10">
6767
<ListView ItemsSource="{Binding StatusDevices , Mode=TwoWay}"

FiscalDeviceStatusDecoderTest/FiscalDeviceStatusDecoderTest.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
15+
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
16+
<PackageReference Include="NSubstitute" Version="4.4.0" />
1517
<PackageReference Include="NUnit" Version="3.13.3" />
1618
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
1719
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />

FiscalDeviceStatusDecoderTest/MainViewModelTest.cs

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FiscalDeviceStatusDecoder.Application;
22
using FiscalDeviceStatusDecoder.Domain;
3+
using NSubstitute;
34
using NUnit.Framework;
45
using System.Windows;
56

@@ -31,18 +32,29 @@ public void Setup()
3132
[TestCase("04 80 80 C0 80 80 B8 05 FF FF")]
3233
[TestCase("048080C08080B805FFFF")]
3334
[TestCase("04H 80H 80H C0H 80H 80H B8H 05H FFH FF")]
34-
public void DecodeToByte_Hex10Numbers_Hex8Numbers(string hex)
35+
public void DecodeToByte_VariousNumbers_OneFormar(string hex)
3536
{
3637
target.DecodeToByte(ref hex);
3738
Assert.That(hex, Is.EqualTo("80 80 C0 80 80 B8 FF FF"));
3839
}
3940

41+
[Test]
42+
[TestCase("04 80 80 C0 80 80 B8 05 FF FF")]
43+
[TestCase("048080C08080B805FFFF")]
44+
[TestCase("04H 80H 80H C0H 80H 80H B8H 05H FFH FF")]
45+
public void InitializeStatusDevices_VariousHex_OneFormatHex(string hex)
46+
{
47+
target.InitializeStatusDevice(target.SelectedDevices, hex);
48+
49+
Assert.That(target.DisplayHex, Is.EqualTo("80 80 C0 80 80 B8"));
50+
}
51+
4052
[Test]
4153
[TestCase("04 80 80 C0 80 80 B8 05 FF FF")]
4254
[TestCase("048080C08080B805FFFF")]
4355
[TestCase("80 80 C0 80 80 B8 FF FF")]
4456
[TestCase("04H 80H 80H C0H 80H 80H B8H 05H FFH FF")]
45-
public void InitializeStatusDevices_Hex8Numbers_Nothing(string hex)
57+
public void InitializeStatusDevices_HexALotOfNumbers_Nothing(string hex)
4658
{
4759
foreach (var devices in MainViewModel.Devices)
4860
{
@@ -83,7 +95,7 @@ public void InitializeStatusDevices_Hex8Numbers_EqualsStatusBit(string hex1, str
8395
[TestCase("80 80 80 80")]
8496
[TestCase("80 80 80 80 80 80 80 80")]
8597
[TestCase("80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80")]
86-
public void InitializeStatusDevices_Hex_MessagesAboutError(string hex)
98+
public void InitializeStatusDevices_HexLessNumber_MessagesAboutError(string hex)
8799
{
88100
// arrange
89101
int countErrorForInvalidHex = 0;
@@ -96,8 +108,78 @@ public void InitializeStatusDevices_Hex_MessagesAboutError(string hex)
96108

97109
target.InitializeStatusDevice(devices, hex);
98110
}
111+
// assert
112+
Assert.That(messageBox.CountMessage, Is.EqualTo(countErrorForInvalidHex));
113+
}
114+
115+
[Test]
116+
[TestCase("04 80 80 C0 80 80 B8 05 FF FF")]
117+
[TestCase("048080C08080B805FFFF")]
118+
[TestCase("04H 80H 80H C0H 80H 80H B8H 05H FFH FF")]
119+
public void InitializeStatusDevices_HexAndInvalidModels_MessagesAboutError(string hex)
120+
{
121+
// arrange
122+
int countErrorForInvalidHex = 0;
123+
124+
// act
125+
foreach (var devices in MainViewModel.Devices)
126+
{
127+
var mock = Substitute.For<DeviceModels>(devices.Manufacturer, devices.QuantityStatusByte, devices.Country, new string[] { "INVALID_MODELS" });
128+
countErrorForInvalidHex++;
129+
130+
target.InitializeStatusDevice(mock, hex);
131+
}
99132

100133
// assert
101134
Assert.That(messageBox.CountMessage, Is.EqualTo(countErrorForInvalidHex));
102135
}
136+
137+
[Test]
138+
[TestCase("04 80 80 C0 80 80 B8 05 FF FF")]
139+
[TestCase("048080C08080B805FFFF")]
140+
[TestCase("04H 80H 80H C0H 80H 80H B8H 05H FFH FF")]
141+
public void SetStatusBytesAndHex_Hex10Numbers_CorrectQuantityBytes(string hex)
142+
{
143+
target.SetStatusBytesAndHex(target.SelectedDevices, hex);
144+
List<string> bytesArray = target.Bytes.Split(' ').ToList();
145+
146+
Assert.That(bytesArray.Count, Is.EqualTo(target.SelectedDevices.QuantityStatusByte));
147+
}
148+
149+
[Test]
150+
[TestCase("04 80 80 C0 80 80 B8 05 FF FF")]
151+
[TestCase("048080C08080B805FFFF")]
152+
[TestCase("04H 80H 80H C0H 80H 80H B8H 05H FFH FF")]
153+
public void SetStatusBytesAndHex_Hex10Numbers_CorrectDisplayStringBytes(string hex)
154+
{
155+
target.SetStatusBytesAndHex(target.SelectedDevices, hex);
156+
157+
Assert.True(target.DisplayBytes.Contains(target.Bytes));
158+
}
159+
160+
[Test]
161+
[TestCase("04 80 80 C0 80 80 B8 05 FF FF")]
162+
[TestCase("048080C08080B805FFFF")]
163+
[TestCase("04H 80H 80H C0H 80H 80H B8H 05H FFH FF")]
164+
public void SetStatusBytesAndHex_Hex10Numbers_CorrectQuantityHex(string hex)
165+
{
166+
target.SetStatusBytesAndHex(target.SelectedDevices, hex);
167+
List<string> bytesHex = target.Hex.Split(' ').ToList();
168+
169+
Assert.That(bytesHex.Count, Is.EqualTo(target.SelectedDevices.QuantityStatusByte));
170+
}
171+
172+
[Test]
173+
[TestCase("04 80 80 C0 80 80 B8 05 FF FF")]
174+
[TestCase("048080C08080B805FFFF")]
175+
[TestCase("04H 80H 80H C0H 80H 80H B8H 05H FFH FF")]
176+
public void SetStatusBytesAndHex_Hex10Numbers_CorrectDisplayStringHex(string hex)
177+
{
178+
int qttyByte = target.SelectedDevices.QuantityStatusByte;
179+
int qttyByteWithSeparator = (qttyByte * 2) + (qttyByte - 1);
180+
181+
target.SetStatusBytesAndHex(target.SelectedDevices, hex);
182+
183+
Assert.That(target.DisplayHex.Length, Is.EqualTo(qttyByteWithSeparator));
184+
}
103185
}

0 commit comments

Comments
 (0)