aboutsummaryrefslogtreecommitdiff
blob: bee9be570063dce7177696afb353e658a0f781e5 (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
/*
   Please use git log for copyright holder and year information

   This file is part of libbash.

   libbash is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   (at your option) any later version.

   libbash is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with libbash.  If not, see <http://www.gnu.org/licenses/>.
*/
///
/// \file echo_tests.cpp
/// \brief series of unit tests for echo built in
/// \author Nathan Eloe
///
#include <iostream>
#include <sstream>
#include <vector>

#include <gtest/gtest.h>

#include "core/interpreter.h"
#include "cppbash_builtin.h"

using namespace std;

static void test_echo(const string& expected, std::initializer_list<string> args)
{
  stringstream test_output;
  interpreter walker;
  cppbash_builtin::exec("echo",args,test_output,cerr,cin,walker);
  ASSERT_EQ(expected, test_output.str());
}

#define TEST_ECHO(name, expected, ...) \
	TEST(echo_builtin_test, name) { test_echo(expected, {__VA_ARGS__}); }

TEST_ECHO(simple_output,                  "hello world\n", "hello", "world")
TEST_ECHO(suppress_newline,               "foo",           "-n", "foo")
TEST_ECHO(enable_escape,                  "foo\t\n",       "-e", "foo\\t")
TEST_ECHO(no_escape,                      "foo\\t\n",      "foo\\t")
TEST_ECHO(only_options,                   "foo -e",        "-n", "foo", "-e")
TEST_ECHO(only_options2,                  "foo -n\n",      "foo", "-n")
TEST_ECHO(combined_options,               "fo\to",         "-ne", "fo\\to")
TEST_ECHO(combined_options2,              "fo\\to",        "-enE", "fo\\to")
TEST_ECHO(fake_options,                   "-nea fo\\to\n", "-nea", "fo\\to")
TEST_ECHO(combined_options_alternating_e, "fo\to",         "-enEeEe", "fo\\to")
TEST_ECHO(conflicting_options,            "fo\\to\n",      "-e", "-E", "fo\\to")
TEST_ECHO(conflicting_options2,           "fo\to\n",       "-e", "-E", "-e", "fo\\to")
TEST_ECHO(oct_escape,                     "]\n",           "-e", "\\0135")
TEST_ECHO(hex_escape,                     "W\n",           "-e", "\\x57")
TEST_ECHO(suppress_escape,                "foo",           "-e", "foo\\cbar")