github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/goconfig/goconfig_test.go (about)

     1  // Copyright 2013 Unknwon
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package goconfig
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	. "github.com/insionng/yougam/libraries/smartystreets/goconvey/convey"
    22  )
    23  
    24  func TestLoadConfigFile(t *testing.T) {
    25  	Convey("Load a single configuration file that does exist", t, func() {
    26  		c, err := LoadConfigFile("testdata/conf.ini")
    27  		So(err, ShouldBeNil)
    28  		So(c, ShouldNotBeNil)
    29  
    30  		Convey("Test GetSectionList", func() {
    31  			So(c.GetSectionList(), ShouldResemble,
    32  				[]string{"DEFAULT", "Demo", "What's this?", "url", "parent",
    33  					"parent.child", "parent.child.child", "auto increment"})
    34  		})
    35  
    36  		Convey("Get value that does exist", func() {
    37  			v, err := c.GetValue("Demo", "key2")
    38  			So(err, ShouldBeNil)
    39  			So(v, ShouldEqual, "test data")
    40  		})
    41  
    42  		Convey("Get value that does not exist", func() {
    43  			_, err := c.GetValue("Demo", "key4")
    44  			So(err, ShouldNotBeNil)
    45  		})
    46  
    47  		Convey("Get value that has empty value", func() {
    48  			_, err := c.GetValue("What's this?", "empty_value")
    49  			So(err, ShouldBeNil)
    50  		})
    51  
    52  		Convey("Get value that section does not exist", func() {
    53  			_, err := c.GetValue("Demo404", "key4")
    54  			So(err, ShouldNotBeNil)
    55  		})
    56  
    57  		Convey("Get value use parent-child feature", func() {
    58  			v, err := c.GetValue("parent.child", "sex")
    59  			So(err, ShouldBeNil)
    60  			So(v, ShouldEqual, "male")
    61  		})
    62  
    63  		Convey("Get value use recursive feature", func() {
    64  			v, err := c.GetValue("", "search")
    65  			So(err, ShouldBeNil)
    66  			So(v, ShouldEqual, "http://www.google.com")
    67  
    68  			v, err = c.GetValue("url", "google_url")
    69  			So(err, ShouldBeNil)
    70  			So(v, ShouldEqual, "http://www.google.fake")
    71  		})
    72  
    73  		Convey("Set value that does exist", func() {
    74  			So(c.SetValue("Demo", "key2", "hello man!"), ShouldBeFalse)
    75  			v, err := c.GetValue("Demo", "key2")
    76  			So(err, ShouldBeNil)
    77  			So(v, ShouldEqual, "hello man!")
    78  		})
    79  
    80  		Convey("Set value that does not exist", func() {
    81  			So(c.SetValue("Demo", "key4", "hello girl!"), ShouldBeTrue)
    82  			v, err := c.GetValue("Demo", "key4")
    83  			So(err, ShouldBeNil)
    84  			So(v, ShouldEqual, "hello girl!")
    85  			So(c.SetValue("", "gowalker", "https://gowalker.org"), ShouldBeTrue)
    86  		})
    87  
    88  		Convey("Test GetKeyList", func() {
    89  			So(c.GetKeyList("Demo"), ShouldResemble,
    90  				[]string{"key1", "key2", "key3", "quote", "key:1",
    91  					"key:2=key:1", "中国", "chinese-var", "array_key"})
    92  		})
    93  
    94  		Convey("Delete a key", func() {
    95  			So(c.DeleteKey("", "key404"), ShouldBeFalse)
    96  			So(c.DeleteKey("Demo", "key404"), ShouldBeFalse)
    97  			So(c.DeleteKey("Demo", "中国"), ShouldBeTrue)
    98  			_, err := c.GetValue("Demo", "中国")
    99  			So(err, ShouldNotBeNil)
   100  			So(c.DeleteKey("404", "key"), ShouldBeFalse)
   101  		})
   102  
   103  		Convey("Delete all the keys", func() {
   104  			for _, key := range c.GetKeyList("Demo") {
   105  				So(c.DeleteKey("Demo", key), ShouldBeTrue)
   106  			}
   107  			So(c.GetKeyList("Demo"), ShouldResemble, []string{})
   108  			So(len(c.GetKeyList("Demo")), ShouldEqual, 0)
   109  		})
   110  
   111  		Convey("Delete section that does not exist", func() {
   112  			So(c.DeleteSection(""), ShouldBeTrue)
   113  			So(c.DeleteSection("404"), ShouldBeFalse)
   114  		})
   115  
   116  		Convey("Get section that exists", func() {
   117  			_, err = c.GetSection("")
   118  			So(err, ShouldBeNil)
   119  		})
   120  
   121  		Convey("Get section that does not exist", func() {
   122  			_, err = c.GetSection("404")
   123  			So(err, ShouldNotBeNil)
   124  		})
   125  
   126  		Convey("Set section comments", func() {
   127  			So(c.SetSectionComments("", "default section comments"), ShouldBeTrue)
   128  		})
   129  
   130  		Convey("Get section comments", func() {
   131  			So(c.GetSectionComments(""), ShouldEqual, "")
   132  		})
   133  
   134  		Convey("Set key comments", func() {
   135  			So(c.SetKeyComments("", "search", "search comments"), ShouldBeTrue)
   136  			So(c.SetKeyComments("404", "search", ""), ShouldBeTrue)
   137  		})
   138  
   139  		Convey("Get key comments", func() {
   140  			So(c.GetKeyComments("", "google"), ShouldEqual, "; Google")
   141  		})
   142  
   143  		Convey("Delete all the sections", func() {
   144  			for _, sec := range c.GetSectionList() {
   145  				So(c.DeleteSection(sec), ShouldBeTrue)
   146  			}
   147  			So(c.GetSectionList(), ShouldResemble, []string{})
   148  			So(len(c.GetSectionList()), ShouldEqual, 0)
   149  		})
   150  	})
   151  
   152  	Convey("Load a single configuration file that does not exist", t, func() {
   153  		_, err := LoadConfigFile("testdata/conf404.ini")
   154  		So(err, ShouldNotBeNil)
   155  	})
   156  
   157  	Convey("Load multiple configuration files", t, func() {
   158  		c, err := LoadConfigFile("testdata/conf.ini", "testdata/conf2.ini")
   159  		So(err, ShouldBeNil)
   160  		So(c, ShouldNotBeNil)
   161  
   162  		Convey("Get value that does not exist in 1st file", func() {
   163  			v, err := c.GetValue("new section", "key1")
   164  			So(err, ShouldBeNil)
   165  			So(v, ShouldEqual, "conf.ini does not have this key")
   166  		})
   167  
   168  		Convey("Get value that overwrited in 2nd file", func() {
   169  			v, err := c.GetValue("Demo", "key2")
   170  			So(err, ShouldBeNil)
   171  			So(v, ShouldEqual, "rewrite this key of conf.ini")
   172  		})
   173  	})
   174  }
   175  
   176  func TestGetKeyList(t *testing.T) {
   177  	Convey("Get key list", t, func() {
   178  		c, err := LoadConfigFile("testdata/conf.ini")
   179  		So(err, ShouldBeNil)
   180  		So(c, ShouldNotBeNil)
   181  
   182  		Convey("Get ket list that does exist", func() {
   183  			So(c.GetKeyList("Demo"), ShouldResemble,
   184  				[]string{"key1", "key2", "key3", "quote", "key:1",
   185  					"key:2=key:1", "中国", "chinese-var", "array_key"})
   186  			So(c.GetKeyList(""), ShouldResemble, []string{"google", "search"})
   187  		})
   188  
   189  		Convey("Get key list that not exist", func() {
   190  			So(c.GetKeyList("404"), ShouldBeNil)
   191  		})
   192  	})
   193  }
   194  
   195  func TestSaveConfigFile(t *testing.T) {
   196  	Convey("Save a ConfigFile to file system", t, func() {
   197  		c, err := LoadConfigFile("testdata/conf.ini", "testdata/conf2.ini")
   198  		So(err, ShouldBeNil)
   199  		So(c, ShouldNotBeNil)
   200  
   201  		So(SaveConfigFile(c, "testdata/conf_test.ini"), ShouldBeNil)
   202  	})
   203  }
   204  
   205  func TestReload(t *testing.T) {
   206  	Convey("Reload a configuration file", t, func() {
   207  		c, err := LoadConfigFile("testdata/conf.ini", "testdata/conf2.ini")
   208  		So(err, ShouldBeNil)
   209  		So(c, ShouldNotBeNil)
   210  
   211  		So(c.Reload(), ShouldBeNil)
   212  	})
   213  }
   214  
   215  func TestAppendFiles(t *testing.T) {
   216  	Convey("Reload a configuration file", t, func() {
   217  		c, err := LoadConfigFile("testdata/conf.ini")
   218  		So(err, ShouldBeNil)
   219  		So(c, ShouldNotBeNil)
   220  
   221  		So(c.AppendFiles("testdata/conf2.ini"), ShouldBeNil)
   222  	})
   223  }
   224  
   225  func TestTypes(t *testing.T) {
   226  	Convey("Return with types", t, func() {
   227  		c, err := LoadConfigFile("testdata/conf.ini")
   228  		So(err, ShouldBeNil)
   229  		So(c, ShouldNotBeNil)
   230  
   231  		Convey("Return bool", func() {
   232  			v, err := c.Bool("parent.child", "married")
   233  			So(err, ShouldBeNil)
   234  			So(v, ShouldBeTrue)
   235  
   236  			_, err = c.Bool("parent.child", "died")
   237  			So(err, ShouldNotBeNil)
   238  		})
   239  
   240  		Convey("Return float64", func() {
   241  			v, err := c.Float64("parent", "money")
   242  			So(err, ShouldBeNil)
   243  			So(v, ShouldEqual, 1.25)
   244  
   245  			_, err = c.Float64("parent", "balance")
   246  			So(err, ShouldNotBeNil)
   247  		})
   248  
   249  		Convey("Return int", func() {
   250  			v, err := c.Int("parent", "age")
   251  			So(err, ShouldBeNil)
   252  			So(v, ShouldEqual, 32)
   253  
   254  			_, err = c.Int("parent", "children")
   255  			So(err, ShouldNotBeNil)
   256  		})
   257  
   258  		Convey("Return int64", func() {
   259  			v, err := c.Int64("parent", "age")
   260  			So(err, ShouldBeNil)
   261  			So(v, ShouldEqual, 32)
   262  
   263  			_, err = c.Int64("parent", "children")
   264  			So(err, ShouldNotBeNil)
   265  		})
   266  	})
   267  }
   268  
   269  func TestMust(t *testing.T) {
   270  	Convey("Must return with type", t, func() {
   271  		c, err := LoadConfigFile("testdata/conf.ini")
   272  		So(err, ShouldBeNil)
   273  		So(c, ShouldNotBeNil)
   274  
   275  		Convey("Return string", func() {
   276  			So(c.MustValue("parent.child", "name"), ShouldEqual, "john")
   277  			So(c.MustValue("parent.child", "died"), ShouldEqual, "")
   278  			So(c.MustValue("parent.child", "died", "no"), ShouldEqual, "no")
   279  		})
   280  
   281  		Convey("Return string and bool", func() {
   282  			val, ok := c.MustValueSet("parent.child", "died")
   283  			So(val, ShouldEqual, "")
   284  			So(ok, ShouldBeFalse)
   285  			val, ok = c.MustValueSet("parent.child", "died", "no")
   286  			So(val, ShouldEqual, "no")
   287  			So(ok, ShouldBeTrue)
   288  		})
   289  
   290  		Convey("Return bool", func() {
   291  			So(c.MustBool("parent.child", "married"), ShouldBeTrue)
   292  			So(c.MustBool("parent.child", "died"), ShouldBeFalse)
   293  			So(c.MustBool("parent.child", "died", true), ShouldBeTrue)
   294  		})
   295  
   296  		Convey("Return float64", func() {
   297  			So(c.MustFloat64("parent", "money"), ShouldEqual, 1.25)
   298  			So(c.MustFloat64("parent", "balance"), ShouldEqual, 0.0)
   299  			So(c.MustFloat64("parent", "balance", 1.25), ShouldEqual, 1.25)
   300  		})
   301  
   302  		Convey("Return int", func() {
   303  			So(c.MustInt("parent", "age"), ShouldEqual, 32)
   304  			So(c.MustInt("parent", "children"), ShouldEqual, 0)
   305  			So(c.MustInt("parent", "children", 3), ShouldEqual, 3)
   306  		})
   307  
   308  		Convey("Return int64", func() {
   309  			So(c.MustInt64("parent", "age"), ShouldEqual, 32)
   310  			So(c.MustInt64("parent", "children"), ShouldEqual, 0)
   311  			So(c.MustInt64("parent", "children", 3), ShouldEqual, 3)
   312  		})
   313  	})
   314  }
   315  
   316  func TestRange(t *testing.T) {
   317  	Convey("Must return with range", t, func() {
   318  		c, err := LoadConfigFile("testdata/conf.ini")
   319  		So(err, ShouldBeNil)
   320  		So(c, ShouldNotBeNil)
   321  
   322  		So(c.MustValueRange("What's this?", "name", "joe", []string{"hello"}), ShouldEqual, "joe")
   323  		So(c.MustValueRange("What's this?", "name404", "joe", []string{"hello"}), ShouldEqual, "joe")
   324  		So(c.MustValueRange("What's this?", "name", "joe", []string{"hello", "try one more value ^-^"}),
   325  			ShouldEqual, "try one more value ^-^")
   326  	})
   327  }
   328  
   329  func TestArray(t *testing.T) {
   330  	Convey("Must return with string array", t, func() {
   331  		c, err := LoadConfigFile("testdata/conf.ini")
   332  		So(err, ShouldBeNil)
   333  		So(c, ShouldNotBeNil)
   334  
   335  		So(fmt.Sprintf("%s", c.MustValueArray("Demo", "array_key", ",")), ShouldEqual, "[1 2 3 4 5]")
   336  		So(fmt.Sprintf("%s", c.MustValueArray("Demo", "array_key404", ",")), ShouldEqual, "[]")
   337  	})
   338  }
   339  
   340  func TestLoadFromData(t *testing.T) {
   341  	Convey("Load config file from data", t, func() {
   342  		c, err := LoadFromData([]byte(""))
   343  		So(err, ShouldBeNil)
   344  		So(c, ShouldNotBeNil)
   345  	})
   346  }