summary refs log tree commit diff stats
path: root/tests/stdlib/tparsecfg.nim
blob: 45dea0531cb3158e9412a528c156faf5769404e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
discard """
  targets: "c js"
"""

import parsecfg, streams

when not defined(js):
  # bug #6046
  var config = newConfig()
  config.setSectionKey("foo", "bar", "-1")
  config.setSectionKey("foo", "foo", "abc")

  from stdtest/specialpaths import buildDir
  import os

  const file = buildDir / "tparsecfg.ini"
  config.writeConfig(file)

  # file now contains
  # [foo]
  # bar=-1
  # foo=abc

  var config2 = loadConfig(file)
  let bar = config2.getSectionValue("foo", "bar")
  let foo = config2.getSectionValue("foo", "foo")
  assert(bar == "-1")
  assert(foo == "abc")


## Creating a configuration file.
var dict1 = newConfig()
dict1.setSectionKey("", "charset", "utf-8")
dict1.setSectionKey("Package", "name", "hello")
dict1.setSectionKey("Package", "--threads", "on")
dict1.setSectionKey("Author", "name", "lihf8515")
dict1.setSectionKey("Author", "qq", "10214028")
dict1.setSectionKey("Author", "email", "lihaifeng@wxm.com")
var ss = newStringStream()
dict1.writeConfig(ss)

## Reading a configuration file.
var dict2 = loadConfig(newStringStream(ss.data))
var charset = dict2.getSectionValue("", "charset")
var threads = dict2.getSectionValue("Package", "--threads")
var pname = dict2.getSectionValue("Package", "name")
var name = dict2.getSectionValue("Author", "name")
var qq = dict2.getSectionValue("Author", "qq")
var email = dict2.getSectionValue("Author", "email")
doAssert charset == "utf-8"
doAssert threads == "on"
doAssert pname == "hello"
doAssert name == "lihf8515"
doAssert qq == "10214028"
doAssert email == "lihaifeng@wxm.com"

## Modifying a configuration file.
var dict3 = loadConfig(newStringStream(ss.data))
dict3.setSectionKey("Author", "name", "lhf")
doAssert $dict3 == """charset=utf-8
[Package]
name=hello
--threads:on
[Author]
name=lhf
qq=10214028
email="lihaifeng@wxm.com"
"""

## Deleting a section key in a configuration file.
var dict4 = loadConfig(newStringStream(ss.data))
dict4.delSectionKey("Author", "email")
doAssert $dict4 == """charset=utf-8
[Package]
name=hello
--threads:on
[Author]
name=lihf8515
qq=10214028
"""