diff --git a/ini_test.go b/ini_test.go index ed57568..ade2a2c 100644 --- a/ini_test.go +++ b/ini_test.go @@ -1585,3 +1585,75 @@ func TestPythonMultiline_EOF(t *testing.T) { require.NoError(t, err) assert.Equal(t, "some text here\n\tsome more text here 2", testData.Value1) } + +func Test_NestedValuesSpanningSections(t *testing.T) { + t.Run("basic nested value", func(t *testing.T) { + f, err := LoadSources(LoadOptions{ + AllowNestedValues: true, + }, []byte(` +[section] +key1 = value1 +key2 = + nested1 = nestedvalue1 +`)) + require.NoError(t, err) + require.NotNil(t, f) + + assert.Equal(t, "value1", f.Section("section").Key("key1").String()) + assert.Equal(t, "", f.Section("section").Key("key2").String()) + assert.Equal(t, []string{"nested1 = nestedvalue1"}, f.Section("section").Key("key2").NestedValues()) + }) + + t.Run("no nested values", func(t *testing.T) { + f, err := LoadSources(LoadOptions{ + AllowNestedValues: true, + }, []byte(` +[section] +key1 = value1 +key2 = +`)) + require.NoError(t, err) + require.NotNil(t, f) + + assert.Equal(t, "value1", f.Section("section").Key("key1").String()) + assert.Equal(t, "", f.Section("section").Key("key2").String()) + }) + + t.Run("no nested values and following sections", func(t *testing.T) { + f, err := LoadSources(LoadOptions{ + AllowNestedValues: true, + }, []byte(` +[section] +key1 = value1 +key2 = + +[section2] +key3 = value3 +`)) + require.NoError(t, err) + require.NotNil(t, f) + + assert.Equal(t, "value1", f.Section("section").Key("key1").String()) + assert.Equal(t, "", f.Section("section").Key("key2").String()) + assert.Equal(t, "value3", f.Section("section2").Key("key3").String()) + }) + + t.Run("no nested values and following sections with indentation", func(t *testing.T) { + f, err := LoadSources(LoadOptions{ + AllowNestedValues: true, + }, []byte(` +[section] +key1 = value1 +key2 = + +[section2] + key3 = value3 +`)) + require.NoError(t, err) + require.NotNil(t, f) + + assert.Equal(t, "value1", f.Section("section").Key("key1").String()) + assert.Equal(t, "", f.Section("section").Key("key2").String()) + assert.Equal(t, "value3", f.Section("section2").Key("key3").String()) + }) +} diff --git a/parser.go b/parser.go index 0eb08f1..ac1c980 100644 --- a/parser.go +++ b/parser.go @@ -441,6 +441,8 @@ func (f *File) parse(reader io.Reader) (err error) { // Reset auto-counter and comments p.comment.Reset() p.count = 1 + // Nested values can't span sections + isLastValueEmpty = false inUnparseableSection = false for i := range f.options.UnparseableSections {