Skip to content

Commit

Permalink
Merge pull request #7 from tylerszabo/permissive_version
Browse files Browse the repository at this point in the history
Resolves issue #6

  - Disable explicit version checking on GLedApi
  - Add --list option
  - Increase verbosity
  - Improve test coverage
  • Loading branch information
tylerszabo authored Apr 15, 2018
2 parents b0f9080 + 437138e commit 351006a
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 20 deletions.
4 changes: 2 additions & 2 deletions GLedApiDotNet/RGBFusionMotherboardImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ internal RGBFusionMotherboard(Raw.GLedAPIv1_0_0Wrapper wrapperAPI)
api = wrapperAPI;

string ver = api.GetSdkVersion();
if (!("1.0.0".Equals(ver)))
if (string.IsNullOrEmpty(ver))
{
throw new GLedAPIException(string.Format("Unknown API version {0}. Expected 1.0.0", ver));
throw new GLedAPIException(string.Format("GLedApi returned empty version"));
}

api.Initialize();
Expand Down
10 changes: 5 additions & 5 deletions GLedApiDotNetTests/GLedApiv1_0_0Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Status
public enum ControlState
{
Uninitialized = 1,
DoneInit,
DoneInitAPI,
DoneGetMaxDivision,
DoneGetLedLayout,
DoneSetLedData
Expand All @@ -57,10 +57,10 @@ public bool IsInitialized
switch (state)
{
case ControlState.Uninitialized:
case ControlState.DoneGetLedLayout:
case ControlState.DoneInitAPI:
case ControlState.DoneGetMaxDivision:
return false;
case ControlState.DoneInit:
case ControlState.DoneGetLedLayout:
case ControlState.DoneSetLedData:
return true;
default:
Expand Down Expand Up @@ -150,7 +150,7 @@ public uint GetLedLayout(byte[] bytArray, int arySize)

public int GetMaxDivision()
{
AssertState(ControlState.DoneInit);
AssertState(ControlState.DoneInitAPI);
state = ControlState.DoneGetMaxDivision;
return maxDivisions;
}
Expand All @@ -176,7 +176,7 @@ public uint Get_IT8295_FwVer(byte[] bytArray, int arySize)
public uint InitAPI()
{
AssertState(ControlState.Uninitialized);
state = ControlState.DoneInit;
state = ControlState.DoneInitAPI;
return nextReturn;
}

Expand Down
1 change: 1 addition & 0 deletions GLedApiDotNetTests/Tests/RGBFusionMotherboardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void TestBadDivisions()
new RGBFusionMotherboard(new GLedApiDotNet.Raw.GLedAPIv1_0_0Wrapper(mock));
}

[Ignore] // This test is disabled until more rigorous version checking is implemented.
[TestMethod]
[ExpectedException(typeof(GLedAPIException))]
public void TestBadVersion()
Expand Down
20 changes: 18 additions & 2 deletions RGBFusionTool/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void Main(string[] args)
string opt_ColorCycle = null;
bool flag_DoCycle = false;
bool flag_Help = false;
bool flag_List = false;
string opt_Brightness = null;

OptionSet options = new OptionSet
Expand All @@ -51,7 +52,11 @@ public void Main(string[] args)
{"cycle|colorcycle:", "cycle colors, changing color every {SECONDS}", v => { flag_DoCycle = true; opt_ColorCycle = v; } },
{"b|brightness=", "brightness (0-100)", v => opt_Brightness = v },

{"?|h|help", "show help and exit", v => flag_Help = true }
{"l|list", "list zones", v => flag_List = true },

{"?|h|help", "show help and exit", v => flag_Help = true },

{"<>", v => { throw new OptionException(string.Format("Unexpected option \"{0}\"", v),"default"); } }
};

try
Expand All @@ -67,6 +72,15 @@ public void Main(string[] args)
return;
}

if (flag_List)
{
for (int i = 0; i < motherboardLEDs.Layout.Length; i++)
{
stdout.WriteLine("Zone {0}: {1}", i, motherboardLEDs.Layout[i]);
}
return;
}

if (!string.IsNullOrWhiteSpace(opt_Brightness))
{
brightness = byte.Parse(opt_Brightness);
Expand All @@ -91,6 +105,7 @@ public void Main(string[] args)
}
if (opt_Verbose > 0) { stdout.WriteLine("Static color: {0}", realColor.ToString()); }
setting = new StaticLedSetting(realColor, brightness);
if (opt_Verbose > 0) { stdout.WriteLine("Brightness: {0}", brightness); }
}

