Skip to content
This repository has been archived by the owner on Aug 23, 2021. It is now read-only.

Commit

Permalink
Merge branch 'release/v1.1.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
karronoli committed Oct 20, 2020
2 parents 6736ea3 + dc08f82 commit de16489
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 6 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/codacy-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This workflow checks out code, performs a Codacy security scan
# and integrates the results with the
# GitHub Advanced Security code scanning feature. For more information on
# the Codacy security scan action usage and parameters, see
# https://github.com/codacy/codacy-analysis-cli-action.
# For more information on Codacy Analysis CLI in general, see
# https://github.com/codacy/codacy-analysis-cli.

name: Codacy Security Scan

on:
push:
branches: [ "master", "main" ]
pull_request:
branches: [ "master", "main" ]

jobs:
codacy-security-scan:
name: Codacy Security Scan
runs-on: ubuntu-latest
steps:
# Checkout the repository to the GitHub Actions runner
- name: Checkout code
uses: actions/checkout@v2

# Execute Codacy Analysis CLI and generate a SARIF output with the security issues identified during the analysis
- name: Run Codacy Analysis CLI
uses: codacy/codacy-analysis-cli-action@1.1.0
with:
# Check https://github.com/codacy/codacy-analysis-cli#project-token to get your project token from your Codacy repository
# You can also omit the token and run the tools that support default configurations
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
verbose: true
output: results.sarif
format: sarif
# Adjust severity of non-security issues
gh-code-scanning-compat: true
# Force 0 exit code to allow SARIF file generation
# This will handover control about PR rejection to the GitHub side
max-allowed-issues: 2147483647

# Upload the SARIF file generated in the previous step
- name: Upload SARIF results file
uses: github/codeql-action/upload-sarif@v1
with:
sarif_file: results.sarif

17 changes: 14 additions & 3 deletions TinySato/Barcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ public void AddCODE128(int narrow_bar_width, int barcode_height, string print_da
this.printer.Add(string.Format("BG{0:D2}{1:D3}{2}", narrow_bar_width, barcode_height, print_data));
}

public void AddCODE128(int narrow_bar_width, int barcode_height, string print_data, Action<Size> set_position_by_size)
public Size AddCODE128(int narrow_bar_width, int barcode_height, string print_data, Action<Size> set_position_by_size)
{
if (!(1 <= narrow_bar_width && narrow_bar_width <= 12))
throw new TinySatoArgumentException("Specify 1-12 dot for Narrow Bar Width.");
if (!(1 <= barcode_height && barcode_height <= 600))
throw new TinySatoArgumentException("Specify 1-600 dot for Barcode Height.");
var print_data_head = print_data.Substring(0, 2);
if (print_data_head == ">G" // Start Code A
|| print_data_head == ">H" // Start Code B
|| print_data_head == ">I") // Start Code C
throw new TinySatoArgumentException($"Remove Start Code from the beginning of print_data. head:{print_data_head}");

var m = (new Regex(@"(\d{6,})$")).Match(print_data);
var index = m.Success ?
Expand All @@ -50,23 +55,29 @@ public void AddCODE128(int narrow_bar_width, int barcode_height, string print_da
+ 11 * front.Length * narrow_bar_width // front
+ 11 * narrow_bar_width // check
+ 13 * narrow_bar_width; // stop

Size symbol_size;
if (back.Length > 0)
{
var width_set_c =
11 * narrow_bar_width // shift
+ 11 * back.Length / 2 * narrow_bar_width; // back
set_position_by_size(new Size(width + width_set_c, barcode_height));
symbol_size = new Size(width + width_set_c, barcode_height);
set_position_by_size(symbol_size);
this.printer.Add(string.Format("BG{0:D2}{1:D3}{2}",
narrow_bar_width, barcode_height,
">H" + front + ">C" + back));
}
else
{
set_position_by_size(new Size(width, barcode_height));
symbol_size = new Size(width, barcode_height);
set_position_by_size(symbol_size);
this.printer.Add(string.Format("BG{0:D2}{1:D3}{2}",
narrow_bar_width, barcode_height,
">H" + front));
}

return symbol_size;
}

public void AddJAN13(int thin_bar_width, int barcode_top, string print_data)
Expand Down
15 changes: 14 additions & 1 deletion TinySato/Printer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public Printer(IPEndPoint endpoint)
throw new TinySatoIOException($"Printer is busy. endpoint: {endpoint}, status: {this.status}");
}

