Skip to content

Commit 050bd2d

Browse files
committed
MOSOrderPositionFinder: add tests, trim output of insertAtSection()
1 parent 810cf70 commit 050bd2d

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

SpeciesHelper/modules/MOSOrderPositionFinder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class MOSOrderPositionFinder {
9090
this.wikitext = this.wikitext.replace( /(\{\{(?:Short description|Shortdesc|Shortdescription|Short desc)\|[^}]+\}\}\n)\n(\{\{)/is, '$1$2' );
9191
}
9292

93-
// this.wikitext = this.wikitext.trim();
93+
this.wikitext = this.wikitext.trim() + '\n';
9494
return this.wikitext;
9595
}
9696

SpeciesHelper/tests/MOSOrderPositionFinder.test.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,70 @@ Body
7373
} );
7474
} );
7575

76+
describe( 'insertAtSection(wikicode, needle, section)', () => {
77+
test( 'Section does not exist yet', () => {
78+
const mopf = new MOSOrderPositionFinder();
79+
const wikicode =
80+
`{{Short description|test}}
81+
82+
Lead
83+
84+
== First heading ==
85+
Body
86+
`;
87+
const needle = '{{Taxonbar}}';
88+
const section = 'taxonBar';
89+
const output =
90+
`{{Short description|test}}
91+
92+
Lead
93+
94+
== First heading ==
95+
Body
96+
97+
{{Taxonbar}}
98+
`;
99+
expect( mopf.insertAtSection( wikicode, needle, section ) ).toBe( output );
100+
} );
101+
102+
test( 'Section already exists', () => {
103+
const mopf = new MOSOrderPositionFinder();
104+
const wikicode =
105+
`{{Short description|test}}
106+
{{Copy edit}}
107+
108+
Lead
109+
110+
== First heading ==
111+
Body
112+
`;
113+
const needle = '{{Close paraphrasing}}';
114+
const section = 'maintenanceTags';
115+
const output =
116+
`{{Short description|test}}
117+
118+
{{Close paraphrasing}}
119+
{{Copy edit}}
120+
121+
Lead
122+
123+
== First heading ==
124+
Body
125+
`;
126+
expect( mopf.insertAtSection( wikicode, needle, section ) ).toBe( output );
127+
} );
128+
129+
test( 'Invalid section name throws error', () => {
130+
const mopf = new MOSOrderPositionFinder();
131+
const wikicode = 'Test';
132+
const needle = '{{test}}';
133+
const section = 'invalidSection';
134+
expect( () => {
135+
mopf.insertAtSection( wikicode, needle, section );
136+
} ).toThrowError( 'MOSOrderPositionFinder: Invalid section name.' );
137+
} );
138+
} );
139+
76140
describe( 'getAllExistingSectionPositions(wikicode)', () => {
77141
test( 'simple', () => {
78142
const mopf = new MOSOrderPositionFinder();
@@ -101,15 +165,17 @@ Body
101165
const wikicode =
102166
`{{AfC Comment}}<!-- do not remove this line-->
103167
104-
Lead`;
168+
Lead
169+
`;
105170
const needle = '{{Speciesbox}}';
106171
const section = 'infoboxes';
107172
const output =
108173
`{{AfC Comment}}<!-- do not remove this line-->
109174
110175
{{Speciesbox}}
111176
112-
Lead`;
177+
Lead
178+
`;
113179
expect( mopf.insertAtSection( wikicode, needle, section ) ).toStrictEqual( output );
114180
} );
115181
} );

0 commit comments

Comments
 (0)