22using System . Collections . Generic ;
33using System . IO ;
44using System . Linq ;
5- using System . Text ;
6- using System . Threading . Tasks ;
7- using System . Windows . Forms ;
85using Microsoft . SqlServer . TransactSql . ScriptDom ;
96
107namespace AgileSQLClub . tSQLtTestController
@@ -20,10 +17,9 @@ public FileScanner(TSqlParser parser)
2017
2118 public ScanResults ScanCode ( string code , ScanResults results , string path )
2219 {
23-
2420 var batches = code . Split ( new [ ] { "\r \n GO\r \n " , "\n GO\n " } , StringSplitOptions . None ) ;
2521 var offset = 0 ;
26- int lineOffset = 0 ;
22+ var lineOffset = 0 ;
2723
2824 foreach ( var batch in batches )
2925 {
@@ -37,13 +33,12 @@ public ScanResults ScanCode(string code, ScanResults results, string path)
3733
3834 private ScanResults AppendResults ( ScanResults results , string batch , string path , int offset , int lineOffset )
3935 {
40-
4136 var reader = new StringReader ( batch ) ;
4237 IList < ParseError > errors ;
4338 var fragment = _parser . Parse ( reader , out errors ) ;
4439 var visitor = new TestVisitor ( path , offset , lineOffset ) ;
4540 fragment . Accept ( visitor ) ;
46-
41+
4742 results . FoundProperties . AddRange ( visitor . ExtendedProperties ) ;
4843 results . FoundClasses . AddRange ( visitor . Schemas ) ;
4944 results . FoundPotentialTests . AddRange ( visitor . Procedures ) ;
@@ -54,16 +49,16 @@ private ScanResults AppendResults(ScanResults results, string batch, string path
5449
5550 public class TestVisitor : TSqlFragmentVisitor
5651 {
52+ private readonly int _lineOffset ;
53+ private readonly int _offset ;
54+ private readonly string _path ;
5755 public readonly List < tSQLtExtendedProperty > ExtendedProperties = new List < tSQLtExtendedProperty > ( ) ;
56+ public readonly List < SqlProcedure > Procedures = new List < SqlProcedure > ( ) ;
5857 public readonly List < SqlSchema > Schemas = new List < SqlSchema > ( ) ;
59- public readonly List < SqlProcedure > Procedures = new List < SqlProcedure > ( ) ;
60- private readonly string _path ;
61- private readonly int _offset ;
62- private readonly int _lineOffset ;
6358
6459 public TestVisitor ( string path , int offset , int lineOffset )
6560 {
66- this . _path = path ;
61+ _path = path ;
6762 _offset = offset ;
6863 _lineOffset = lineOffset ;
6964 }
@@ -75,9 +70,9 @@ public override void Visit(CreateProcedureStatement proc)
7570 name . Schema = son . SchemaIdentifier ? . Value . UnQuote ( ) ;
7671 name . Object = son . BaseIdentifier ? . Value . UnQuote ( ) ;
7772
78- if ( name . Object . ToLowerInvariant ( ) . StartsWith ( "test" ) )
79- Procedures . Add ( new SqlProcedure ( name , _path , _offset + proc . StartOffset , proc . FragmentLength , _lineOffset + proc . StartLine ) ) ;
80-
73+ if ( name . Object . ToLowerInvariant ( ) . StartsWith ( "test" ) )
74+ Procedures . Add ( new SqlProcedure ( name , _path , _offset + proc . StartOffset , proc . FragmentLength , _lineOffset + proc . StartLine ) ) ;
75+
8176 base . Visit ( proc ) ;
8277 }
8378
@@ -95,14 +90,11 @@ public override void Visit(ExecuteStatement exec)
9590
9691 if ( ! ( exec . ExecuteSpecification . ExecutableEntity is ExecutableProcedureReference ) )
9792 return ;
98-
93+
9994 var refereced = ( ExecutableProcedureReference ) exec . ExecuteSpecification . ExecutableEntity ;
10095
101- if ( refereced . ProcedureReference . ProcedureReference != null && refereced . ProcedureReference . ProcedureReference . Name != null &&
102- refereced . ProcedureReference . ProcedureReference . Name . BaseIdentifier . Value . IndexOf (
103- "sp_addextendedproperty" , StringComparison . OrdinalIgnoreCase ) > - 1 )
96+ if ( refereced . ProcedureReference . ProcedureReference != null && refereced . ProcedureReference . ProcedureReference . Name != null && refereced . ProcedureReference . ProcedureReference . Name . BaseIdentifier . Value . IndexOf ( "sp_addextendedproperty" , StringComparison . OrdinalIgnoreCase ) > - 1 )
10497 {
105-
10698 var propertyValue =
10799 refereced . Parameters . FirstOrDefault (
108100 p =>
@@ -112,13 +104,27 @@ public override void Visit(ExecuteStatement exec)
112104 if ( propertyValue == null )
113105 return ;
114106
115- var schemaName = ( ( refereced . Parameters . FirstOrDefault ( p => p . Variable . Name . ToLowerInvariant ( ) == "@level0name" ) ) ? . ParameterValue as StringLiteral ) ? . Value ;
116- if ( ! String . IsNullOrEmpty ( schemaName ) )
117- ExtendedProperties . Add ( new tSQLtExtendedProperty ( schemaName . UnQuote ( ) ) ) ;
107+ var schemaName = ( refereced . Parameters . FirstOrDefault ( p => p . Variable ? . Name . ToLowerInvariant ( ) == "@level0name" ) ? . ParameterValue as StringLiteral ) ? . Value ;
108+ if ( ! string . IsNullOrEmpty ( schemaName ) )
109+ ExtendedProperties . Add ( new tSQLtExtendedProperty ( schemaName . UnQuote ( ) ) ) ;
118110 }
119-
120- }
121111
112+ var name = refereced . ProcedureReference . ProcedureReference ? . Name ;
113+ if ( name == null )
114+ return ;
115+
116+ if ( name . SchemaIdentifier ? . Value . ToLowerInvariant ( ) == "tsqlt" && name . BaseIdentifier ? . Value . ToLowerInvariant ( ) == "newtestclass" )
117+ {
118+ var parameterValue = ( refereced . Parameters . FirstOrDefault ( ) ? . ParameterValue as StringLiteral ) ? . Value ;
119+
120+ if ( ! string . IsNullOrEmpty ( parameterValue ) )
121+ {
122+ ExtendedProperties . Add ( new tSQLtExtendedProperty ( parameterValue . UnQuote ( ) ) ) ;
123+ Schemas . Add ( new SqlSchema ( parameterValue . UnQuote ( ) , _path ) ) ;
124+ }
125+ }
126+
127+ }
122128 }
123129
124130
@@ -131,12 +137,14 @@ public class ScanResults
131137
132138 public class SqlObjectName
133139 {
134- public string Schema ;
135140 public string Object ;
141+ public string Schema ;
136142 }
137143
138144 public class SqlProcedure
139145 {
146+ public SqlObjectName Name ;
147+
140148 public SqlProcedure ( SqlObjectName name , string path , int startPos , int endPos , int startLine )
141149 {
142150 Name = name ;
@@ -150,40 +158,37 @@ public SqlProcedure(SqlObjectName name, string path, int startPos, int endPos, i
150158 public int StartPos { get ; set ; }
151159 public int EndPos { get ; set ; }
152160 public int StartLine { get ; set ; }
153-
154- public SqlObjectName Name ;
155-
156161 }
157162
158163 public class SqlSchema
159164 {
165+ public string Name ;
166+
167+ public string Path ;
168+
160169 public SqlSchema ( string schemaName , string path )
161170 {
162171 Name = schemaName ;
163172 Path = path ;
164173 }
165-
166- public string Path ;
167- public string Name ;
168-
169174 }
170175
171176 public class tSQLtExtendedProperty
172177 {
178+ public string SchemaName ;
179+
173180 public tSQLtExtendedProperty ( string schemaName )
174181 {
175182 SchemaName = schemaName ;
176183 }
177-
178- public string SchemaName ;
179184 }
180185
181186
182187 public class TestFinder
183188 {
184- private readonly TSqlParser _parser ;
185189 private readonly List < string > _filePaths ;
186190 private readonly string _lookupPath ;
191+ private readonly TSqlParser _parser ;
187192
188193 public TestFinder ( TSqlParser parser , List < string > filePaths )
189194 {
@@ -198,25 +203,24 @@ public List<TestClass> GetTests()
198203
199204 foreach ( var path in _filePaths )
200205 {
201- var scanner = new FileScanner ( _parser ) ;
206+ var scanner = new FileScanner ( _parser ) ;
202207 results = scanner . ScanCode ( File . ReadAllText ( path ) , results , path ) ;
203208 }
204209
205210 var foundClasses =
206211 results . FoundClasses . Where (
207212 p =>
208213 results . FoundPotentialTests . Any (
209- e => String . Equals ( p . Name , e . Name . Schema , StringComparison . OrdinalIgnoreCase ) ) ) ;
214+ e => string . Equals ( p . Name , e . Name . Schema , StringComparison . OrdinalIgnoreCase ) ) ) ;
210215
211216 var foundTests =
212217 results . FoundPotentialTests . Where (
213218 p =>
214219 results . FoundPotentialTests . Any (
215- s => String . Equals ( s . Name . Schema , p . Name . Schema , StringComparison . OrdinalIgnoreCase ) ) ) ;
220+ s => string . Equals ( s . Name . Schema , p . Name . Schema , StringComparison . OrdinalIgnoreCase ) ) ) ;
216221
217222 return classes ;
218223 }
219-
220224 }
221225
222226 public class TestClasses
@@ -236,4 +240,4 @@ public class Test
236240 public string Path ;
237241 public int Line { get ; set ; }
238242 }
239- }
243+ }
0 commit comments