if (setting != null)
Expand All @@ -101,7 +116,8 @@ public void Main(string[] args)
catch (Exception e)
{
ShowHelp(options, stderr);
stderr.WriteLine("Error: {0}", e.Message);
stderr.WriteLine();
stderr.WriteLine("Error: {0}", e.ToString());
throw;
}
return;
Expand Down
4 changes: 2 additions & 2 deletions RGBFusionTool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@
// 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("0.9.0.2")]
[assembly: AssemblyFileVersion("0.9.0.2")]
[assembly: AssemblyVersion("0.9.0.3")]
[assembly: AssemblyFileVersion("0.9.0.3")]
14 changes: 12 additions & 2 deletions RGBFusionTool/RGBFusionMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,24 @@ private class LazyMotherboard : IRGBFusionMotherboard
public void SetAll(LedSetting ledSetting) => motherboard.Value.SetAll(ledSetting);
}

private static void Main(string[] args)
private static int Main(string[] args)
{
Application application = new Application(
new LazyMotherboard(),
Console.Out,
Console.Error
);
application.Main(args);

try
{
application.Main(args);
}
catch
{
return 1;
}

return 0;
}
}
}
106 changes: 99 additions & 7 deletions RGBFusionToolTests/RGBFusionToolExeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,26 @@ public void NoArgs()
}

