| 
									
										
										
										
											2017-04-05 21:38:15 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 19:33:57 +08:00
										 |  |  | const identifierUtil = require("../lib/util/identifier"); | 
					
						
							| 
									
										
										
										
											2017-04-05 21:38:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 19:33:57 +08:00
										 |  |  | describe("util/identifier", () => { | 
					
						
							|  |  |  | 	describe("makePathsRelative", () => { | 
					
						
							| 
									
										
										
										
											2017-04-05 21:38:15 +08:00
										 |  |  | 		describe("given a context and a pathConstruct", () => { | 
					
						
							|  |  |  | 			it("computes the correct relative results for the path construct", () => { | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 				for (const [context, pathConstruct, expected] of [ | 
					
						
							| 
									
										
										
										
											2018-12-23 20:22:07 +08:00
										 |  |  | 					[ | 
					
						
							|  |  |  | 						"/some/dir/", | 
					
						
							|  |  |  | 						"/some/dir/to/somewhere|some/other/dir!../more/dir", | 
					
						
							|  |  |  | 						"./to/somewhere|some/other/dir!../more/dir" | 
					
						
							|  |  |  | 					], | 
					
						
							|  |  |  | 					[ | 
					
						
							|  |  |  | 						"/dir/", | 
					
						
							|  |  |  | 						"/dir/to/somewhere|some/other/dir!../more/dir", | 
					
						
							|  |  |  | 						"./to/somewhere|some/other/dir!../more/dir" | 
					
						
							|  |  |  | 					], | 
					
						
							|  |  |  | 					[ | 
					
						
							|  |  |  | 						"/", | 
					
						
							|  |  |  | 						"/dir/to/somewhere|some/other/dir!../more/dir", | 
					
						
							|  |  |  | 						"./dir/to/somewhere|some/other/dir!../more/dir" | 
					
						
							|  |  |  | 					], | 
					
						
							|  |  |  | 					[ | 
					
						
							| 
									
										
										
										
											2018-12-26 17:40:42 +08:00
										 |  |  | 						"c:\\some\\dir\\", | 
					
						
							|  |  |  | 						"c:\\some\\dir\\to\\somewhere|some/other/dir!../more/dir", | 
					
						
							| 
									
										
										
										
											2018-12-25 18:34:48 +08:00
										 |  |  | 						"./to/somewhere|some/other/dir!../more/dir" | 
					
						
							| 
									
										
										
										
											2018-12-23 20:22:07 +08:00
										 |  |  | 					], | 
					
						
							|  |  |  | 					[ | 
					
						
							| 
									
										
										
										
											2018-12-26 17:40:42 +08:00
										 |  |  | 						"c:\\some\\dir\\", | 
					
						
							|  |  |  | 						"C:\\some\\dir\\to\\somewhere|some/other/dir!../more/dir", | 
					
						
							| 
									
										
										
										
											2018-12-25 18:34:48 +08:00
										 |  |  | 						"./to/somewhere|some/other/dir!../more/dir" | 
					
						
							| 
									
										
										
										
											2018-12-26 17:40:42 +08:00
										 |  |  | 					], | 
					
						
							|  |  |  | 					[ | 
					
						
							|  |  |  | 						"C:\\some\\dir", | 
					
						
							|  |  |  | 						"C:\\some\\dir\\to\\somewhere|some/other/dir!../more/dir", | 
					
						
							|  |  |  | 						"./to/somewhere|some/other/dir!../more/dir" | 
					
						
							|  |  |  | 					], | 
					
						
							|  |  |  | 					[ | 
					
						
							|  |  |  | 						"C:\\\\some\\dir", | 
					
						
							|  |  |  | 						"c:\\some\\\\dir\\to\\\\somewhere|some/other/dir!../more/dir", | 
					
						
							|  |  |  | 						"./to/somewhere|some/other/dir!../more/dir" | 
					
						
							|  |  |  | 					], | 
					
						
							| 
									
										
										
										
											2019-02-19 15:58:46 +08:00
										 |  |  | 					["/dir", "/dir/to/somewhere??ref-123", "./to/somewhere??ref-123"] | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 				]) { | 
					
						
							| 
									
										
										
										
											2019-02-19 15:58:46 +08:00
										 |  |  | 					expect(identifierUtil.makePathsRelative(context, pathConstruct)).toBe( | 
					
						
							|  |  |  | 						expected | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-04-05 21:38:15 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2021-03-16 20:07:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	describe("getUndoPath", () => { | 
					
						
							|  |  |  | 		const cases = [ | 
					
						
							|  |  |  | 			["file.js", ""], | 
					
						
							|  |  |  | 			["file.js", "./", true], | 
					
						
							|  |  |  | 			["dir/file.js", "../"], | 
					
						
							|  |  |  | 			["dir/file.js", "../", true], | 
					
						
							|  |  |  | 			["./file.js", ""], | 
					
						
							|  |  |  | 			[".dir/file.js", "../"], | 
					
						
							|  |  |  | 			["./dir/file.js", "../"], | 
					
						
							|  |  |  | 			["./dir/././file.js", "../"], | 
					
						
							|  |  |  | 			["./dir/../file.js", ""], | 
					
						
							|  |  |  | 			["./dir/../file.js", "./", true], | 
					
						
							|  |  |  | 			["../file.js", "d/"], | 
					
						
							|  |  |  | 			["../file.js", "./d/", true], | 
					
						
							|  |  |  | 			["../dir/file.js", "../d/"], | 
					
						
							|  |  |  | 			[".././../dir/file.js", "../c/d/"], | 
					
						
							|  |  |  | 			["./.././../dir/file.js", "../c/d/"], | 
					
						
							|  |  |  | 			["../dir/../file.js", "d/"], | 
					
						
							|  |  |  | 			["../dir/../file.js", "./d/", true] | 
					
						
							|  |  |  | 		]; | 
					
						
							|  |  |  | 		for (const [filename, expected, enforceRelative] of cases) { | 
					
						
							|  |  |  | 			it(`should handle ${filename} correctly${ | 
					
						
							|  |  |  | 				enforceRelative ? " (enforced relative path)" : "" | 
					
						
							|  |  |  | 			}`, () => {
 | 
					
						
							|  |  |  | 				for (const outputPath of [ | 
					
						
							|  |  |  | 					"/a/b/c/d", | 
					
						
							|  |  |  | 					"C:\\a\\b\\c\\d", | 
					
						
							|  |  |  | 					"/a/b/c/d/", | 
					
						
							|  |  |  | 					"C:\\a\\b\\c\\d\\" | 
					
						
							|  |  |  | 				]) { | 
					
						
							|  |  |  | 					expect( | 
					
						
							|  |  |  | 						identifierUtil.getUndoPath(filename, outputPath, enforceRelative) | 
					
						
							|  |  |  | 					).toBe(expected); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2022-02-03 23:21:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	describe("parseResourceWithoutFragment", () => { | 
					
						
							|  |  |  | 		// [input, expectedPath, expectedQuery]
 | 
					
						
							|  |  |  | 		/** @type {[string, string, string][]} */ | 
					
						
							|  |  |  | 		const cases = [ | 
					
						
							| 
									
										
										
										
											2022-02-04 23:12:20 +08:00
										 |  |  | 			["path#hash?query", "path#hash", "?query"], | 
					
						
							| 
									
										
										
										
											2022-02-03 23:21:06 +08:00
										 |  |  | 			["path?query#hash", "path", "?query#hash"], | 
					
						
							|  |  |  | 			["\0#path\0??\0#query#hash", "#path?", "?#query#hash"], | 
					
						
							|  |  |  | 			[ | 
					
						
							|  |  |  | 				'./loader.js?{"items":["a\0^","b\0!","c#","d"]}', | 
					
						
							|  |  |  | 				"./loader.js", | 
					
						
							|  |  |  | 				'?{"items":["a^","b!","c#","d"]}' | 
					
						
							|  |  |  | 			], | 
					
						
							|  |  |  | 			[ | 
					
						
							|  |  |  | 				"C:\\Users\\\0#\\repo\\loader.js?", | 
					
						
							|  |  |  | 				"C:\\Users\\#\\repo\\loader.js", | 
					
						
							|  |  |  | 				"?" | 
					
						
							|  |  |  | 			], | 
					
						
							|  |  |  | 			["/Users/\0#/repo/loader-\0#.js", "/Users/#/repo/loader-#.js", ""] | 
					
						
							|  |  |  | 		]; | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 		for (const case_ of cases) { | 
					
						
							| 
									
										
										
										
											2022-02-03 23:21:06 +08:00
										 |  |  | 			it(case_[0], () => { | 
					
						
							|  |  |  | 				const { resource, path, query } = | 
					
						
							|  |  |  | 					identifierUtil.parseResourceWithoutFragment(case_[0]); | 
					
						
							|  |  |  | 				expect(case_[0]).toBe(resource); | 
					
						
							|  |  |  | 				expect(case_[1]).toBe(path); | 
					
						
							|  |  |  | 				expect(case_[2]).toBe(query); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-02-03 23:21:06 +08:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-04-05 21:38:15 +08:00
										 |  |  | }); |