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

Issue 810 About Duplicater Anchor #890

Merged
merged 1 commit into from
Sep 30, 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
36 changes: 18 additions & 18 deletions src/Microdown-BookTester-Tests/MicReferenceCheckerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -494,38 +494,38 @@ MicReferenceCheckerTest >> testDuplicatedAnchorInDifferentFilesOfTheSameDir [
visitor checkDirectory: dir.
self deny: visitor isOkay.

self assert: visitor duplicatedAnchors size equals: 1.
self assert: visitor duplicatedAnchors size equals: 2.
self
assert: visitor duplicatedAnchors first anchorLabel
equals: 'ancS1'.
dict := visitor results groupedBy: [ :each | each class ].
duplicated := (dict at: MicDuplicatedAnchorResult) first.
self assert: visitor results first sourceFileReference fullName equals: '/myDirectory/file2.md'.
self
assert: visitor results first sourceFileReference fullName
equals: '/myDirectory/file2.md'.
self assert: duplicated anchorLabel equals: 'ancS1'



]

{ #category : 'tests - duplicated' }
MicReferenceCheckerTest >> testDuplicatedAnchors [

| defAncS0TripleAncS1RefAncS1AncS0 checker dict dup1 |
defAncS0TripleAncS1RefAncS1AncS0 := self defAncS0TripleAncS1RefAncS1AncS0.
defAncS0TripleAncS1RefAncS1AncS0 := self
defAncS0TripleAncS1RefAncS1AncS0.
checker := MicReferenceChecker new.
checker checkList: { defAncS0TripleAncS1RefAncS1AncS0 }.
self deny: checker isOkay.
self
assert: (checker results collect: [ :each | each anchorLabel ])
equals: OrderedCollection <- #( 'ancS1' 'ancS1' ).
equals: OrderedCollection <- #( 'ancS1' 'ancS1' 'ancS1' ).

dict := checker results groupedBy: [ :each | each class ].

dup1 := (dict at: MicDuplicatedAnchorResult) first.
self assert: dup1 sourceFileReference fullName equals: '/myDirectory/defAncS0TripleAncS1RefAncS1AncS0.md'.
self assert: dup1 anchorLabel equals: 'ancS1'.


self
assert: dup1 sourceFileReference fullName
equals: '/myDirectory/defAncS0TripleAncS1RefAncS1AncS0.md'.
self assert: dup1 anchorLabel equals: 'ancS1'
]

{ #category : 'tests - duplicated' }
Expand All @@ -538,7 +538,7 @@ MicReferenceCheckerTest >> testDuplicatedBetweenSectionFigureEq [
self deny: visitor isOkay.
self
assert: (visitor results collect: [ :each | each anchorLabel ])
equals: OrderedCollection <- #( 'ancS1' 'ancS0' 'fig2' )
equals: OrderedCollection <- #( 'ancS1' 'ancS1' 'ancS0' 'ancS0' 'fig2' 'fig2' )
]

{ #category : 'tests - duplicated' }
Expand All @@ -552,7 +552,7 @@ MicReferenceCheckerTest >> testDuplicatedBetweenSectionFigureEqInDifferentFile [
self deny: visitor isOkay.
self
assert: (visitor results collect: [ :each | each anchorLabel ])
equals: OrderedCollection <- #( 'ancS1' 'ancS0' 'fig2' )
equals: OrderedCollection <- #( 'ancS1' 'ancS1' 'ancS0' 'ancS0' 'fig2' 'fig2')
]

{ #category : 'tests - duplicated' }
Expand All @@ -565,7 +565,7 @@ MicReferenceCheckerTest >> testDuplicatedFigures [
self deny: checker isOkay.
self
assert: (checker results collect: [ :each | each anchorLabel ])
equals: OrderedCollection <- #( 'fig1' 'fig1' 'fig1' 'ancS1' ).
equals: OrderedCollection <- #( 'fig1' 'fig1' 'fig1' 'fig1' 'ancS1' ).

dict := checker results groupedBy: [ :each | each class ].

Expand All @@ -589,15 +589,15 @@ MicReferenceCheckerTest >> testDuplicatedMaths [
equals: defAnCS0DoubleEq2DoubleEq1RefEq1 fullName.
self
assert: (checker results collect: [ :each | each anchorLabel ])
equals: OrderedCollection <- #( 'Eq2' 'Eq1' ).
equals: OrderedCollection <- #( 'Eq2' 'Eq2' 'Eq1' 'Eq1' ).

dict := checker results groupedBy: [ :each | each class ].

dup1 := (dict at: MicDuplicatedAnchorResult) first.
self assert: dup1 sourceFileReference fullName equals: '/myDirectory/defAnCS0DoubleEq2DoubleEq1RefEq1.md'.
self assert: dup1 anchorLabel equals: 'Eq2'.

dup2 := (dict at: MicDuplicatedAnchorResult) second.
dup2 := (dict at: MicDuplicatedAnchorResult) third.
self assert: dup2 sourceFileReference fullName equals: '/myDirectory/defAnCS0DoubleEq2DoubleEq1RefEq1.md'.
self assert: dup2 anchorLabel equals: 'Eq1'.
]
Expand Down
29 changes: 20 additions & 9 deletions src/Microdown-BookTester/MicReferenceChecker.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ MicReferenceChecker >> addDuplicatedAnchor: anAnchor [
results add: micResultInstance
]

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

anchors do: [ :each |
each anchorLabel = anAnchor anchorLabel ifTrue: [
(duplicatedAnchors includes: each) ifFalse: [
duplicatedAnchors add: each.
self addDuplicatedAnchor: each ] ] ]
]

{ #category : 'main API' }
MicReferenceChecker >> checkDirectory: aDir [
"Take the directory, parse all its children with microdown file parser and let the visitor visit each time then return visitor is ok which should be true if every thing is okay, the visitor turned out to treat the many documents that it visits as one, so if anchor is duplicated in another file it will detect that . "
Expand Down Expand Up @@ -97,9 +107,10 @@ MicReferenceChecker >> collectBadReferences [
" should be called just after all the docs are visited otherwise the result can be wrong"

| badReference existingAnchorNames |
existingAnchorNames := (anchors collect: [ :each | each anchorLabel ]).
existingAnchorNames := anchors collect: [ :each | each anchorLabel ].
badReference := references reject: [ :anchorReference |
existingAnchorNames includes: anchorReference anchorLabel ].
existingAnchorNames includes:
anchorReference anchorLabel ].
badReference do: [ :each | self addBadReferenceAnchor: each ]
]

Expand All @@ -119,12 +130,11 @@ MicReferenceChecker >> fileSystem: aFileSystem [
MicReferenceChecker >> handleAnchorOf: anElement [

anElement hasAnchor ifFalse: [ ^ self ].
(self hasAlreadyDefinedAs: anElement)
ifTrue: [
duplicatedAnchors add: anElement.
self addDuplicatedAnchor: anElement ].
anchors add: anElement

(self hasAlreadyDefinedAs: anElement) ifTrue: [
duplicatedAnchors add: anElement.
self addDuplicatedAnchor: anElement.
self addDuplicatedFirstAnchor: anElement. ].
anchors add: anElement
]

{ #category : 'internal' }
Expand Down Expand Up @@ -241,7 +251,8 @@ MicReferenceChecker >> visitAnchor: anAnchor [
isAlready := self hasAlreadyDefinedAs: anAnchor.
isAlready ifTrue: [
duplicatedAnchors add: anAnchor.
self addDuplicatedAnchor: anAnchor ].
self addDuplicatedAnchor: anAnchor.
self addDuplicatedFirstAnchor: anAnchor. ].
anchors add: anAnchor
]

Expand Down
Loading