summaryrefslogtreecommitdiffstats
path: root/third_party/wasm2c/src/test-option-parser.cc
blob: 757e6578ff775675146dabf43fb41634fb02a4cd (plain)
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
// Copyright 2019 WebAssembly Community Group participants
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "gtest/gtest.h"

#include <string>

#include "wabt/option-parser.h"

using namespace wabt;

#define ERROR_ENDING "\nTry '--help' for more information."

TEST(OptionParser, LongFlag) {
  bool flag = false;
  OptionParser parser("prog", "desc");
  parser.AddOption("flag", "help", [&]() { flag = true; });
  const char* args[] = {"prog name", "--flag"};
  parser.Parse(2, const_cast<char**>(args));
  EXPECT_EQ(true, flag);
}

TEST(OptionParser, ShortAndLongFlag) {
  int count = 0;
  OptionParser parser("prog", "desc");
  parser.AddOption('f', "flag", "help", [&]() { ++count; });
  const char* args[] = {"prog name", "-f", "--flag", "-f", "--flag"};
  parser.Parse(5, const_cast<char**>(args));
  EXPECT_EQ(4, count);
}

TEST(OptionParser, ShortFlagCombined) {
  int count = 0;
  OptionParser parser("prog", "desc");
  parser.AddOption('a', "a", "help", [&]() { count += 1; });
  parser.AddOption('b', "b", "help", [&]() { count += 2; });
  const char* args[] = {"prog name", "-aa", "-abb"};
  parser.Parse(3, const_cast<char**>(args));
  EXPECT_EQ(7, count);
}

TEST(OptionParser, UnknownShortOption) {
  std::string error;
  OptionParser parser("prog", "desc");
  parser.SetErrorCallback([&](const char* msg) { error = msg; });
  const char* args[] = {"prog name", "-f"};
  parser.Parse(2, const_cast<char**>(args));
  EXPECT_EQ(error, "prog: unknown option '-f'" ERROR_ENDING);
}

TEST(OptionParser, UnknownLongOption) {
  std::string error;
  OptionParser parser("prog", "desc");
  parser.SetErrorCallback([&](const char* msg) { error = msg; });
  const char* args[] = {"prog name", "--foo"};
  parser.Parse(2, const_cast<char**>(args));
  EXPECT_EQ(error, "prog: unknown option '--foo'" ERROR_ENDING);
}

TEST(OptionParser, ShortAndLongParam) {
  std::string param;
  OptionParser parser("prog", "desc");
  parser.AddOption('p', "param", "metavar", "help",
                   [&](const char* arg) { param += arg; });
  const char* args[] = {"prog name", "-p", "h", "--param", "el", "--param=lo"};
  parser.Parse(6, const_cast<char**>(args));
  EXPECT_EQ("hello", param);
}

TEST(OptionParser, MissingParam) {
  std::string error;
  std::string param;
  OptionParser parser("prog", "desc");
  parser.SetErrorCallback([&](const char* msg) { error = msg; });
  parser.AddOption('p', "param", "metavar", "help",
                   [&](const char* arg) { param = arg; });
  const char* args[] = {"prog name", "--param"};
  parser.Parse(2, const_cast<char**>(args));
  EXPECT_EQ("", param);
  EXPECT_EQ(error, "prog: option '--param' requires argument" ERROR_ENDING);
}

TEST(OptionParser, MissingArgument) {
  std::string error;
  OptionParser parser("prog", "desc");
  parser.AddArgument("arg", OptionParser::ArgumentCount::One,
                     [&](const char* arg) {});
  parser.SetErrorCallback([&](const char* msg) { error = msg; });
  const char* args[] = {"prog name"};
  parser.Parse(1, const_cast<char**>(args));
  EXPECT_EQ(error, "prog: expected arg argument." ERROR_ENDING);
}

TEST(OptionParser, FlagCombinedAfterShortParam) {
  std::string error;
  std::string param;
  bool has_x = false;

  OptionParser parser("prog", "desc");
  parser.SetErrorCallback([&](const char* msg) { error = msg; });
  parser.AddOption('p', "p", "metavar", "help",
                   [&](const char* arg) { param = arg; });
  parser.AddOption('x', "x", "help", [&]() { has_x = true; });
  const char* args[] = {"prog name", "-px", "stuff"};
  parser.Parse(3, const_cast<char**>(args));
  EXPECT_EQ("", param);
  EXPECT_TRUE(has_x);
  EXPECT_EQ(error, "prog: unexpected argument 'stuff'" ERROR_ENDING);
}

TEST(OptionParser, OneArgument) {
  std::string argument;
  OptionParser parser("prog", "desc");
  parser.AddArgument("arg", OptionParser::ArgumentCount::One,
                     [&](const char* arg) { argument = arg; });
  const char* args[] = {"prog name", "hello"};
  parser.Parse(2, const_cast<char**>(args));
  EXPECT_EQ("hello", argument);
}

TEST(OptionParser, TooManyArguments) {
  std::string error;
  OptionParser parser("prog", "desc");
  parser.SetErrorCallback([&](const char* msg) { error = msg; });
  parser.AddArgument("arg", OptionParser::ArgumentCount::One,
                     [&](const char* arg) {});
  const char* args[] = {"prog name", "hello", "goodbye"};
  parser.Parse(3, const_cast<char**>(args));
  EXPECT_EQ(error, "prog: unexpected argument 'goodbye'" ERROR_ENDING);
}

TEST(OptionParser, OneOrMoreArguments) {
  std::string argument;
  OptionParser parser("prog", "desc");
  parser.AddArgument("arg", OptionParser::ArgumentCount::OneOrMore,
                     [&](const char* arg) { argument += arg; });
  const char* args[] = {"prog name", "hello", "goodbye"};
  parser.Parse(3, const_cast<char**>(args));
  EXPECT_EQ("hellogoodbye", argument);
}

TEST(OptionParser, ZeroOrMoreArguments) {
  std::string argument;
  OptionParser parser("prog", "desc");
  parser.AddArgument("arg", OptionParser::ArgumentCount::ZeroOrMore,
                     [&](const char* arg) { argument += arg; });

  const char* args_none[] = {"prog name"};
  parser.Parse(1, const_cast<char**>(args_none));
  EXPECT_EQ("", argument);

  const char* args_many[] = {"prog name", "hello", "goodbye"};
  parser.Parse(3, const_cast<char**>(args_many));
  EXPECT_EQ("hellogoodbye", argument);
}

TEST(OptionParser, StopProccessing) {
  std::string argument;
  bool has_x = false;
  OptionParser parser("prog", "desc");
  parser.AddArgument("arg", OptionParser::ArgumentCount::ZeroOrMore,
                     [&](const char* arg) { argument += arg; });
  parser.AddOption('x', "x", "help", [&]() { has_x = true; });

  const char* args_many[] = {"prog name", "-x", "--", "foo", "-x", "-y", "bar"};
  parser.Parse(7, const_cast<char**>(args_many));
  EXPECT_TRUE(has_x);
  EXPECT_EQ("foo-x-ybar", argument);
}