forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonPatchDocumentGetPathTest.cs
121 lines (95 loc) · 2.78 KB
/
JsonPatchDocumentGetPathTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch;
public class JsonPatchDocumentGetPathTest
{
[Fact]
public void ExpressionType_MemberAccess()
{
// Arrange
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
// Act
var path = patchDocument.GetPath(p => p.SimpleObject.IntegerList, "-");
// Assert
Assert.Equal("/SimpleObject/IntegerList/-", path);
}
[Fact]
public void ExpressionType_ArrayIndex()
{
// Arrange
var patchDocument = new JsonPatchDocument<int[]>();
// Act
var path = patchDocument.GetPath(p => p[3], null);
// Assert
Assert.Equal("/3", path);
}
[Fact]
public void ExpressionType_Call()
{
// Arrange
var patchDocument = new JsonPatchDocument<Dictionary<string, int>>();
// Act
var path = patchDocument.GetPath(p => p["key"], "3");
// Assert
Assert.Equal("/key/3", path);
}
[Fact]
public void ExpressionType_Parameter_NullPosition()
{
// Arrange
var patchDocument = new JsonPatchDocument<SimpleObject>();
// Act
var path = patchDocument.GetPath(p => p, null);
// Assert
Assert.Equal("/", path);
}
[Fact]
public void ExpressionType_Parameter_WithPosition()
{
// Arrange
var patchDocument = new JsonPatchDocument<SimpleObject>();
// Act
var path = patchDocument.GetPath(p => p, "-");
// Assert
Assert.Equal("/-", path);
}
[Fact]
public void ExpressionType_Convert()
{
// Arrange
var patchDocument = new JsonPatchDocument<NestedObjectWithDerivedClass>();
// Act
var path = patchDocument.GetPath(p => (BaseClass)p.DerivedObject, null);
// Assert
Assert.Equal("/DerivedObject", path);
}
[Fact]
public void ExpressionType_NotSupported()
{
// Arrange
var patchDocument = new JsonPatchDocument<SimpleObject>();
// Act
var exception = Assert.Throws<InvalidOperationException>(() =>
{
patchDocument.GetPath(p => p.IntegerValue >= 4, null);
});
// Assert
Assert.Equal("The expression '(p.IntegerValue >= 4)' is not supported. Supported expressions include member access and indexer expressions.", exception.Message);
}
}
internal class DerivedClass : BaseClass
{
public DerivedClass()
{
}
}
internal class NestedObjectWithDerivedClass
{
public DerivedClass DerivedObject { get; set; }
}
internal class BaseClass
{
}