public void Encode_ValidContent_ShouldEncodeCorrectly(string content, string expectedResult) { // Act string result = Gs1Encoder.Encode(content, '/'); // Assert result.Should().Be(expectedResult); }
public void Encode_InvalidContent_ShouldThrowException(string content, string expectedExceptionMessage) { // Act Action action = () => Gs1Encoder.Encode(content, '/'); // Assert action.Should().Throw <ArgumentException>() .WithMessage($"*{expectedExceptionMessage}*") .Which.ParamName.Should().Be("content"); }
public static IBarcodeIntCS Encode(string content, bool includeChecksum = true, bool gs1ModeEnabled = false) { if (content == null) { throw new ArgumentNullException(nameof(content)); } if (gs1ModeEnabled) { content = Gs1Encoder.Encode(content, Constants.FNC1); } char[] contentChars = content.ToCharArray(); if (contentChars.Length <= 0 || contentChars.Length > 80) { throw new ArgumentException($"Content length should be between 1 and 80 but got {contentChars.Length}", nameof(content)); } BitList?idxList = GetCodeIndexList(contentChars); if (!idxList.HasValue) { throw new InvalidOperationException($"{content} could not be encoded"); } var result = new BitList(); var sum = 0; var i = 0; foreach (var idx in idxList.Value.GetBytes()) { if (i == 0) { sum = idx; } else { sum += i * idx; } result.AddBit(Constants.EncodingTable[idx]); i++; } sum = sum % 103; if (includeChecksum) { result.AddBit(Constants.EncodingTable[sum]); } result.AddBit(Constants.EncodingTable[Constants.StopSymbol]); return(new Base1DCodeIntCS(result, BarcodeType.Code128, content, sum, Constants.Margin)); }
internal static byte[] EncodeGs1(string content) => EncodeText(Gs1Encoder.Encode(content, (char)DataMatrixSpecialCodewords.FNC1), skipFnc1: true);