Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference checker with Yasser and Quentin #729

Merged
merged 12 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 42 additions & 52 deletions src/BaselineOfMicrodown/BaselineOfMicrodown.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,20 @@ BaselineOfMicrodown >> baseline: spec [

<baseline>
spec for: #common do: [

self xmlParserHtml: spec.
self xmlParserHtml: spec.

spec
package: #Microdown;
package: #'Microdown-Tests'
with: [ spec requires: #( #Microdown ) ];

with: [ spec requires: #( #Microdown ) ];
package: #'Microdown-Pharo-Tools'
with: [ spec requires: #( #Microdown ) ];

with: [ spec requires: #( #Microdown ) ];
package: #'Microdown-RichTextComposer'
with: [ spec requires: #( #Microdown ) ];
with: [ spec requires: #( #Microdown ) ];
package: #'Microdown-RichTextComposer-Tests'
with: [ spec requires: #( #'Microdown-RichTextComposer' ) ];

with: [ spec requires: #( #'Microdown-RichTextComposer' ) ];
package: #'Microdown-MathFlaky-Tests'
with: [ spec requires: #( #'Microdown-RichTextComposer-Tests' ) ];

with: [ spec requires: #( #'Microdown-RichTextComposer-Tests' ) ];
"package: #'Microdown-RichTextPresenter'
with: [ spec requires: #( #Microdown ) ];
package: #'Microdown-RichTextPresenter-Tests'
Expand All @@ -42,62 +37,57 @@ BaselineOfMicrodown >> baseline: spec [
with: [ spec requires: #( #Microdown #'Microdown-RichTextPresenter') ];
package: #'Microdown-DocumentBrowser-Tests'
with: [ spec requires: #( #'Microdown-DocumentBrowser' ) ];"

package: #'Microdown-Transformer'
with: [ spec requires: #( #Microdown ) ];
with: [ spec requires: #( #Microdown ) ];
package: #'Microdown-Transformer-Tests'
with: [ spec requires: #( #'Microdown-Transformer' ) ];

with: [ spec requires: #( #'Microdown-Transformer' ) ];
package: #'Microdown-Evaluator'
with: [ spec requires: #( #'Microdown-Transformer' ) ];
with: [ spec requires: #( #'Microdown-Transformer' ) ];
package: #'Microdown-Evaluator-Tests'
with: [ spec requires: #( #'Microdown-Evaluator') ];

with: [ spec requires: #( #'Microdown-Evaluator' ) ];
package: #'Microdown-HTMLExporter'
with: [ spec requires: #( #Microdown #'Microdown-LaTeXExporter') ];
package: #'Microdown-HTMLExporter-Tests'
with: [ spec requires: #( #'Microdown-HTMLExporter' 'Microdown-Tests' 'XMLParserHTML') ];

with: [ spec requires: #( #Microdown #'Microdown-LaTeXExporter' ) ];
package: #'Microdown-HTMLExporter-Tests' with: [
spec requires:
#( #'Microdown-HTMLExporter' 'Microdown-Tests' 'XMLParserHTML' ) ];
package: #'Microdown-LaTeXExporter'
with: [ spec requires: #( #Microdown ) ];
package: #'Microdown-LaTeXExporter-Tests'
with: [ spec requires: #( #'Microdown-LaTeXExporter' #'Microdown-Tests') ];

with: [ spec requires: #( #Microdown ) ];
package: #'Microdown-LaTeXExporter-Tests' with: [
spec requires: #( #'Microdown-LaTeXExporter'
#'Microdown-Tests' ) ];
package: #'Microdown-BeamerExporter'
with: [ spec requires: #( #'Microdown-LaTeXExporter' ) ];
with: [ spec requires: #( #'Microdown-LaTeXExporter' ) ];
package: #'Microdown-BeamerExporter-Tests'
with: [ spec requires: #( #'Microdown-LaTeXExporter-Tests') ];

with: [ spec requires: #( #'Microdown-LaTeXExporter-Tests' ) ];
package: #'Microdown-PrettyPrinter'
with: [ spec requires: #( #Microdown ) ];

package: #'Microdown-PrettyPrinter-Tests'
with: [ spec requires: #( #'Microdown-PrettyPrinter' #'Microdown-Tests') ].


with: [ spec requires: #( #Microdown ) ];
package: #'Microdown-ReferenceChecker' with: [
spec requires: #( #'Microdown' ) ];
package: #'Microdown-PrettyPrinter-Tests' with: [
spec requires: #( #'Microdown-PrettyPrinter'
#'Microdown-Tests' ) ].


"I do not want group without tests for now"
spec
group: 'Core' with: #('Microdown');
group: 'Tests' with: #('Core' 'Microdown-Tests');
group: 'RichText' with: #('Core' 'Microdown-RichTextComposer' );
group: 'Extensions' with: #(
"
group: 'Core' with: #( 'Microdown' );
group: 'Tests' with: #( 'Core' 'Microdown-Tests' );
group: 'RichText' with: #( 'Core' 'Microdown-RichTextComposer' );
group: 'Extensions'
with: #( #'Microdown-Evaluator' #'Microdown-Evaluator-Tests' #'Microdown-ReferenceChecker'
#'Microdown-PrettyPrinter' #'Microdown-PrettyPrinter-Tests'
#'Microdown-HTMLExporter' #'Microdown-HTMLExporter-Tests'
#'Microdown-LaTeXExporter' #'Microdown-LaTeXExporter-Tests'
#'Microdown-Transformer' #'Microdown-Transformer-Tests' );
group: 'All'
with: #( 'Core' 'Tests' 'Extensions' 'Microdown-Pharo-Tools'
'RichText' )
"
#'Microdown-RichTextPresenter'
#'Microdown-RichTextPresenter-Tests'
#'Microdown-DocumentBrowser'
#'Microdown-DocumentBrowser-Tests'
"
#'Microdown-Evaluator'
#'Microdown-Evaluator-Tests'
#'Microdown-PrettyPrinter'
#'Microdown-PrettyPrinter-Tests'
#'Microdown-HTMLExporter'
#'Microdown-HTMLExporter-Tests'
#'Microdown-LaTeXExporter'
#'Microdown-LaTeXExporter-Tests'
#'Microdown-Transformer'
#'Microdown-Transformer-Tests');
group: 'All' with: #('Core' 'Tests' 'Extensions' 'Microdown-Pharo-Tools' 'RichText') ]
" ]
]

{ #category : 'external projects' }
Expand Down
68 changes: 68 additions & 0 deletions src/Microdown-ParentChildrenChecker/ParentChildrenChecker.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Class {
#name : 'ParentChildrenChecker',
#superclass : 'MicrodownVisitor',
#instVars : [
'childrenList',
'parentsList'
],
#category : 'Microdown-ParentChildrenChecker',
#package : 'Microdown-ParentChildrenChecker'
}

{ #category : 'visiting main API' }
ParentChildrenChecker >> addChild: aChild [
childrenList add: aChild .
]

{ #category : 'visiting main API' }
ParentChildrenChecker >> addParent: aParent [
parentsList add: aParent .
]

{ #category : 'visiting main API' }
ParentChildrenChecker >> childrenList [
^ childrenList
]

{ #category : 'visiting main API' }
ParentChildrenChecker >> childrenList: anObject [
childrenList := anObject
]

{ #category : 'visiting main API' }
ParentChildrenChecker >> initialize [
super initialize.
childrenList := OrderedCollection new.
parentsList := OrderedCollection new.
]

{ #category : 'visiting main API' }
ParentChildrenChecker >> parentsList [
^ parentsList
]

{ #category : 'visiting main API' }
ParentChildrenChecker >> parentsList: anObject [
parentsList := anObject
]

{ #category : 'visiting main API' }
ParentChildrenChecker >> visit: anElement [ [
"Check if the parent of the element correctly includes this element as a child"
anElement parent ifNotNil: [
(anElement parent children includes: anElement) ifFalse: [
"Instead of adding an error, add to parentsList"
self addParent: anElement parent
]
].

"Visit each child and check if it correctly points back to its parent"
anElement children do: [ :child |
child parent = anElement ifFalse: [
"Instead of adding an error, add to childrenList"
self addChild: child
].
self visit: child
].
]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Class {
#name : 'ParentChildrenCheckerTest',
#superclass : 'TestCase',
#instVars : [
'checker'
],
#category : 'Microdown-ParentChildrenChecker',
#package : 'Microdown-ParentChildrenChecker'
}

{ #category : 'running' }
ParentChildrenCheckerTest >> setUp [
checker := ParentChildrenChecker new.

]

{ #category : 'tests' }
ParentChildrenCheckerTest >> testAddChild [
"Test adding a child to the childrenList."
checker addChild: #child1.
self assert: (checker childrenList size = 1).
self assert: (checker childrenList includes: #child1).

]

{ #category : 'tests' }
ParentChildrenCheckerTest >> testAddParent [
"Test adding a parent to the parentsList."
checker addParent: #parent1.
self assert: (checker parentsList size = 1).
self assert: (checker parentsList includes: #parent1).
]

{ #category : 'running' }
ParentChildrenCheckerTest >> testInitialization [
"Test that collections are initialized properly and are empty."
self assert: checker childrenList isEmpty.
self assert: checker parentsList isEmpty.

]

{ #category : 'tests' }
ParentChildrenCheckerTest >> testMultipleAdds [
"Test adding multiple different children and parents."
checker addChild: #child1.
checker addChild: #child2.
checker addParent: #parent1.
checker addParent: #parent2.
self assert: (checker childrenList size = 2).
self assert: (checker parentsList size = 2).
self assert: (checker childrenList includes: #child1).
self assert: (checker childrenList includes: #child2).
self assert: (checker parentsList includes: #parent1).
self assert: (checker parentsList includes: #parent2).

]
1 change: 1 addition & 0 deletions src/Microdown-ParentChildrenChecker/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : 'Microdown-ParentChildrenChecker' }
94 changes: 94 additions & 0 deletions src/Microdown-ReferenceChecker/MicReferenceChecker.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"
I'm a little tool that checks whether a document has (1) references to undeclared anchors or (2) duplicated anchors.

For now Microdown is file agnostic so this is impossible to report in which file such duplication or reference occurs.
Once file support is introduced I should be revised to report better information to the user.
"
Class {
#name : 'MicReferenceChecker',
#superclass : 'MicrodownVisitor',
#instVars : [
'references',
'anchors',
'duplicatedAnchors'
],
#category : 'Microdown-ReferenceChecker',
#package : 'Microdown-ReferenceChecker'
}

{ #category : 'reporting' }
MicReferenceChecker >> duplicatedAnchors [

^ duplicatedAnchors
]

{ #category : 'visiting' }
MicReferenceChecker >> handleAnchorOf: anElement [

anElement hasAnchor ifTrue: [
(anchors includes: anElement anchor)
ifTrue: [ duplicatedAnchors add: anElement anchor ]
ifFalse: [ anchors add: anElement anchor ] ]

"Pay attention if we want to report all the occurrences of the
anchor definition we would have to count the one already included
in anchors - Imagine that you have File1/anc1 File2/anc1 and File3/anc1
the reporting should be anc1 is File1, File2 and File3"
]

{ #category : 'initialization' }
MicReferenceChecker >> initialize [

super initialize.
references := OrderedCollection new.
anchors := OrderedCollection new.
duplicatedAnchors := OrderedCollection new
]

{ #category : 'testing' }
MicReferenceChecker >> isOk [

^ duplicatedAnchors isEmpty and: [
references allSatisfy: [ :each | anchors includes: each ] ]
]

{ #category : 'reporting' }
MicReferenceChecker >> unknownAnchors [

| unk |
unk := references copy.
anchors do: [ :each |
unk remove: each ifAbsent: [ ] ].
^ unk
]

{ #category : 'visiting' }
MicReferenceChecker >> visitAnchor: anAnchor [

(anchors includes: anAnchor label)
ifTrue: [
duplicatedAnchors add: anAnchor label
"Pay attention if we want to report all the occurrences of the
anchor definition we would have to count the one already included
in anchors - Imagine that you have File1/anc1 File2/anc1 and File3/anc1
the reporting should be anc1 is File1, File2 and File3" ]
ifFalse: [ anchors add: anAnchor label ]
]

{ #category : 'visiting' }
MicReferenceChecker >> visitAnchorReference: anAnchorReference [

references add: anAnchorReference bodyString
]

{ #category : 'visiting' }
MicReferenceChecker >> visitFigure: aFigure [

self handleAnchorOf: aFigure
]

{ #category : 'visiting' }
MicReferenceChecker >> visitMath: aMathEnv [

self handleAnchorOf: aMathEnv
]
Loading
Loading