@jsprismarine/brigadier 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +0 -0
- package/README.md +57 -39
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/AmbiguityConsumer.js +0 -0
- package/dist/lib/AmbiguityConsumer.js.map +0 -0
- package/dist/lib/Command.js +0 -0
- package/dist/lib/Command.js.map +0 -0
- package/dist/lib/CommandDispatcher.js +45 -28
- package/dist/lib/CommandDispatcher.js.map +1 -1
- package/dist/lib/ImmutableStringReader.js +0 -0
- package/dist/lib/ImmutableStringReader.js.map +0 -0
- package/dist/lib/LiteralMessage.js +0 -0
- package/dist/lib/LiteralMessage.js.map +0 -0
- package/dist/lib/Message.js +0 -0
- package/dist/lib/Message.js.map +0 -0
- package/dist/lib/ParseResults.js +0 -0
- package/dist/lib/ParseResults.js.map +0 -0
- package/dist/lib/Predicate.js +0 -0
- package/dist/lib/Predicate.js.map +0 -0
- package/dist/lib/RedirectModifier.js +0 -0
- package/dist/lib/RedirectModifier.js.map +0 -0
- package/dist/lib/ResultConsumer.js +0 -0
- package/dist/lib/ResultConsumer.js.map +0 -0
- package/dist/lib/SingleRedirectModifier.js +0 -0
- package/dist/lib/SingleRedirectModifier.js.map +0 -0
- package/dist/lib/StringReader.js +0 -0
- package/dist/lib/StringReader.js.map +0 -0
- package/dist/lib/arguments/ArgumentType.js +1 -1
- package/dist/lib/arguments/ArgumentType.js.map +1 -1
- package/dist/lib/arguments/BoolArgumentType.js +0 -0
- package/dist/lib/arguments/BoolArgumentType.js.map +0 -0
- package/dist/lib/arguments/FloatArgumentType.js +0 -0
- package/dist/lib/arguments/FloatArgumentType.js.map +0 -0
- package/dist/lib/arguments/IntegerArgumentType.js +0 -0
- package/dist/lib/arguments/IntegerArgumentType.js.map +0 -0
- package/dist/lib/arguments/StringArgumentType.js +0 -0
- package/dist/lib/arguments/StringArgumentType.js.map +0 -0
- package/dist/lib/builder/ArgumentBuilder.js +0 -0
- package/dist/lib/builder/ArgumentBuilder.js.map +0 -0
- package/dist/lib/builder/LiteralArgumentBuilder.js +0 -0
- package/dist/lib/builder/LiteralArgumentBuilder.js.map +1 -1
- package/dist/lib/builder/RequiredArgumentBuilder.js +0 -0
- package/dist/lib/builder/RequiredArgumentBuilder.js.map +1 -1
- package/dist/lib/context/CommandContext.js +4 -4
- package/dist/lib/context/CommandContext.js.map +1 -1
- package/dist/lib/context/CommandContextBuilder.js +0 -0
- package/dist/lib/context/CommandContextBuilder.js.map +0 -0
- package/dist/lib/context/ParsedArgument.js +0 -0
- package/dist/lib/context/ParsedArgument.js.map +0 -0
- package/dist/lib/context/ParsedCommandNode.js +0 -0
- package/dist/lib/context/ParsedCommandNode.js.map +0 -0
- package/dist/lib/context/StringRange.js +0 -0
- package/dist/lib/context/StringRange.js.map +0 -0
- package/dist/lib/context/SuggestionContext.js +0 -0
- package/dist/lib/context/SuggestionContext.js.map +0 -0
- package/dist/lib/exceptions/BuiltInExceptionProvider.js +0 -0
- package/dist/lib/exceptions/BuiltInExceptionProvider.js.map +0 -0
- package/dist/lib/exceptions/BuiltInExceptions.js +0 -0
- package/dist/lib/exceptions/BuiltInExceptions.js.map +0 -0
- package/dist/lib/exceptions/CommandExceptionType.js +0 -0
- package/dist/lib/exceptions/CommandExceptionType.js.map +0 -0
- package/dist/lib/exceptions/CommandSyntaxException.js +0 -0
- package/dist/lib/exceptions/CommandSyntaxException.js.map +0 -0
- package/dist/lib/exceptions/DynamicCommandExceptionType.js +0 -0
- package/dist/lib/exceptions/DynamicCommandExceptionType.js.map +0 -0
- package/dist/lib/exceptions/SimpleCommandExceptionType.js +0 -0
- package/dist/lib/exceptions/SimpleCommandExceptionType.js.map +0 -0
- package/dist/lib/suggestion/IntegerSuggestion.js +0 -0
- package/dist/lib/suggestion/IntegerSuggestion.js.map +0 -0
- package/dist/lib/suggestion/Suggestion.js +1 -1
- package/dist/lib/suggestion/Suggestion.js.map +1 -1
- package/dist/lib/suggestion/SuggestionProvider.js +0 -0
- package/dist/lib/suggestion/SuggestionProvider.js.map +0 -0
- package/dist/lib/suggestion/Suggestions.js +1 -1
- package/dist/lib/suggestion/Suggestions.js.map +1 -1
- package/dist/lib/suggestion/SuggestionsBuilder.js +0 -0
- package/dist/lib/suggestion/SuggestionsBuilder.js.map +0 -0
- package/dist/lib/tree/ArgumentCommandNode.js +17 -16
- package/dist/lib/tree/ArgumentCommandNode.js.map +1 -1
- package/dist/lib/tree/CommandNode.js +14 -13
- package/dist/lib/tree/CommandNode.js.map +1 -1
- package/dist/lib/tree/LiteralCommandNode.js +0 -0
- package/dist/lib/tree/LiteralCommandNode.js.map +0 -0
- package/dist/lib/tree/RootCommandNode.js +0 -0
- package/dist/lib/tree/RootCommandNode.js.map +0 -0
- package/dist/lib/util/isEqual.js +0 -0
- package/dist/lib/util/isEqual.js.map +0 -0
- package/index.js +0 -0
- package/package.json +55 -48
- package/.prettierrc +0 -10
- package/.renovaterc +0 -3
- package/CHANGELOG.md +0 -30
- package/dist/index.d.ts +0 -23
- package/dist/lib/AmbiguityConsumer.d.ts +0 -4
- package/dist/lib/Command.d.ts +0 -4
- package/dist/lib/CommandDispatcher.d.ts +0 -29
- package/dist/lib/ImmutableStringReader.d.ts +0 -12
- package/dist/lib/LiteralMessage.d.ts +0 -7
- package/dist/lib/Message.d.ts +0 -3
- package/dist/lib/ParseResults.d.ts +0 -13
- package/dist/lib/Predicate.d.ts +0 -3
- package/dist/lib/Primitive.js +0 -2
- package/dist/lib/RedirectModifier.d.ts +0 -4
- package/dist/lib/ResultConsumer.d.ts +0 -4
- package/dist/lib/SingleRedirectModifier.d.ts +0 -4
- package/dist/lib/StringReader.d.ts +0 -27
- package/dist/lib/arguments/ArgumentType.d.ts +0 -21
- package/dist/lib/arguments/BoolArgumentType.d.ts +0 -13
- package/dist/lib/arguments/FloatArgumentType.d.ts +0 -16
- package/dist/lib/arguments/IntegerArgumentType.d.ts +0 -16
- package/dist/lib/arguments/StringArgumentType.d.ts +0 -21
- package/dist/lib/builder/ArgumentBuilder.d.ts +0 -28
- package/dist/lib/builder/LiteralArgumentBuilder.d.ts +0 -11
- package/dist/lib/builder/RequiredArgumentBuilder.d.ts +0 -18
- package/dist/lib/context/CommandContext.d.ts +0 -33
- package/dist/lib/context/CommandContextBuilder.d.ts +0 -38
- package/dist/lib/context/ParsedArgument.d.ts +0 -9
- package/dist/lib/context/ParsedCommandNode.d.ts +0 -11
- package/dist/lib/context/StringRange.d.ts +0 -16
- package/dist/lib/context/SuggestionContext.d.ts +0 -6
- package/dist/lib/exceptions/BuiltInExceptionProvider.d.ts +0 -23
- package/dist/lib/exceptions/BuiltInExceptions.d.ts +0 -43
- package/dist/lib/exceptions/CommandExceptionType.d.ts +0 -2
- package/dist/lib/exceptions/CommandSyntaxException.d.ts +0 -19
- package/dist/lib/exceptions/Dynamic2CommandExceptionType.js +0 -19
- package/dist/lib/exceptions/DynamicCommandExceptionType.d.ts +0 -10
- package/dist/lib/exceptions/SimpleCommandExceptionType.d.ts +0 -11
- package/dist/lib/suggestion/IntegerSuggestion.d.ts +0 -12
- package/dist/lib/suggestion/Suggestion.d.ts +0 -17
- package/dist/lib/suggestion/SuggestionProvider.d.ts +0 -6
- package/dist/lib/suggestion/Suggestions.d.ts +0 -16
- package/dist/lib/suggestion/SuggestionsBuilder.d.ts +0 -18
- package/dist/lib/tree/ArgumentCommandNode.d.ts +0 -31
- package/dist/lib/tree/CommandNode.d.ts +0 -45
- package/dist/lib/tree/LiteralCommandNode.d.ts +0 -27
- package/dist/lib/tree/RootCommandNode.d.ts +0 -21
- package/dist/lib/util/isEqual.d.ts +0 -1
- package/example/fill.js +0 -47
- package/example/fill.ts +0 -50
- package/src/index.ts +0 -50
- package/src/lib/AmbiguityConsumer.ts +0 -6
- package/src/lib/Command.ts +0 -5
- package/src/lib/CommandDispatcher.ts +0 -390
- package/src/lib/ImmutableStringReader.ts +0 -22
- package/src/lib/LiteralMessage.ts +0 -17
- package/src/lib/Message.ts +0 -3
- package/src/lib/ParseResults.ts +0 -32
- package/src/lib/Predicate.ts +0 -3
- package/src/lib/RedirectModifier.ts +0 -5
- package/src/lib/ResultConsumer.ts +0 -6
- package/src/lib/SingleRedirectModifier.ts +0 -6
- package/src/lib/StringReader.ts +0 -210
- package/src/lib/arguments/ArgumentType.ts +0 -27
- package/src/lib/arguments/BoolArgumentType.ts +0 -41
- package/src/lib/arguments/FloatArgumentType.ts +0 -75
- package/src/lib/arguments/IntegerArgumentType.ts +0 -75
- package/src/lib/arguments/StringArgumentType.ts +0 -83
- package/src/lib/builder/ArgumentBuilder.ts +0 -94
- package/src/lib/builder/LiteralArgumentBuilder.ts +0 -35
- package/src/lib/builder/RequiredArgumentBuilder.ts +0 -54
- package/src/lib/context/CommandContext.ts +0 -118
- package/src/lib/context/CommandContextBuilder.ts +0 -155
- package/src/lib/context/ParsedArgument.ts +0 -27
- package/src/lib/context/ParsedCommandNode.ts +0 -35
- package/src/lib/context/StringRange.ts +0 -57
- package/src/lib/context/SuggestionContext.ts +0 -12
- package/src/lib/exceptions/BuiltInExceptionProvider.ts +0 -43
- package/src/lib/exceptions/BuiltInExceptions.ts +0 -121
- package/src/lib/exceptions/CommandExceptionType.ts +0 -2
- package/src/lib/exceptions/CommandSyntaxException.ts +0 -77
- package/src/lib/exceptions/DynamicCommandExceptionType.ts +0 -22
- package/src/lib/exceptions/SimpleCommandExceptionType.ts +0 -26
- package/src/lib/suggestion/IntegerSuggestion.ts +0 -46
- package/src/lib/suggestion/Suggestion.ts +0 -87
- package/src/lib/suggestion/SuggestionProvider.ts +0 -7
- package/src/lib/suggestion/Suggestions.ts +0 -85
- package/src/lib/suggestion/SuggestionsBuilder.ts +0 -69
- package/src/lib/tree/ArgumentCommandNode.ts +0 -118
- package/src/lib/tree/CommandNode.ts +0 -200
- package/src/lib/tree/LiteralCommandNode.ts +0 -111
- package/src/lib/tree/RootCommandNode.ts +0 -62
- package/src/lib/util/isEqual.ts +0 -75
- package/test/CommandDispatcherTest.ts +0 -427
- package/test/CommandDispatcherUsagesTest.ts +0 -163
- package/test/CommandSuggestionsTest.ts +0 -283
- package/test/StringReaderTest.ts +0 -391
- package/test/arguments/BoolArgumentTypeTest.ts +0 -18
- package/test/arguments/FloatArgumentTypeTest.ts +0 -61
- package/test/arguments/IntegerArgumentTypeTest.ts +0 -63
- package/test/arguments/StringArgumentTypeTest.ts +0 -61
- package/test/benchmarks/ExecuteBenchmark.js +0 -33
- package/test/benchmarks/ParsingBenchmark.js +0 -96
- package/test/builder/ArgumentBuilderTest.ts +0 -61
- package/test/builder/LiteralArgumentBuilderTest.ts +0 -37
- package/test/builder/RequiredArgumentBuilderTest.ts +0 -40
- package/test/context/CommandContextTest.ts +0 -77
- package/test/context/ParsedArgumentTest.ts +0 -19
- package/test/exceptions/DynamicCommandSyntaxExceptionTypeTest.ts +0 -18
- package/test/exceptions/SimpleCommandSyntaxExceptionTypeTest.ts +0 -33
- package/test/suggestion/SuggestionBuilderTest.ts +0 -66
- package/test/suggestion/SuggestionTest.ts +0 -65
- package/test/suggestion/SuggestionsTest.ts +0 -24
- package/test/tree/AbstractCommandNodeTest.ts +0 -57
- package/test/tree/ArgumentCommandNodeTest.ts +0 -81
- package/test/tree/LiteralCommandNodeTest.ts +0 -112
- package/test/tree/RootCommandNodeTest.ts +0 -70
- package/test/utils/testEquality.ts +0 -10
- package/tsconfig.json +0 -40
- package/tsconfig.tsbuildinfo +0 -1578
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import Command from "../Command"
|
|
2
|
-
import CommandNode from "./CommandNode"
|
|
3
|
-
import Predicate from "../Predicate"
|
|
4
|
-
import RedirectModifier from "../RedirectModifier"
|
|
5
|
-
import StringReader from "../StringReader"
|
|
6
|
-
import LiteralArgumentBuilder from "../builder/LiteralArgumentBuilder"
|
|
7
|
-
import CommandContext from "../context/CommandContext"
|
|
8
|
-
import CommandContextBuilder from "../context/CommandContextBuilder"
|
|
9
|
-
import StringRange from "../context/StringRange"
|
|
10
|
-
import CommandSyntaxException from "../exceptions/CommandSyntaxException"
|
|
11
|
-
import Suggestions from "../suggestion/Suggestions"
|
|
12
|
-
import SuggestionsBuilder from "../suggestion/SuggestionsBuilder"
|
|
13
|
-
|
|
14
|
-
export default class LiteralCommandNode<S> extends CommandNode<S> {
|
|
15
|
-
|
|
16
|
-
private literal: string;
|
|
17
|
-
|
|
18
|
-
public constructor(literal: string, command: Command<S>, requirement: Predicate<S>, redirect: CommandNode<S>, modifier: RedirectModifier<S>, forks: boolean) {
|
|
19
|
-
super(command, requirement, redirect, modifier, forks);
|
|
20
|
-
this.literal = literal;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public getNodeType(): string {
|
|
24
|
-
return "literal"
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public getLiteral(): string {
|
|
28
|
-
return this.literal;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public getName(): string {
|
|
32
|
-
return this.literal;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public parse(reader: StringReader, contextBuilder: CommandContextBuilder<S>) {
|
|
36
|
-
let start = reader.getCursor();
|
|
37
|
-
let end = this.__parse(reader);
|
|
38
|
-
if (end > -1) {
|
|
39
|
-
contextBuilder.withNode(this, StringRange.between(start, end));
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.literalIncorrect().createWithContext(reader, this.literal);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
private __parse(reader: StringReader): number {
|
|
47
|
-
let start = reader.getCursor();
|
|
48
|
-
if (reader.canRead(this.literal.length)) {
|
|
49
|
-
let end = start + this.literal.length;
|
|
50
|
-
if (reader.getString().substring(start, end) === this.literal) {
|
|
51
|
-
reader.setCursor(end);
|
|
52
|
-
if (!reader.canRead() || reader.peek() == ' ') {
|
|
53
|
-
return end;
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
reader.setCursor(start);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return -1;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public listSuggestions(context: CommandContext<S>, builder: SuggestionsBuilder): Promise<Suggestions> {
|
|
64
|
-
if (this.literal.toLowerCase().startsWith(builder.getRemaining().toLowerCase())) {
|
|
65
|
-
return builder.suggest(this.literal).buildPromise();
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
return Suggestions.empty();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
public isValidInput(input: string): boolean {
|
|
74
|
-
return this.__parse(new StringReader(input)) > -1;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public equals(o: object): boolean {
|
|
78
|
-
if (this === o) return true;
|
|
79
|
-
if (!(o instanceof LiteralCommandNode)) return false;
|
|
80
|
-
|
|
81
|
-
if (!(this.literal === o.literal)) return false;
|
|
82
|
-
|
|
83
|
-
return super.equals(o);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
public getUsageText(): string {
|
|
87
|
-
return this.literal;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
public createBuilder(): LiteralArgumentBuilder<S> {
|
|
91
|
-
let builder: LiteralArgumentBuilder<S> = LiteralArgumentBuilder.literal(this.literal);
|
|
92
|
-
builder.requires(this.getRequirement());
|
|
93
|
-
builder.forward(this.getRedirect(), this.getRedirectModifier(), this.isFork());
|
|
94
|
-
if (this.getCommand() != null)
|
|
95
|
-
builder.executes(this.getCommand());
|
|
96
|
-
|
|
97
|
-
return builder;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
public getSortedKey(): string {
|
|
101
|
-
return this.literal;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
public getExamples(): Iterable<string> {
|
|
105
|
-
return [ this.literal ];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
public toString(): string {
|
|
109
|
-
return "<literal " + this.literal + ">";
|
|
110
|
-
}
|
|
111
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import CommandNode from "./CommandNode"
|
|
2
|
-
import StringReader from "../StringReader"
|
|
3
|
-
import ArgumentBuilder from "../builder/ArgumentBuilder"
|
|
4
|
-
import CommandContext from "../context/CommandContext"
|
|
5
|
-
import CommandContextBuilder from "../context/CommandContextBuilder"
|
|
6
|
-
import Suggestions from "../suggestion/Suggestions"
|
|
7
|
-
import SuggestionsBuilder from "../suggestion/SuggestionsBuilder"
|
|
8
|
-
|
|
9
|
-
export default class RootCommandNode<S> extends CommandNode<S> {
|
|
10
|
-
|
|
11
|
-
public constructor () {
|
|
12
|
-
super(null, s => true, null, (s: CommandContext<S>) => s.getSource(), false);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
public getNodeType(): string {
|
|
16
|
-
return "root"
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public getName(): string {
|
|
20
|
-
return "";
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public getUsageText(): string {
|
|
24
|
-
return "";
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public parse(reader: StringReader, contextBuilder: CommandContextBuilder<S>) {
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public listSuggestions(context: CommandContext<S>, builder: SuggestionsBuilder): Promise<Suggestions> {
|
|
32
|
-
return Suggestions.empty();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public isValidInput(input: String): boolean {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public equals(o: object): boolean {
|
|
40
|
-
|
|
41
|
-
if (this === o) return true;
|
|
42
|
-
if (!(o instanceof RootCommandNode)) return false;
|
|
43
|
-
|
|
44
|
-
return super.equals(o);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public createBuilder(): ArgumentBuilder<S, any> {
|
|
48
|
-
throw new Error("Cannot convert root into a builder");
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public getSortedKey(): string {
|
|
52
|
-
return "";
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public getExamples(): Iterable<string> {
|
|
56
|
-
return [];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public toString(): string {
|
|
60
|
-
return "<root>";
|
|
61
|
-
}
|
|
62
|
-
}
|
package/src/lib/util/isEqual.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
export default function isEqual(a: any, b: any): boolean { // @Warning: May cause an infinite loop
|
|
2
|
-
|
|
3
|
-
if (a === b)
|
|
4
|
-
return true;
|
|
5
|
-
|
|
6
|
-
if (typeof a != typeof b)
|
|
7
|
-
return false;
|
|
8
|
-
|
|
9
|
-
if (!(a instanceof Object))
|
|
10
|
-
return false;
|
|
11
|
-
|
|
12
|
-
if (typeof a === "function")
|
|
13
|
-
return a.toString() === b.toString();
|
|
14
|
-
|
|
15
|
-
if (a.constructor !== b.constructor)
|
|
16
|
-
return false;
|
|
17
|
-
|
|
18
|
-
if (a instanceof Map)
|
|
19
|
-
return isMapEqual(a, b);
|
|
20
|
-
|
|
21
|
-
if (a instanceof Set)
|
|
22
|
-
return isArrayEqual([...a], [...b]);
|
|
23
|
-
|
|
24
|
-
if (a instanceof Array)
|
|
25
|
-
return isArrayEqual(a, b);
|
|
26
|
-
|
|
27
|
-
if (typeof a === "object")
|
|
28
|
-
return isObjectEqual(a, b);
|
|
29
|
-
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function isMapEqual(a: Map<any, any>, b: Map<any, any>): boolean {
|
|
34
|
-
|
|
35
|
-
if (a.size != b.size) return false;
|
|
36
|
-
|
|
37
|
-
for (let [key, val] of a) {
|
|
38
|
-
const testVal = b.get(key);
|
|
39
|
-
if (!isEqual(testVal, val))
|
|
40
|
-
return false;
|
|
41
|
-
if (testVal === undefined && !b.has(key))
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function isArrayEqual(a: Array<any>, b: Array<any>): boolean {
|
|
49
|
-
|
|
50
|
-
if (a.length != b.length) return false;
|
|
51
|
-
|
|
52
|
-
for(let i = 0; i < a.length; i++)
|
|
53
|
-
if (!isEqual(a[i], b[i]))
|
|
54
|
-
return false;
|
|
55
|
-
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function isObjectEqual(a: TypicalObject, b: TypicalObject): boolean {
|
|
60
|
-
|
|
61
|
-
const aKeys = Object.keys(a);
|
|
62
|
-
const bKeys = Object.keys(b);
|
|
63
|
-
|
|
64
|
-
if (aKeys.length != bKeys.length) return false;
|
|
65
|
-
if (!aKeys.every(key => b.hasOwnProperty(key))) return false;
|
|
66
|
-
|
|
67
|
-
return aKeys.every((key: string) => {
|
|
68
|
-
return isEqual(a[key], b[key])
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
interface TypicalObject {
|
|
73
|
-
[ index: string ]: any;
|
|
74
|
-
[ index: number ]: any;
|
|
75
|
-
};
|
|
@@ -1,427 +0,0 @@
|
|
|
1
|
-
import { assert, expect } from "chai"
|
|
2
|
-
import Command from "../src/lib/Command"
|
|
3
|
-
import RedirectModifier from "../src/lib/RedirectModifier"
|
|
4
|
-
import CommandDispatcher from "../src/lib/CommandDispatcher"
|
|
5
|
-
import CommandSyntaxException from "../src/lib/exceptions/CommandSyntaxException"
|
|
6
|
-
import { literal } from "../src/lib/builder/LiteralArgumentBuilder"
|
|
7
|
-
import { argument } from "../src/lib/builder/RequiredArgumentBuilder"
|
|
8
|
-
import { DefaultType } from "../src/lib/arguments/ArgumentType"
|
|
9
|
-
import StringReader from "../src/lib/StringReader";
|
|
10
|
-
|
|
11
|
-
const { integer } = DefaultType
|
|
12
|
-
|
|
13
|
-
interface MockedCommand<S> extends Command<S> {
|
|
14
|
-
execHistory: object;
|
|
15
|
-
when(arg: any, returnValue: number): void;
|
|
16
|
-
verify(arg: any): number;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const createMockedCommand = (): MockedCommand<Object> => {
|
|
20
|
-
const execHistory = [];
|
|
21
|
-
const execCount = [];
|
|
22
|
-
const whenArg = new Map<any, number>();
|
|
23
|
-
const command = (arg): number => {
|
|
24
|
-
let i = execHistory.indexOf(arg);
|
|
25
|
-
if (i === -1) {
|
|
26
|
-
i = execCount.length;
|
|
27
|
-
execCount[i] = 0;
|
|
28
|
-
execHistory[i] = arg;
|
|
29
|
-
}
|
|
30
|
-
execCount[i]++;
|
|
31
|
-
|
|
32
|
-
if (whenArg.has(arg))
|
|
33
|
-
return whenArg.get(arg);
|
|
34
|
-
|
|
35
|
-
if (whenArg.has("anything"))
|
|
36
|
-
return whenArg.get("anything");
|
|
37
|
-
|
|
38
|
-
return 69;
|
|
39
|
-
}
|
|
40
|
-
command.verify = (arg) => {
|
|
41
|
-
let i = execHistory.indexOf(arg);
|
|
42
|
-
if (i > -1)
|
|
43
|
-
return execCount[i];
|
|
44
|
-
else if (arg === "anything") {
|
|
45
|
-
let result = 0;
|
|
46
|
-
execCount.forEach(v => result += v);
|
|
47
|
-
return result;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
command.when = (o, r: number) => {
|
|
51
|
-
whenArg.set(o, r);
|
|
52
|
-
}
|
|
53
|
-
command.execHistory = execHistory;
|
|
54
|
-
return command;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
describe('CommandDispatcherTest', () => {
|
|
58
|
-
|
|
59
|
-
let command;
|
|
60
|
-
let subject: CommandDispatcher<Object>;
|
|
61
|
-
const source: Object = {};
|
|
62
|
-
|
|
63
|
-
beforeEach(() => {
|
|
64
|
-
subject = new CommandDispatcher();
|
|
65
|
-
|
|
66
|
-
command = createMockedCommand();
|
|
67
|
-
command.when("anything", 42);
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
function inputWithOffset(input: string, offset: number): StringReader {
|
|
71
|
-
const result = new StringReader(input);
|
|
72
|
-
result.setCursor(offset);
|
|
73
|
-
return result;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
it('testCreateAndExecuteCommand', () => {
|
|
77
|
-
subject.register(literal("foo").executes(command));
|
|
78
|
-
assert.equal(subject.execute("foo", source), 42);
|
|
79
|
-
assert.equal(command.verify("anything") === 1, true);
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
it('testCreateAndExecuteOffsetCommand', () => {
|
|
83
|
-
subject.register(literal("foo").executes(command));
|
|
84
|
-
assert.equal(subject.execute(inputWithOffset("/foo", 1), source), 42);
|
|
85
|
-
assert.equal(command.verify("anything") === 1, true);
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
it('testCreateAndMergeCommands', () => {
|
|
89
|
-
subject.register(literal("base").then(literal("foo").executes(command)));
|
|
90
|
-
subject.register(literal("base").then(literal("bar").executes(command)));
|
|
91
|
-
|
|
92
|
-
assert.equal(subject.execute("base foo", source), 42);
|
|
93
|
-
assert.equal(subject.execute("base bar", source), 42);
|
|
94
|
-
assert.equal(command.verify("anything") === 2, true);
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
it('testExecuteUnknownCommand', done => {
|
|
98
|
-
subject.register(literal("bar"));
|
|
99
|
-
subject.register(literal("baz"));
|
|
100
|
-
|
|
101
|
-
try {
|
|
102
|
-
subject.execute("foo", source);
|
|
103
|
-
} catch (ex) {
|
|
104
|
-
assert.equal(ex.getType(), CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand());
|
|
105
|
-
assert.equal(ex.getCursor(), 0);
|
|
106
|
-
done();
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
assert.fail();
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
it('testExecuteImpermissibleCommand', done => {
|
|
114
|
-
subject.register(literal("foo").requires(s => false));
|
|
115
|
-
|
|
116
|
-
try {
|
|
117
|
-
subject.execute("foo", source);
|
|
118
|
-
} catch (ex) {
|
|
119
|
-
assert.equal(ex.getType(), CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand());
|
|
120
|
-
assert.equal(ex.getCursor(), 0);
|
|
121
|
-
done();
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
assert.fail();
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
it('testExecuteEmptyCommand', done => {
|
|
129
|
-
subject.register(literal(""));
|
|
130
|
-
|
|
131
|
-
try {
|
|
132
|
-
subject.execute("", source);
|
|
133
|
-
} catch (ex) {
|
|
134
|
-
assert.equal(ex.getType(), CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand());
|
|
135
|
-
assert.equal(ex.getCursor(), 0);
|
|
136
|
-
done();
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
assert.fail();
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
it('testExecuteUnknownSubcommand', done => {
|
|
144
|
-
subject.register(literal("foo").executes(command));
|
|
145
|
-
|
|
146
|
-
try {
|
|
147
|
-
subject.execute("foo bar", source);
|
|
148
|
-
} catch (ex) {
|
|
149
|
-
assert.equal(ex.getType(), CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument());
|
|
150
|
-
assert.equal(ex.getCursor(), 4);
|
|
151
|
-
done();
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
assert.fail();
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
it('testExecuteIncorrectLiteral', done => {
|
|
159
|
-
subject.register(literal("foo").executes(command).then(literal("bar")));
|
|
160
|
-
|
|
161
|
-
try {
|
|
162
|
-
subject.execute("foo baz", source);
|
|
163
|
-
} catch (ex) {
|
|
164
|
-
assert.equal(ex.getType(), CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument());
|
|
165
|
-
assert.equal(ex.getCursor(), 4);
|
|
166
|
-
done();
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
assert.fail();
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
it('testExecuteAmbiguousIncorrectArgument', done => {
|
|
174
|
-
subject.register(
|
|
175
|
-
literal("foo").executes(command)
|
|
176
|
-
.then(literal("bar"))
|
|
177
|
-
.then(literal("baz"))
|
|
178
|
-
);
|
|
179
|
-
|
|
180
|
-
try {
|
|
181
|
-
subject.execute("foo unknown", source);
|
|
182
|
-
} catch (ex) {
|
|
183
|
-
assert.equal(ex.getType(), CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument());
|
|
184
|
-
assert.equal(ex.getCursor(), 4);
|
|
185
|
-
done();
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
assert.fail();
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
it('testExecuteSubcommand', () => {
|
|
193
|
-
const subCommand = createMockedCommand()
|
|
194
|
-
subCommand.when("anything", 100);
|
|
195
|
-
|
|
196
|
-
subject.register(literal("foo").then(
|
|
197
|
-
literal("a")
|
|
198
|
-
).then(
|
|
199
|
-
literal("=").executes(subCommand)
|
|
200
|
-
).then(
|
|
201
|
-
literal("c")
|
|
202
|
-
).executes(command));
|
|
203
|
-
|
|
204
|
-
assert.equal(subject.execute("foo =", source), 100);
|
|
205
|
-
assert.equal(subCommand.verify("anything") === 1, true);
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
it('testParseIncompleteLiteral', () => {
|
|
209
|
-
subject.register(literal("foo").then(literal("bar").executes(command)));
|
|
210
|
-
|
|
211
|
-
const parse = subject.parse("foo ", source);
|
|
212
|
-
assert.equal(parse.getReader().getRemaining(), " ");
|
|
213
|
-
assert.equal(parse.getContext().getNodes().length, 1);
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
it('testParseIncompleteArgument', () => {
|
|
217
|
-
subject.register(literal("foo").then(argument("bar", integer()).executes(command)));
|
|
218
|
-
|
|
219
|
-
const parse = subject.parse("foo ", source);
|
|
220
|
-
assert.equal(parse.getReader().getRemaining(), " ");
|
|
221
|
-
assert.equal(parse.getContext().getNodes().length, 1);
|
|
222
|
-
})
|
|
223
|
-
|
|
224
|
-
it('testExecuteAmbiguiousParentSubcommand', () => {
|
|
225
|
-
const subCommand = createMockedCommand()
|
|
226
|
-
subCommand.when("anything", 100);
|
|
227
|
-
|
|
228
|
-
subject.register(
|
|
229
|
-
literal("test")
|
|
230
|
-
.then(
|
|
231
|
-
argument("incorrect", integer())
|
|
232
|
-
.executes(command)
|
|
233
|
-
)
|
|
234
|
-
.then(
|
|
235
|
-
argument("right", integer())
|
|
236
|
-
.then(
|
|
237
|
-
argument("sub", integer())
|
|
238
|
-
.executes(subCommand)
|
|
239
|
-
)
|
|
240
|
-
)
|
|
241
|
-
);
|
|
242
|
-
|
|
243
|
-
assert.equal(subject.execute("test 1 2", source), 100);
|
|
244
|
-
assert.equal(subCommand.verify("anything") > 0, true);
|
|
245
|
-
assert.equal(command.verify("anything") === 0, true);
|
|
246
|
-
})
|
|
247
|
-
|
|
248
|
-
it('testExecuteAmbiguiousParentSubcommandViaRedirect', () => {
|
|
249
|
-
const subCommand = createMockedCommand()
|
|
250
|
-
subCommand.when("anything", 100);
|
|
251
|
-
|
|
252
|
-
const real = subject.register(
|
|
253
|
-
literal("test")
|
|
254
|
-
.then(
|
|
255
|
-
argument("incorrect", integer())
|
|
256
|
-
.executes(command)
|
|
257
|
-
)
|
|
258
|
-
.then(
|
|
259
|
-
argument("right", integer())
|
|
260
|
-
.then(
|
|
261
|
-
argument("sub", integer())
|
|
262
|
-
.executes(subCommand)
|
|
263
|
-
)
|
|
264
|
-
)
|
|
265
|
-
);
|
|
266
|
-
|
|
267
|
-
subject.register(literal("redirect").redirect(real));
|
|
268
|
-
|
|
269
|
-
assert.equal(subject.execute("redirect 1 2", source), 100);
|
|
270
|
-
assert.equal(subCommand.verify("anything") > 0, true);
|
|
271
|
-
assert.equal(command.verify("anything") === 0, true);
|
|
272
|
-
})
|
|
273
|
-
|
|
274
|
-
it('testExecuteRedirectedMultipleTimes', () => {
|
|
275
|
-
const concreteNode = subject.register(literal("actual").executes(command));
|
|
276
|
-
const redirectNode = subject.register(literal("redirected").redirect(subject.getRoot()));
|
|
277
|
-
|
|
278
|
-
const input = "redirected redirected actual";
|
|
279
|
-
|
|
280
|
-
const parse = subject.parse(input, source);
|
|
281
|
-
assert.equal(parse.getContext().getRange().get(input), "redirected");
|
|
282
|
-
assert.equal(parse.getContext().getNodes().length, 1);
|
|
283
|
-
assert.equal(parse.getContext().getRootNode(), subject.getRoot());
|
|
284
|
-
expect(parse.getContext().getNodes()[0].getRange()).to.deep.equal(parse.getContext().getRange());
|
|
285
|
-
assert.equal(parse.getContext().getNodes()[0].getNode(), redirectNode);
|
|
286
|
-
|
|
287
|
-
const child1 = parse.getContext().getChild();
|
|
288
|
-
expect(child1).to.not.equal(null)
|
|
289
|
-
assert.equal(child1.getRange().get(input), "redirected");
|
|
290
|
-
assert.equal(child1.getNodes().length, 1);
|
|
291
|
-
assert.equal(child1.getRootNode(), subject.getRoot());
|
|
292
|
-
expect(child1.getNodes()[0].getRange()).to.deep.equal(child1.getRange());
|
|
293
|
-
assert.equal(child1.getNodes()[0].getNode(), redirectNode);
|
|
294
|
-
|
|
295
|
-
const child2 = child1.getChild();
|
|
296
|
-
expect(child2).to.not.equal(null)
|
|
297
|
-
assert.equal(child2.getRange().get(input), "actual");
|
|
298
|
-
assert.equal(child2.getNodes().length, 1);
|
|
299
|
-
assert.equal(child2.getRootNode(), subject.getRoot());
|
|
300
|
-
expect(child2.getNodes()[0].getRange()).to.deep.equal(child2.getRange());
|
|
301
|
-
assert.equal(child2.getNodes()[0].getNode(), concreteNode);
|
|
302
|
-
|
|
303
|
-
assert.equal(subject.execute(parse), 42);
|
|
304
|
-
assert.equal(command.verify("anything") > 0, true);
|
|
305
|
-
})
|
|
306
|
-
|
|
307
|
-
it('testExecuteRedirected', () => {
|
|
308
|
-
const source1 = { name: "Obj1" };
|
|
309
|
-
const source2 = { name: "Obj2" };
|
|
310
|
-
const mockedModifier: RedirectModifier<Object> = {
|
|
311
|
-
apply(obj) {
|
|
312
|
-
if (obj.getSource() === source)
|
|
313
|
-
return [source1, source2]
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
const concreteNode = subject.register(literal("actual").executes(command));
|
|
318
|
-
const redirectNode = subject.register(literal("redirected").fork(subject.getRoot(), mockedModifier));
|
|
319
|
-
|
|
320
|
-
const input = "redirected actual";
|
|
321
|
-
const parse = subject.parse(input, source);
|
|
322
|
-
assert.equal(parse.getContext().getRange().get(input), "redirected");
|
|
323
|
-
assert.equal(parse.getContext().getNodes().length, 1);
|
|
324
|
-
assert.equal(parse.getContext().getRootNode(), subject.getRoot());
|
|
325
|
-
expect(parse.getContext().getNodes()[0].getRange()).to.deep.equal(parse.getContext().getRange());
|
|
326
|
-
assert.equal(parse.getContext().getNodes()[0].getNode(), redirectNode);
|
|
327
|
-
assert.equal(parse.getContext().getSource(), source);
|
|
328
|
-
|
|
329
|
-
const parent = parse.getContext().getChild();
|
|
330
|
-
expect(parent).to.not.equal(null);
|
|
331
|
-
assert.equal(parent.getRange().get(input), "actual");
|
|
332
|
-
assert.equal(parent.getNodes().length, 1);
|
|
333
|
-
assert.equal(parse.getContext().getRootNode(), subject.getRoot());
|
|
334
|
-
expect(parent.getNodes()[0].getRange()).to.deep.equal(parent.getRange());
|
|
335
|
-
assert.equal(parent.getNodes()[0].getNode(), concreteNode);
|
|
336
|
-
assert.equal(parent.getSource(), source);
|
|
337
|
-
assert.equal(subject.execute(parse), 2);
|
|
338
|
-
|
|
339
|
-
let flag1 = 0, flag2 = 0;
|
|
340
|
-
command.execHistory.forEach(v => {
|
|
341
|
-
if (v.source === source1) flag1++;
|
|
342
|
-
if (v.source === source2) flag2++;
|
|
343
|
-
})
|
|
344
|
-
if (flag1 === 0 || flag2 === 0)
|
|
345
|
-
assert.fail();
|
|
346
|
-
})
|
|
347
|
-
|
|
348
|
-
it('testExecuteOrphanedSubcommand', done => {
|
|
349
|
-
subject.register(literal("foo").then(
|
|
350
|
-
argument("bar", integer())
|
|
351
|
-
).executes(command));
|
|
352
|
-
|
|
353
|
-
try {
|
|
354
|
-
subject.execute("foo 5", source);
|
|
355
|
-
} catch (ex) {
|
|
356
|
-
assert.equal(ex.getType(), CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand());
|
|
357
|
-
assert.equal(ex.getCursor(), 5);
|
|
358
|
-
done();
|
|
359
|
-
return;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
assert.fail();
|
|
363
|
-
})
|
|
364
|
-
|
|
365
|
-
it('testExecute_invalidOther', () => {
|
|
366
|
-
const wrongCommand = createMockedCommand();
|
|
367
|
-
|
|
368
|
-
subject.register(literal("w").executes(wrongCommand));
|
|
369
|
-
subject.register(literal("world").executes(command));
|
|
370
|
-
|
|
371
|
-
assert.equal(subject.execute("world", source), 42);
|
|
372
|
-
assert.equal(wrongCommand.verify("anything") === 0, true);
|
|
373
|
-
assert.equal(command.verify("anything") > 0, true);
|
|
374
|
-
})
|
|
375
|
-
|
|
376
|
-
it('parse_noSpaceSeparator', done => {
|
|
377
|
-
subject.register(literal("foo").then(argument("bar", integer()).executes(command)));
|
|
378
|
-
|
|
379
|
-
try {
|
|
380
|
-
subject.execute("foo$", source);
|
|
381
|
-
} catch (ex) {
|
|
382
|
-
assert.equal(ex.getType(), CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand());
|
|
383
|
-
assert.equal(ex.getCursor(), 0);
|
|
384
|
-
done();
|
|
385
|
-
return;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
assert.fail();
|
|
389
|
-
})
|
|
390
|
-
|
|
391
|
-
it('testExecuteInvalidSubcommand', done => {
|
|
392
|
-
subject.register(literal("foo").then(
|
|
393
|
-
argument("bar", integer())
|
|
394
|
-
).executes(command));
|
|
395
|
-
|
|
396
|
-
try {
|
|
397
|
-
subject.execute("foo bar", source);
|
|
398
|
-
} catch (ex) {
|
|
399
|
-
assert.equal(ex.getType(), CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedInt());
|
|
400
|
-
assert.equal(ex.getCursor(), 4);
|
|
401
|
-
done();
|
|
402
|
-
return;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
assert.fail();
|
|
406
|
-
})
|
|
407
|
-
|
|
408
|
-
it('testGetPath', () => {
|
|
409
|
-
const bar = literal("bar").build();
|
|
410
|
-
subject.register(literal("foo").then(bar));
|
|
411
|
-
|
|
412
|
-
expect(subject.getPath(bar)).to.deep.equal(["foo", "bar"]);
|
|
413
|
-
})
|
|
414
|
-
|
|
415
|
-
it('testFindNodeExists', () => {
|
|
416
|
-
const bar = literal("bar").build();
|
|
417
|
-
subject.register(literal("foo").then(bar));
|
|
418
|
-
|
|
419
|
-
assert.equal(subject.findNode(["foo", "bar"]), bar);
|
|
420
|
-
})
|
|
421
|
-
|
|
422
|
-
it('testFindNodeDoesntExist', () => {
|
|
423
|
-
expect(subject.findNode(["foo", "bar"])).to.equal(null);
|
|
424
|
-
})
|
|
425
|
-
})
|
|
426
|
-
|
|
427
|
-
|