// Color options
[DataRow(new string[] { "--color" }, DisplayName = "No value")]
[DataRow(new string[] { "--color=Invalid" }, DisplayName = "Bad name")]
[DataRow(new string[] { "--color" }, DisplayName = "--color")]
[DataRow(new string[] { "--color=Invalid" }, DisplayName = "--color=Invalid")]
[DataRow(new string[] { "--color", "--color=Red" }, DisplayName = "--color --color=Red")]
// ColorCycle options
[DataRow(new string[] { "--colorcycle=Invalid" }, DisplayName = "Cycle, Bad name")]
[DataRow(new string[] { "--colorcycle=9999" }, DisplayName = "Cycle, Too high")]
[DataRow(new string[] { "--colorcycle=Invalid" }, DisplayName = "--colorcycle=Invalid")]
[DataRow(new string[] { "--colorcycle=9999" }, DisplayName = "--colorcycle=9999")]
[DataRow(new string[] { "--colorcycle=--colorcycle" }, DisplayName = "--colorcycle=--colorcycle")]
// Brightness options
[DataRow(new string[] { "--color=Red", "--brightness" }, DisplayName = "Brightness, No value")]
[DataRow(new string[] { "--color=Red", "--brightness=Invalid" }, DisplayName = "Brightness, Bad name")]
[DataRow(new string[] { "--color=Red", "--brightness=101" }, DisplayName = "Brightness, Too high")]
[DataRow(new string[] { "--color=Red", "--brightness" }, DisplayName = "--color=Red --brightness")]
[DataRow(new string[] { "--color=Red", "--brightness=Invalid" }, DisplayName = "--color=Red --brightness=Invalid")]
[DataRow(new string[] { "--color=Red", "--brightness=101" }, DisplayName = "--color=Red --brightness=101")]
// Extra parameters
[DataRow(new string[] { "1" }, DisplayName = "1")]
[DataRow(new string[] { "0" }, DisplayName = "0")]
[DataRow(new string[] { "--badopt" }, DisplayName = "--badopt")]
[DataRow(new string[] { "badopt" }, DisplayName = "badopt")]
[DataRow(new string[] { "--", "badopt" }, DisplayName = "-- badopt")]
// Ambiguious options
[DataRow(new string[] { "--colorcycle", "4.0" }, DisplayName = "--colorcycle 4.0")] // Optional values expect explicit "="
[DataRow(new string[] { "--colorcycle 4.0" }, DisplayName = "--colorcycle 4.0 (OneWord)")] // Optional values expect explicit "="
[DataTestMethod]
public void BadOptions(string[] args)
{
Expand Down Expand Up @@ -131,6 +142,40 @@ public void Color(string[] args)
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[DataRow(new string[] { "-v", "--color=DodgerBlue" }, DisplayName = "-v --color=DodgerBlue")]
[DataRow(new string[] { "-vv", "--color=DodgerBlue" }, DisplayName = "-vv --color=DodgerBlue")]
[DataRow(new string[] { "--verbose", "--color=DodgerBlue" }, DisplayName = "--verbose --color=DodgerBlue")]
[DataRow(new string[] { "--color=DodgerBlue", "--verbose" }, DisplayName = "--color=DodgerBlue --verbose")]
[DataTestMethod]
public void Color_verbose_name(string[] args)
{
rgbFusionTool.Main(args);

StringAssert.DoesNotMatch(stderr.ToString(), ANY, "Expect stderr is empty");
StringAssert.Matches(stdout.ToString(), new Regex("color\\b.*dodgerblue", RegexOptions.IgnoreCase), "Expect stdout includes color name");

TestHelper.AssertAllLeds(mock,
GLedApiDotNetTests.Tests.LedSettingTests.SettingByteArrays.StaticDodgerBlue,
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[DataRow(new string[] { "-v", "--color=1E90FF" }, DisplayName = "-v --color=1E90FF")]
[DataRow(new string[] { "-vv", "--color=1E90FF" }, DisplayName = "-vv --color=1E90FF")]
[DataRow(new string[] { "--verbose", "--color=1E90FF" }, DisplayName = "--verbose --color=1E90FF")]
[DataRow(new string[] { "--color=1E90FF", "--verbose" }, DisplayName = "--color=1E90FF --verbose")]
[DataTestMethod]
public void Color_verbose_rgb(string[] args)
{
rgbFusionTool.Main(args);

StringAssert.DoesNotMatch(stderr.ToString(), ANY, "Expect stderr is empty");
StringAssert.Matches(stdout.ToString(), new Regex("color\\b.*\\b30\\b.*\\b144\\b.*\\b255\\b", RegexOptions.IgnoreCase), "Expect stdout includes color values");

TestHelper.AssertAllLeds(mock,
GLedApiDotNetTests.Tests.LedSettingTests.SettingByteArrays.StaticDodgerBlue,
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[DataRow(new string[] { "--color=Red", "-b50" }, DisplayName = "-b50")]
[DataRow(new string[] { "--color=Red", "-b", "50" }, DisplayName = "-b 50")]
[DataRow(new string[] { "--color=Red", "-b 50" }, DisplayName = "-b 50 (OneWord)")]
Expand All @@ -149,6 +194,23 @@ public void Brightness(string[] args)
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[DataRow(new string[] { "-v", "--color=Red", "--brightness=50" }, DisplayName = "-v --color=Red --brightness=50")]
[DataRow(new string[] { "-vv", "--color=Red", "--brightness=50" }, DisplayName = "-vv --color=Red --brightness=50")]
[DataRow(new string[] { "--verbose", "--color=Red", "--brightness=50" }, DisplayName = "--verbose --color=Red --brightness=50")]
[DataRow(new string[] { "--color=Red", "--brightness=50", "--verbose" }, DisplayName = "--color=Red --brightness=50 --verbose")]
[DataTestMethod]
public void Brightness_verbose(string[] args)
{
rgbFusionTool.Main(args);

StringAssert.DoesNotMatch(stderr.ToString(), ANY, "Expect stderr is empty");
StringAssert.Matches(stdout.ToString(), new Regex("brightness\\b.*\\b50\\b", RegexOptions.IgnoreCase), "Expect stdout includes brightness value");

TestHelper.AssertAllLeds(mock,
GLedApiDotNetTests.Tests.LedSettingTests.SettingByteArrays.StaticRed50,
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[DataRow(new string[] { "--colorcycle" }, DisplayName = "--colorcycle")]
[DataRow(new string[] { "--cycle" }, DisplayName = "--cycle")]
[DataRow(new string[] { "--colorcycle=1" }, DisplayName = "--colorcycle=1")]
Expand All @@ -168,6 +230,23 @@ public void ColorCycle_1s(string[] args)
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[DataRow(new string[] { "-v", "--colorcycle" }, DisplayName = "-v --colorcycle")]
[DataRow(new string[] { "-vv", "--colorcycle" }, DisplayName = "-vv --colorcycle")]
[DataRow(new string[] { "--verbose", "--colorcycle" }, DisplayName = "--verbose --colorcycle")]
[DataRow(new string[] { "--colorcycle", "--verbose" }, DisplayName = "--colorcycle --verbose")]
[DataTestMethod]
public void ColorCycle_verbose(string[] args)
{
rgbFusionTool.Main(args);

StringAssert.DoesNotMatch(stderr.ToString(), ANY, "Expect stderr is empty");
StringAssert.Matches(stdout.ToString(), new Regex("(color\\w*)?cycle\\b.*\\b1(\\.0*)?\\b\\ss", RegexOptions.IgnoreCase), "Expect stdout includes mode and time");

TestHelper.AssertAllLeds(mock,
GLedApiDotNetTests.Tests.LedSettingTests.SettingByteArrays.ColorCycleA_1s,
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[DataRow(new string[] { "--colorcycle=4" }, DisplayName = "--colorcycle=4")]
[DataRow(new string[] { "--cycle=4" }, DisplayName = "--cycle=4")]
[DataRow(new string[] { "--colorcycle=4.0" }, DisplayName = "--colorcycle=4.0")]
Expand Down Expand Up @@ -201,5 +280,18 @@ public void ColorCycle_500ms(string[] args)
GLedApiDotNetTests.Tests.LedSettingTests.SettingByteArrays.ColorCycleA_500ms,
GLedApiv1_0_0Mock.DEFAULT_MAXDIVISIONS);
}

[DataRow(new string[] { "--list" }, DisplayName = "--list")]
[DataRow(new string[] { "-l" }, DisplayName = "-l")]
[DataTestMethod]
public void List(string[] args)
{
rgbFusionTool.Main(args);

StringAssert.DoesNotMatch(stderr.ToString(), ANY, "Expect stderr is empty");
StringAssert.Matches(stdout.ToString(), new Regex("zone \\d+", RegexOptions.IgnoreCase), "Expect stdout lists zones");

Assert.IsTrue(mock.IsInitialized, "Expect initialized");
}
}
}

0 comments on commit 351006a

Please sign in to comment.