aboutsummaryrefslogtreecommitdiff
blob: eec5e415a113aad3387578670b47018f26ff31f0 (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
/*
   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 declare_tests.cpp
/// \brief series of unit tests for declare built in
///
#include <iostream>
#include <sstream>
#include <vector>

#include <gtest/gtest.h>

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

using namespace std;

namespace
{
  template <typename T>
  void test_declare(const string& expected, std::initializer_list<string> args)
  {
    stringstream test_output;
    interpreter walker;
    try
    {
      cppbash_builtin::exec("declare",args,cout,test_output,cin,walker);
      FAIL();
    }
    catch(T& e)
    {
      EXPECT_EQ(expected, e.what());
    }
  }
}

TEST(declare_builtin_test, invalid_arguments)
{
  test_declare<libbash::illegal_argument_exception>("declare: arguments required", {});
  test_declare<libbash::unsupported_exception>("declare: -ap is not supported yet", {"-ap"});
  test_declare<libbash::illegal_argument_exception>("declare: unrecognized option: -L", {"-L"});
}

TEST(declare_built_test, declarations)
{
  interpreter walker;

  EXPECT_EQ(0, cppbash_builtin::exec("declare", {"var"}, cout, cerr, cin, walker));

  EXPECT_EQ(0, cppbash_builtin::exec("declare", {"foo1=bar"}, cout, cerr, cin, walker));
  EXPECT_STREQ("bar", walker.resolve<std::string>("foo1").c_str());

  walker.define("foo2", "bar");
  EXPECT_EQ(0, cppbash_builtin::exec("declare", {"foo2"}, cout, cerr, cin, walker));
  EXPECT_STREQ("bar", walker.resolve<std::string>("foo2").c_str());

  EXPECT_EQ(0, cppbash_builtin::exec("declare", {"var1=foo var2 var3=bar"}, cout, cerr, cin, walker));
  EXPECT_STREQ("foo", walker.resolve<std::string>("var1").c_str());
  EXPECT_STREQ("", walker.resolve<std::string>("var2").c_str());
  EXPECT_STREQ("bar", walker.resolve<std::string>("var3").c_str());

  EXPECT_EQ(0, cppbash_builtin::exec("declare", {"var=\"foo bar\""}, cout, cerr, cin, walker));
  EXPECT_STREQ("foo bar", walker.resolve<std::string>("var").c_str());
}

TEST(declare_builtin_test, _F)
{
  stringstream expression("function foo() { :; }; function bar() { :; }");
  interpreter walker;
  bash_ast ast(expression);
  ast.interpret_with(walker);

  stringstream test_output1;
  EXPECT_EQ(0, cppbash_builtin::exec("declare", {"-F foo"}, test_output1, cerr, cin, walker));
  EXPECT_EQ("foo\n", test_output1.str());

  stringstream test_output2;
  EXPECT_EQ(1, cppbash_builtin::exec("declare", {"-F foo bar test"}, test_output2, cerr, cin, walker));
  EXPECT_EQ("foo\nbar\n", test_output2.str());

  stringstream test_output3;
  EXPECT_EQ(0, cppbash_builtin::exec("declare", {"+F foo bar test"}, test_output3, cerr, cin, walker));
  EXPECT_EQ("", test_output3.str());

  stringstream test_output4;
  EXPECT_EQ(0, cppbash_builtin::exec("declare", {"-F"}, test_output3, cerr, cin, walker));
  EXPECT_EQ("declare -f bar\ndeclare -f foo\n", test_output3.str());
}

TEST(declare_built_test, _p)
{
  interpreter walker;
  walker.define("foo", "bar");

  stringstream test_output1;
  EXPECT_EQ(0, cppbash_builtin::exec("declare", {"-p foo"}, test_output1, cerr, cin, walker));
  EXPECT_EQ("declare -- foo=\"bar\"\n", test_output1.str());

  stringstream test_output2;
  EXPECT_EQ(1, cppbash_builtin::exec("declare", {"-p bar test"}, test_output2, cerr, cin, walker));
  EXPECT_EQ("-bash: declare: bar: not found\n-bash: declare: test: not found\n", test_output2.str());
}

#define TEST_DECLARE(name, expected, ...) \
	TEST(declare_builtin_test, name) { test_declare<libbash::unsupported_exception>(expected, {__VA_ARGS__}); }

TEST_DECLARE(_a, "declare -a is not supported yet", "-a", "world")
TEST_DECLARE(_A, "declare -A is not supported yet", "-A", "world")
TEST_DECLARE(_f, "declare -f is not supported yet", "-f", "world")
TEST_DECLARE(_i, "declare -i is not supported yet", "-i", "world")
TEST_DECLARE(_l, "declare -l is not supported yet", "-l", "world")
TEST_DECLARE(_r, "declare -r is not supported yet", "-r", "world")
TEST_DECLARE(_t, "declare -t is not supported yet", "-t", "world")
TEST_DECLARE(_u, "declare -u is not supported yet", "-u", "world")
TEST_DECLARE(_x, "declare -x is not supported yet", "-x", "world")
TEST_DECLARE(pa, "declare +a is not supported yet", "+a", "world")
TEST_DECLARE(pA, "declare +A is not supported yet", "+A", "world")
TEST_DECLARE(pf, "declare +f is not supported yet", "+f", "world")
TEST_DECLARE(pi, "declare +i is not supported yet", "+i", "world")
TEST_DECLARE(pl, "declare +l is not supported yet", "+l", "world")
TEST_DECLARE(pr, "declare +r is not supported yet", "+r", "world")
TEST_DECLARE(pt, "declare +t is not supported yet", "+t", "world")
TEST_DECLARE(pu, "declare +u is not supported yet", "+u", "world")
TEST_DECLARE(px, "declare +x is not supported yet", "+x", "world")