@@ -15,76 +15,62 @@ namespace Microsoft.OpenApi.Tests.Workspaces
15
15
16
16
public class OpenApiReferencableTests
17
17
{
18
- public static IEnumerable < object [ ] > ReferencableElementResolvesEmptyJsonPointerToItselfTestData =>
19
- new List < object [ ] >
18
+ private static readonly OpenApiCallback _callbackFragment = new OpenApiCallback ( ) ;
19
+ private static readonly OpenApiExample _exampleFragment = new OpenApiExample ( ) ;
20
+ private static readonly OpenApiLink _linkFragment = new OpenApiLink ( ) ;
21
+ private static readonly OpenApiHeader _headerFragment = new OpenApiHeader ( ) ;
22
+ private static readonly OpenApiParameter _parameterFragment = new OpenApiParameter
20
23
{
21
- new object [ ] { new OpenApiCallback ( ) } ,
22
- new object [ ] { new OpenApiExample ( ) } ,
23
- new object [ ] { new OpenApiHeader ( ) } ,
24
- new object [ ] { new OpenApiLink ( ) } ,
25
- new object [ ] { new OpenApiParameter ( ) } ,
26
- new object [ ] { new OpenApiRequestBody ( ) } ,
27
- new object [ ] { new OpenApiResponse ( ) } ,
28
- new object [ ] { new OpenApiSchema ( ) } ,
29
- new object [ ] { new OpenApiSecurityScheme ( ) } ,
30
- new object [ ] { new OpenApiTag ( ) }
31
-
24
+ Schema = new OpenApiSchema ( ) ,
25
+ Examples = new Dictionary < string , OpenApiExample >
26
+ {
27
+ { "example1" , new OpenApiExample ( ) }
28
+ }
32
29
} ;
30
+ private static readonly OpenApiRequestBody _requestBodyFragment = new OpenApiRequestBody ( ) ;
31
+ private static readonly OpenApiResponse _responseFragment = new OpenApiResponse ( ) ;
32
+ private static readonly OpenApiSchema _schemaFragment = new OpenApiSchema ( ) ;
33
+ private static readonly OpenApiSecurityScheme _securitySchemeFragment = new OpenApiSecurityScheme ( ) ;
34
+ private static readonly OpenApiTag _tagFragment = new OpenApiTag ( ) ;
33
35
34
- [ Theory ]
35
- [ MemberData ( nameof ( ReferencableElementResolvesEmptyJsonPointerToItselfTestData ) ) ]
36
- public void ReferencableElementResolvesEmptyJsonPointerToItself ( IOpenApiReferenceable referencableElement )
37
- {
38
- // Arrange - above
39
-
40
- // Act
41
- var resolvedReference = referencableElement . ResolveReference ( string . Empty ) ;
42
-
43
- // Assert
44
- Assert . Same ( referencableElement , resolvedReference ) ;
45
- }
46
-
47
- [ Fact ]
48
- public void ParameterElementCanResolveReferenceToSchemaProperty ( )
36
+ public static IEnumerable < object [ ] > ReferencableElementsCanResolveReferencesTestData =>
37
+ new List < object [ ] >
49
38
{
50
- // Arrange
51
- var parameterElement = new OpenApiParameter
52
- {
53
- Schema = new OpenApiSchema ( )
54
- } ;
55
-
56
- // Act
57
- var resolvedReference = parameterElement . ResolveReference ( "schema" ) ;
58
-
59
- // Assert
60
- Assert . Same ( parameterElement . Schema , resolvedReference ) ;
61
- }
39
+ new object [ ] { _callbackFragment , "/" , _callbackFragment } ,
40
+ new object [ ] { _exampleFragment , "/" , _exampleFragment } ,
41
+ new object [ ] { _linkFragment , "/" , _linkFragment } ,
42
+ new object [ ] { _headerFragment , "/" , _headerFragment } ,
43
+ new object [ ] { _parameterFragment , "/" , _parameterFragment } ,
44
+ new object [ ] { _parameterFragment , "/schema" , _parameterFragment . Schema } ,
45
+ new object [ ] { _parameterFragment , "/examples/example1" , _parameterFragment . Examples [ "example1" ] } ,
46
+ new object [ ] { _requestBodyFragment , "/" , _requestBodyFragment } ,
47
+ new object [ ] { _responseFragment , "/" , _responseFragment } ,
48
+ new object [ ] { _schemaFragment , "/" , _schemaFragment } ,
49
+ new object [ ] { _securitySchemeFragment , "/" , _securitySchemeFragment } ,
50
+ new object [ ] { _tagFragment , "/" , _tagFragment } ,
51
+ } ;
62
52
63
- [ Fact ]
64
- public void ParameterElementCanResolveReferenceToExampleTmpDbgImproveMyName ( )
53
+ [ Theory ]
54
+ [ MemberData ( nameof ( ReferencableElementsCanResolveReferencesTestData ) ) ]
55
+ public void ReferencableElementsCanResolveReferences (
56
+ IOpenApiReferenceable element ,
57
+ string pointer ,
58
+ IOpenApiElement expectedResolvedElement )
65
59
{
66
- // Arrange
67
- var parameterElement = new OpenApiParameter
68
- {
69
- Examples = new Dictionary < string , OpenApiExample > ( )
70
- {
71
- { "example1" , new OpenApiExample ( ) }
72
- } ,
73
- } ;
74
-
75
60
// Act
76
- var resolvedReference = parameterElement . ResolveReference ( "examples/example1" ) ;
61
+ var actualResolvedElement = element . ResolveReference ( pointer ) ;
77
62
78
63
// Assert
79
- Assert . Same ( parameterElement . Examples [ "example1" ] , resolvedReference ) ;
64
+ Assert . Same ( expectedResolvedElement , actualResolvedElement ) ;
80
65
}
81
66
82
67
public static IEnumerable < object [ ] > ParameterElementShouldThrowOnInvalidReferenceIdTestData =>
83
68
new List < object [ ] >
84
69
{
85
- new object [ ] { "/ " } ,
70
+ new object [ ] { "" } ,
86
71
new object [ ] { "a" } ,
87
72
new object [ ] { "examples" } ,
73
+ new object [ ] { "examples/" } ,
88
74
new object [ ] { "examples/a" } ,
89
75
90
76
} ;
@@ -93,21 +79,12 @@ public void ParameterElementCanResolveReferenceToExampleTmpDbgImproveMyName()
93
79
[ MemberData ( nameof ( ParameterElementShouldThrowOnInvalidReferenceIdTestData ) ) ]
94
80
public void ParameterElementShouldThrowOnInvalidReferenceId ( string jsonPointer )
95
81
{
96
- // Arrange
97
- var parameterElement = new OpenApiParameter
98
- {
99
- Examples = new Dictionary < string , OpenApiExample > ( )
100
- {
101
- { "example1" , new OpenApiExample ( ) }
102
- } ,
103
- } ;
104
-
105
82
// Act
106
- Action resolveReference = ( ) => parameterElement . ResolveReference ( jsonPointer ) ;
83
+ Action resolveReference = ( ) => _parameterFragment . ResolveReference ( jsonPointer ) ;
107
84
108
85
// Assert
109
86
var exception = Assert . Throws < OpenApiException > ( resolveReference ) ;
110
- Assert . Equal ( String . Format ( SRResource . InvalidReferenceId , jsonPointer ) , exception . Message ) ;
87
+ Assert . Equal ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) , exception . Message ) ;
111
88
}
112
89
}
113
90
}
0 commit comments