Skip to content

Commit

Permalink
Merge pull request #64 from Blazored/63-regression-int-serialisation
Browse files Browse the repository at this point in the history
Fixes bug in deserialising logic
  • Loading branch information
chrissainty committed Mar 30, 2020
2 parents 8077549 + ca2931b commit f1d9d08
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 79 deletions.
34 changes: 0 additions & 34 deletions .vscode/launch.json

This file was deleted.

42 changes: 0 additions & 42 deletions .vscode/tasks.json

This file was deleted.

17 changes: 17 additions & 0 deletions Blazored.LocalStorage.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{B82A5126-EE01-4B4D-B642-F954B6E4E695}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazored.LocalStorage.Tests", "tests\Blazored.LocalStorage.Tests\Blazored.LocalStorage.Tests.csproj", "{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -63,13 +67,26 @@ Global
{767D657A-5410-4714-9477-99929D3FF709}.Release|x64.Build.0 = Release|Any CPU
{767D657A-5410-4714-9477-99929D3FF709}.Release|x86.ActiveCfg = Release|Any CPU
{767D657A-5410-4714-9477-99929D3FF709}.Release|x86.Build.0 = Release|Any CPU
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}.Debug|x64.ActiveCfg = Debug|Any CPU
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}.Debug|x64.Build.0 = Debug|Any CPU
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}.Debug|x86.ActiveCfg = Debug|Any CPU
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}.Debug|x86.Build.0 = Debug|Any CPU
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}.Release|Any CPU.Build.0 = Release|Any CPU
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}.Release|x64.ActiveCfg = Release|Any CPU
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}.Release|x64.Build.0 = Release|Any CPU
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}.Release|x86.ActiveCfg = Release|Any CPU
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D6953CE0-6AD5-434C-99AA-7FF1E26724D4} = {F20B2AAB-FB71-4A1C-82FE-F1750B922BF6}
{767D657A-5410-4714-9477-99929D3FF709} = {F20B2AAB-FB71-4A1C-82FE-F1750B922BF6}
{5A6A013E-325D-4A5F-B2FA-659B4FD2BDBC} = {B82A5126-EE01-4B4D-B642-F954B6E4E695}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2B91883F-A85A-49EB-A9C1-ABD567A9375E}
Expand Down
3 changes: 3 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ steps:
- script: dotnet build --configuration $(buildConfiguration) src/Blazored.LocalStorage/Blazored.LocalStorage.csproj
displayName: 'Building $(buildConfiguration)...'

- script: dotnet test ./tests/Blazored.LocalStorage.Tests/Blazored.LocalStorage.Tests.csproj --configuration $(buildConfiguration)
displayName: 'Testing $(buildConfiguration)...'

- task: DotNetCoreCLI@2
displayName: 'Generating Nuget Package... '
inputs:
Expand Down
2 changes: 1 addition & 1 deletion samples/BlazorServerSide/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddServerSideBlazor(config => config.DetailedErrors = true);
services.AddBlazoredLocalStorage();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Blazored.LocalStorage/Blazored.LocalStorage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<RootNamespace>Blazored.LocalStorage</RootNamespace>

<PackageId>Blazored.LocalStorage</PackageId>
<Version>2.1.3</Version>
<Version>2.1.4</Version>
<Authors>Chris Sainty</Authors>
<Description>A library to provide access to local storage in Blazor applications</Description>
<Copyright>Copyright 2018 (c) Chris Sainty. All rights reserved.</Copyright>
Expand Down
3 changes: 2 additions & 1 deletion src/Blazored.LocalStorage/LocalStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public async Task<T> GetItemAsync<T>(string key)
return default(T);

