1 | import {InlineFlagResolver} from "../src/classLibrary/ParsingStrategy/InlineFlagResolver";
|
2 | import {Utility} from "../src/classLibrary/Utility";
|
3 | import {FileSystemInstance, IDirectory, IFile} from "../src/classLibrary/File";
|
4 | import {Core, Kore, SortType} from "@kirinnee/core";
|
5 | import {should} from 'chai';
|
6 |
|
7 | should();
|
8 | let core: Core = new Kore();
|
9 | let util: Utility = new Utility(core);
|
10 | core.ExtendPrimitives();
|
11 |
|
12 | let inlineFlagParser: InlineFlagResolver = new InlineFlagResolver(util, ["//"]);
|
13 |
|
14 | let simpleFlagObject: object = {
|
15 | remove: {
|
16 | one: true,
|
17 | two: false,
|
18 | three: {
|
19 | one: true,
|
20 | two: false
|
21 | }
|
22 | },
|
23 | stay: true
|
24 | };
|
25 |
|
26 | describe("InlineFlagResolver", () => {
|
27 |
|
28 | describe("ResolveContents", () => {
|
29 | it("should remove false flags and remove injected code", () => {
|
30 | let files: FileSystemInstance[] = [
|
31 | {
|
32 | destinationAbsolutePath: "root/flag~remove.one~one/one",
|
33 | relativePath: "flag~remove.one~one/one",
|
34 | sourceAbsolutePath: "root/source/flag~remove.one~one/one"
|
35 | },
|
36 | {
|
37 | destinationAbsolutePath: "root/flag~remove.two~two/two",
|
38 | relativePath: "flag~remove.two~two/two",
|
39 | sourceAbsolutePath: "root/source/flag~remove.two~two/two"
|
40 | },
|
41 | {
|
42 | destinationAbsolutePath: "root/flag~remove.three.one~one/31",
|
43 | relativePath: "flag~remove.three.one~one/31",
|
44 | sourceAbsolutePath: "root/source/flag~remove.three.one~one/31",
|
45 | },
|
46 | {
|
47 | destinationAbsolutePath: "root/flag~remove.three.two~two/32",
|
48 | relativePath: "flag~remove.three.two~two/32",
|
49 | sourceAbsolutePath: "root/source/flag~remove.three.two~two/32",
|
50 | },
|
51 | {
|
52 | destinationAbsolutePath: "root/flag~stay~stay/stay",
|
53 | relativePath: "flag~stay~stay/stay",
|
54 | sourceAbsolutePath: "root/source/flag~stay~stay/stay",
|
55 | },
|
56 | {
|
57 | destinationAbsolutePath: "root/test/stay",
|
58 | relativePath: "test/stay",
|
59 | sourceAbsolutePath: "root/source/test/stay",
|
60 | }
|
61 | ];
|
62 | let expected: FileSystemInstance[] = [
|
63 | {
|
64 | destinationAbsolutePath: "root/one/one",
|
65 | relativePath: "flag~remove.one~one/one",
|
66 | sourceAbsolutePath: "root/source/flag~remove.one~one/one"
|
67 | },
|
68 | {
|
69 | destinationAbsolutePath: "root/one/31",
|
70 | relativePath: "flag~remove.three.one~one/31",
|
71 | sourceAbsolutePath: "root/source/flag~remove.three.one~one/31",
|
72 | },
|
73 | {
|
74 | destinationAbsolutePath: "root/stay/stay",
|
75 | relativePath: "flag~stay~stay/stay",
|
76 | sourceAbsolutePath: "root/source/flag~stay~stay/stay",
|
77 | },
|
78 | {
|
79 | destinationAbsolutePath: "root/test/stay",
|
80 | relativePath: "test/stay",
|
81 | sourceAbsolutePath: "root/source/test/stay",
|
82 | }
|
83 | ];
|
84 |
|
85 | inlineFlagParser.ResolveFiles(simpleFlagObject, files)
|
86 | .should.deep.equal(expected);
|
87 | });
|
88 | });
|
89 |
|
90 | describe("ResolveFileContent", () => {
|
91 | it("should remove lines that have false flag and remove flags from the rest of the lines", () => {
|
92 | let content: string =
|
93 | `Lorem ipsum dolor sit amet, //flag~remove.one~ consectetur adipiscing elit.
|
94 | Integer quis est vulputate, interdum neque sed, pulvinar lacus.flag~remove.two~
|
95 | Curabitur dolor massa, varius cursus nunc sed, tincidunt mollis est.flag~remove.three.one~
|
96 | flag~remove.three.two~Orci varius natoque penatibus et magnis dis parturient montes, nascetur
|
97 | flag~stay~ridiculus mus. Donec eu velit fermentum, maximus tortor sit amet, rhoncus
|
98 | eros. Nullam semper libero in ullamcorper rhoncus. Fusce ligula sem,
|
99 | fringilla non enim vitae, congue interdum mi. Ut sed ex et neque laoreet
|
100 | eleifend. Donec maximus urna eros. Nunc gravida sollicitudin dignissim.
|
101 | Sed consequat ipsum at congue vulputate. In ac ipsum vel dui pellentesque blandit.
|
102 | Pellentesque tempus quis orci eu vulputate. Fusce vehicula nibh
|
103 | eget finibus venenatis.`;
|
104 | let eString: string =
|
105 | `Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
106 | Curabitur dolor massa, varius cursus nunc sed, tincidunt mollis est.
|
107 | ridiculus mus. Donec eu velit fermentum, maximus tortor sit amet, rhoncus
|
108 | eros. Nullam semper libero in ullamcorper rhoncus. Fusce ligula sem,
|
109 | fringilla non enim vitae, congue interdum mi. Ut sed ex et neque laoreet
|
110 | eleifend. Donec maximus urna eros. Nunc gravida sollicitudin dignissim.
|
111 | Sed consequat ipsum at congue vulputate. In ac ipsum vel dui pellentesque blandit.
|
112 | Pellentesque tempus quis orci eu vulputate. Fusce vehicula nibh
|
113 | eget finibus venenatis.`;
|
114 |
|
115 | let map: Map<string, boolean> = util.FlattenFlagObject(simpleFlagObject).MapKey(inlineFlagParser.ModifyFlagKeys);
|
116 | let actual: FileSystemInstance = {
|
117 | destinationAbsolutePath: "dest",
|
118 | relativePath: "rel",
|
119 | sourceAbsolutePath: "src",
|
120 | content: content
|
121 | } as FileSystemInstance;
|
122 | let expected: FileSystemInstance = {
|
123 | destinationAbsolutePath: "dest",
|
124 | relativePath: "rel",
|
125 | sourceAbsolutePath: "src",
|
126 | content: eString
|
127 | } as FileSystemInstance;
|
128 |
|
129 | inlineFlagParser.ResolveFileContent(map, actual).should.deep.equal(expected);
|
130 | });
|
131 | });
|
132 |
|
133 | describe("Count", () => {
|
134 | it("should count th number of each flag instance", () => {
|
135 |
|
136 | let file: IFile = {
|
137 | sourceAbsolutePath: "flag~remove.one~one/one",
|
138 | destinationAbsolutePath: "flag~remove.one~one/one",
|
139 | relativePath: "flag~remove.one~one/one",
|
140 | content: "flag~remove.one~ flag~remove.two~ asd\nflag~remove.three.one~"
|
141 | };
|
142 | let dir: IDirectory = {
|
143 | sourceAbsolutePath: "flag~remove.three.two~one/one",
|
144 | destinationAbsolutePath: "flag~remove.three.two~one/one",
|
145 | relativePath: "flag~remove.three.two~one/one",
|
146 | };
|
147 | let dir2: IDirectory = {
|
148 | sourceAbsolutePath: "flag~stay~one/one",
|
149 | destinationAbsolutePath: "flag~stay~one/one",
|
150 | relativePath: "flag~stay~one/one",
|
151 | };
|
152 |
|
153 | let files: FileSystemInstance[] = [file, dir, dir2];
|
154 | let expected: [string, number][] = new Map<string, number>([
|
155 | ["remove.one", 2],
|
156 | ["remove.two", 1],
|
157 | ["remove.three.one", 1],
|
158 | ["remove.three.two", 1],
|
159 | ["stay", 1]
|
160 | ]).SortByKey(SortType.AtoZ).Arr();
|
161 |
|
162 | let oldMap: Map<string, number> = new Map<string, number>([
|
163 | ["remove.one", 12],
|
164 | ["stay", 4],
|
165 | ["remove.three.two", 5]
|
166 | ]);
|
167 | let oldExpected: [string, number][] = new Map([
|
168 | ["remove.one", 14],
|
169 | ["remove.two", 1],
|
170 | ["remove.three.one", 1],
|
171 | ["remove.three.two", 6],
|
172 | ["stay", 5]
|
173 | ]).SortByKey(SortType.AtoZ).Arr();
|
174 |
|
175 | let actual: [string, number][] = inlineFlagParser
|
176 | .Count(simpleFlagObject, files, new Map<string, number>([]), false)
|
177 | .SortByKey(SortType.AtoZ)
|
178 | .Arr();
|
179 |
|
180 | let oldActual: [string, number][] = inlineFlagParser
|
181 | .Count(simpleFlagObject, files, oldMap, false)
|
182 | .SortByKey(SortType.AtoZ)
|
183 | .Arr();
|
184 | actual.should.deep.equal(expected);
|
185 | oldActual.should.deep.equal(oldExpected);
|
186 | });
|
187 | });
|
188 |
|
189 | describe("ModifyFlagKeys", () => {
|
190 | it("should correctly modify flag key", () => {
|
191 | let actual = inlineFlagParser.ModifyFlagKeys("someFlag");
|
192 | let expected = "flag~someFlag~";
|
193 | actual.should.equal(expected);
|
194 | });
|
195 | });
|
196 |
|
197 | });
|
198 |
|
199 |
|