-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertToNumericBaseCommand.cs
More file actions
80 lines (70 loc) · 2.78 KB
/
ConvertToNumericBaseCommand.cs
File metadata and controls
80 lines (70 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using Example.Specification;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
using System.Management.Automation;
namespace Example.Application
{
[Cmdlet(VerbsData.ConvertTo, "NumericBase", ConfirmImpact = ConfirmImpact.Low)]
[Alias("ConvertTo-Base")]
public sealed class ConvertToNumericBaseCommand : Cmdlet
{
private const String SpecificationResultFile = "Example.Specification.json";
[Parameter(Mandatory = true, ValueFromPipeline = true)]
public Int32 SourceDigit { get; set; } = Int32.MinValue;
[Parameter(Mandatory = true)]
public Int32 SourceBase { get; set; } = 10;
[Parameter(Mandatory = true)]
[Alias("TargetBase")]
public Int32 DestinationBase { get; set; } = 10;
[Parameter(Mandatory = true)]
public IStack<Int32> DataStructure { get; set; }
private String SpecPath => Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), SpecificationResultFile);
protected override void BeginProcessing()
{
JArray specResults = JArray.Parse(File.ReadAllText(SpecPath));
if (! specResults
.Where(x => x["ModuleId"]["Name"].ToString() == "Example.Specification")
.Where(x => x["TypeId"]["Name"].ToString() == DataStructure.GetType().Name)
.SelectMany(x => x["TestResultIds"].Children())
.All(x => (Boolean)x["Passed"]))
{
ThrowTerminatingError(new ErrorRecord(
new ArgumentException($"Type {DataStructure.GetType().Name} is not known to meet specification. See {SpecPath}"),
"SpecificationSecurityException",
ErrorCategory.PermissionDenied,
DataStructure));
}
}
protected override void ProcessRecord()
{
DataStructure.Push(SourceDigit);
}
protected override void EndProcessing()
{
Int32 number = 0;
if (DataStructure.Count > 0)
{
// digit == 0
number += DataStructure.Pop();
}
Int32 digit = 1;
while (DataStructure.Count > 0)
{
digit *= SourceBase;
number += digit * DataStructure.Pop();
}
while (number > 0)
{
number = Math.DivRem(number, DestinationBase, out var remainder);
DataStructure.Push(remainder);
}
while (DataStructure.Count > 0)
{
WriteObject(DataStructure.Pop());
}
}
}
}