aboutsummaryrefslogtreecommitdiff
blob: 759e456160b209eeaa853c70bfcdd035c07f0919 (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
/*
   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 source_tests.cpp
/// \brief series of unit tests for source built in
///

#include <iostream>
#include <string>

#include <gtest/gtest.h>

#include "builtins/builtin_exceptions.h"
#include "core/interpreter.h"
#include "cppbash_builtin.h"
#include "test.h"

TEST(source_builtin_test, source_true)
{
  interpreter walker;

  int status = cppbash_builtin::exec("source",
                                     {get_src_dir() + "/scripts/source_true.sh"},
                                     std::cout,
                                     std::cerr,
                                     std::cin,
                                     walker);
  EXPECT_EQ(status, 0);

  EXPECT_TRUE(walker.has_function("foo"));

  // Call the function defined in source_true.sh
  std::stringstream output;
  walker.set_output_stream(&output);
  interpreter::local_scope current_scope(walker);
  walker.call("foo", {});
  EXPECT_STREQ("hi\n", output.str().c_str());

  EXPECT_STREQ("hello", walker.resolve<std::string>("FOO001").c_str());
  EXPECT_STREQ((get_src_dir() + "/scripts/source_true.sh").c_str(),
                walker.resolve<std::string>("FOO002").c_str());
  EXPECT_STREQ("", walker.resolve<std::string>("0").c_str());
}

TEST(source_builtin_test, source_false)
{
  interpreter walker;
  int status = cppbash_builtin::exec("source",
                                     {get_src_dir() + "/scripts/source_false.sh"},
                                     std::cout,
                                     std::cerr,
                                     std::cin,
                                     walker);
  EXPECT_EQ(status, 1);
}

TEST(source_builtin_test, source_return)
{
  interpreter walker;
  int status = cppbash_builtin::exec("source",
                                     {get_src_dir() + "/scripts/source_return.sh"},
                                     std::cout,
                                     std::cerr,
                                     std::cin,
                                     walker);
  EXPECT_EQ(status, 10);
  EXPECT_TRUE(walker.is_unset_or_null("NOT_EXIST", 0));
}

TEST(source_builtin_test, invalid)
{
  interpreter walker;
  EXPECT_THROW(cppbash_builtin::exec("source",
                                     {},
                                     std::cout,
                                     std::cerr,
                                     std::cin,
                                     walker),
               libbash::interpreter_exception);
  EXPECT_THROW(cppbash_builtin::exec("source",
                                     {get_src_dir() + "/scripts/illegal_script.sh"},
                                     std::cout,
                                     std::cerr,
                                     std::cin,
                                     walker),
               libbash::interpreter_exception);
}