Skip to content

Commit

Permalink
add new unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielklecha committed Jan 22, 2024
1 parent 4daa14e commit e4922c2
Show file tree
Hide file tree
Showing 15 changed files with 1,797 additions and 800 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-build
- name: Pack
run: dotnet pack SharpIpp/SharpIpp.csproj --configuration Release --no-build
- name: Publish
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/test-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test commit

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

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-build
6 changes: 0 additions & 6 deletions SharpIpp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ VisualStudioVersion = 17.6.33723.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpIpp", "SharpIpp\SharpIpp.csproj", "{4718D86A-C465-43C7-8A36-959F53C8E6DB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpIpp.Tests", "SharpIpp.Tests\SharpIpp.Tests.csproj", "{04CB48CC-C2D2-4861-888A-0F5A65EA5851}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpIppServerExample", "SharpIppServerExample\SharpIppServerExample.csproj", "{DF4FFED2-D990-4D16-BE15-5DF5B244F9FF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpIppClientExample", "SharpIppClientExample\SharpIppClientExample.csproj", "{7ED644D7-06E3-42DE-9673-6A7DE3E6B7BA}"
Expand All @@ -25,10 +23,6 @@ Global
{4718D86A-C465-43C7-8A36-959F53C8E6DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4718D86A-C465-43C7-8A36-959F53C8E6DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4718D86A-C465-43C7-8A36-959F53C8E6DB}.Release|Any CPU.Build.0 = Release|Any CPU
{04CB48CC-C2D2-4861-888A-0F5A65EA5851}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{04CB48CC-C2D2-4861-888A-0F5A65EA5851}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04CB48CC-C2D2-4861-888A-0F5A65EA5851}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04CB48CC-C2D2-4861-888A-0F5A65EA5851}.Release|Any CPU.Build.0 = Release|Any CPU
{DF4FFED2-D990-4D16-BE15-5DF5B244F9FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF4FFED2-D990-4D16-BE15-5DF5B244F9FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF4FFED2-D990-4D16-BE15-5DF5B244F9FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
2 changes: 2 additions & 0 deletions SharpIpp/Mapping/Profiles/TypesProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public void CreateMaps(IMapperConstructor mapper)
mapper.CreateIppMap<int, ResolutionUnit>((src, map) => (ResolutionUnit)src);
mapper.CreateIppMap<int, PrinterType>((src, map) => (PrinterType)src);
mapper.CreateIppMap<string, IppVersion>( ( src, map ) => new IppVersion( src ) );
mapper.CreateIppMap<NoValue, int>( ( src, map ) => 0 );
mapper.CreateIppMap<NoValue, JobState>( ( src, jobs) => 0);

//All name parameters can come as StringWithLanguage or string
//mappers for string\language mapping
Expand Down
2 changes: 1 addition & 1 deletion SharpIpp/Mapping/SimpleMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private TDest Map<TDest>(object source, Type sourceType, TDest dest)
{
return (TDest)source;
}

foreach (var (map, type) in PossiblePairs(sourceType, destType))
{
switch (type)
Expand Down
2 changes: 0 additions & 2 deletions SharpIpp/Protocol/IppProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
using SharpIpp.Protocol.Extensions;
using SharpIpp.Protocol.Models;

[assembly: InternalsVisibleTo("SharpIpp.Tests")]

namespace SharpIpp.Protocol
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion SharpIpp/SharpIpp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<RepositoryType>git</RepositoryType>
<Product>SharpIppNext</Product>
<PackageId>SharpIppNext</PackageId>
<Version>1.0.3.0</Version>
<Version>1.0.4.0</Version>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
2 changes: 1 addition & 1 deletion SharpIpp/SharpIppClient.Cups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SharpIpp
public partial class SharpIppClient
{
/// <inheritdoc />
public Task<CUPSGetPrintersResponse> GetCUPSPrintersAsync(CUPSGetPrintersRequest request, CancellationToken cancellationToken) =>
public Task<CUPSGetPrintersResponse> GetCUPSPrintersAsync(CUPSGetPrintersRequest request, CancellationToken cancellationToken = default) =>
SendAsync(request, ConstructIppRequest, Construct<CUPSGetPrintersResponse>, cancellationToken);
}
}
38 changes: 17 additions & 21 deletions SharpIpp/SharpIppClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
Expand All @@ -22,24 +23,29 @@ public partial class SharpIppClient : ISharpIppClient

private readonly bool _disposeHttpClient;
private readonly HttpClient _httpClient;
private readonly IIppProtocol _ippProtocol = new IppProtocol();
private readonly IIppProtocol _ippProtocol;

static SharpIppClient()
{
MapperSingleton = new Lazy<IMapper>(MapperFactory);
}

public SharpIppClient() : this(new HttpClient(), true)
public SharpIppClient() : this(new HttpClient(), new IppProtocol(), true)
{
}

public SharpIppClient(HttpClient httpClient) : this(httpClient, false)
public SharpIppClient(HttpClient httpClient) : this(httpClient, new IppProtocol(), false )
{
}

internal SharpIppClient(HttpClient httpClient, bool disposeHttpClient)
public SharpIppClient(HttpClient httpClient, IIppProtocol ippProtocol) : this( httpClient, ippProtocol, false )
{
}

internal SharpIppClient(HttpClient httpClient, IIppProtocol ippProtocol, bool disposeHttpClient)
{
_httpClient = httpClient;
_ippProtocol = ippProtocol;
_disposeHttpClient = disposeHttpClient;
}

Expand All @@ -50,13 +56,12 @@ internal SharpIppClient(HttpClient httpClient, bool disposeHttpClient)
/// but response still contains valid ipp-data in the body that can be parsed for better error description
/// Seems like they are printer specific
/// </summary>
public HttpStatusCode[] PlausibleHttpStatusCodes { get; set; } =
{
private static readonly HttpStatusCode[] _plausibleHttpStatusCodes = [
HttpStatusCode.Continue,
HttpStatusCode.Unauthorized,
HttpStatusCode.Forbidden,
HttpStatusCode.UpgradeRequired,
};
];

/// <inheritdoc />
public async Task<IIppResponseMessage> SendAsync(
Expand Down Expand Up @@ -84,10 +89,7 @@ public async Task<IIppResponseMessage> SendAsync(
}
catch (HttpRequestException ex)
{
var plausibleHttpStatusCodes = PlausibleHttpStatusCodes;
var isPlausibleHttpStatusCode = Array.IndexOf(plausibleHttpStatusCodes, response.StatusCode) >= 0;

if (!isPlausibleHttpStatusCode)
if (!_plausibleHttpStatusCodes.Contains(response.StatusCode))
{
throw;
}
Expand All @@ -101,11 +103,10 @@ public async Task<IIppResponseMessage> SendAsync(
{
using var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
ippResponse = await _ippProtocol.ReadIppResponseAsync(responseStream, cancellationToken).ConfigureAwait(false);

if (ippResponse == null)
throw new Exception( "Unable to parse response" );
if (!ippResponse.IsSuccessfulStatusCode())
{
throw new IppResponseException($"Printer returned error code\n{ippResponse}", ippResponse);
}
throw new IppResponseException($"Printer returned error code", ippResponse);
}
catch
{
Expand All @@ -122,12 +123,7 @@ public async Task<IIppResponseMessage> SendAsync(
return ippResponse;
}

if (ippResponse != null)
{
throw new IppResponseException(httpException.Message, httpException, ippResponse);
}

throw httpException;
throw new IppResponseException(httpException.Message, httpException, ippResponse);
}

public void Dispose()
Expand Down
7 changes: 5 additions & 2 deletions SharpIppServerExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
$"/{printerOptions.Name}",
"/ipp/printer",
$"/ipp/printer/{printerOptions.Name}"
}.ForEach( path => app.MapPost( path, async (HttpContext context, PrinterJobsService printerService) =>
await printerService.ProcessRequestAsync(context.Request.Body, context.Response.Body)));
}.ForEach( path => app.MapPost( path, async ( HttpContext context, PrinterJobsService printerService ) =>
{
context.Response.ContentType = "application/ipp";
await printerService.ProcessRequestAsync( context.Request.Body, context.Response.Body );
} ) );
app.Run();
25 changes: 25 additions & 0 deletions SharpIppTests/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using FluentAssertions.Execution;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpIppTests.Extensions;

[ExcludeFromCodeCoverage]
public static class ObjectExtensions
{
public static bool VerifyAssertionScope<T>( this T value, Action<T> assertion )
{
using var assertionScope = new AssertionScope();
assertion.Invoke( value );
var failures = assertionScope.Discard();
if(!failures.Any())
return true;
foreach(var failure in failures )
System.Diagnostics.Trace.WriteLine( failure );
return false;
}
}
Loading

0 comments on commit e4922c2

Please sign in to comment.