...

Source file src/gopkg.in/ini.v1/parser_test.go

Documentation: gopkg.in/ini.v1

     1  // Copyright 2016 Unknwon
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package ini
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestBOM(t *testing.T) {
    25  	t.Run("test handling BOM", func(t *testing.T) {
    26  		t.Run("UTF-8-BOM", func(t *testing.T) {
    27  			f, err := Load("testdata/UTF-8-BOM.ini")
    28  			require.NoError(t, err)
    29  			require.NotNil(t, f)
    30  
    31  			assert.Equal(t, "example@email.com", f.Section("author").Key("E-MAIL").String())
    32  		})
    33  
    34  		t.Run("UTF-16-LE-BOM", func(t *testing.T) {
    35  			f, err := Load("testdata/UTF-16-LE-BOM.ini")
    36  			require.NoError(t, err)
    37  			require.NotNil(t, f)
    38  		})
    39  
    40  		t.Run("UTF-16-BE-BOM", func(t *testing.T) {
    41  		})
    42  	})
    43  }
    44  
    45  func TestBadLoad(t *testing.T) {
    46  	t.Run("load with bad data", func(t *testing.T) {
    47  		t.Run("bad section name", func(t *testing.T) {
    48  			_, err := Load([]byte("[]"))
    49  			require.Error(t, err)
    50  
    51  			_, err = Load([]byte("["))
    52  			require.Error(t, err)
    53  		})
    54  
    55  		t.Run("bad keys", func(t *testing.T) {
    56  			_, err := Load([]byte(`"""name`))
    57  			require.Error(t, err)
    58  
    59  			_, err = Load([]byte(`"""name"""`))
    60  			require.Error(t, err)
    61  
    62  			_, err = Load([]byte(`""=1`))
    63  			require.Error(t, err)
    64  
    65  			_, err = Load([]byte(`=`))
    66  			require.Error(t, err)
    67  
    68  			_, err = Load([]byte(`name`))
    69  			require.Error(t, err)
    70  		})
    71  
    72  		t.Run("bad values", func(t *testing.T) {
    73  			_, err := Load([]byte(`name="""Unknwon`))
    74  			require.Error(t, err)
    75  		})
    76  	})
    77  }
    78  

View as plain text