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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
require "./spec_helper"
require "ini"
describe "INI" do
describe "parse" do
context "from String" do
it "fails on malformed section" do
expect_raises(INI::ParseException, "Unterminated section") do
INI.parse("[section")
end
end
it "fails on data after section" do
expect_raises(INI::ParseException, "Data after section") do
INI.parse("[section] foo ")
end
end
it "fails on malformed declaration" do
expect_raises(INI::ParseException, "Expected declaration") do
INI.parse("foobar")
end
expect_raises(INI::ParseException, "Expected declaration") do
INI.parse("foo: bar")
end
end
it "parses key = value" do
INI.parse("key = value").should eq({"" => {"key" => "value"}})
end
it "parses empty values" do
INI.parse("key = ").should eq({"" => {"key" => ""}})
end
it "ignores whitespaces" do
INI.parse(" key = value ").should eq({"" => {"key" => "value"}})
INI.parse(" [foo]").should eq({"foo" => Hash(String, String).new})
end
it "ignores comments" do
INI.parse("; foo\n# bar\nkey = value").should eq({"" => {"key" => "value"}})
end
it "parses sections" do
INI.parse("[section]\na = 1").should eq({"section" => {"a" => "1"}})
end
it "parses a reopened section" do
INI.parse("[foo]\na=1\n[foo]\nb=2").should eq({"foo" => {"a" => "1", "b" => "2"}})
end
it "parses empty section" do
INI.parse("[section]").should eq({"section" => Hash(String, String).new})
end
end
context "from IO" do
it "parses a file" do
File.open datapath("test_file.ini") do |file|
INI.parse(file).should eq({
"general" => {
"log_level" => "D",
},
"section1" => {
"foo" => "1.1",
"bar" => "2",
},
"section2" => {
"x.y.z" => "coco lala",
},
})
end
end
end
end
describe "build to an INI-formatted output" do
it "builds from a Hash" do
INI.build({
"general" => {
"log_level" => 'D',
},
"section1" => {
"foo" => 1.1,
"bar" => 2,
},
"section2" => {
"x.y.z" => "coco lala",
},
}, true).should eq(File.read datapath("test_file.ini"))
end
it "builds from a NamedTuple" do
INI.build({
"general": {
"log_level": 'D',
},
"section1": {
"foo": 1.1,
"bar": 2,
},
"section2": {
"x.y.z": "coco lala",
},
}, true).should eq(File.read datapath("test_file.ini"))
end
it "builds with no spaces around `=`" do
INI.build({"foo" => {"a" => "1"}}, false).should eq("[foo]\na=1\n\n")
end
it "builds with no sections" do
INI.build({"" => {"a" => "1"}}, false).should eq("a=1\n")
end
it "builds an empty section before non-empty sections" do
INI.build({"foo" => {"a" => "1"}, "" => {"b" => "2"}}, false).should eq("b=2\n[foo]\na=1\n\n")
end
end
end
|