Merge lp:~thumper/unity/fix-881106 into lp:unity

Proposed by Tim Penhey
Status: Merged
Approved by: Tim Penhey
Approved revision: no longer in the source branch.
Merged at revision: 1760
Proposed branch: lp:~thumper/unity/fix-881106
Merge into: lp:unity
Diff against target: 140 lines (+79/-8)
2 files modified
UnityCore/ModelRowAdaptor.cpp (+6/-2)
tests/test_model.cpp (+73/-6)
To merge this branch: bzr merge lp:~thumper/unity/fix-881106
Reviewer Review Type Date Requested Status
Marco Trevisan (Treviño) Approve
Review via email: mp+83237@code.launchpad.net

Commit message

Don't try to create a std::string with a null pointer.

Description of the change

dee_model_get_string can return NULL, which std::string doesn't like.

I'd like to make a test for this, but need to create a fake dee model.

Can someone recommend a very simple way to mock a dee model that has a null string value?

To post a comment you must log in.
Revision history for this message
Michal Hruby (mhr3) wrote :

DeeModel *model = dee_sequence_model_new ();
dee_model_set_schema (model, "s", NULL);
dee_model_append (model, NULL);

Should do...

Revision history for this message
Mikkel Kamstrup Erlandsen (kamstrup) wrote :

On 24 November 2011 09:23, Michal Hruby <email address hidden> wrote:
> DeeModel *model = dee_sequence_model_new ();
> dee_model_set_schema (model, "s", NULL);
> dee_model_append (model, NULL);

I think you'll get wild complaints from dee about this, so your tests
should not fail if there's output on stderr. If it's just a matter of
getting dee_model_get_string() to return NULL you can also just do the
first two lines above and then dee_model_get_string(model, NULL, 27);

Revision history for this message
Tim Penhey (thumper) wrote :

Michal,

I have the following:

  DeeModel* model = dee_sequence_model_new();
  dee_model_set_schema(model, "s", NULL);
  // Add on a null string.
  DeeModelIter* iter = dee_model_append(model, NULL);

  // Check that the method we call is null.
  const gchar* value = dee_model_get_string(model, iter, 0);
  ASSERT_THAT(value, IsNull());

This fails, as dee_model_get_string actually returns a valid gchar* pointing to "".

This isn't what I want :(

The actual interesting part of the test is this:
   RowAdaptorBase row(model, iter);
   ASSERT_THAT(row.GetStringAt(0), Eq(""));

But I want o assert that the actual method call it is using returns a NULL, but it doesn't.

I can use 1 instead of 0, but his gives spewage on std::err as Mikkel says.

I've tried many different ways to get a null char* into the model, but I seem to be failing.
Is it legal?

Revision history for this message
Mikkel Kamstrup Erlandsen (kamstrup) wrote :

Hehe, that may be the thing :-) It is not legal to store an NULL in a
DeeModel, sorry if I misunderstood.

Revision history for this message
Michal Hruby (mhr3) wrote :

Right, we need an actual wrong usage, so something more along the lines of:

dee_model_set_schema (model, "i", NULL);
iter = dee_model_append (model, 0);

dee_model_get_string (model, iter, 0); // will definitely return NULL

Revision history for this message
Marco Trevisan (Treviño) (3v1n0) wrote :

