aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/CmdTest.scala
blob: f78611d7aa5edefef69657b4e3e2f55cbd3c6fe6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package commando

import utest._

object CmdTests extends TestSuite {

  val cbx = commando.Command(
    "cbx",
    commando.Optional("server", Some('s'), Some(commando.Positional("name"))),
    commando.Command(
      "version",
      commando.Optional("verbose", Some('v'), Some(commando.Positional("k=v", false)))),
    commando.Command("login",
                commando.Positional("server_url"),
                commando.Positional("username", false),
                commando.Positional("password", false)),
    commando.Command("run",
                commando.Optional("file", Some('f'), Some(commando.Positional("file_name"))),
                commando.Optional("force", None),
                commando.Positional("pipeline", false)),
    commando.Command("level1",
                commando.Command("level2-1",
                            commando.Positional("p2"),
                            commando.Command("level3", commando.Positional("p3"))),
                commando.Command("level2-2"))
  )

  def parse(in: String): CommandLine = commando.parse(cbx, in.split(" ").tail) match {
    case Left(ex) => throw ex
    case Right(res) => res
  }

  def shouldFail(in: String) =
    assert(commando.parse(cbx, in.split(" ").tail).isLeft)

  val tests = Tests {
    "printUsage" - {
      cbx.usage
    }
    "simple" - {
      assert(
        parse("cbx version").subcommand.get == CommandLine("version",
                                                           Map.empty,
                                                           None))
    }
    "emptyAllowedOption" - {
      assert(
        parse("cbx version -v").subcommand.get == CommandLine(
          "version",
          Map("verbose" -> ""),
          None))
      assert(
        parse("cbx version --verbose").subcommand.get == CommandLine(
          "version",
          Map("verbose" -> ""),
          None))
    }
    "setAllowedOption" - {
      assert(
        parse("cbx version -v x").subcommand.get == CommandLine(
          "version",
          Map("verbose" -> "x"),
          None))
      assert(
        parse("cbx version --verbose x").subcommand.get == CommandLine(
          "version",
          Map("verbose" -> "x"),
          None))
      assert(
        parse("cbx version --verbose=x").subcommand.get == CommandLine(
          "version",
          Map("verbose" -> "x"),
          None))
      assert(
        parse("cbx version --verbose=x=y").subcommand.get == CommandLine(
          "version",
          Map("verbose" -> "x=y"),
          None))
      assert(
        parse("cbx version --verbose=x=y,z=w").subcommand.get == CommandLine(
          "version",
          Map("verbose" -> "x=y,z=w"),
          None))
      assert(
        parse("cbx version --verbose x=y,z=w").subcommand.get == CommandLine(
          "version",
          Map("verbose" -> "x=y,z=w"),
          None))
      shouldFail("cbx version --verbose x=y z=w")
    }
    "requiredArgOption" - {
      assert(parse("cbx run").subcommand.get == CommandLine("run", Map(), None)) // make sure it works first
      assert(
        parse("cbx run -f x").subcommand.get == CommandLine("run",
                                                            Map("file" -> "x"),
                                                            None))
      assert(
        parse("cbx run --file x").subcommand.get == CommandLine(
          "run",
          Map("file" -> "x"),
          None))
      assert(
        parse("cbx run --file=x").subcommand.get == CommandLine(
          "run",
          Map("file" -> "x"),
          None))
      assert(
        parse("cbx run --file=x=y,z=w").subcommand.get == CommandLine(
          "run",
          Map("file" -> "x=y,z=w"),
          None))
      shouldFail("cbx run --file")
      shouldFail("cbx run --file --")
    }
    "noArgOption" - {
      shouldFail("cbx run --force=x")
      assert(
        parse("cbx run --force x").subcommand.get == CommandLine(
          "run",
          Map("force" -> "", "pipeline" -> "x"),
          None))
    }
    "globalOption" - {
      assert(parse("cbx --server run run").arguments == Map("server" -> "run"))
      assert(
        parse("cbx --server run run").subcommand.get == CommandLine("run",
                                                                    Map.empty,
                                                                    None))
      assert(parse("cbx -s run run").arguments == Map("server" -> "run"))
      assert(
        parse("cbx -s run run").subcommand.get == CommandLine("run",
                                                              Map.empty,
                                                              None))
      assert(parse("cbx --server=run run").arguments == Map("server" -> "run"))
      assert(
        parse("cbx --server=run run").subcommand.get == CommandLine("run",
                                                                    Map.empty,
                                                                    None))
      shouldFail("cbx -x run")
      shouldFail("cbx --x run")
    }
    "parameter" - {
      assert(
        parse("cbx login x").subcommand.get == CommandLine(
          "login",
          Map("server_url" -> "x"),
          None))
      assert(
        parse("cbx login x y").subcommand.get == CommandLine(
          "login",
          Map("server_url" -> "x", "username" -> "y"),
          None))
      assert(
        parse("cbx login x y z").subcommand.get == CommandLine(
          "login",
          Map("server_url" -> "x", "username" -> "y", "password" -> "z"),
          None))
      shouldFail("cbx login - y z w")
      assert(
        parse("cbx login - y").subcommand.get == CommandLine(
          "login",
          Map("server_url" -> "-", "username" -> "y"),
          None))
    }
    "outOfOrderOptions" - {
      assert(
        parse("cbx run --force pipelinename -f x").subcommand.get == CommandLine(
          "run",
          Map("force" -> "", "pipeline" -> "pipelinename", "file" -> "x"),
          None))
      assert(
        parse("cbx run --force -- -f").subcommand.get == CommandLine(
          "run",
          Map("force" -> "", "pipeline" -> "-f"),
          None))
      assert(
        parse("cbx run --force -- --file").subcommand.get == CommandLine(
          "run",
          Map("force" -> "", "pipeline" -> "--file"),
          None))
      assert(
        parse("cbx run --force -- --").subcommand.get == CommandLine(
          "run",
          Map("force" -> "", "pipeline" -> "--"),
          None))
      shouldFail("cbx run --force -- -f x") // too many parameters
    }
    "nested1" - {
      val line = parse("cbx level1 level2-1 x=y level3 z").subcommand.get
      val expected = CommandLine(
        "level1",
        Map.empty,
        Some(
          CommandLine("level2-1",
                      Map("p2" -> "x=y"),
                      Some(CommandLine("level3", Map("p3" -> "z"), None)))))
      assert(line == expected)
    }
    "nested2" - {
      val line = parse("cbx level1 level2-2 --").subcommand.get
      val expected = CommandLine("level1",
                                 Map.empty,
                                 Some(CommandLine("level2-2", Map.empty, None)))
      assert(line == expected)
    }
  }
}