This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eaee26f
commit a9eb669
Showing
5 changed files
with
102 additions
and
0 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
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,37 @@ | ||
import IRule from "../interfaces/IRule"; | ||
import IValidableScript from "../interfaces/IValidableScript"; | ||
|
||
/** | ||
* This rule checks if the script has some duplicate parameter | ||
*/ | ||
export default class RuleDuplicatedParams implements IRule { | ||
/** | ||
* Method to validate the rule | ||
* @param element The element to validate | ||
*/ | ||
public isValid(element: IValidableScript): boolean { | ||
const list = this._getDuplicatedParamsList(element); | ||
return (list.length === 0); | ||
} | ||
/** | ||
* Method to generate a warning message in case the rule is invalid | ||
* @param element The element to validate | ||
*/ | ||
public getWarnMessage(element: IValidableScript): string { | ||
const list = this._getDuplicatedParamsList(element); | ||
return `Script "${element.doc.name}" has duplicated ${list.join(", ")} arguments.`; | ||
} | ||
|
||
private _getDuplicatedParamsList(element: IValidableScript): string[] { | ||
const set = new Set<string>(); | ||
const list: string[] = []; | ||
for (const param of element.doc.params) { | ||
if (set.has(param.name)) { | ||
list.push(param.name); | ||
} else { | ||
set.add(param.name); | ||
} | ||
} | ||
return list; | ||
} | ||
} |
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,56 @@ | ||
import { | ||
Expect, | ||
Test, | ||
TestFixture, | ||
} from "alsatian"; | ||
|
||
import DocParam from "../../../../src/doc_models/DocParam"; | ||
import RuleDuplicatedParams from "../../../../src/validation/rules/RuleDuplicatedParams"; | ||
import MockValidableScript from "./__mock__/MockValidableScript.mock"; | ||
|
||
/* tslint:disable:max-classes-per-file completed-docs */ | ||
|
||
@TestFixture("RuleDuplicateParamFixture") | ||
export class RuleDuplicateParamFixture { | ||
|
||
@Test("isValid") | ||
public isValid() { | ||
const vs = new MockValidableScript(); | ||
const p1 = new DocParam(); | ||
const p2 = new DocParam(); | ||
p1.name = "arg1"; | ||
p2.name = "arg2"; | ||
vs.doc.params.push(p1, p2); | ||
|
||
const rule = new RuleDuplicatedParams(); | ||
Expect(rule.isValid(vs)).toBe(true); | ||
} | ||
|
||
@Test("isValid_not") | ||
public isValid_not() { | ||
const vs = new MockValidableScript(); | ||
const p1 = new DocParam(); | ||
const p2 = new DocParam(); | ||
p1.name = "arg1"; | ||
p2.name = "arg1"; | ||
vs.doc.params.push(p1, p2); | ||
|
||
const rule = new RuleDuplicatedParams(); | ||
Expect(rule.isValid(vs)).toBe(false); | ||
} | ||
|
||
@Test("isValid") | ||
public getWarnMessage() { | ||
const vs = new MockValidableScript(); | ||
const p1 = new DocParam(); | ||
const p2 = new DocParam(); | ||
p1.name = "arg1"; | ||
p2.name = "arg1"; | ||
vs.doc.params.push(p1, p2); | ||
|
||
const rule = new RuleDuplicatedParams(); | ||
const str = rule.getWarnMessage(vs); | ||
Expect(str).toContain("foo"); | ||
Expect(str).toContain("arg1"); | ||
} | ||
} |