Gentoo Archives: gentoo-commits

From: "Petteri Räty" <betelgeuse@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/libbash:master commit in: src/core/, src/core/tests/
Date: Mon, 04 Apr 2011 15:53:18
Message-Id: 8b4e14c2089780faa5e5d705821491648cfc926e.betelgeuse@gentoo
1 commit: 8b4e14c2089780faa5e5d705821491648cfc926e
2 Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
3 AuthorDate: Mon Apr 4 05:16:55 2011 +0000
4 Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
5 CommitDate: Mon Apr 4 09:49:41 2011 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=8b4e14c2
7
8 Add a member indicating whether a variable is null
9
10 ---
11 src/core/interpreter.h | 19 +++++++++++++++++--
12 src/core/symbols.hpp | 22 +++++++++++++++++++---
13 src/core/tests/interpreter_test.cpp | 9 +++++++++
14 src/core/tests/symbols_test.cpp | 9 +++++++++
15 4 files changed, 54 insertions(+), 5 deletions(-)
16
17 diff --git a/src/core/interpreter.h b/src/core/interpreter.h
18 index ef95caa..858916e 100644
19 --- a/src/core/interpreter.h
20 +++ b/src/core/interpreter.h
21 @@ -358,6 +358,19 @@ public:
22 return std::static_pointer_cast<variable>(value)->get_value<T>();
23 }
24
25 + /// \brief check whether the value of the variable is null, return true
26 + /// if the variable is undefined
27 + /// \param variable name
28 + /// \return whether the value of the variable is null
29 + bool is_null(const std::string& name)
30 + {
31 + std::shared_ptr<symbol> value = members.resolve(name);
32 + if(value)
33 + return std::static_pointer_cast<variable>(value)->is_null();
34 + else
35 + return true;
36 + }
37 +
38 /// \brief update the variable value, raise interpreter_exception if
39 /// it's readonly, will define the variable if it doesn't exist
40 /// \param variable name
41 @@ -378,13 +391,15 @@ public:
42 /// \param the name of the variable
43 /// \param the value of the variable
44 /// \param whether it's readonly, default is false
45 + /// \param whether it's null, default is false
46 template <typename T>
47 void define(const std::string& name,
48 const T& value,
49 - bool readonly=false)
50 + bool readonly=false,
51 + bool is_null=false)
52 {
53 std::shared_ptr<variable> target(
54 - new variable(name, value, readonly));
55 + new variable(name, value, readonly, is_null));
56 members.define(target);
57 }
58 };
59
60 diff --git a/src/core/symbols.hpp b/src/core/symbols.hpp
61 index 53b699e..a4ae514 100644
62 --- a/src/core/symbols.hpp
63 +++ b/src/core/symbols.hpp
64 @@ -141,10 +141,17 @@ class variable: public symbol
65 /// \brief whether the symbol is readonly
66 bool readonly;
67
68 + /// \var private::null_value
69 + /// \brief whether the symbol is null
70 + bool null_value;
71 +
72 public:
73 template <typename T>
74 - variable(const std::string& name, T v, bool ro=false)
75 - : symbol(name), value(v), readonly(ro){}
76 + variable(const std::string& name,
77 + T v,
78 + bool ro=false,
79 + bool is_null=false)
80 + : symbol(name), value(v), readonly(ro), null_value(is_null){}
81
82 /// \brief retrieve actual value of the symbol
83 /// \return the value of the symbol
84 @@ -157,13 +164,22 @@ public:
85
86 /// \brief set the value of the symbol, raise exception if it's readonly
87 /// \param the new value to be set
88 + /// \param whether to set the variable to null value, default is false
89 template <typename T>
90 - void set_value(T new_value)
91 + void set_value(T new_value, bool is_null=false)
92 {
93 if(readonly)
94 throw interpreter_exception(get_name() + " is readonly variable");
95 + null_value = is_null;
96 value = new_value;
97 }
98 +
99 + /// \brief check whether the value of the variable is null
100 + /// \return whether the value of the variable is null
101 + bool is_null() const
102 + {
103 + return null_value;
104 + }
105 };
106
107 ///
108
109 diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpreter_test.cpp
110 index e5976e5..c3f7888 100644
111 --- a/src/core/tests/interpreter_test.cpp
112 +++ b/src/core/tests/interpreter_test.cpp
113 @@ -44,6 +44,15 @@ TEST(interpreter, define_resolve_string)
114 EXPECT_STREQ("", walker.resolve<string>("undefined").c_str());
115 }
116
117 +TEST(interpreter, is_null)
118 +{
119 + interpreter walker;
120 + walker.define("foo", "hello");
121 + EXPECT_FALSE(walker.is_null("foo"));
122 + walker.define("foo", "hello", false, true);
123 + EXPECT_TRUE(walker.is_null("foo"));
124 +}
125 +
126 TEST(interpreter, set_int_value)
127 {
128 interpreter walker;
129
130 diff --git a/src/core/tests/symbols_test.cpp b/src/core/tests/symbols_test.cpp
131 index 891e9cb..7620be0 100644
132 --- a/src/core/tests/symbols_test.cpp
133 +++ b/src/core/tests/symbols_test.cpp
134 @@ -65,6 +65,15 @@ TEST(symbol_test, string_variable)
135 EXPECT_EQ(123, int_string.get_value<int>());
136 }
137
138 +TEST(symbol_test, is_null)
139 +{
140 + variable var("foo", 10);
141 + EXPECT_FALSE(var.is_null());
142 + var.set_value("bar", true);
143 + EXPECT_TRUE(var.is_null());
144 + EXPECT_TRUE(variable("foo", "", false, true).is_null());
145 +}
146 +
147 TEST(scope_test, define_resolve)
148 {
149 scope members;