2017-11-06 17:05:45 +00:00
|
|
|
import { expect } from "chai"
|
|
|
|
import { enrichRule } from "../source/engine/rules"
|
2017-11-07 18:46:40 +00:00
|
|
|
import { analyse } from "../source/engine/traverse"
|
2017-11-07 08:48:13 +00:00
|
|
|
import { collectMissingVariables } from "../source/engine/generateQuestions"
|
2017-11-06 17:05:45 +00:00
|
|
|
import yaml from "js-yaml"
|
2017-11-07 08:48:13 +00:00
|
|
|
import dedent from "dedent-js"
|
2017-11-06 17:05:45 +00:00
|
|
|
|
|
|
|
describe("inversions", () => {
|
|
|
|
it("should handle non inverted example", () => {
|
|
|
|
let fakeState = { brut: 2300 }
|
|
|
|
let stateSelector = name => fakeState[name]
|
|
|
|
|
|
|
|
let
|
|
|
|
rawRules = dedent`
|
|
|
|
- nom: net
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: brut
|
|
|
|
taux: 77%
|
|
|
|
|
|
|
|
- nom: brut
|
|
|
|
format: euro
|
|
|
|
`,
|
|
|
|
rules = yaml.safeLoad(rawRules).map(enrichRule),
|
2017-11-07 18:46:40 +00:00
|
|
|
analysis = analyse(rules, "net")(stateSelector)
|
2017-11-06 17:05:45 +00:00
|
|
|
|
2017-11-07 18:46:40 +00:00
|
|
|
expect(analysis.targets[0].nodeValue).to.be.closeTo(1771, 0.001)
|
2017-11-06 17:05:45 +00:00
|
|
|
})
|
2017-11-14 15:01:16 +00:00
|
|
|
|
2017-11-06 17:05:45 +00:00
|
|
|
|
2017-11-15 15:37:04 +00:00
|
|
|
it("should handle simple inversion", () => {
|
2017-11-07 08:48:13 +00:00
|
|
|
let fakeState = { net: 2000 }
|
|
|
|
let stateSelector = name => fakeState[name]
|
2017-11-06 17:05:45 +00:00
|
|
|
|
2017-11-07 08:48:13 +00:00
|
|
|
let rawRules = dedent`
|
2017-11-06 17:05:45 +00:00
|
|
|
- nom: net
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: brut
|
|
|
|
taux: 77%
|
|
|
|
|
|
|
|
- nom: brut
|
|
|
|
format: euro
|
2017-11-06 19:12:08 +00:00
|
|
|
inversions possibles:
|
2017-11-06 17:05:45 +00:00
|
|
|
- net
|
|
|
|
`,
|
2017-11-07 08:48:13 +00:00
|
|
|
rules = yaml.safeLoad(rawRules).map(enrichRule),
|
2017-11-07 18:46:40 +00:00
|
|
|
analysis = analyse(rules, "brut")(stateSelector)
|
2017-11-07 08:48:13 +00:00
|
|
|
|
2017-11-07 18:46:40 +00:00
|
|
|
expect(analysis.targets[0].nodeValue).to.be.closeTo(2000 / (77 / 100), 0.0001 * 2000)
|
2017-11-07 08:48:13 +00:00
|
|
|
})
|
|
|
|
|
2017-11-16 13:04:51 +00:00
|
|
|
it("should ask the input of one of the possible inversions", () => {
|
|
|
|
let rawRules = dedent`
|
|
|
|
- nom: net
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: assiette
|
|
|
|
variations:
|
|
|
|
- si: cadre
|
|
|
|
taux: 80%
|
|
|
|
- si: ≠ cadre
|
|
|
|
taux: 70%
|
|
|
|
|
|
|
|
- nom: brut
|
|
|
|
format: euro
|
|
|
|
inversions possibles:
|
|
|
|
- net
|
|
|
|
- nom: cadre
|
|
|
|
- nom: assiette
|
|
|
|
formule: 67 + brut
|
|
|
|
|
|
|
|
`,
|
|
|
|
rules = yaml.safeLoad(rawRules).map(enrichRule),
|
|
|
|
stateSelector = name => null,
|
|
|
|
analysis = analyse(rules, "brut")(stateSelector),
|
|
|
|
missing = collectMissingVariables(analysis.targets)
|
|
|
|
|
|
|
|
expect(analysis.targets[0].nodeValue).to.be.null
|
|
|
|
expect(missing).to.have.key("brut")
|
|
|
|
})
|
|
|
|
|
2017-11-07 08:48:13 +00:00
|
|
|
it("should handle inversions with missing variables", () => {
|
|
|
|
let rawRules = dedent`
|
|
|
|
- nom: net
|
|
|
|
formule:
|
|
|
|
multiplication:
|
2017-11-13 14:50:48 +00:00
|
|
|
assiette: assiette
|
2017-11-07 08:48:13 +00:00
|
|
|
variations:
|
|
|
|
- si: cadre
|
|
|
|
taux: 80%
|
|
|
|
- si: ≠ cadre
|
|
|
|
taux: 70%
|
|
|
|
|
|
|
|
- nom: brut
|
|
|
|
format: euro
|
|
|
|
inversions possibles:
|
|
|
|
- net
|
|
|
|
- nom: cadre
|
2017-11-13 14:50:48 +00:00
|
|
|
- nom: assiette
|
|
|
|
formule: 67 + brut
|
|
|
|
|
2017-11-07 08:48:13 +00:00
|
|
|
`,
|
|
|
|
rules = yaml.safeLoad(rawRules).map(enrichRule),
|
|
|
|
stateSelector = name => ({ net: 2000 }[name]),
|
2017-11-07 18:46:40 +00:00
|
|
|
analysis = analyse(rules, "brut")(stateSelector),
|
|
|
|
missing = collectMissingVariables(analysis.targets)
|
2017-11-06 17:05:45 +00:00
|
|
|
|
2017-11-07 18:46:40 +00:00
|
|
|
expect(analysis.targets[0].nodeValue).to.be.null
|
2017-11-07 08:48:13 +00:00
|
|
|
expect(missing).to.have.key("cadre")
|
|
|
|
})
|
2017-11-14 15:01:16 +00:00
|
|
|
|
|
|
|
it("shouldn't report a missing salary if another salary was input", () => {
|
|
|
|
let rawRules = dedent`
|
|
|
|
- nom: net
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: assiette
|
|
|
|
variations:
|
|
|
|
- si: cadre
|
|
|
|
taux: 80%
|
|
|
|
- si: ≠ cadre
|
|
|
|
taux: 70%
|
|
|
|
|
|
|
|
- nom: total
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: assiette
|
|
|
|
taux: 150%
|
|
|
|
|
|
|
|
- nom: brut
|
|
|
|
format: euro
|
|
|
|
inversions possibles:
|
|
|
|
- net
|
|
|
|
- total
|
|
|
|
|
|
|
|
- nom: cadre
|
|
|
|
|
|
|
|
- nom: assiette
|
|
|
|
formule: 67 + brut
|
|
|
|
|
|
|
|
`,
|
|
|
|
rules = yaml.safeLoad(rawRules).map(enrichRule),
|
|
|
|
stateSelector = name => ({ net: 2000, cadre: 'oui' }[name]),
|
|
|
|
analysis = analyse(rules, "total")(stateSelector),
|
|
|
|
missing = collectMissingVariables(analysis.targets)
|
|
|
|
|
|
|
|
expect(analysis.targets[0].nodeValue).to.equal(3750)
|
|
|
|
expect(missing).to.be.empty
|
|
|
|
})
|
|
|
|
|
2017-11-06 17:05:45 +00:00
|
|
|
})
|