public void Add(string operation)
internal void Add(string operation)
{
operations.Add(Encoding.ASCII.GetBytes(ESC + operation));
}
Expand All @@ -117,6 +117,19 @@ internal void Add(byte[] raw_operation)
operations.Add(raw_operation);
}

public void PushOperation(IEnumerable<byte> operation)
{
operations.Add(operation.ToArray());
}

public byte[] PopOperation()
{
var last = operations.Last();
operations.RemoveAt(operations.Count - 1);

return last;
}

protected void Insert(int index, string operation)
{
operations.Insert(
Expand Down
4 changes: 2 additions & 2 deletions TinySato/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
//
// すべての値を指定するか、下のように '*' を使ってビルドおよびリビジョン番号を
// 既定値にすることができます:
[assembly: AssemblyVersion("1.1.6.*")]
[assembly: AssemblyFileVersion("1.1.6")]
[assembly: AssemblyVersion("1.1.7.*")]
[assembly: AssemblyFileVersion("1.1.7")]
73 changes: 73 additions & 0 deletions UnitTestProject/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,5 +301,78 @@ public async Task ExampleGraphic()
CollectionAssert.AreEqual(expected, actual);
}
}

[TestMethod]
public async Task AddCODE128()
{
var task = ResponseForPrint();
int bar_width = 1, symbol_height = 2, X = 3, Y = 4;
// start B + data1 + shift C + data2 + check + stop
var print_data = "AaBbCc123456789";
var symbol_width = 11 + 11 * 7 + 11 + 11 * 8 / 2 + 11 + 13;
var operation = new byte[] { ESC }.Concat(Encoding.ASCII.GetBytes(
"BG" + $"{bar_width:D2}" + $"{symbol_height:D3}"
+ ">HAaBbCc1>C23456789"));

using (var printer = new Printer(printEP))
{
var size_codeBC = printer.Barcode.AddCODE128(bar_width, symbol_height, print_data,
(Size size) =>
{
Assert.AreEqual(symbol_width, size.Width);
Assert.AreEqual(symbol_height, size.Height);
printer.MoveToX(X);
printer.MoveToY(Y);
});
Assert.AreEqual(symbol_width, size_codeBC.Width);
Assert.AreEqual(symbol_height, size_codeBC.Height);

var dummy = Encoding.ASCII.GetBytes("dummy");
printer.PushOperation(dummy);
CollectionAssert.AreEqual(dummy, printer.PopOperation());

// start B + data + check + stop
var symbol_width_codeB = 11 + 11 * 8 + 11 + 13;
var size_codeB = printer.Barcode.AddCODE128(bar_width, symbol_height,
"ABC12345",
(Size size) =>
{
Assert.AreEqual(symbol_width_codeB, size.Width);
Assert.AreEqual(symbol_height, size.Height);
});
Assert.AreEqual(symbol_width_codeB, size_codeB.Width);
Assert.AreEqual(symbol_height, size_codeB.Height);
printer.PopOperation();

try
{
// Explicitly passing the start code throws an exception.
_ = printer.Barcode.AddCODE128(1, 1, ">G", (Size _) => { });
Assert.Fail();
}
catch (Exception e)
{
Assert.IsInstanceOfType(e, typeof(TinySatoArgumentException));
}

printer.Send();
}

var expected = (new byte[] { ENQ, STX }).Concat(new string[]
{
"A",
$"H{X:D4}",
$"V{Y:D4}",
}.SelectMany(x => (new byte[] { ESC }).Concat(Encoding.ASCII.GetBytes(x))))
.Concat(operation)
.Concat(new byte[] { ESC, ASCII_Z, ETX });

using (task)
{
var actual = await task;
CollectionAssert.AreEqual(expected.ToArray(), actual);
}
}
}
}

0 comments on commit de16489

Please sign in to comment.