File: share_check.cpp

package info (click to toggle)
teg 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,036 kB
  • sloc: cpp: 16,819; xml: 1,313; makefile: 268; sh: 195; ansic: 112
file content (71 lines) | stat: -rw-r--r-- 1,500 bytes parent folder | download | duplicates (2)
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
// SPDX-License-Identifier: GPL-2.0-or-later

#include "share.h"

#include <type_traits>

#include <gtest/gtest.h>

TEST(Share, is_option)
{
	EXPECT_TRUE(is_option("--foo", "--foo"));
	EXPECT_TRUE(is_option("--foo", "-f"));
	EXPECT_TRUE(is_option("--foo", "-fx"));
	EXPECT_FALSE(is_option("--foo", "--fooabr"));
	EXPECT_FALSE(is_option("--foo", "-x"));
	EXPECT_FALSE(is_option("--foo", "-xf"));
}

TEST(Share, get_option)
{
	char const* argv[] = {
		"/foo/bar",
		"--bar=42",
		"--baz", "23",
		"--invalid=",
		"-x=y",
		"-z", "z",
		"-E"
	};
	constexpr auto argc = std::extent<decltype(argv)>::value;
	int i = 1;

	// not present
	EXPECT_EQ(nullptr, get_option("--abc", argv, &i, argc));
	EXPECT_EQ(i, 1);

	// not the correct start position
	i = 1;
	EXPECT_EQ(nullptr, get_option("--baz", argv, &i, argc));
	EXPECT_EQ(i, 1);

	// first argument
	i = 1;
	EXPECT_STREQ("42", get_option("--bar", argv, &i, argc));
	EXPECT_EQ(i, 1);

	// second argument
	i = 2;
	EXPECT_STREQ("23", get_option("--baz", argv, &i, argc));
	EXPECT_EQ(i, 3);

	// third/defect argument
	i = 3;
	EXPECT_EQ(nullptr, get_option("--invalid", argv, &i, argc));
	EXPECT_EQ(i, 3);

	// short option with equal sign
	i = 5;
	EXPECT_STREQ("y", get_option("--xxx", argv, &i, argc));
	EXPECT_EQ(i, 5);

	// short option with space delimiter
	i = 6;
	EXPECT_STREQ("z", get_option("--zzz", argv, &i, argc));
	EXPECT_EQ(i, 7);

	// defect last option
	i = 7;
	EXPECT_EQ(nullptr, get_option("--End", argv, &i, argc));
	EXPECT_EQ(i, 7);
}