Skip to content

Commit

Permalink
sharping (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
elringus authored Feb 12, 2024
1 parent 85f7a2b commit 3af3d80
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 94 deletions.
1 change: 0 additions & 1 deletion backend/Naninovel.Common.Test/Bindings/InjectionTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Bootsharp;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Naninovel.Bindings.Test;
using Naninovel.Observing.Test;

[assembly: JSExport([typeof(IServiceOneA)])]
Expand Down
65 changes: 65 additions & 0 deletions backend/Naninovel.Common.Test/Bindings/Mocks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
global using static Naninovel.Bindings.Test.Mocks;
using Naninovel.Observing.Test;

namespace Naninovel.Bindings.Test;

public static class Mocks
{
public interface IHandlerSpecifier;
public interface IHandlerOne<T> where T : IHandlerSpecifier;
public interface IHandlerTwo<T> where T : IHandlerSpecifier;
public interface IServiceOneA;
public interface IServiceOneB;
public interface IServiceOneC;
public interface IServiceTwoA;
public interface IServiceTwoB;
public interface IServiceTwoC;
public class HandlerSpecifierA : IHandlerSpecifier;
public class HandlerSpecifierB : IHandlerSpecifier;
public class ServiceOneA : IServiceOneA, IHandlerOne<HandlerSpecifierA>;
public class ServiceOneB : IServiceOneB, IHandlerOne<HandlerSpecifierB>;
public class ServiceOneC : IServiceOneC, IHandlerOne<HandlerSpecifierA>, IHandlerOne<HandlerSpecifierB>;
public class ServiceTwoA : IServiceTwoA, IHandlerTwo<HandlerSpecifierA>;
public class ServiceTwoB : IServiceTwoB, IHandlerTwo<HandlerSpecifierB>;
public class ServiceTwoC : IServiceTwoC, IHandlerTwo<HandlerSpecifierA>, IHandlerTwo<HandlerSpecifierB>;

public interface IRegistrar
{
void Register<T> (IHandlerOne<T> handlerOne) where T : IHandlerSpecifier;
void Register<T> (IHandlerTwo<T> handlerOne) where T : IHandlerSpecifier;
}

public interface IMockGenericObserver<T>;

public interface IMockNotifier
{
void Notify ();
Task NotifyAsync ();
}

public interface IMockObserverService
{
bool HandleInvoked { get; }
bool HandleAsyncInvoked { get; }
}

public class MockObserver : IMockObserverService, IMockObserver
{
public bool HandleInvoked { get; private set; }
public bool HandleAsyncInvoked { get; private set; }

public void Handle () => HandleInvoked = true;

public Task HandleAsync ()
{
HandleAsyncInvoked = true;
return Task.CompletedTask;
}
}

public class MockNotifier (IObserverNotifier<IMockObserver> notifier) : IMockNotifier
{
public void Notify () => notifier.Notify(o => o.Handle());
public Task NotifyAsync () => notifier.NotifyAsync(o => o.HandleAsync());
}
}
51 changes: 0 additions & 51 deletions backend/Naninovel.Common.Test/Bindings/Mocks/MockObservers.cs

This file was deleted.

25 changes: 0 additions & 25 deletions backend/Naninovel.Common.Test/Bindings/Mocks/MockServices.cs

This file was deleted.

13 changes: 9 additions & 4 deletions backend/Naninovel.Common.Test/Bridging/Mocks/MockMessages.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
global using static Naninovel.Bridging.Test.MockMessages;

namespace Naninovel.Bridging.Test;

public class ClientMessage : IClientMessage;
public class ServerMessage : IServerMessage;
public class ServerMessageA : IServerMessage;
public class ServerMessageB : IServerMessage;
public static class MockMessages
{
public class ClientMessage : IClientMessage;
public class ServerMessage : IServerMessage;
public class ServerMessageA : IServerMessage;
public class ServerMessageB : IServerMessage;
}
12 changes: 6 additions & 6 deletions backend/Naninovel.Common.Test/SerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

namespace Naninovel.Bridging.Test;

public class SerializerTest
public partial class SerializerTest
{
[ExcludeFromCodeCoverage] public record Record(string Value);
[ExcludeFromCodeCoverage] public record Message(Record[] Records);
[JsonSerializable(typeof(Message))]
internal partial class AdditionalContext : JsonSerializerContext;

[ExcludeFromCodeCoverage] public record Record (string Value);
[ExcludeFromCodeCoverage] public record Message (Record[] Records);

[Fact]
public void CanSerializeCommonTypes ()
Expand Down Expand Up @@ -86,6 +89,3 @@ public void CanDeserializeNull ()
Assert.Null(serializer.DeserializeOrNull<Record>(" "));
}
}

