2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/spec/unit/nickel_conf_spec.lua

188 lines
4.3 KiB
Lua
Raw Normal View History

2016-02-03 18:35:47 +00:00
describe("Nickel configuation module", function()
local lfs, NickelConf
setup(function()
require("commonrequire")
lfs = require("libs/libkoreader-lfs")
NickelConf = require("device/kobo/nickel_conf")
end)
2016-02-03 18:35:47 +00:00
describe("Frontlight module", function()
it("should read value", function()
local fn = os.tmpname()
local fd = io.open(fn, "w")
fd:write([[
[OtherThing]
foo=bar
[PowerOptions]
FrontLightLevel=55
FrontLightState=true
2016-02-03 18:35:47 +00:00
[YetAnotherThing]
bar=baz
]])
fd:close()
NickelConf._set_kobo_conf_path(fn)
assert.Equals(NickelConf.frontLightLevel.get(), 55)
assert.Equals(NickelConf.frontLightState.get(), true)
os.remove(fn)
end)
it("should also read value", function()
local fn = os.tmpname()
local fd = io.open(fn, "w")
fd:write([[
[OtherThing]
foo=bar
[PowerOptions]
FrontLightLevel=30
FrontLightState=false
[YetAnotherThing]
bar=baz
]])
fd:close()
NickelConf._set_kobo_conf_path(fn)
assert.Equals(NickelConf.frontLightLevel.get(), 30)
assert.Equals(NickelConf.frontLightState.get(), false)
os.remove(fn)
end)
it("should have default value", function()
local fn = os.tmpname()
local fd = io.open(fn, "w")
fd:write([[
[OtherThing]
foo=bar
[YetAnotherThing]
bar=baz
]])
fd:close()
NickelConf._set_kobo_conf_path(fn)
assert.Equals(NickelConf.frontLightLevel.get(), 1)
assert.Equals(NickelConf.frontLightState.get(), nil)
2016-02-03 18:35:47 +00:00
os.remove(fn)
end)
it("should create section", function()
local fn = os.tmpname()
local fd = io.open(fn, "w")
fd:write([[
[OtherThing]
FrontLightLevel=6
]])
fd:close()
NickelConf._set_kobo_conf_path(fn)
NickelConf.frontLightLevel.set(100)
NickelConf.frontLightState.set(true)
2016-02-03 18:35:47 +00:00
fd = io.open(fn, "r")
assert.Equals(fd:read("*a"), [[
[OtherThing]
FrontLightLevel=6
[PowerOptions]
FrontLightLevel=100
]])
fd:close()
os.remove(fn)
fd = io.open(fn, "w")
fd:write("")
fd:close()
NickelConf.frontLightLevel.set(20)
NickelConf.frontLightState.set(false)
2016-02-03 18:35:47 +00:00
fd = io.open(fn, "r")
assert.Equals(fd:read("*a"), [[
[PowerOptions]
FrontLightLevel=20
]])
fd:close()
os.remove(fn)
end)
it("should replace value", function()
local fn = os.tmpname()
local fd = io.open(fn, "w")
fd:write([[
[OtherThing]
foo=bar
[PowerOptions]
FrontLightLevel=6
FrontLightState=false
2016-02-03 18:35:47 +00:00
[YetAnotherThing]
bar=baz
]])
fd:close()
NickelConf._set_kobo_conf_path(fn)
NickelConf.frontLightLevel.set(100)
NickelConf.frontLightState.set(true)
2016-02-03 18:35:47 +00:00
fd = io.open(fn, "r")
assert.Equals(fd:read("*a"), [[
[OtherThing]
foo=bar
[PowerOptions]
FrontLightLevel=100
FrontLightState=true
2016-02-03 18:35:47 +00:00
[YetAnotherThing]
bar=baz
]])
fd:close()
os.remove(fn)
end)
it("should insert entry", function()
local fn = os.tmpname()
local fd = io.open(fn, "w")
fd:write([[
[PowerOptions]
foo=bar
[OtherThing]
bar=baz
]])
fd:close()
NickelConf._set_kobo_conf_path(fn)
NickelConf.frontLightLevel.set(1)
NickelConf.frontLightState.set(true)
2016-02-03 18:35:47 +00:00
fd = io.open(fn, "r")
assert.Equals([[
2016-02-03 18:35:47 +00:00
[PowerOptions]
foo=bar
FrontLightLevel=1
[OtherThing]
bar=baz
]], fd:read("*a"))
2016-02-03 18:35:47 +00:00
fd:close()
os.remove(fn)
end)
it("should create config file", function()
local fd
2016-02-03 18:35:47 +00:00
local fn = "/tmp/abcfoobarbaz449"
assert.is_not.Equals(lfs.attributes(fn, "mode"), "file")
finally(function() os.remove(fn) end)
NickelConf._set_kobo_conf_path(fn)
NickelConf.frontLightLevel.set(15)
NickelConf.frontLightState.set(false)
2016-02-03 18:35:47 +00:00
fd = io.open(fn, "r")
assert.Equals([[
[PowerOptions]
FrontLightLevel=15
]],
fd:read("*a"))
fd:close()
end)
end)
end)