Skip to content

Commit

Permalink
Merge pull request #20 from sarahtathy/sarah-branch
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
Ducasse authored Aug 8, 2024
2 parents 25638f4 + bd95e86 commit 4a3c0ff
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 25 deletions.
3 changes: 1 addition & 2 deletions src/MicroEd-Spec/MDEditorPresenter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ MDEditorPresenter >> saveFile [
"SAVE button is pressed: save file, then print message if actually saved (if no cancel)"

self mdFile: (self mdFile
save: textInput text asString
withExtension: syntaxState extension).
save: textInput text asString ).
self window ifNotNil: [self updateTitle].

]
Expand Down
77 changes: 64 additions & 13 deletions src/MicroEd-Tests/MDFileTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ Class {
#instVars : [
'memoryFS',
'fileExample',
'fileWithoutFileReference',
'toDelete',
'fileReferenceWithName'
'fileReferenceWithName',
'mdfile',
'parser'
],
#category : 'MicroEd-Tests',
#package : 'MicroEd-Tests'
Expand All @@ -18,13 +19,15 @@ MDFileTest >> setUp [
super setUp.
memoryFS := FileSystem memory.
toDelete := OrderedCollection new.
fileWithoutFileReference := MDFile new.
mdfile := MDFile new.
fileExample := memoryFS / 'example.md'.
fileReferenceWithName := (memoryFS / 'anotherFile.md')
ensureCreateFile.
toDelete add: fileExample.
toDelete add: fileExample.
fileExample writeStreamDo: [ :stream |
stream nextPutAll: 'example file' ]
stream nextPutAll: 'example file' ].

parser := MicrodownParser new.
]

{ #category : 'running' }
Expand All @@ -39,30 +42,29 @@ MDFileTest >> tearDown [
{ #category : 'running' }
MDFileTest >> testMDFileCreationWithExistingFile [

| file |
file := MDFile new.
file initializeWithFileReference: fileExample.
self assert: file hasFile.
mdfile initializeWithFileReference: fileExample.
self assert: mdfile hasFile.

]

{ #category : 'running' }
MDFileTest >> testMDFileCreationWithoutExistingFile [

| fileWithoutFileReference |
fileWithoutFileReference := MDFile new.
self assert: fileWithoutFileReference hasFile not.
]

{ #category : 'running' }
MDFileTest >> testMDFileExistingFileWithName [


| file fileReferenceWithoutName |
| fileReferenceWithoutName |

fileReferenceWithoutName := (memoryFS / 'name.md') ensureCreateFile.
toDelete add: fileReferenceWithoutName.
file := MDFile new.
file initializeWithFileReference: fileReferenceWithoutName.
self assert: (file basename endsWith: 'name.md').
mdfile initializeWithFileReference: fileReferenceWithoutName.
self assert: (mdfile basename endsWith: 'name.md').


]
Expand All @@ -80,6 +82,55 @@ MDFileTest >> testMDFileExistingFileWithoutName [

]

{ #category : 'running' }
MDFileTest >> testMDFileHasFileReferenceAddMetaData [

| root file |

file := memoryFS / 'test2'.
file ensureCreateFile.
mdfile initializeWithFileReference: file.
mdfile addMetaDataElements.

root := MicrodownParser parse: mdfile contents.
self assert: root hasMetaDataElement.


]

{ #category : 'running' }
MDFileTest >> testMDFileHasFileReferenceHasMetaData [

| root file |

file := memoryFS / 'test2'.
file ensureCreateFile.
mdfile initializeWithFileReference: file.
root := MicrodownParser parse: mdfile fileReference contents.
mdfile addMetaDataElements.

root := MicrodownParser parse: mdfile contents.
self assert: root hasMetaDataElement.

mdfile fileReference delete.


]

{ #category : 'running' }
MDFileTest >> testMDFileHasFileReferenceHasNotMetaData [

| root file |

file := memoryFS / 'test3'.
file ensureCreateFile.
mdfile initializeWithFileReference: file.
root := MicrodownParser parse: mdfile contents.
self deny: root hasMetaDataElement.


]

{ #category : 'running' }
MDFileTest >> testMDFileHasFileReferenceWithFormatISO [

Expand Down
55 changes: 45 additions & 10 deletions src/MicroEd/MDFile.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Class {
#name : 'MDFile',
#superclass : 'Object',
#instVars : [
'fileReference'
'fileReference',
'nameOfFileReferenceBeforeSaved'
],
#category : 'MicroEd',
#package : 'MicroEd'
Expand All @@ -19,18 +20,45 @@ MDFile class >> newFromFileReference: aFileReference [
yourself
]

{ #category : 'add' }
MDFile >> addMetaDataElements [

| root |
root := MicrodownParser parse: fileReference contents.

root hasMetaDataElement
ifFalse: [
root := MicrodownParser parse: '{
"author" : "addos"
}
'.
root metaDataElement
atKey: 'author' put: (fileReference entry reference path at: 3);
atKey: 'title' put: nameOfFileReferenceBeforeSaved;
atKey: 'lastModificationDate' put: fileReference modificationTime asDate yyyymmdd ]
ifTrue: [
root metaDataElement
atKey: 'lastModificationDate'
put: fileReference modificationTime asDate yyyymmdd].

self visit: root.
]

{ #category : 'accessing' }
MDFile >> basename [
"Answer a String representing the receiver's file name"

| dateCreationOfFileReference |
fileReference ifNil: [ ^ 'Untitled.md' ].
fileReference ifNil: [
nameOfFileReferenceBeforeSaved := 'Untitled'.
^ 'Untitled' ].

nameOfFileReferenceBeforeSaved := fileReference basename.
dateCreationOfFileReference := DateAndTime now asDate yyyymmdd.
(fileReference basename includesSubstring:
dateCreationOfFileReference) ifFalse: [
^ dateCreationOfFileReference , fileReference basename ].
^ fileReference basename

^ dateCreationOfFileReference , '_' , fileReference basename ].
^ fileReference basename
]

{ #category : 'accessing' }
Expand Down Expand Up @@ -95,15 +123,22 @@ MDFile >> initializeWithFileReference: aFileReference [
]

{ #category : 'accessing' }
MDFile >> save: aString withExtension: anExtension [
" before put the file reference, ensure that the creation of the basename is set (without the renaming)
fileReference extension = anExtension asString ifFalse: [
fileReference renameTo:
self basename , '.' , anExtension asString ]. "
MDFile >> save: aString [

fileReference asFileReference writeStreamDo: [ :str |
str
nextPutAll: aString ].



]

{ #category : 'add' }
MDFile >> visit: aRoot [

| visitor |
visitor := MicTextualMicrodownExporter new.
visitor visit: aRoot.
fileReference writeStreamDo: [ :stream |
stream nextPutAll: visitor contents ]
]

0 comments on commit 4a3c0ff

Please sign in to comment.