11@TestOn ('vm' )
2+ import 'package:collection/src/iterable_extensions.dart' ;
23import 'package:dart_code_metrics/src/analyzers/lint_analyzer/metrics/metrics_list/number_of_parameters_metric.dart' ;
34import 'package:dart_code_metrics/src/analyzers/lint_analyzer/metrics/models/metric_value_level.dart' ;
45import 'package:dart_code_metrics/src/analyzers/lint_analyzer/metrics/scope_visitor.dart' ;
@@ -19,127 +20,136 @@ Future<void> main() async {
1920 final example = await FileResolver .resolve (_examplePath);
2021 example.unit.visitChildren (scopeVisitor);
2122
22- group ('NumberOfParametersMetric computes parameters received by the' , () {
23- test ('simple function' , () {
24- final metricValue = metric.compute (
25- scopeVisitor.functions.first.declaration,
26- scopeVisitor.classes,
27- scopeVisitor.functions,
28- example,
29- [],
30- );
31-
32- expect (metricValue.metricsId, equals (metric.id));
33- expect (metricValue.value, equals (0 ));
34- expect (metricValue.level, equals (MetricValueLevel .none));
35- expect (
36- metricValue.comment,
37- equals ('This function has 0 parameters.' ),
38- );
39- expect (metricValue.recommendation, isNull);
40- expect (metricValue.context, isEmpty);
41- });
42-
43- test ('simple function with arguments' , () {
44- final metricValue = metric.compute (
45- scopeVisitor.functions.toList ()[1 ].declaration,
46- scopeVisitor.classes,
47- scopeVisitor.functions,
48- example,
49- [],
50- );
51-
52- expect (metricValue.metricsId, equals (metric.id));
53- expect (metricValue.value, equals (2 ));
54- expect (metricValue.level, equals (MetricValueLevel .none));
55- expect (
56- metricValue.comment,
57- equals ('This function has 2 parameters.' ),
58- );
59- expect (metricValue.recommendation, isNull);
60- expect (metricValue.context, isEmpty);
23+ group ('NumberOfParametersMetric' , () {
24+ group ('computes parameters received by the' , () {
25+ test ('simple function' , () {
26+ final metricValue = metric.compute (
27+ scopeVisitor.functions.toList ()[0 ].declaration,
28+ scopeVisitor.classes,
29+ scopeVisitor.functions,
30+ example,
31+ [],
32+ );
33+
34+ expect (metricValue.metricsId, equals (metric.id));
35+ expect (metricValue.value, equals (0 ));
36+ expect (metricValue.level, equals (MetricValueLevel .none));
37+ expect (metricValue.comment, equals ('This function has 0 parameters.' ));
38+ expect (metricValue.recommendation, isNull);
39+ expect (metricValue.context, isEmpty);
40+ });
41+
42+ test ('simple function with arguments' , () {
43+ final metricValue = metric.compute (
44+ scopeVisitor.functions.toList ()[1 ].declaration,
45+ scopeVisitor.classes,
46+ scopeVisitor.functions,
47+ example,
48+ [],
49+ );
50+
51+ expect (metricValue.metricsId, equals (metric.id));
52+ expect (metricValue.value, equals (2 ));
53+ expect (metricValue.level, equals (MetricValueLevel .none));
54+ expect (metricValue.comment, equals ('This function has 2 parameters.' ));
55+ expect (metricValue.recommendation, isNull);
56+ expect (metricValue.context, isEmpty);
57+ });
58+
59+ test ('simple setter' , () {
60+ final metricValue = metric.compute (
61+ scopeVisitor.functions.toList ()[2 ].declaration,
62+ scopeVisitor.classes,
63+ scopeVisitor.functions,
64+ example,
65+ [],
66+ );
67+
68+ expect (metricValue.metricsId, equals (metric.id));
69+ expect (metricValue.value, equals (1 ));
70+ expect (metricValue.level, equals (MetricValueLevel .none));
71+ expect (metricValue.comment, equals ('This function has 1 parameter.' ));
72+ expect (metricValue.recommendation, isNull);
73+ expect (metricValue.context, isEmpty);
74+ });
75+
76+ test ('simple getter' , () {
77+ final metricValue = metric.compute (
78+ scopeVisitor.functions.toList ()[3 ].declaration,
79+ scopeVisitor.classes,
80+ scopeVisitor.functions,
81+ example,
82+ [],
83+ );
84+
85+ expect (metricValue.metricsId, equals (metric.id));
86+ expect (metricValue.value, equals (0 ));
87+ expect (metricValue.level, equals (MetricValueLevel .none));
88+ expect (metricValue.comment, equals ('This function has 0 parameters.' ));
89+ expect (metricValue.recommendation, isNull);
90+ expect (metricValue.context, isEmpty);
91+ });
92+
93+ test ('class method : NumberOfParametersMetric.commentMessage' , () {
94+ final metricValue = metric.compute (
95+ scopeVisitor.functions.toList ()[5 ].declaration,
96+ scopeVisitor.classes,
97+ scopeVisitor.functions,
98+ example,
99+ [],
100+ );
101+
102+ expect (metricValue.metricsId, equals (metric.id));
103+ expect (metricValue.value, equals (3 ));
104+ expect (metricValue.level, equals (MetricValueLevel .noted));
105+ expect (metricValue.comment, equals ('This method has 3 parameters.' ));
106+ expect (metricValue.recommendation, isNull);
107+ expect (metricValue.context, isEmpty);
108+ });
109+
110+ test ('class method : NumberOfParametersMetric.computeImplementation' , () {
111+ final metricValue = metric.compute (
112+ scopeVisitor.functions.toList ()[6 ].declaration,
113+ scopeVisitor.classes,
114+ scopeVisitor.functions,
115+ example,
116+ [],
117+ );
118+
119+ expect (metricValue.metricsId, equals (metric.id));
120+ expect (metricValue.value, equals (5 ));
121+ expect (metricValue.level, equals (MetricValueLevel .warning));
122+ expect (
123+ metricValue.comment,
124+ equals (
125+ 'This method has 5 parameters, exceeds the maximum of 3 allowed.' ,
126+ ),
127+ );
128+ expect (metricValue.recommendation, isNull);
129+ expect (metricValue.context, isEmpty);
130+ });
61131 });
62132
63- test ('simple setter' , () {
64- final metricValue = metric.compute (
65- scopeVisitor.functions.toList ()[2 ].declaration,
66- scopeVisitor.classes,
67- scopeVisitor.functions,
68- example,
69- [],
70- );
71-
72- expect (metricValue.metricsId, equals (metric.id));
73- expect (metricValue.value, equals (1 ));
74- expect (metricValue.level, equals (MetricValueLevel .none));
75- expect (
76- metricValue.comment,
77- equals ('This function has 1 parameter.' ),
78- );
79- expect (metricValue.recommendation, isNull);
80- expect (metricValue.context, isEmpty);
81- });
133+ test ('supports returns only for functions and methods' , () {
134+ const supports = [true , true , true , true , false , true , true ];
82135
83- test ('simple getter' , () {
84- final metricValue = metric.compute (
85- scopeVisitor.functions.toList ()[3 ].declaration,
86- scopeVisitor.classes,
87- scopeVisitor.functions,
88- example,
89- [],
136+ assert (
137+ scopeVisitor.functions.length == supports.length,
138+ 'arrays must be same lengths' ,
90139 );
91140
92- expect (metricValue.metricsId, equals (metric.id));
93- expect (metricValue.value, equals (0 ));
94- expect (metricValue.level, equals (MetricValueLevel .none));
95- expect (
96- metricValue.comment,
97- equals ('This function has 0 parameters.' ),
98- );
99- expect (metricValue.recommendation, isNull);
100- expect (metricValue.context, isEmpty);
101- });
102-
103- test ('class method' , () {
104- final metricValue = metric.compute (
105- scopeVisitor.functions.toList ()[4 ].declaration,
106- scopeVisitor.classes,
107- scopeVisitor.functions,
108- example,
109- [],
110- );
111-
112- expect (metricValue.metricsId, equals (metric.id));
113- expect (metricValue.value, equals (3 ));
114- expect (metricValue.level, equals (MetricValueLevel .noted));
115- expect (
116- metricValue.comment,
117- equals ('This method has 3 parameters.' ),
118- );
119- expect (metricValue.recommendation, isNull);
120- expect (metricValue.context, isEmpty);
121- });
122-
123- test ('class method 2' , () {
124- final metricValue = metric.compute (
125- scopeVisitor.functions.last.declaration,
126- scopeVisitor.classes,
127- scopeVisitor.functions,
128- example,
129- [],
130- );
131-
132- expect (metricValue.metricsId, equals (metric.id));
133- expect (metricValue.value, equals (4 ));
134- expect (metricValue.level, equals (MetricValueLevel .warning));
135- expect (
136- metricValue.comment,
137- equals (
138- 'This method has 4 parameters, exceeds the maximum of 3 allowed.' ,
139- ),
140- );
141- expect (metricValue.recommendation, isNull);
142- expect (metricValue.context, isEmpty);
141+ scopeVisitor.functions.forEachIndexed ((index, value) {
142+ expect (
143+ metric.supports (
144+ value.declaration,
145+ scopeVisitor.classes,
146+ scopeVisitor.functions,
147+ example,
148+ [],
149+ ),
150+ equals (supports[index]),
151+ );
152+ });
143153 });
144154 });
145155}
0 commit comments