Skip to content

Commit 2e9e013

Browse files
committed
Manage more kinds of from import
1 parent 97cba72 commit 2e9e013

1 file changed

Lines changed: 232 additions & 22 deletions

File tree

‎src/FAST-Python-Tools-Tests/FASTPythonImporterTest.class.st‎

Lines changed: 232 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ Class {
1717
FASTPythonImporterTest class >> generateAssertionsFor: entity on: aStream identation: aNumber [
1818

1919
aStream
20-
nextPutAll: 'self assert: entity sourceCode equals: ';
20+
nextPutAll: 'self assert: self topEntity sourceCode equals: ';
2121
print: entity sourceCode;
2222
nextPutAll: (entity sourceCode lines size > 1
2323
ifTrue: [ ' withPlateformLineEnding.' ]
2424
ifFalse: [ '.' ]);
2525
cr;
2626
tab: aNumber;
27-
nextPutAll: 'self assert: (entity isOfType: ';
27+
nextPutAll: 'self assert: (self topEntity isOfType: ';
2828
nextPutAll: entity class name;
2929
nextPutAll: ').';
3030
cr;
3131
tab: aNumber.
3232

3333
(entity mooseDescription allProperties select: [ :property | self shouldSelectProperty: property of: entity ]) do: [ :property |
3434
aStream
35-
nextPutAll: 'self assert: entity ';
35+
nextPutAll: 'self assert: self topEntity ';
3636
nextPutAll: property implementingSelector;
3737
nextPutAll: ' equals: ';
3838
store: (property getRawFrom: entity);
@@ -45,7 +45,7 @@ FASTPythonImporterTest class >> generateAssertionsFor: entity on: aStream identa
4545
(entity perform: selector) ifNotNil: [ :result |
4646
result ifNotEmpty: [
4747
aStream
48-
nextPutAll: 'self assert: entity ';
48+
nextPutAll: 'self assert: self topEntity ';
4949
nextPutAll: selector;
5050
nextPutAll: ' size equals: ';
5151
print: result size;
@@ -61,21 +61,16 @@ FASTPythonImporterTest class >> generateAssertionsFor: entity on: aStream identa
6161
print: index;
6262
nextPutAll: ').';
6363
cr;
64-
tab: aNumber + 1;
65-
nextPutAll: 'entity := stack top.';
66-
cr;
6764
tab: aNumber + 1.
6865
self generateAssertionsFor: child on: aStream identation: aNumber + 1.
6966

70-
properties last = selector ifFalse: [
71-
aStream
72-
nextPutAll: 'stack pop.';
73-
cr;
74-
cr;
75-
tab: aNumber;
76-
nextPutAll: 'entity := stack top.';
77-
cr;
78-
tab: aNumber ] ] ] ] ] ]
67+
aStream
68+
nextPutAll: 'stack pop.';
69+
cr;
70+
cr;
71+
tab: (result last = child
72+
ifTrue: [ aNumber ]
73+
ifFalse: [ aNumber + 1 ]) ] ] ] ] ]
7974
]
8075