[JsonSerializable(typeof(SerializerTest.Message))]
internal partial class AdditionalContext : JsonSerializerContext;
12 changes: 8 additions & 4 deletions backend/Naninovel.Common/Metadata/MetadataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ public void Update (Project meta)
/// <param name="paramAliasOrId">Alias or ID of the parameter; use empty or null for nameless.</param>
public Parameter? FindParameter (string commandAliasOrId, string paramAliasOrId)
{
return FindCommand(commandAliasOrId)?.Parameters
.FirstOrDefault(p => p.Nameless && string.IsNullOrEmpty(paramAliasOrId) ||
!string.IsNullOrEmpty(p.Alias) && string.Equals(p.Alias, paramAliasOrId, StringComparison.OrdinalIgnoreCase) ||
string.Equals(p.Id, paramAliasOrId, StringComparison.OrdinalIgnoreCase));
return FindCommand(commandAliasOrId)?.Parameters.FirstOrDefault(IsMatch);

bool IsMatch (Parameter p)
{
if (p.Nameless && string.IsNullOrEmpty(paramAliasOrId)) return true;
if (!string.IsNullOrEmpty(p.Alias) && string.Equals(p.Alias, paramAliasOrId, StringComparison.OrdinalIgnoreCase)) return true;
return string.Equals(p.Id, paramAliasOrId, StringComparison.OrdinalIgnoreCase);
}
}

private void Reset ()
Expand Down
3 changes: 2 additions & 1 deletion backend/Naninovel.Common/Parsing/Lexers/CommandBodyLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static bool IsInlinedOpening (LexState state)

public static bool IsEndReached (LexState state, bool inlined)
{
return state.EndReached || inlined && state.IsUnescaped(InlinedClose[0]);
if (inlined && state.IsUnescaped(InlinedClose[0])) return true;
return state.EndReached;
}

public void AddCommandBody (LexState state, bool inlined)
Expand Down
3 changes: 2 additions & 1 deletion backend/Naninovel.Common/Parsing/Parsers/MixedValueParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ bool ShouldRemove (int i)
{
if (value[i] != '\\' || IsEscaped(value, i)) return false;
var prev = value[i + 1];
return unescapeQuotes && prev == '"' || IsPlainTextControlChar(prev, value.ElementAtOrDefault(i + 2));
if (unescapeQuotes && prev == '"') return true;
return IsPlainTextControlChar(prev, value.ElementAtOrDefault(i + 2));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ bool IsAnySpaceOrUnclosedQuotes ()
bool ShouldEscape (int i)
{
if (IsIgnored(ignoreRanges, i)) return false;
return IsPlainTextControlChar(value[i], value.ElementAtOrDefault(i + 1)) || wrap && value[i] == '"';
if (wrap && value[i] == '"') return true;
return IsPlainTextControlChar(value[i], value.ElementAtOrDefault(i + 1));
}

static bool IsIgnored (IEnumerable<(int start, int length)> ignoredRanges, int i)
Expand Down

0 comments on commit 3af3d80

Please sign in to comment.