Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for Print method #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions ESCPOS_NET.UnitTest/GlobalTests/GeneralTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using ESCPOS_NET.Emitters;
using System;
using System.Linq;
using System.Reflection;
using System.Text;
using Xunit;

namespace ESCPOS_NET.UnitTest.GlobalTests
{
public class GeneralTests
{
private const string NO_NEW_LINE_TEXT = "Just a text";
private const string CHAR10_CHAR13_TEXT = "Another \r\ntext";
private const string CHAR13_TEXT = "Char\r13";
private const string CHAR10_TEXT = "Char\n10";
private const string MULTIPLE_CHAR10_CHAR13_LONG_TEXT = "ASKdjlaklsjdlkajsdklajsl kjdsalkjdslakj sdlkjalksdja skjdlkas\r\ndaskljaklsjdalksjdlakjsdlkj lkjas lkajdlkjalsjd \r\n ASLKJD ASJDLjlakjs KJASlkjsLKJS LJadslkjalkdjLAKJSDL Kj\r\n&^#@#(@*$&U SAJND AKSJ KJHSDK(#Y$(*U#@\r\n \\r\\n";
private const string LONG_TEXT_WITHOUT_CHAR10 = "ASKdjlaklsjdlkajsdklajsl kjdsalkjdslakj sdlkjalksdja skjdlkas\ndaskljaklsjdalksjdlakjsdlkj lkjas lkajdlkjalsjd \n ASLKJD ASJDLjlakjs KJASlkjsLKJS LJadslkjalkdjLAKJSDL Kj\n&^#@#(@*$&U SAJND AKSJ KJHSDK(#Y$(*U#@\n \\r\\n";

[Theory]
[InlineData("EPSON", NO_NEW_LINE_TEXT)]
[InlineData("EPSON", CHAR10_CHAR13_TEXT)]
[InlineData("EPSON", CHAR13_TEXT)]
[InlineData("EPSON", CHAR10_TEXT)]
[InlineData("EPSON", MULTIPLE_CHAR10_CHAR13_LONG_TEXT)]
[InlineData("EPSON", "")]

public void PrintChar13Removal_Success(string emitter, string text)
{
var type = Assembly.LoadFrom("ESCPOS_NET").GetType($"ESCPOS_NET.Emitters.{emitter}", true);
ICommandEmitter e = (ICommandEmitter)Activator.CreateInstance(type);

var bytes = e.Print(text);

Assert.True(!bytes.Any( b => b == 13), $"{nameof(e.Print)} didn't remove all \\r characters from the text.");
}

[Theory]
[InlineData("EPSON", NO_NEW_LINE_TEXT, NO_NEW_LINE_TEXT)]
[InlineData("EPSON", CHAR10_CHAR13_TEXT, "Another \ntext")]
[InlineData("EPSON", CHAR13_TEXT, "Char\n13")]
[InlineData("EPSON", CHAR10_TEXT, CHAR10_TEXT)]
[InlineData("EPSON", MULTIPLE_CHAR10_CHAR13_LONG_TEXT, LONG_TEXT_WITHOUT_CHAR10)]
[InlineData("EPSON", "", "")]

public void PrintChar13Replacement_Success(string emitter, string text, string expectedResult)
{
var type = Assembly.LoadFrom("ESCPOS_NET").GetType($"ESCPOS_NET.Emitters.{emitter}", true);
ICommandEmitter e = (ICommandEmitter)Activator.CreateInstance(type);

var bytes = e.Print(text);
Assert.Equal(expectedResult, Encoding.UTF8.GetString(bytes));
}

[Theory]
[InlineData("EPSON")]
public void PrintThrowIfTextIsNull(string emitter)
{
var type = Assembly.LoadFrom("ESCPOS_NET").GetType($"ESCPOS_NET.Emitters.{emitter}", true);
ICommandEmitter e = (ICommandEmitter)Activator.CreateInstance(type);

Assert.ThrowsAny<Exception>(() => e.Print(null));
}

}
}