@@ -6,6 +6,9 @@ import { TasksFile } from '../../src/Scripting/TasksFile';
66import { Task } from '../../src/Task/Task' ;
77import { TaskLocation } from '../../src/Task/TaskLocation' ;
88import { ListItem } from '../../src/Task/ListItem' ;
9+ import { TaskBuilder } from '../TestingTools/TaskBuilder' ;
10+ import { fromLine } from '../TestingTools/TestHelpers' ;
11+ import { createChildListItem } from './ListItemHelpers' ;
912
1013window . moment = moment ;
1114
@@ -96,3 +99,107 @@ describe('list item tests', () => {
9699 }
97100 } ) ;
98101} ) ;
102+
103+ describe ( 'identicalTo' , ( ) => {
104+ it ( 'should test same markdown' , ( ) => {
105+ const listItem1 = new ListItem ( '- same description' , null ) ;
106+ const listItem2 = new ListItem ( '- same description' , null ) ;
107+ expect ( listItem1 . identicalTo ( listItem2 ) ) . toEqual ( true ) ;
108+ } ) ;
109+
110+ it ( 'should test different markdown' , ( ) => {
111+ const listItem1 = new ListItem ( '- description' , null ) ;
112+ const listItem2 = new ListItem ( '- description two' , null ) ;
113+ expect ( listItem1 . identicalTo ( listItem2 ) ) . toEqual ( false ) ;
114+ } ) ;
115+
116+ it ( 'should recognise list items with different number of children' , ( ) => {
117+ const item1 = new ListItem ( '- item' , null ) ;
118+ createChildListItem ( '- child of item1' , item1 ) ;
119+
120+ const item2 = new ListItem ( '- item' , null ) ;
121+
122+ expect ( item2 . identicalTo ( item1 ) ) . toEqual ( false ) ;
123+ } ) ;
124+
125+ it ( 'should recognise list items with different children' , ( ) => {
126+ const item1 = new ListItem ( '- item' , null ) ;
127+ createChildListItem ( '- child of item1' , item1 ) ;
128+
129+ const item2 = new ListItem ( '- item' , null ) ;
130+ createChildListItem ( '- child of item2' , item2 ) ;
131+
132+ expect ( item2 . identicalTo ( item1 ) ) . toEqual ( false ) ;
133+ } ) ;
134+
135+ it ( 'should recognise ListItem and Task as different' , ( ) => {
136+ const listItem = new ListItem ( '- [ ] description' , null ) ;
137+ const task = fromLine ( { line : '- [ ] description' } ) ;
138+
139+ expect ( listItem . identicalTo ( task ) ) . toEqual ( false ) ;
140+ } ) ;
141+ } ) ;
142+
143+ describe ( 'checking if list item lists are identical' , ( ) => {
144+ it ( 'should treat empty lists as identical' , ( ) => {
145+ const list1 : ListItem [ ] = [ ] ;
146+ const list2 : ListItem [ ] = [ ] ;
147+ expect ( ListItem . listsAreIdentical ( list1 , list2 ) ) . toBe ( true ) ;
148+ } ) ;
149+
150+ it ( 'should treat different sized lists as different' , ( ) => {
151+ const list1 : ListItem [ ] = [ ] ;
152+ const list2 : ListItem [ ] = [ new ListItem ( '- x' , null ) ] ;
153+ expect ( ListItem . listsAreIdentical ( list1 , list2 ) ) . toBe ( false ) ;
154+ } ) ;
155+
156+ it ( 'should detect matching list items as same' , ( ) => {
157+ const list1 : ListItem [ ] = [ new ListItem ( '- 1' , null ) ] ;
158+ const list2 : ListItem [ ] = [ new ListItem ( '- 1' , null ) ] ;
159+ expect ( ListItem . listsAreIdentical ( list1 , list2 ) ) . toBe ( true ) ;
160+ } ) ;
161+
162+ it ( '- should detect non-matching list items as different' , ( ) => {
163+ const list1 : ListItem [ ] = [ new ListItem ( '- 1' , null ) ] ;
164+ const list2 : ListItem [ ] = [ new ListItem ( '- 2' , null ) ] ;
165+ expect ( ListItem . listsAreIdentical ( list1 , list2 ) ) . toBe ( false ) ;
166+ } ) ;
167+ } ) ;
168+
169+ describe ( 'checking if task lists are identical' , ( ) => {
170+ it ( 'should treat empty lists as identical' , ( ) => {
171+ const list1 : Task [ ] = [ ] ;
172+ const list2 : Task [ ] = [ ] ;
173+ expect ( ListItem . listsAreIdentical ( list1 , list2 ) ) . toBe ( true ) ;
174+ } ) ;
175+
176+ it ( 'should treat different sized lists as different' , ( ) => {
177+ const list1 : Task [ ] = [ ] ;
178+ const list2 : Task [ ] = [ new TaskBuilder ( ) . build ( ) ] ;
179+ expect ( ListItem . listsAreIdentical ( list1 , list2 ) ) . toBe ( false ) ;
180+ } ) ;
181+
182+ it ( 'should detect matching tasks as same' , ( ) => {
183+ const list1 : Task [ ] = [ new TaskBuilder ( ) . description ( '1' ) . build ( ) ] ;
184+ const list2 : Task [ ] = [ new TaskBuilder ( ) . description ( '1' ) . build ( ) ] ;
185+ expect ( ListItem . listsAreIdentical ( list1 , list2 ) ) . toBe ( true ) ;
186+ } ) ;
187+
188+ it ( 'should detect non-matching tasks as different' , ( ) => {
189+ const list1 : Task [ ] = [ new TaskBuilder ( ) . description ( '1' ) . build ( ) ] ;
190+ const list2 : Task [ ] = [ new TaskBuilder ( ) . description ( '2' ) . build ( ) ] ;
191+ expect ( ListItem . listsAreIdentical ( list1 , list2 ) ) . toBe ( false ) ;
192+ } ) ;
193+ } ) ;
194+
195+ describe ( 'checking if mixed lists are identical' , ( ) => {
196+ it ( 'should recognise mixed lists as unequal' , ( ) => {
197+ const list1 = [ new ListItem ( '- [ ] description' , null ) ] ;
198+ const list2 = [ fromLine ( { line : '- [ ] description' } ) ] ;
199+
200+ expect ( ListItem . listsAreIdentical ( list1 , list1 ) ) . toEqual ( true ) ;
201+ expect ( ListItem . listsAreIdentical ( list1 , list2 ) ) . toEqual ( false ) ;
202+ expect ( ListItem . listsAreIdentical ( list2 , list1 ) ) . toEqual ( false ) ;
203+ expect ( ListItem . listsAreIdentical ( list2 , list2 ) ) . toEqual ( true ) ;
204+ } ) ;
205+ } ) ;
0 commit comments