From b31a218a1c964cf90dc37858c654a91392c25e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phaneDucasse?= Date: Fri, 3 May 2024 18:01:16 +0200 Subject: [PATCH 1/2] new version of the parent child checker --- .../MicParentChildrenChecker.class.st | 94 +++++++++++++++++++ .../MicParentChildrenCheckerTest.class.st | 69 ++++++++++++++ .../ParentChildrenChecker.class.st | 68 -------------- .../ParentChildrenCheckerTest.class.st | 56 ----------- 4 files changed, 163 insertions(+), 124 deletions(-) create mode 100644 src/Microdown-ParentChildrenChecker/MicParentChildrenChecker.class.st create mode 100644 src/Microdown-ParentChildrenChecker/MicParentChildrenCheckerTest.class.st delete mode 100644 src/Microdown-ParentChildrenChecker/ParentChildrenChecker.class.st delete mode 100644 src/Microdown-ParentChildrenChecker/ParentChildrenCheckerTest.class.st diff --git a/src/Microdown-ParentChildrenChecker/MicParentChildrenChecker.class.st b/src/Microdown-ParentChildrenChecker/MicParentChildrenChecker.class.st new file mode 100644 index 00000000..a6d231dd --- /dev/null +++ b/src/Microdown-ParentChildrenChecker/MicParentChildrenChecker.class.st @@ -0,0 +1,94 @@ +Class { + #name : 'MicParentChildrenChecker', + #superclass : 'MicrodownVisitor', + #instVars : [ + 'orphanList', + 'badReferencingParentList', + 'confusedKids' + ], + #category : 'Microdown-ParentChildrenChecker', + #package : 'Microdown-ParentChildrenChecker' +} + +{ #category : 'visiting main API' } +MicParentChildrenChecker >> addChild: aChild [ + orphanList add: aChild . +] + +{ #category : 'visiting main API' } +MicParentChildrenChecker >> addParent: aParent [ + badReferencingParentList add: aParent . +] + +{ #category : 'testing' } +MicParentChildrenChecker >> badReferencingParentList [ + + ^ badReferencingParentList +] + +{ #category : 'visiting main API' } +MicParentChildrenChecker >> childrenList [ + ^ orphanList +] + +{ #category : 'visiting main API' } +MicParentChildrenChecker >> childrenList: anObject [ + orphanList := anObject +] + +{ #category : 'testing' } +MicParentChildrenChecker >> confusedKids [ + + ^ confusedKids +] + +{ #category : 'visiting main API' } +MicParentChildrenChecker >> initialize [ + + super initialize. + orphanList := OrderedCollection new. + badReferencingParentList := OrderedCollection new. + confusedKids := OrderedCollection new +] + +{ #category : 'testing' } +MicParentChildrenChecker >> isOk [ + + ^ badReferencingParentList isEmpty and: + ( orphanList isEmpty and: [ confusedKids isEmpty ]) +] + +{ #category : 'testing' } +MicParentChildrenChecker >> orphanList [ + ^orphanList +] + +{ #category : 'visiting main API' } +MicParentChildrenChecker >> parentsList [ + ^ badReferencingParentList +] + +{ #category : 'visiting main API' } +MicParentChildrenChecker >> parentsList: anObject [ + badReferencingParentList := anObject +] + +{ #category : 'visiting main API' } +MicParentChildrenChecker >> visit: anElement [ + "Check if the parent of the element correctly includes this element as a child" + + anElement parent + ifNil: [ + anElement class = MicRootBlock + ifFalse: [ orphanList add: anElement ] + ifTrue: [ anElement children do: [ :each | self visit: each ] ] ] + ifNotNil: [ :p | + "We cannot identify bad parent that are refered by child not in the children + list, because by construction the algo only considers the children of an element). + (p children includes: anElement) ifFalse: [ self addParent: p ]." + + p children do: [ :child | + child parent = p ifFalse: [ confusedKids addChild: child ] ]. + + anElement children do: [ :each | self visit: each ] ] +] diff --git a/src/Microdown-ParentChildrenChecker/MicParentChildrenCheckerTest.class.st b/src/Microdown-ParentChildrenChecker/MicParentChildrenCheckerTest.class.st new file mode 100644 index 00000000..fb1e35be --- /dev/null +++ b/src/Microdown-ParentChildrenChecker/MicParentChildrenCheckerTest.class.st @@ -0,0 +1,69 @@ +Class { + #name : 'MicParentChildrenCheckerTest', + #superclass : 'TestCase', + #category : 'Microdown-ParentChildrenChecker', + #package : 'Microdown-ParentChildrenChecker' +} + +{ #category : 'tests' } +MicParentChildrenCheckerTest >> document [ + ^ Microdown parse: '#Microdown is quite cool + +Here is some code + +```language=Pharo&caption=Beautiful&anchor=Fig1 + 1000 factorial / 999 factorial +``` + +Here is a figure and a link: [http://pharo.org](http://pharo.org). + +![Pharologo](https://files.pharo.org/media/logo/logo.png size=80&anchor=figLogo.) + + + +Here is a list: +- item 1 + 1. sub item 1 + 3. sub item 2 +- item 2 + + +**Bold**, _italic_, `monospace` + +In Pharo, Microdown supports hyperlinks to: +- classes e.g., `Point`, +- methodes e.g., `Point class`, `Point>>#setX:setY:`, and +- packages e.g., `#''Microdown-Tests''` (for packages). + +You can edit this file clicking on `ClySyntaxHelpMorph>>#rawMicrodownSyntax`.'. + +] + +{ #category : 'tests' } +MicParentChildrenCheckerTest >> testSimpleDocumentIsWellFormed [ + + | visitor | + visitor := MicParentChildrenChecker new. + self document accept: visitor. + self assert: visitor isOk. + + + + + +] + +{ #category : 'tests' } +MicParentChildrenCheckerTest >> testSimpleDocumentWithOrphans [ + + | brokenDocument visitor orphan | + visitor := MicParentChildrenChecker new. + brokenDocument := Microdown parse: '# Microdown is quite cool'. + orphan := brokenDocument children first children first. + orphan basicParent: nil. + self assert: orphan parent isNil. + + visitor visit: brokenDocument. + + self deny: visitor isOk +] diff --git a/src/Microdown-ParentChildrenChecker/ParentChildrenChecker.class.st b/src/Microdown-ParentChildrenChecker/ParentChildrenChecker.class.st deleted file mode 100644 index 63859456..00000000 --- a/src/Microdown-ParentChildrenChecker/ParentChildrenChecker.class.st +++ /dev/null @@ -1,68 +0,0 @@ -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 - ]. -] -] diff --git a/src/Microdown-ParentChildrenChecker/ParentChildrenCheckerTest.class.st b/src/Microdown-ParentChildrenChecker/ParentChildrenCheckerTest.class.st deleted file mode 100644 index 4cbceef9..00000000 --- a/src/Microdown-ParentChildrenChecker/ParentChildrenCheckerTest.class.st +++ /dev/null @@ -1,56 +0,0 @@ -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). - -] From d1af987fdcac5b91ee2bf0b161b11d297f658f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phaneDucasse?= Date: Fri, 3 May 2024 18:13:11 +0200 Subject: [PATCH 2/2] Cleaner --- .../MicParentChildrenChecker.class.st | 76 +++++-------------- .../MicParentChildrenCheckerTest.class.st | 15 ++-- 2 files changed, 23 insertions(+), 68 deletions(-) diff --git a/src/Microdown-ParentChildrenChecker/MicParentChildrenChecker.class.st b/src/Microdown-ParentChildrenChecker/MicParentChildrenChecker.class.st index a6d231dd..fd79c21c 100644 --- a/src/Microdown-ParentChildrenChecker/MicParentChildrenChecker.class.st +++ b/src/Microdown-ParentChildrenChecker/MicParentChildrenChecker.class.st @@ -1,9 +1,8 @@ Class { #name : 'MicParentChildrenChecker', - #superclass : 'MicrodownVisitor', + #superclass : 'Object', #instVars : [ 'orphanList', - 'badReferencingParentList', 'confusedKids' ], #category : 'Microdown-ParentChildrenChecker', @@ -11,32 +10,24 @@ Class { } { #category : 'visiting main API' } -MicParentChildrenChecker >> addChild: aChild [ - orphanList add: aChild . -] - -{ #category : 'visiting main API' } -MicParentChildrenChecker >> addParent: aParent [ - badReferencingParentList add: aParent . -] - -{ #category : 'testing' } -MicParentChildrenChecker >> badReferencingParentList [ - - ^ badReferencingParentList -] +MicParentChildrenChecker >> check: anElement [ + "Check if the parent of the element correctly includes this element as a child" -{ #category : 'visiting main API' } -MicParentChildrenChecker >> childrenList [ - ^ orphanList -] + anElement parent + ifNil: [ + anElement class = MicRootBlock + ifFalse: [ orphanList add: anElement ] + ifTrue: [ anElement children do: [ :each | self check: each ] ] ] + ifNotNil: [ :p | "We cannot identify bad parent that are refered by child not in the children + list, because by construction the algo only considers the children of an element). + (p children includes: anElement) ifFalse: [ self addParent: p ]." + p children do: [ :child | + child parent = p ifFalse: [ confusedKids add: child ] ]. -{ #category : 'visiting main API' } -MicParentChildrenChecker >> childrenList: anObject [ - orphanList := anObject + anElement children do: [ :each | self check: each ] ] ] -{ #category : 'testing' } +{ #category : 'accessing' } MicParentChildrenChecker >> confusedKids [ ^ confusedKids @@ -47,48 +38,17 @@ MicParentChildrenChecker >> initialize [ super initialize. orphanList := OrderedCollection new. - badReferencingParentList := OrderedCollection new. confusedKids := OrderedCollection new ] { #category : 'testing' } MicParentChildrenChecker >> isOk [ - ^ badReferencingParentList isEmpty and: - ( orphanList isEmpty and: [ confusedKids isEmpty ]) + ^ confusedKids isEmpty and: + (orphanList isEmpty and: [ confusedKids isEmpty ]) ] -{ #category : 'testing' } +{ #category : 'accessing' } MicParentChildrenChecker >> orphanList [ ^orphanList ] - -{ #category : 'visiting main API' } -MicParentChildrenChecker >> parentsList [ - ^ badReferencingParentList -] - -{ #category : 'visiting main API' } -MicParentChildrenChecker >> parentsList: anObject [ - badReferencingParentList := anObject -] - -{ #category : 'visiting main API' } -MicParentChildrenChecker >> visit: anElement [ - "Check if the parent of the element correctly includes this element as a child" - - anElement parent - ifNil: [ - anElement class = MicRootBlock - ifFalse: [ orphanList add: anElement ] - ifTrue: [ anElement children do: [ :each | self visit: each ] ] ] - ifNotNil: [ :p | - "We cannot identify bad parent that are refered by child not in the children - list, because by construction the algo only considers the children of an element). - (p children includes: anElement) ifFalse: [ self addParent: p ]." - - p children do: [ :child | - child parent = p ifFalse: [ confusedKids addChild: child ] ]. - - anElement children do: [ :each | self visit: each ] ] -] diff --git a/src/Microdown-ParentChildrenChecker/MicParentChildrenCheckerTest.class.st b/src/Microdown-ParentChildrenChecker/MicParentChildrenCheckerTest.class.st index fb1e35be..09758be0 100644 --- a/src/Microdown-ParentChildrenChecker/MicParentChildrenCheckerTest.class.st +++ b/src/Microdown-ParentChildrenChecker/MicParentChildrenCheckerTest.class.st @@ -42,15 +42,10 @@ You can edit this file clicking on `ClySyntaxHelpMorph>>#rawMicrodownSyntax`.'. { #category : 'tests' } MicParentChildrenCheckerTest >> testSimpleDocumentIsWellFormed [ - | visitor | - visitor := MicParentChildrenChecker new. - self document accept: visitor. - self assert: visitor isOk. - - - - - + | checker | + checker := MicParentChildrenChecker new. + checker check: self document. + self assert: checker isOk ] { #category : 'tests' } @@ -63,7 +58,7 @@ MicParentChildrenCheckerTest >> testSimpleDocumentWithOrphans [ orphan basicParent: nil. self assert: orphan parent isNil. - visitor visit: brokenDocument. + visitor check: brokenDocument. self deny: visitor isOk ]