1
- package fr .zcraft .quartzlib .components .commands . internal ;
1
+ package fr .zcraft .quartzlib .components .commands ;
2
2
3
3
import org .junit .jupiter .api .Assertions ;
4
+ import org .junit .jupiter .api .BeforeEach ;
4
5
import org .junit .jupiter .api .Test ;
5
6
6
7
import java .util .stream .StreamSupport ;
@@ -16,20 +17,27 @@ static public void staticMethod () {}
16
17
}
17
18
18
19
public class CommandGraphTests {
20
+ private CommandManager commands ;
21
+
22
+ @ BeforeEach
23
+ public void beforeEach () {
24
+ commands = new CommandManager ();
25
+ }
26
+
19
27
@ Test public void canDiscoverBasicSubcommands () {
20
28
class FooCommand {
21
29
public void add () {}
22
30
public void get () {}
23
31
public void list () {}
24
32
}
25
33
26
- CommandGroup commandGroup = new CommandGroup (FooCommand .class , () -> new FooCommand (), "foo" );
34
+ CommandGroup commandGroup = new CommandGroup (FooCommand .class , () -> new FooCommand (), "foo" , new ArgumentTypeHandlerCollection () );
27
35
String [] commandNames = StreamSupport .stream (commandGroup .getSubCommands ().spliterator (), false ).map (CommandNode ::getName ).toArray (String []::new );
28
36
Assertions .assertArrayEquals (new String [] {"add" , "get" , "list" }, commandNames );
29
37
}
30
38
31
39
@ Test public void onlyDiscoversPublicMethods () {
32
- CommandGroup commandGroup = new CommandGroup (CommandWithStatics .class , () -> new CommandWithStatics (), "foo" );
40
+ CommandGroup commandGroup = new CommandGroup (CommandWithStatics .class , () -> new CommandWithStatics (), "foo" , new ArgumentTypeHandlerCollection () );
33
41
String [] commandNames = StreamSupport .stream (commandGroup .getSubCommands ().spliterator (), false ).map (CommandNode ::getName ).toArray (String []::new );
34
42
Assertions .assertArrayEquals (new String [] {"add" , "delete" }, commandNames );
35
43
}
@@ -43,9 +51,32 @@ class FooCommand {
43
51
public void list () { ran [2 ] = true ; }
44
52
}
45
53
46
- FooCommand f = new FooCommand ();
47
- CommandGroup commandGroup = new CommandGroup (FooCommand .class , () -> new FooCommand (),"foo" );
48
- commandGroup .run ("get" );
54
+ commands .registerCommand ("foo" , FooCommand .class , () -> new FooCommand ());
55
+ commands .run ("foo" , "get" );
49
56
Assertions .assertArrayEquals (new boolean [] { false , true , false }, ran );
50
57
}
58
+
59
+ @ Test public void canReceiveStringArguments () {
60
+ final String [] argValue = {"" };
61
+
62
+ class FooCommand {
63
+ public void add (String arg ) { argValue [0 ] = arg ; }
64
+ }
65
+
66
+ commands .registerCommand ("foo" , FooCommand .class , () -> new FooCommand ());
67
+ commands .run ("foo" , "add" , "pomf" );
68
+ Assertions .assertArrayEquals (new String [] { "pomf" }, argValue );
69
+ }
70
+
71
+ @ Test public void canReceiveParsedArguments () {
72
+ final int [] argValue = {0 };
73
+
74
+ class FooCommand {
75
+ public void add (Integer arg ) { argValue [0 ] = arg ; }
76
+ }
77
+
78
+ commands .registerCommand ("foo" , FooCommand .class , () -> new FooCommand ());
79
+ commands .run ("foo" , "add" , "42" );
80
+ Assertions .assertArrayEquals (new int [] { 42 }, argValue );
81
+ }
51
82
}
0 commit comments