8176
{ #category : 'as yet unclassified' }
@@ -96,10 +91,6 @@ FASTPythonImporterTest class >> generateTestNamed: aString fromCode: code [
9691
cr;
9792
cr;
9893
tab;
99-
nextPutAll: '| entity |';
100-
cr;
101-
cr;
102-
tab;
10394
nextPutAll: 'self parse: ';
10495
print: code;
10596
nextPut: $.;
@@ -121,8 +112,7 @@ FASTPythonImporterTest class >> generateTestNamed: aString fromCode: code [
121112
aStream
122113
cr;
123114
tab;
124-
nextPutAll: 'stack push: fast rootEntities anyOne genericChildren first.
125-
entity := stack top.';
115+
nextPutAll: 'stack push: fast rootEntities anyOne genericChildren first.';
126116
"We exclude assertions on module in generation"cr;
127117
tab.
128118
self generateAssertionsFor: model rootEntities anyOne genericChildren first on: aStream identation: 1 ].
@@ -438,6 +428,220 @@ FASTPythonImporterTest >> testFromImport [
438428

439429
]
440430

431+
{ #category : 'tests' }
432+
FASTPythonImporterTest >> testFromImportMultiple [
433+
"Generated as regression test by FASTPythonImporterTest class>>#generateTestNamed:fromCode: "
434+
435+
self parse: 'from collections import Counter, defaultdic'.
436+
437+
self assert: (fast allWithType: FASTPyModule) size equals: 1.
438+
self assert: (fast allWithType: FASTPyImportFromStatement) size equals: 1.
439+
self assert: (fast allWithType: FASTPyFromImportModule) size equals: 1.
440+
self assert: (fast allWithType: FASTPyImportedEntity) size equals: 2.
441+
442+
stack push: fast rootEntities anyOne genericChildren first.
443+
self assert: self topEntity sourceCode equals: 'from collections import Counter, defaultdic'.
444+
self assert: (self topEntity isOfType: FASTPyImportFromStatement).
445+
self assert: self topEntity fromImportModule size equals: 1.
446+
447+
stack push: (stack top fromImportModule at: 1).
448+
self assert: self topEntity sourceCode equals: 'collections'.
449+
self assert: (self topEntity isOfType: FASTPyFromImportModule).
450+
self assert: self topEntity segments equals: #('collections').
451+
stack pop.
452+
453+
self assert: self topEntity importedEntities size equals: 2.
454+
455+
stack push: (stack top importedEntities at: 1).
456+
self assert: self topEntity sourceCode equals: 'Counter'.
457+
self assert: (self topEntity isOfType: FASTPyImportedEntity).
458+
self assert: self topEntity segments equals: #('Counter').
459+
stack pop.
460+
461+
stack push: (stack top importedEntities at: 2).
462+
self assert: self topEntity sourceCode equals: 'defaultdic'.
463+
self assert: (self topEntity isOfType: FASTPyImportedEntity).
464+
self assert: self topEntity segments equals: #('defaultdic').
465+
stack pop.
466+
467+
468+
]
469+
470+
{ #category : 'tests' }
471+
FASTPythonImporterTest >> testFromImportMultipleWithAlias [
472+
"Generated as regression test by FASTPythonImporterTest class>>#generateTestNamed:fromCode: "
473+
474+
self parse: 'from math import sin, cos as cosinus, pi'.
475+
476+
self assert: (fast allWithType: FASTPyModule) size equals: 1.
477+
self assert: (fast allWithType: FASTPyImportFromStatement) size equals: 1.
478+
self assert: (fast allWithType: FASTPyFromImportModule) size equals: 1.
479+
self assert: (fast allWithType: FASTPyImportedEntity) size equals: 3.
480+
481+
stack push: fast rootEntities anyOne genericChildren first.
482+
self assert: self topEntity sourceCode equals: 'from math import sin, cos as cosinus, pi'.
483+
self assert: (self topEntity isOfType: FASTPyImportFromStatement).
484+
self assert: self topEntity fromImportModule size equals: 1.
485+
486+
stack push: (stack top fromImportModule at: 1).
487+
self assert: self topEntity sourceCode equals: 'math'.
488+
self assert: (self topEntity isOfType: FASTPyFromImportModule).
489+
self assert: self topEntity segments equals: #('math').
490+
stack pop.
491+
492+
self assert: self topEntity importedEntities size equals: 3.
493+
494+
stack push: (stack top importedEntities at: 1).
495+
self assert: self topEntity sourceCode equals: 'sin'.
496+
self assert: (self topEntity isOfType: FASTPyImportedEntity).
497+
self assert: self topEntity segments equals: #('sin').
498+
stack pop.
499+
500+
stack push: (stack top importedEntities at: 2).
501+
self assert: self topEntity sourceCode equals: 'cos as cosinus'.
502+
self assert: (self topEntity isOfType: FASTPyImportedEntity).
503+
self assert: self topEntity segments equals: #('cos').
504+
self assert: self topEntity alias equals: 'cosinus'.
505+
stack pop.
506+
507+
stack push: (stack top importedEntities at: 3).
508+
self assert: self topEntity sourceCode equals: 'pi'.
509+
self assert: (self topEntity isOfType: FASTPyImportedEntity).
510+
self assert: self topEntity segments equals: #('pi').
511+
stack pop.
512+
513+
514+
]
515+
516+
{ #category : 'tests' }
517+
FASTPythonImporterTest >> testFromImportMultipleWithParenthesis [
518+
"Generated as regression test by FASTPythonImporterTest class>>#generateTestNamed:fromCode: "
519+
520+
self parse: 'from math import (count, cycle, repeat,)'.
521+
522+
self assert: (fast allWithType: FASTPyModule) size equals: 1.
523+
self assert: (fast allWithType: FASTPyImportFromStatement) size equals: 1.
524+
self assert: (fast allWithType: FASTPyFromImportModule) size equals: 1.
525+
self assert: (fast allWithType: FASTPyImportedEntity) size equals: 3.
526+
527+
stack push: fast rootEntities anyOne genericChildren first.
528+
self assert: self topEntity sourceCode equals: 'from math import (count, cycle, repeat,)'.
529+
self assert: (self topEntity isOfType: FASTPyImportFromStatement).
530+
self assert: self topEntity fromImportModule size equals: 1.
531+
532+
stack push: (stack top fromImportModule at: 1).
533+
self assert: self topEntity sourceCode equals: 'math'.
534+
self assert: (self topEntity isOfType: FASTPyFromImportModule).
535+
self assert: self topEntity segments equals: #('math').
536+
stack pop.
537+
538+
self assert: self topEntity importedEntities size equals: 3.
539+
540+
stack push: (stack top importedEntities at: 1).
541+
self assert: self topEntity sourceCode equals: 'count'.
542+
self assert: (self topEntity isOfType: FASTPyImportedEntity).
543+
self assert: self topEntity segments equals: #('count').
544+
stack pop.
545+
546+
stack push: (stack top importedEntities at: 2).
547+
self assert: self topEntity sourceCode equals: 'cycle'.
548+
self assert: (self topEntity isOfType: FASTPyImportedEntity).
549+
self assert: self topEntity segments equals: #('cycle').
550+
stack pop.
551+
552+
stack push: (stack top importedEntities at: 3).
553+
self assert: self topEntity sourceCode equals: 'repeat'.
554+
self assert: (self topEntity isOfType: FASTPyImportedEntity).
555+
self assert: self topEntity segments equals: #('repeat').
556+
stack pop.
557+
558+
559+
]
560+
561+
{ #category : 'tests' }
562+
FASTPythonImporterTest >> testFromImportMultipleWithParenthesisAndAlias [
563+
"Generated as regression test by FASTPythonImporterTest class>>#generateTestNamed:fromCode: "
564+
565+
self parse: 'from math import (count, cycle as c, repeat,)'.
566+
567+
self assert: (fast allWithType: FASTPyModule) size equals: 1.
568+
self assert: (fast allWithType: FASTPyImportFromStatement) size equals: 1.
569+
self assert: (fast allWithType: FASTPyFromImportModule) size equals: 1.
570+
self assert: (fast allWithType: FASTPyImportedEntity) size equals: 3.
571+
572+
stack push: fast rootEntities anyOne genericChildren first.
573+
self assert: self topEntity sourceCode equals: 'from math import (count, cycle as c, repeat,)'.
574+
self assert: (self topEntity isOfType: FASTPyImportFromStatement).
575+
self assert: self topEntity fromImportModule size equals: 1.
576+
577+
stack push: (stack top fromImportModule at: 1).
578+
self assert: self topEntity sourceCode equals: 'math'.
579+
self assert: (self topEntity isOfType: FASTPyFromImportModule).
580+
self assert: self topEntity segments equals: #('math').
581+
stack pop.
582+
583+
self assert: self topEntity importedEntities size equals: 3.
584+
585+
stack push: (stack top importedEntities at: 1).
586+
self assert: self topEntity sourceCode equals: 'count'.
587+
self assert: (self topEntity isOfType: FASTPyImportedEntity).
588+
self assert: self topEntity segments equals: #('count').
589+
stack pop.
590+
591+
stack push: (stack top importedEntities at: 2).
592+
self assert: self topEntity sourceCode equals: 'cycle as c'.
593+
self assert: (self topEntity isOfType: FASTPyImportedEntity).
594+
self assert: self topEntity segments equals: #('cycle').
595+
self assert: self topEntity alias equals: 'c'.
596+
stack pop.
597+
598+
stack push: (stack top importedEntities at: 3).
599+
self assert: self topEntity sourceCode equals: 'repeat'.
600+
self assert: (self topEntity isOfType: FASTPyImportedEntity).
601+
self assert: self topEntity segments equals: #('repeat').
602+
stack pop.
603+
604+
605+
]
606+
607+
{ #category : 'tests' }
608+
FASTPythonImporterTest >> testFromImportWithAlias [
609+
"Generated as regression test by FASTPythonImporterTest class>>#generateTestNamed:fromCode: "
610+
611+
| entity |
612+
613+
self parse: 'from datetime import date as d'.
614+
615+
self assert: (fast allWithType: FASTPyModule) size equals: 1.
616+
self assert: (fast allWithType: FASTPyImportFromStatement) size equals: 1.
617+
self assert: (fast allWithType: FASTPyFromImportModule) size equals: 1.
618+
self assert: (fast allWithType: FASTPyImportedEntity) size equals: 1.
619+
620+
stack push: fast rootEntities anyOne genericChildren first.
621+
entity := stack top.
622+
self assert: entity sourceCode equals: 'from datetime import date as d'.
623+
self assert: (entity isOfType: FASTPyImportFromStatement).
624+
self assert: entity fromImportModule size equals: 1.
625+
626+
stack push: (stack top fromImportModule at: 1).
627+
entity := stack top.
628+
self assert: entity sourceCode equals: 'datetime'.
629+
self assert: (entity isOfType: FASTPyFromImportModule).
630+
self assert: entity segments equals: #('datetime').
631+
stack pop.
632+
633+
entity := stack top.
634+
self assert: entity importedEntities size equals: 1.
635+
636+
stack push: (stack top importedEntities at: 1).
637+
entity := stack top.
638+
self assert: entity sourceCode equals: 'date as d'.
639+
self assert: (entity isOfType: FASTPyImportedEntity).
640+
self assert: entity segments equals: #('date').
641+
self assert: entity alias equals: 'd'.
642+
643+
]
644+
441645
{ #category : 'tests' }
442646
FASTPythonImporterTest >> testFunctionDefinition [
443647

@@ -796,3 +1000,9 @@ FASTPythonImporterTest >> testUnaryOperator [
7961000
self assert: (operator argument isOfType: FASTPyInteger).
7971001
self assert: operator operator equals: '-'
7981002
]
1003+
1004+
{ #category : 'running' }
1005+
FASTPythonImporterTest >> topEntity [
1006+
1007+
^ stack top
1008+
]

0 commit comments

Comments
 (0)