Back to index

unity  6.0.0
test_glib_variant.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2011 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00018  */
00019 
00020 #include <list>
00021 #include <algorithm>
00022 #include <gmock/gmock.h>
00023 #include <UnityCore/Variant.h>
00024 
00025 using namespace std;
00026 using namespace testing;
00027 using namespace unity;
00028 using namespace unity::glib;
00029 
00030 namespace
00031 {
00032 
00033 bool IsVariant(Variant const& variant)
00034 {
00035   return g_variant_get_type_string (variant) != NULL;
00036 }
00037 
00038 bool IsFloating(Variant const& variant)
00039 {
00040   return g_variant_is_floating (variant);
00041 }
00042 
00043 bool IsSameVariant(Variant const& v1, Variant const& v2)
00044 {
00045   GVariant *gv1 = v1;
00046   GVariant *gv2 = v2;
00047 
00048   return gv1 == gv2;
00049 }
00050 
00051 bool ValuesEqual(Variant const& v1, Variant const& v2)
00052 {
00053   return g_variant_equal ((GVariant*)v1, (GVariant*)v2);
00054 }
00055 
00056 TEST(TestGLibVariant, Construct)
00057 {
00058   Variant v (g_variant_new_string ("foo"));
00059 
00060   EXPECT_TRUE(IsVariant(v));
00061   EXPECT_FALSE(IsFloating(v));
00062 }
00063 
00064 TEST(TestGLibVariant, ConstructSteal)
00065 {
00066   GVariant *gv = g_variant_new_string ("qoo");
00067   g_variant_ref_sink (gv);
00068   Variant v (gv, StealRef());
00069 
00070   EXPECT_TRUE(IsVariant(v));
00071   EXPECT_FALSE(IsFloating(v));
00072 }
00073 
00074 TEST(TestGLibVariant, Copy)
00075 {
00076   Variant v1 (g_variant_new_string ("bar"));
00077   Variant v2 (v1);
00078 
00079   EXPECT_TRUE(IsVariant(v1));
00080   EXPECT_TRUE(IsVariant(v2));
00081   EXPECT_FALSE(IsFloating(v1));
00082   EXPECT_FALSE(IsFloating(v2));
00083   EXPECT_TRUE(IsSameVariant(v1, v2));
00084   EXPECT_TRUE(ValuesEqual(v1, v2));
00085 }
00086 
00087 TEST(TestGLibVariant, KeepsRef)
00088 {
00089   GVariant *gv = g_variant_new_int32 (456);
00090   g_variant_ref_sink (gv);
00091 
00092   Variant v (gv);
00093 
00094   EXPECT_TRUE(IsVariant(v));
00095   EXPECT_FALSE(IsFloating(v));
00096 
00097   g_variant_unref (gv);
00098 
00099   EXPECT_TRUE(IsVariant(v));
00100   EXPECT_FALSE(IsFloating(v));
00101   EXPECT_EQ(v.GetInt(), 456);
00102 }
00103 
00104 TEST(TestGLibVariant, UseGVariantMethod)
00105 {
00106   Variant v (g_variant_new_int32 (123));
00107 
00108   EXPECT_TRUE(IsVariant(v));
00109   EXPECT_FALSE(IsFloating(v));
00110   EXPECT_EQ(v.GetInt(), 123);
00111 
00112   EXPECT_TRUE(g_variant_is_of_type (v, G_VARIANT_TYPE ("i")));
00113 }
00114 
00115 TEST(TestGLibVariant, HintsMap)
00116 {
00117   GVariantBuilder b;
00118 
00119   g_variant_builder_init (&b, G_VARIANT_TYPE ("a{sv}"));
00120   variant::BuilderWrapper bw (&b);
00121   bw.add ("string-key", "string-value");
00122   bw.add ("int-key", 123);
00123   bw.add ("bool-key", true);
00124   bw.add ("last", "foo");
00125 
00126   GVariant *dict_variant = g_variant_builder_end (&b);
00127   Variant dict (g_variant_new_tuple (&dict_variant, 1));
00128 
00129   EXPECT_TRUE(IsVariant(dict));
00130   EXPECT_FALSE(IsFloating(dict));
00131 
00132   HintsMap hints;
00133   EXPECT_TRUE(dict.ASVToHints (hints));
00134 
00135   EXPECT_EQ(hints["string-key"].GetString(), "string-value");
00136   EXPECT_EQ(hints["int-key"].GetInt(), 123);
00137   EXPECT_EQ(hints["bool-key"].GetBool(), true);
00138   EXPECT_EQ(hints["last"].GetString(), "foo");
00139 
00140   // throw away all references to the original variant
00141   dict = g_variant_new_string ("bar");
00142   EXPECT_TRUE(IsVariant(dict));
00143   EXPECT_FALSE(IsFloating(dict));
00144   EXPECT_EQ(dict.GetString(), "bar");
00145 
00146   // this has to still work
00147   EXPECT_EQ(hints["string-key"].GetString(), "string-value");
00148   EXPECT_EQ(hints["int-key"].GetInt(), 123);
00149   EXPECT_EQ(hints["bool-key"].GetBool(), true);
00150   EXPECT_EQ(hints["last"].GetString(), "foo");
00151 }
00152 
00153 } // Namespace