aboutsummaryrefslogtreecommitdiff
blob: bfdba833d11b45293ea3293ccb006f3329eeeac4 (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
/*
   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 lib_test.cpp
/// \brief series of unit tests for generated walker.
///

#include <sstream>

#include <gtest/gtest.h>

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

static void check_string_assignment(const char* script,
                                    const std::string& name,
                                    const char* exp_value)
{
  interpreter walker;
  std::istringstream input(script);
  bash_ast ast(input);
  ast.interpret_with(walker);
  EXPECT_STREQ(exp_value, walker.resolve<std::string>(name).c_str());
}

#define TEST_STRING_ASSIGNMENT(name, script, var_name, exp_value)\
  TEST(string_assignment, name) \
  { check_string_assignment(script, var_name, exp_value); }

TEST_STRING_ASSIGNMENT(str_assignment,          "str=\"abc\"",          "str",    "abc")
TEST_STRING_ASSIGNMENT(str_assignment2,         "str=\"abc_def\"",      "str",    "abc_def")
TEST_STRING_ASSIGNMENT(str_assignment3,         "str=\"abc def\"",      "str",    "abc def")
TEST_STRING_ASSIGNMENT(str_assignment4,
                       "str=\"case esac do done elif else fi if for function in select then until while time\"",
                       "str",
                       "case esac do done elif else fi if for function in select then until while time")
TEST_STRING_ASSIGNMENT(str_assignment5,
                       "str=\"123 abc = % %% - . .. :  -a -aa test _ ~ ++ -- *= /= %= += -= <<= >>= &= ^= |= \\a ^ aä\"",
                       "str",
                       "123 abc = % %% - . .. :  -a -aa test _ ~ ++ -- *= /= %= += -= <<= >>= &= ^= |= \\a ^ aä")
TEST_STRING_ASSIGNMENT(str_assignment6,
                       "str=\"/ \n \r\n & && ||| || > < ' : ; , ( (( ) )) ;; { } >= <=\"",
                       "str",
                       "/ \n \r\n & && ||| || > < ' : ; , ( (( ) )) ;; { } >= <=")

TEST(array_index, out_of_bound)
{
  interpreter walker;

  std::string script = "a[-1]=\"1\"";
  std::istringstream input(script);
  bash_ast ast(input);
  EXPECT_THROW(ast.interpret_with(walker), interpreter_exception);

  std::string script2 = "a=(1 2 [-5]=1)";
  std::istringstream input2(script2);
  bash_ast ast2(input2);
  EXPECT_THROW(ast2.interpret_with(walker), interpreter_exception);
}

TEST(extglob, used_when_disabled)
{
  interpreter walker;

  std::string script = "echo ${abc/?([a-z])}";
  std::istringstream input(script);
  bash_ast ast(input);
  try
  {
    ast.interpret_with(walker);
  }
  catch(interpreter_exception& e)
  {
    EXPECT_STREQ(e.what(), "Entered extended pattern matching with extglob disabled");
  }
}