diff --git a/src/Microdown-ParentChildrenChecker/MicParentChildrenChecker.class.st b/src/Microdown-ParentChildrenChecker/MicParentChildrenChecker.class.st new file mode 100644 index 00000000..fd79c21c --- /dev/null +++ b/src/Microdown-ParentChildrenChecker/MicParentChildrenChecker.class.st @@ -0,0 +1,54 @@ +Class { + #name : 'MicParentChildrenChecker', + #superclass : 'Object', + #instVars : [ + 'orphanList', + 'confusedKids' + ], + #category : 'Microdown-ParentChildrenChecker', + #package : 'Microdown-ParentChildrenChecker' +} + +{ #category : 'visiting main API' } +MicParentChildrenChecker >> check: 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 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 ] ]. + + anElement children do: [ :each | self check: each ] ] +] + +{ #category : 'accessing' } +MicParentChildrenChecker >> confusedKids [ + + ^ confusedKids +] + +{ #category : 'visiting main API' } +MicParentChildrenChecker >> initialize [ + + super initialize. + orphanList := OrderedCollection new. + confusedKids := OrderedCollection new +] + +{ #category : 'testing' } +MicParentChildrenChecker >> isOk [ + + ^ confusedKids isEmpty and: + (orphanList isEmpty and: [ confusedKids isEmpty ]) +] + +{ #category : 'accessing' } +MicParentChildrenChecker >> orphanList [ + ^orphanList +] diff --git a/src/Microdown-ParentChildrenChecker/MicParentChildrenCheckerTest.class.st b/src/Microdown-ParentChildrenChecker/MicParentChildrenCheckerTest.class.st new file mode 100644 index 00000000..09758be0 --- /dev/null +++ b/src/Microdown-ParentChildrenChecker/MicParentChildrenCheckerTest.class.st @@ -0,0 +1,64 @@ +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 [ + + | checker | + checker := MicParentChildrenChecker new. + checker check: self document. + self assert: checker 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 check: 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). - -]