-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Right now the --fix option will find a random test pnr and replace the real one with that. Future improvement: Find replacement a bit smarter. Perhaps with the same birth date.
- Loading branch information
1 parent
8c4699f
commit a7a4436
Showing
5 changed files
with
194 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const Substitutor = require("./substitutor"); | ||
const FileMockDataReader = require("./fileMockDataReader"); | ||
const fs = require(`fs`); | ||
|
||
const substitutor = new Substitutor(FileMockDataReader.fetch()); | ||
|
||
class Fixer { | ||
static fixPnrs(finderResult) { | ||
finderResult.forEach(item => { | ||
let fileContent = fs.readFileSync(item.file, "utf8"); | ||
item.results.forEach(result => { | ||
const realPnr = result.pnr; | ||
let testPnr = substitutor.getReplacement(realPnr); | ||
testPnr = Fixer.matchFormat(realPnr, testPnr); | ||
fileContent = fileContent.replace(realPnr, testPnr); | ||
result.replacement = testPnr; | ||
}); | ||
fs.writeFileSync(item.file, fileContent, "utf8"); | ||
}); | ||
} | ||
|
||
static matchFormat(pnrToMatch, testPnr) { | ||
let realPnrBackwards = pnrToMatch | ||
.slice() | ||
.split("") | ||
.reverse(); | ||
let testPnrBackwards = testPnr | ||
.slice() | ||
.split("") | ||
.reverse(); | ||
let lastReplacedIndex = -1; | ||
testPnrBackwards.forEach(char => { | ||
let charHasBeenReplaced = false; | ||
while ( | ||
!charHasBeenReplaced && | ||
lastReplacedIndex < realPnrBackwards.length | ||
) { | ||
const nextIndexToReplace = lastReplacedIndex + 1; | ||
if (Fixer.isCharDigit(realPnrBackwards[nextIndexToReplace])) { | ||
realPnrBackwards[nextIndexToReplace] = char; | ||
charHasBeenReplaced = true; | ||
} | ||
lastReplacedIndex = nextIndexToReplace; | ||
} | ||
}); | ||
|
||
return realPnrBackwards.reverse().join(""); | ||
} | ||
|
||
static isCharDigit(char) { | ||
return !!parseInt(char); | ||
} | ||
} | ||
|
||
module.exports = Fixer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const mockFs = { | ||
writeFileSync: jest.fn(), | ||
readFileSync: jest.fn() | ||
}; | ||
jest.mock("fs", () => mockFs); | ||
|
||
const mockFileMockDataReader = { | ||
fetch: jest.fn() | ||
}; | ||
jest.mock("./fileMockDataReader", () => mockFileMockDataReader); | ||
|
||
const mockSubstitutor = { | ||
getReplacement: jest.fn() | ||
}; | ||
jest.mock("./substitutor", () => { | ||
return jest.fn().mockImplementation(() => mockSubstitutor); | ||
}); | ||
|
||
const Fixer = require("./fixer"); | ||
|
||
describe("Fixer", () => { | ||
test("overwrites file after replacment", () => { | ||
mockFs.readFileSync = () => `file content`; | ||
const finderResult = [ | ||
{ | ||
file: "file.js", | ||
results: [] | ||
} | ||
]; | ||
|
||
Fixer.fixPnrs(finderResult); | ||
expect(mockFs.writeFileSync).toBeCalledWith( | ||
`file.js`, | ||
expect.any(String), | ||
expect.any(String) | ||
); | ||
}); | ||
|
||
test("replaces real prn with test data", () => { | ||
mockFs.readFileSync = () => `Bogus pnr: "199999999999".`; | ||
mockSubstitutor.getReplacement = () => "198888888888"; | ||
const finderResult = [ | ||
{ | ||
file: "file.js", | ||
results: [ | ||
{ | ||
pnr: "199999999999" | ||
} | ||
] | ||
} | ||
]; | ||
|
||
Fixer.fixPnrs(finderResult); | ||
expect(mockFs.writeFileSync).toBeCalledWith( | ||
expect.any(String), | ||
`Bogus pnr: "198888888888".`, | ||
expect.any(String) | ||
); | ||
}); | ||
|
||
describe("matchFormat", () => { | ||
test("can format with spaces", () => { | ||
expect(Fixer.matchFormat("1 2 3", "789")).toBe("7 8 9"); | ||
}); | ||
test("can format with hyphen", () => { | ||
expect(Fixer.matchFormat("12-3", "789")).toBe("78-9"); | ||
}); | ||
test("can format with plus", () => { | ||
expect(Fixer.matchFormat("12+3", "789")).toBe("78+9"); | ||
}); | ||
test("can format with spaces and hypen", () => { | ||
expect(Fixer.matchFormat("12 - 3", "789")).toBe("78 - 9"); | ||
}); | ||
test("can format with spaces and plus", () => { | ||
expect(Fixer.matchFormat("12 + 3", "789")).toBe("78 + 9"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const Pnr = require("./pnr"); | ||
|
||
class Substitutor { | ||
constructor(testPnrs) { | ||
this.testPnrs = testPnrs; | ||
this.prevSubstitutions = {}; | ||
} | ||
|
||
/** | ||
* Returns a random replacemennt PNR from the test data given to the constructor. | ||
* This method will return the same replacement for the given `realPnr` over and over again. | ||
* @param {String} realPnr | ||
*/ | ||
getReplacement(realPnr) { | ||
const normalizedRealPnr = Pnr.normalizePnr(realPnr); | ||
if (normalizedRealPnr in this.prevSubstitutions) { | ||
return this.prevSubstitutions[normalizedRealPnr]; | ||
} | ||
|
||
const i = Math.floor(Math.random() * (this.testPnrs.length - 1)); | ||
const testPnr = this.testPnrs[i]; | ||
this.prevSubstitutions[normalizedRealPnr] = testPnr; | ||
return testPnr; | ||
} | ||
} | ||
|
||
module.exports = Substitutor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const Substitutor = require("./substitutor"); | ||
|
||
describe("Substitutor", () => { | ||
test("can pick a replacement test pnr", () => { | ||
const substitutor = new Substitutor(["2"]); | ||
expect(substitutor.getReplacement("1")).toBe("2"); | ||
}); | ||
|
||
test("shall return the same replacement over and over for a pnr", () => { | ||
const substitutor = new Substitutor(["2"]); | ||
const firstReplacement = substitutor.getReplacement("1"); | ||
expect(firstReplacement).toBe("2"); | ||
|
||
substitutor.testPnrs = []; | ||
expect(substitutor.getReplacement("1")).toEqual(firstReplacement); | ||
}); | ||
}); |