if (serialisedData.StartsWith("{") && serialisedData.EndsWith("}")
|| serialisedData.StartsWith("\"") && serialisedData.EndsWith("\""))
|| serialisedData.StartsWith("\"") && serialisedData.EndsWith("\"")
|| typeof(T) != typeof(string))
{
return JsonSerializer.Deserialize<T>(serialisedData, _jsonOptions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="System.Text.Json" Version="4.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Blazored.LocalStorage\Blazored.LocalStorage.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using Blazored.LocalStorage.JsonConverters;
using Blazored.LocalStorage.Tests.Mocks;
using FluentAssertions;
using Moq;
using System.Text.Json;
using System.Threading.Tasks;
using Xunit;

namespace Blazored.LocalStorage.Tests.LocalStorageServiceTests
{
public class GetItemAsync
{
private JsonSerializerOptions _jsonOptions;
private Mock<JSRuntimeWrapper> _mockJSRuntime;
private LocalStorageService _sut;

private static string _key = "testKey";

public GetItemAsync()
{
_mockJSRuntime = new Mock<JSRuntimeWrapper>();
_jsonOptions = new JsonSerializerOptions();
_jsonOptions.Converters.Add(new TimespanJsonConverter());
_sut = new LocalStorageService(_mockJSRuntime.Object);
}

[Theory]
[InlineData("stringTest")]
[InlineData(11)]
[InlineData(11.11)]
public async Task Should_DeserialiseToCorrectType<T>(T value)
{
// Arrange
var serialisedData = "";
if (typeof(T) == typeof(string))
serialisedData = value.ToString();
else
serialisedData = JsonSerializer.Serialize(value, _jsonOptions);

_mockJSRuntime.Setup(x => x.InvokeAsync<string>("localStorage.getItem", new[] { _key }))
.Returns(() => new ValueTask<string>(serialisedData));

// Act
var result = await _sut.GetItemAsync<T>(_key);

// Assert
Assert.Equal(value, result);
_mockJSRuntime.Verify();
}

[Fact]
public async Task Should_DeserialiseValueToNullableInt()
{
// Arrange
int? value = 6;
var serialisedData = JsonSerializer.Serialize(value, _jsonOptions);

_mockJSRuntime.Setup(x => x.InvokeAsync<string>("localStorage.getItem", new[] { _key }))
.Returns(() => new ValueTask<string>(serialisedData));

// Act
var result = await _sut.GetItemAsync<int?>(_key);

// Assert
Assert.Equal(value, result);
}

[Fact]
public async Task Should_DeserialiseValueToDecimal()
{
// Arrange
decimal value = 6.00m;
var serialisedData = JsonSerializer.Serialize(value, _jsonOptions);

_mockJSRuntime.Setup(x => x.InvokeAsync<string>("localStorage.getItem", new[] { _key }))
.Returns(() => new ValueTask<string>(serialisedData));

// Act
var result = await _sut.GetItemAsync<decimal>(_key);

// Assert
Assert.Equal(value, result);
}

[Fact]
public async Task Should_DeserialiseValueToComplexType()
{
// Arrange
TestObject value = new TestObject { Id = 1, Name = "John Smith" };
var serialisedData = JsonSerializer.Serialize(value, _jsonOptions);

_mockJSRuntime.Setup(x => x.InvokeAsync<string>("localStorage.getItem", new[] { _key }))
.Returns(() => new ValueTask<string>(serialisedData));

// Act
var result = await _sut.GetItemAsync<TestObject>(_key);

// Assert
result.Should().BeEquivalentTo(value);
}
}

public class TestObject
{
public int Id { get; set; }
public string Name { get; set; }
}
}
23 changes: 23 additions & 0 deletions tests/Blazored.LocalStorage.Tests/Mocks/JSRuntimeWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.JSInterop;
using Microsoft.JSInterop.Infrastructure;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Blazored.LocalStorage.Tests.Mocks
{
public class JSRuntimeWrapper : IJSRuntime
{
public virtual ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args)
{
throw new NotImplementedException();
}

public virtual ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object[] args)
{
throw new NotImplementedException();
}
}
}

0 comments on commit f1d9d08

Please sign in to comment.