11using System ;
2- using System . Collections . Generic ;
32using System . IO ;
43using System . Linq ;
54
65using Microsoft . CodeAnalysis ;
76using Microsoft . CodeAnalysis . CSharp ;
8- using Microsoft . CodeAnalysis . CSharp . Syntax ;
9-
10- using NUnit . Framework ;
11-
12- using schema . binary . attributes ;
13- using schema . util . strings ;
147
158#pragma warning disable CS8604
169
@@ -25,174 +18,4 @@ internal static class BinarySchemaTestUtil {
2518 ( ( string ) AppContext . GetData ( "TRUSTED_PLATFORM_ASSEMBLIES" ) )
2619 . Split ( Path . PathSeparator )
2720 . Select ( path => MetadataReference . CreateFromFile ( path ) ) ) ;
28-
29- public static IBinarySchemaContainer ParseFirst ( string src )
30- => ParseAll ( src ) . First ( ) ;
31-
32- public static IReadOnlyList < IBinarySchemaContainer > ParseAll ( string src ) {
33- var syntaxTree = CSharpSyntaxTree . ParseText ( src ) ;
34- var compilation = BinarySchemaTestUtil . Compilation . Clone ( )
35- . AddSyntaxTrees ( syntaxTree ) ;
36-
37- var semanticModel = compilation . GetSemanticModel ( syntaxTree ) ;
38-
39- var structures = syntaxTree
40- . GetRoot ( )
41- . DescendantTokens ( )
42- . Where ( t => t is {
43- Text : "BinarySchema" , Parent . Parent : AttributeSyntax
44- } )
45- . Select ( t => t . Parent ? . Parent as AttributeSyntax )
46- . Select ( attributeSyntax => {
47- var attributeSpan = attributeSyntax ! . FullSpan ;
48-
49- var classIndex =
50- src . IndexOf ( "class" ,
51- attributeSpan . Start +
52- attributeSpan . Length ) ;
53- var classNameIndex
54- = src . IndexOf ( ' ' , classIndex ) + 1 ;
55- var classNameLength =
56- src . IndexOfFirst (
57- [ ' ' , '(' ] ,
58- classNameIndex ) -
59- classNameIndex ;
60-
61- var typeName =
62- src . Substring (
63- classNameIndex ,
64- classNameLength ) ;
65- var angleBracketIndex = typeName . IndexOf ( '<' ) ;
66- if ( angleBracketIndex > - 1 ) {
67- typeName
68- = typeName . Substring (
69- 0 ,
70- angleBracketIndex ) ;
71- }
72-
73- var typeNode = syntaxTree . GetRoot ( )
74- . DescendantTokens ( )
75- . Single ( t =>
76- t . Text ==
77- typeName &&
78- t . Parent is
79- ClassDeclarationSyntax
80- or StructDeclarationSyntax
81- )
82- . Parent ;
83-
84- var symbol
85- = semanticModel
86- . GetDeclaredSymbol ( typeNode ) ;
87- var namedTypeSymbol
88- = symbol as INamedTypeSymbol ;
89-
90- return new BinarySchemaContainerParser ( )
91- . ParseContainer ( namedTypeSymbol ) ;
92- } )
93- . ToArray ( ) ;
94-
95- var structureByNamedTypeSymbol =
96- new Dictionary < INamedTypeSymbol , IBinarySchemaContainer > ( ) ;
97- foreach ( var structure in structures ) {
98- structureByNamedTypeSymbol [ structure . TypeSymbol ] = structure ;
99- }
100-
101- var sizeOfMemberInBytesDependencyFixer =
102- new WSizeOfMemberInBytesDependencyFixer ( ) ;
103- foreach ( var structure in structures ) {
104- foreach ( var member in structure . Members . OfType < ISchemaValueMember > ( ) ) {
105- if ( member . MemberType is IIntegerMemberType integerMemberType ) {
106- if ( integerMemberType . AccessChainToSizeOf != null ) {
107- sizeOfMemberInBytesDependencyFixer . AddDependenciesForContainer (
108- structureByNamedTypeSymbol ,
109- integerMemberType . AccessChainToSizeOf ) ;
110- }
111-
112- var pointerToAttribute = integerMemberType . PointerToAttribute ;
113- if ( pointerToAttribute != null ) {
114- sizeOfMemberInBytesDependencyFixer . AddDependenciesForContainer (
115- structureByNamedTypeSymbol ,
116- pointerToAttribute . AccessChainToOtherMember ) ;
117- }
118- }
119- }
120- }
121-
122- return structures ;
123- }
124-
125- public static void AssertDiagnostics (
126- IReadOnlyList < Diagnostic > actualDiagnostics ,
127- params DiagnosticDescriptor [ ] expectedDiagnostics ) {
128- var message = "" ;
129-
130-
131- if ( actualDiagnostics . Count != expectedDiagnostics . Length ) {
132- message +=
133- $ "Expected { expectedDiagnostics . Length } diagnostics but got { actualDiagnostics . Count } .\n ";
134- }
135-
136- bool [ ] actualMatches = new bool [ actualDiagnostics . Count ] ;
137- bool [ ] expectedMatches = new bool [ expectedDiagnostics . Length ] ;
138-
139- for ( var a = 0 ; a < actualDiagnostics . Count ; ++ a ) {
140- var actualDiagnostic = actualDiagnostics [ a ] ;
141-
142- for ( var e = 0 ; e < expectedDiagnostics . Length ; ++ e ) {
143- if ( expectedMatches [ e ] ) {
144- continue ;
145- }
146-
147- var expectedDiagnostic = expectedDiagnostics [ e ] ;
148- if ( actualDiagnostic . Descriptor . Equals ( expectedDiagnostic ) ) {
149- actualMatches [ a ] = true ;
150- expectedMatches [ a ] = true ;
151- break ;
152- }
153- }
154- }
155-
156- var allActualMatched = actualMatches . All ( value => value ) ;
157- var allExpectedMatched = expectedMatches . All ( value => value ) ;
158-
159- if ( allActualMatched && allExpectedMatched ) {
160- return ;
161- }
162-
163- if ( ! allActualMatched ) {
164- message += "\n " ;
165- message += "Unexpected actual diagnostics:\n " ;
166-
167- var i = 0 ;
168- foreach ( var ( hadMatch , actualDiagnostic ) in actualMatches . Zip (
169- actualDiagnostics ) ) {
170- if ( hadMatch ) {
171- continue ;
172- }
173-
174- message += $ " { i ++ } ) { actualDiagnostic . GetMessage ( ) } \n ";
175- }
176- }
177-
178- if ( ! allExpectedMatched ) {
179- message += "\n " ;
180- message += "Unmatched expected diagnostics:\n " ;
181-
182- var i = 0 ;
183- foreach ( var ( hadMatch , expectedDiagnostic ) in Enumerable . Zip (
184- expectedMatches ,
185- expectedDiagnostics ) ) {
186- if ( hadMatch ) {
187- continue ;
188- }
189-
190- message += $ " { i ++ } ) { expectedDiagnostic . MessageFormat } \n ";
191- }
192- }
193-
194- if ( message . Length != 0 ) {
195- Assert . Fail ( message ) ;
196- }
197- }
19821}
0 commit comments