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
|
package ini
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func TestValidDataFiles(t *testing.T) {
const expectedFileSuffix = "_expected"
err := filepath.Walk(filepath.Join("testdata", "valid"),
func(path string, info os.FileInfo, fnErr error) (err error) {
if strings.HasSuffix(path, expectedFileSuffix) {
return nil
}
if info.IsDir() {
return nil
}
f, err := os.Open(path)
if err != nil {
t.Errorf("%s: unexpected error, %v", path, err)
}
defer func() {
closeErr := f.Close()
if err == nil {
err = closeErr
} else if closeErr != nil {
err = fmt.Errorf("file close error: %v, original error: %w", closeErr, err)
}
}()
v, err := Parse(f, path)
if err != nil {
t.Errorf("%s: unexpected parse error, %v", path, err)
}
expectedPath := path + "_expected"
e := map[string]interface{}{}
b, err := ioutil.ReadFile(expectedPath)
if err != nil {
// ignore files that do not have an expected file
return nil
}
err = json.Unmarshal(b, &e)
if err != nil {
t.Errorf("unexpected error during deserialization, %v", err)
}
for profile, tableIface := range e {
p, ok := v.GetSection(profile)
if !ok {
t.Fatal("could not find profile " + profile)
}
table := tableIface.(map[string]interface{})
for k, v := range table {
switch e := v.(type) {
case string:
var a string
if p.values[k].mp != nil {
a = fmt.Sprintf("%v", p.values[k].mp)
} else {
a = p.values[k].str
}
if e != a {
t.Errorf("%s: expected %v, but received %v for profile %v", path, e, a, profile)
}
default:
t.Errorf("unexpected type: %T", e)
}
}
}
return nil
})
if err != nil {
t.Fatalf("Error while walking the file tree rooted at root, %d", err)
}
}
|