1/*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {DiffType} from 'viewers/common/diff_type';
18import {AddDiffs} from './add_diffs';
19import {DiffNode} from './diff_node';
20
21export function executeAddDiffsTests<T extends DiffNode>(
22  nodeEqualityTester: (first: any, second: any) => boolean | undefined,
23  makeRoot: (value?: string) => T,
24  makeChildAndAddToRoot: (rootNode: T, value?: string) => T,
25  addDiffs: AddDiffs<T>,
26) {
27  describe('AddDiffs', () => {
28    let newRoot: T;
29    let oldRoot: T;
30    let expectedRoot: T;
31
32    beforeEach(() => {
33      jasmine.addCustomEqualityTester(nodeEqualityTester);
34      newRoot = makeRoot();
35      oldRoot = makeRoot();
36      expectedRoot = makeRoot();
37    });
38
39    it('handles two identical trees', async () => {
40      await addDiffs.executeInPlace(newRoot, newRoot);
41      expect(newRoot).toEqual(expectedRoot);
42    });
43
44    it('adds MODIFIED', async () => {
45      makeChildAndAddToRoot(newRoot);
46      makeChildAndAddToRoot(oldRoot, 'oldValue');
47
48      const expectedChild = makeChildAndAddToRoot(expectedRoot);
49      expectedChild.setDiff(DiffType.MODIFIED);
50
51      await addDiffs.executeInPlace(newRoot, oldRoot);
52      expect(newRoot).toEqual(expectedRoot);
53    });
54
55    it('adds ADDED', async () => {
56      makeChildAndAddToRoot(newRoot);
57
58      const expectedChild = makeChildAndAddToRoot(expectedRoot);
59      expectedChild.setDiff(DiffType.ADDED);
60
61      await addDiffs.executeInPlace(newRoot, oldRoot);
62      expect(newRoot).toEqual(expectedRoot);
63    });
64
65    it('adds DELETED', async () => {
66      makeChildAndAddToRoot(oldRoot);
67
68      const expectedChild = makeChildAndAddToRoot(expectedRoot);
69      expectedChild.setDiff(DiffType.DELETED);
70
71      await addDiffs.executeInPlace(newRoot, oldRoot);
72      expect(newRoot).toEqual(expectedRoot);
73    });
74  });
75}
76