merge two parsecfg tests into one (#16237)
* try to fix #16206 * merge two parsecfg tests into one * Revert "fix" This reverts commit 668bdec2c499cf9967abfb7aad24975a04b092eb. * Revert "try to fix #16206" This reverts commit c399cc2153190299c9cbb0ad83f6ce85a7bbbb89.
This commit is contained in:
parent
629b22e3d5
commit
849bc36eda
2 changed files with 74 additions and 73 deletions
|
|
@ -1,56 +0,0 @@
|
||||||
discard """
|
|
||||||
targets: "c js"
|
|
||||||
"""
|
|
||||||
import parsecfg, streams
|
|
||||||
|
|
||||||
## 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
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
@ -1,23 +1,80 @@
|
||||||
discard """
|
discard """
|
||||||
output: '''OK'''
|
targets: "c js"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
#bug #6046
|
import parsecfg, streams
|
||||||
import parsecfg
|
|
||||||
|
|
||||||
var config = newConfig()
|
when not defined(js):
|
||||||
config.setSectionKey("foo","bar","-1")
|
# bug #6046
|
||||||
config.setSectionKey("foo","foo","abc")
|
var config = newConfig()
|
||||||
config.writeConfig("test.ini")
|
config.setSectionKey("foo", "bar", "-1")
|
||||||
|
config.setSectionKey("foo", "foo", "abc")
|
||||||
|
|
||||||
# test.ini now contains
|
from stdtest/specialpaths import buildDir
|
||||||
# [foo]
|
import os
|
||||||
# bar=-1
|
|
||||||
# foo=abc
|
|
||||||
|
|
||||||
var config2 = loadConfig("test.ini")
|
const file = buildDir / "tparsecfg.ini"
|
||||||
let bar = config2.getSectionValue("foo","bar")
|
config.writeConfig(file)
|
||||||
let foo = config2.getSectionValue("foo","foo")
|
|
||||||
assert(bar == "-1")
|
# file now contains
|
||||||
assert(foo == "abc")
|
# [foo]
|
||||||
echo "OK"
|
# 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
|
||||||
|
"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue