Skip to content

Commit dc21b73

Browse files
committed
Green tests for MDFileTest
1 parent f34f722 commit dc21b73

File tree

2 files changed

+55
-81
lines changed

2 files changed

+55
-81
lines changed

src/MicroEd-Tests/MDFileTest.class.st

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ MDFileTest >> setUp [
1919
| fileNamer |
2020
super setUp.
2121
memoryFS := FileSystem memory.
22-
22+
2323
mdFollowingConventionFile := MDFile new.
2424
fileNamer := mdFollowingConventionFile fileNamer.
25-
fileNamer date:
26-
(Date readFrom: '2024-11-02' readStream pattern: 'y-m-d').
25+
fileNamer date: (Date readFrom: '2024-11-02' readStream pattern: 'y-m-d').
2726

28-
fileReferenceFollowingConventions := memoryFS / (mdFollowingConventionFile fileNamer newNameBasedOn: 'example').
27+
fileReferenceFollowingConventions := memoryFS
28+
/
29+
(mdFollowingConventionFile
30+
fileNamer newNameBasedOn:
31+
'example').
2932

3033
fileReferenceFollowingConventions writeStreamDo: [ :stream |
3134
stream nextPutAll: 'example file' ].
@@ -38,11 +41,13 @@ MDFileTest >> setUp [
3841

3942
{ #category : 'tests - contents' }
4043
MDFileTest >> testContentsIsSaved [
41-
42-
self assert: fileReferenceFollowingConventions contents equals: 'example file'.
44+
45+
self
46+
assert: fileReferenceFollowingConventions contents
47+
equals: 'example file'.
4348

4449
self
45-
assert: (memoryFS / 'example.md') contents
50+
assert: (memoryFS / '2024-11-02_example.md') contents
4651
equals: 'example file'
4752
]
4853

@@ -68,65 +73,33 @@ MDFileTest >> testMDFileCreationWithoutExistingFile [
6873

6974
| fileWithoutFileReference |
7075
fileWithoutFileReference := MDFile new.
71-
self assert: fileWithoutFileReference hasFile not.
76+
self deny: fileWithoutFileReference hasFile
7277
]
7378

7479
{ #category : 'tests' }
7580
MDFileTest >> testMDFileExistingFileWithName [
7681

7782
| fileReferenceWithoutName |
7883
fileReferenceWithoutName := (memoryFS / 'name.md') ensureCreateFile.
79-
mdFollowingConventionFile initializeWithFileReference:
80-
fileReferenceWithoutName.
81-
self assert: (mdFollowingConventionFile basename endsWith: 'name.md')
82-
]
83-
84-
{ #category : 'tests' }
85-
MDFileTest >> testMDFileExistingFileWithoutName [
86-
87-
| file fileReferenceWithoutName |
88-
89-
fileReferenceWithoutName := (memoryFS / '.md') ensureCreateFile.
90-
toDelete add: fileReferenceWithoutName.
91-
file := MDFile new.
92-
file initializeWithFileReference: fileReferenceWithoutName.
93-
self denyEmpty: file basename.
94-
84+
mdFollowingConventionFile initializeWithFileReference: fileReferenceWithoutName.
85+
self assert: (mdFollowingConventionFile fileReference basename endsWith: 'name.md')
9586
]
9687

9788
{ #category : 'tests' }
9889
MDFileTest >> testMDFileHasFileReferenceAddMetaData [
9990

10091
| root file |
101-
10292
file := memoryFS / 'test2'.
103-
file ensureCreateFile.
104-
mdFollowingConventionFile initializeWithFileReference: file.
105-
mdFollowingConventionFile addMetaDataElements.
106-
107-
root := MicrodownParser parse: mdFollowingConventionFile contents.
108-
self assert: root hasMetaDataElement.
109-
110-
111-
]
112-
113-
{ #category : 'tests' }
114-
MDFileTest >> testMDFileHasFileReferenceHasMetaData [
93+
file ensureCreateFile.
94+
mdFollowingConventionFile initializeWithFileReference: file.
95+
mdFollowingConventionFile addMetaDataElements.
11596

116-
| root file |
117-
118-
file := memoryFS / 'test2'.
119-
file ensureCreateFile.
120-
mdFollowingConventionFile initializeWithFileReference: file.
121-
root := MicrodownParser parse: mdFollowingConventionFile fileReference contents.
122-
mdFollowingConventionFile addMetaDataElements.
123-
12497
root := MicrodownParser parse: mdFollowingConventionFile contents.
12598
self assert: root hasMetaDataElement.
126-
127-
mdFollowingConventionFile fileReference delete.
128-
129-
99+
self
100+
assert: (root metaDataElement atKey: 'title')
101+
equals: 'unnamed'.
102+
130103
]
131104

132105
{ #category : 'tests' }
@@ -146,11 +119,11 @@ MDFileTest >> testMDFileHasFileReferenceHasNotMetaData [
146119
{ #category : 'tests' }
147120
MDFileTest >> testMDFileHasFileReferenceWithFormatISO [
148121

149-
| file date |
150-
file := MDFile newFromFileReference: fileReferenceWithName.
151-
date := Date today yyyymmdd.
152-
self assert: (file basename includesSubstring: date).
153-
self deny: fileReferenceWithName exists
122+
| file |
123+
file := MDFile new.
124+
file fileNamer date: (Date newDay: 21 month: 9 year: 1969).
125+
self assert: file fileReference basename equals: '1969-09-21_unnamed.md'.
126+
154127
]
155128

156129
{ #category : 'tests - title' }

src/MicroEd/MDFile.class.st

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,36 @@ MDFile class >> newFromFileReference: aFileReference [
3131
{ #category : 'add' }
3232
MDFile >> addMetaDataElements [
3333

34-
| root |
35-
root := MicrodownParser parse: fileReference contents.
34+
| root newDoc |
35+
36+
root := MicrodownParser parse: self fileReference contents.
3637

3738
root hasMetaDataElement
3839
ifFalse: [
39-
root := MicrodownParser parse: '{ "author" : "addos" }'.
40+
"pay attention MD is line based. so the } should be on another line."
41+
newDoc := MicrodownParser parse: '{
42+
"author" : "addos"
43+
}'.
44+
root addChild: newDoc children first.
45+
"yes the metadata element in the first one.
46+
The API of Microdown could be better."
4047
root metaDataElement
41-
atKey: 'author' put: (fileReference entry reference path at: 3);
42-
atKey: 'title' put: nameOfFileReferenceBeforeSaved;
48+
atKey: 'author'
49+
put: 'toto for now';
50+
51+
atKey: 'title'
52+
put: self title;
53+
4354
atKey: 'lastModificationDate'
44-
put: fileReference modificationTime asDate yyyymmdd ]
55+
put: self fileReference modificationTime asDate yyyymmdd ]
4556
ifTrue: [
4657
root metaDataElement
4758
atKey: 'lastModificationDate'
48-
put: fileReference modificationTime asDate yyyymmdd ].
59+
put: self fileReference modificationTime asDate yyyymmdd ].
4960

5061
self writeFileContentsOf: root
5162
]
5263

53-
{ #category : 'accessing' }
54-
MDFile >> basename [
55-
"Answer a String representing the receiver's file name"
56-
57-
| dateCreationOfFileReference |
58-
fileReference ifNil: [
59-
nameOfFileReferenceBeforeSaved := 'Untitled'.
60-
^ nameOfFileReferenceBeforeSaved ].
61-
62-
nameOfFileReferenceBeforeSaved := fileReference basename.
63-
dateCreationOfFileReference := Date today yyyymmdd.
64-
(fileReference basename includesSubstring: dateCreationOfFileReference)
65-
ifFalse: [ ^ dateCreationOfFileReference , '_' , fileReference basename ].
66-
^ fileReference basename
67-
]
68-
6964
{ #category : 'accessing' }
7065
MDFile >> contents [
7166

@@ -82,13 +77,13 @@ MDFile >> contents: aContent [
8277
{ #category : 'testing' }
8378
MDFile >> exists [
8479

85-
^ fileReference exists
80+
^ self fileReference exists
8681
]
8782

8883
{ #category : 'accessing' }
8984
MDFile >> extension [
9085

91-
^ fileReference extension
86+
^ self fileReference extension
9287
]
9388

9489
{ #category : 'accessing' }
@@ -121,6 +116,12 @@ MDFile >> fileReference: aFileReference [
121116
fileName := aFileReference basenameWithoutExtension.
122117
]
123118

119+
{ #category : 'testing' }
120+
MDFile >> hasFile [
121+
122+
^ fileReference isNotNil
123+
]
124+
124125
{ #category : 'testing' }
125126
MDFile >> hasTitle [
126127

@@ -143,8 +144,8 @@ MDFile >> initializeWithFileReference: aFileReference [
143144
fileReference := aFileReference.
144145
"Create of a new file reference which has the ISO format with the default extension: 'md' "
145146
newFileReference := fileReference extension isEmptyOrNil
146-
ifTrue: [ (self basename , '.md') ]
147-
ifFalse: [ self basename ].
147+
ifTrue: [ (fileReference basename , '.md') ]
148+
ifFalse: [ fileReference basename ].
148149
aFileReference delete.
149150

150151
fileReference := newFileReference asFileReference.

0 commit comments

Comments
 (0)