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
1=== modified file 'UnityCore/ModelRowAdaptor.cpp'
2--- UnityCore/ModelRowAdaptor.cpp 2011-08-25 16:40:28 +0000
3+++ UnityCore/ModelRowAdaptor.cpp 2011-11-28 01:15:28 +0000
4@@ -41,7 +41,7 @@
5 model_ = other.model_;
6 iter_ = other.iter_;
7 tag_ = other.tag_;
8-
9+
10 return *this;
11 }
12
13@@ -49,7 +49,11 @@
14 {
15 if (!model_ || !iter_)
16 return "";
17- return dee_model_get_string(model_, iter_, position);
18+ const gchar* value = dee_model_get_string(model_, iter_, position);
19+ if (value)
20+ return value;
21+ else
22+ return ""; // std::strings don't like null.
23 }
24
25 bool RowAdaptorBase::GetBoolAt(int position)
26
27=== modified file 'tests/test_model.cpp'
28--- tests/test_model.cpp 2011-08-02 08:38:34 +0000
29+++ tests/test_model.cpp 2011-11-28 01:15:28 +0000
30@@ -1,4 +1,4 @@
31-#include <gtest/gtest.h>
32+#include <gmock/gmock.h>
33 #include <glib-object.h>
34
35 #include <UnityCore/GLibWrapper.h>
36@@ -8,6 +8,7 @@
37
38 using namespace std;
39 using namespace unity::dash;
40+using namespace testing;
41
42 namespace
43 {
44@@ -19,20 +20,18 @@
45 {
46 public:
47 TestAdaptor(DeeModel* model, DeeModelIter* iter, DeeModelTag* tag)
48+ : RowAdaptorBase(model, iter, tag)
49 {
50- model_ = model;
51- iter_ = iter;
52- tag_ = tag;
53 }
54
55 unsigned int index()
56 {
57- return dee_model_get_uint32(model_, iter_, 0);
58+ return GetUIntAt(0);
59 }
60
61 string name()
62 {
63- return dee_model_get_string(model_, iter_, 1);
64+ return GetStringAt(1);
65 }
66 };
67
68@@ -101,4 +100,72 @@
69 }
70 }
71
72+
73+
74+void discard_g_log_calls(const gchar* log_domain,
75+ GLogLevelFlags log_level,
76+ const gchar* message,
77+ gpointer user_data)
78+{
79+ // do nothing for the messages.
80+}
81+
82+class TestRowAdapterBase : public Test
83+{
84+public:
85+ void SetUp() {
86+ logger_ = g_log_set_default_handler(discard_g_log_calls, NULL);
87+ }
88+ void TearDown() {
89+ g_log_set_default_handler(logger_, NULL);
90+ }
91+private:
92+ GLogFunc logger_;
93+};
94+
95+TEST_F(TestRowAdapterBase, TestGetStringNull)
96+{
97+ DeeModel* model = dee_sequence_model_new();
98+ dee_model_set_schema(model, "i", NULL);
99+ // Add in zero to an int field
100+ DeeModelIter* iter = dee_model_append(model, 0);
101+
102+ RowAdaptorBase row(model, iter);
103+
104+ // Check that the method we call is null.
105+ const gchar* value = dee_model_get_string(model, iter, 0);
106+ ASSERT_THAT(value, IsNull());
107+ ASSERT_THAT(model, NotNull());
108+ ASSERT_THAT(iter, NotNull());
109+ ASSERT_THAT(row.GetStringAt(0), Eq(""));
110+}
111+
112+TEST_F(TestRowAdapterBase, TestGetStringEmpty)
113+{
114+ DeeModel* model = dee_sequence_model_new();
115+ dee_model_set_schema(model, "s", NULL);
116+ // Add on a empty string.
117+ DeeModelIter* iter = dee_model_append(model, "");
118+
119+ RowAdaptorBase row(model, iter);
120+
121+ ASSERT_THAT(model, NotNull());
122+ ASSERT_THAT(iter, NotNull());
123+ ASSERT_THAT(row.GetStringAt(0), Eq(""));
124+}
125+
126+TEST_F(TestRowAdapterBase, TestGetStringValue)
127+{
128+ DeeModel* model = dee_sequence_model_new();
129+ dee_model_set_schema(model, "s", NULL);
130+ // Add on a real string.
131+ DeeModelIter* iter = dee_model_append(model, "Hello");
132+
133+ RowAdaptorBase row(model, iter);
134+
135+ ASSERT_THAT(model, NotNull());
136+ ASSERT_THAT(iter, NotNull());
137+ ASSERT_THAT(row.GetStringAt(0), Eq("Hello"));
138+}
139+
140 }