Everything in the code is fine, tested and works properly.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'UnityCore/ModelRowAdaptor.cpp'
--- UnityCore/ModelRowAdaptor.cpp 2011-08-25 16:40:28 +0000
+++ UnityCore/ModelRowAdaptor.cpp 2011-11-28 01:15:28 +0000
@@ -41,7 +41,7 @@
41 model_ = other.model_;41 model_ = other.model_;
42 iter_ = other.iter_;42 iter_ = other.iter_;
43 tag_ = other.tag_;43 tag_ = other.tag_;
44 44
45 return *this;45 return *this;
46}46}
4747
@@ -49,7 +49,11 @@
49{49{
50 if (!model_ || !iter_)50 if (!model_ || !iter_)
51 return "";51 return "";
52 return dee_model_get_string(model_, iter_, position);52 const gchar* value = dee_model_get_string(model_, iter_, position);
53 if (value)
54 return value;
55 else
56 return ""; // std::strings don't like null.
53}57}
5458
55bool RowAdaptorBase::GetBoolAt(int position)59bool RowAdaptorBase::GetBoolAt(int position)
5660
=== modified file 'tests/test_model.cpp'
--- tests/test_model.cpp 2011-08-02 08:38:34 +0000
+++ tests/test_model.cpp 2011-11-28 01:15:28 +0000
@@ -1,4 +1,4 @@
1#include <gtest/gtest.h>1#include <gmock/gmock.h>
2#include <glib-object.h>2#include <glib-object.h>
33
4#include <UnityCore/GLibWrapper.h>4#include <UnityCore/GLibWrapper.h>
@@ -8,6 +8,7 @@
88
9using namespace std;9using namespace std;
10using namespace unity::dash;10using namespace unity::dash;
11using namespace testing;
1112
12namespace13namespace
13{14{
@@ -19,20 +20,18 @@
19{20{
20public:21public:
21 TestAdaptor(DeeModel* model, DeeModelIter* iter, DeeModelTag* tag)22 TestAdaptor(DeeModel* model, DeeModelIter* iter, DeeModelTag* tag)
23 : RowAdaptorBase(model, iter, tag)
22 {24 {
23 model_ = model;
24 iter_ = iter;
25 tag_ = tag;
26 }25 }
2726
28 unsigned int index()27 unsigned int index()
29 {28 {
30 return dee_model_get_uint32(model_, iter_, 0);29 return GetUIntAt(0);
31 }30 }
3231
33 string name()32 string name()
34 {33 {
35 return dee_model_get_string(model_, iter_, 1);34 return GetStringAt(1);
36 }35 }
37};36};
3837
@@ -101,4 +100,72 @@
101 }100 }
102}101}
103102
103
104
105void discard_g_log_calls(const gchar* log_domain,
106 GLogLevelFlags log_level,
107 const gchar* message,
108 gpointer user_data)
109{
110 // do nothing for the messages.
111}
112
113class TestRowAdapterBase : public Test
114{
115public:
116 void SetUp() {
117 logger_ = g_log_set_default_handler(discard_g_log_calls, NULL);
118 }
119 void TearDown() {
120 g_log_set_default_handler(logger_, NULL);
121 }
122private:
123 GLogFunc logger_;
124};
125
126TEST_F(TestRowAdapterBase, TestGetStringNull)
127{
128 DeeModel* model = dee_sequence_model_new();
129 dee_model_set_schema(model, "i", NULL);
130 // Add in zero to an int field
131 DeeModelIter* iter = dee_model_append(model, 0);
132
133 RowAdaptorBase row(model, iter);
134
135 // Check that the method we call is null.
136 const gchar* value = dee_model_get_string(model, iter, 0);
137 ASSERT_THAT(value, IsNull());
138 ASSERT_THAT(model, NotNull());
139 ASSERT_THAT(iter, NotNull());
140 ASSERT_THAT(row.GetStringAt(0), Eq(""));
141}
142
143TEST_F(TestRowAdapterBase, TestGetStringEmpty)
144{
145 DeeModel* model = dee_sequence_model_new();
146 dee_model_set_schema(model, "s", NULL);
147 // Add on a empty string.
148 DeeModelIter* iter = dee_model_append(model, "");
149
150 RowAdaptorBase row(model, iter);
151
152 ASSERT_THAT(model, NotNull());
153 ASSERT_THAT(iter, NotNull());
154 ASSERT_THAT(row.GetStringAt(0), Eq(""));
155}
156
157TEST_F(TestRowAdapterBase, TestGetStringValue)
158{
159 DeeModel* model = dee_sequence_model_new();
160 dee_model_set_schema(model, "s", NULL);
161 // Add on a real string.
162 DeeModelIter* iter = dee_model_append(model, "Hello");
163
164 RowAdaptorBase row(model, iter);
165
166 ASSERT_THAT(model, NotNull());
167 ASSERT_THAT(iter, NotNull());
168 ASSERT_THAT(row.GetStringAt(0), Eq("Hello"));
169}
170
104}171}