mirror of https://github.com/Squidex/squidex.git
Browse Source
# Conflicts: # backend/src/Squidex.Data.EntityFramework/Squidex.Data.EntityFramework.csproj # backend/src/Squidex.Data.MongoDb/Squidex.Data.MongoDb.csproj # backend/src/Squidex.Domain.Apps.Core.Model/Squidex.Domain.Apps.Core.Model.csproj # backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/Internal/JintExtensions.cs # backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/JintScriptEngine.cs # backend/src/Squidex.Domain.Apps.Core.Operations/Squidex.Domain.Apps.Core.Operations.csproj # backend/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj # backend/src/Squidex/Squidex.csproj # backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/Scripting/JintScriptEngineTests.cspull/1330/head
19 changed files with 351 additions and 32 deletions
@ -0,0 +1,52 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using Jint; |
||||
|
using Jint.Native.Object; |
||||
|
using Squidex.Domain.Apps.Core.Scripting.Internal; |
||||
|
using Squidex.Infrastructure.Json.Objects; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Core.Operations.Scripting; |
||||
|
|
||||
|
public class JsonMapperTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_map_json_objects_into_a_shared_shape() |
||||
|
{ |
||||
|
var engine = new Engine(o => o.Strict()); |
||||
|
|
||||
|
var mapped = (ObjectInstance)JsonMapper.Map(CreateJson(), engine); |
||||
|
var nested = (ObjectInstance)mapped.Get("nested"); |
||||
|
|
||||
|
// Sharing the layout is what makes reading properties of many content items fast. It only affects
|
||||
|
// performance and never behavior, which is why it is asserted here: building these objects with a
|
||||
|
// custom ObjectInstance class again would silently undo it and no other test would notice.
|
||||
|
Assert.True(engine.Advanced.HasSharedShape(mapped)); |
||||
|
Assert.True(engine.Advanced.HasSharedShape(nested)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_share_the_shape_between_objects_of_the_same_shape() |
||||
|
{ |
||||
|
var engine = new Engine(o => o.Strict()); |
||||
|
|
||||
|
var first = (ObjectInstance)JsonMapper.Map(CreateJson(), engine); |
||||
|
var second = (ObjectInstance)JsonMapper.Map(CreateJson(), engine); |
||||
|
|
||||
|
Assert.True(engine.Advanced.HasSharedShape(first)); |
||||
|
Assert.True(engine.Advanced.HasSharedShape(second)); |
||||
|
} |
||||
|
|
||||
|
private static JsonValue CreateJson() |
||||
|
{ |
||||
|
return JsonValue.Create( |
||||
|
new JsonObject() |
||||
|
.Add("name", JsonValue.Create("squidex")) |
||||
|
.Add("count", JsonValue.Create(3)) |
||||
|
.Add("nested", JsonValue.Create(new JsonObject().Add("flag", JsonValue.True)))); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,33 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Core.TestHelpers; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Turns on Jint's self checks for this test assembly.
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// Our ContentWrapper classes and the object converter implement Jint extension points where Jint relies on
|
||||
|
/// our answers being consistent, without checking them - checking would cost as much as the shortcut saves.
|
||||
|
/// A mistake there is silent in production: a key can disappear from Object.keys, or a converted type can be
|
||||
|
/// skipped. With this switch on Jint verifies the answers and throws on the first mismatch, so a mistake
|
||||
|
/// fails a test instead.
|
||||
|
/// <para>
|
||||
|
/// The switch has to be set before the first Jint type is used, which is what the module initializer
|
||||
|
/// guarantees. It stays off in production, where the checks would only cost performance.
|
||||
|
/// </para>
|
||||
|
/// </remarks>
|
||||
|
internal static class JintHostContractVerification |
||||
|
{ |
||||
|
[ModuleInitializer] |
||||
|
internal static void Enable() |
||||
|
{ |
||||
|
AppContext.SetSwitch("Jint.EnableHostContractVerification", true); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
|
Before Width: | Height: | Size: 120 KiB After Width: | Height: | Size: 121 KiB |
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 617 B |
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue