File: shconf_test.go

package info (click to toggle)
golang-github-tredoe-osutil 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: makefile: 4
file content (122 lines) | stat: -rw-r--r-- 2,535 bytes parent folder | download
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
122
// Copyright 2012 Jonas mg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package shconf

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
)

var testdata = []struct {
	k string
	v string
}{
	{"BOOL", "true"},
	{"INT", "-2"},
	{"UINT", "5"},
	{"FLOAT", "3.3"},
	{"STRING", "small"},
}

type conf struct {
	BOOL   bool
	INT    int
	UINT   uint
	FLOAT  float64
	STRING string
}

func TestParseFile(t *testing.T) {
	// == Create temporary file
	file, _ := ioutil.TempFile("", "test")

	buf := bufio.NewWriter(file)
	buf.WriteString("# main comment\n\n")
	buf.WriteString(fmt.Sprintf("%s=%s\n", testdata[0].k, testdata[0].v))
	buf.WriteString(fmt.Sprintf("%s=%s\n\n", testdata[1].k, testdata[1].v))
	buf.WriteString(fmt.Sprintf("%s=%s\n\n", testdata[2].k, testdata[2].v))
	buf.WriteString("# Another comment\n")
	buf.WriteString(fmt.Sprintf("%s=%s\n", testdata[3].k, testdata[3].v))
	buf.WriteString(fmt.Sprintf("%s=%s\n", testdata[4].k, testdata[4].v))
	buf.Flush()
	file.Close()

	defer func() {
		files, err := filepath.Glob(file.Name() + "*")
		if err != nil {
			t.Error(err)
			return
		}
		for _, v := range files {
			if err = os.Remove(v); err != nil {
				t.Error(err)
			}
		}
	}()

	// == Parser
	conf_ok := &conf{}
	conf_bad := conf{}

	cfg, err := ParseFile(file.Name())
	if err != nil {
		t.Fatal(err)
	}

	for k, _ := range cfg.data {
		switch k {
		case "BOOL":
			_, err = cfg.Getbool(k)
		case "INT":
			_, err = cfg.Getint(k)
		case "UINT":
			_, err = cfg.Getuint(k)
		case "FLOAT":
			_, err = cfg.Getfloat(k)
		case "STRING":
			_, err = cfg.Get(k)
		}
		if err != nil {
			t.Errorf("parser: %q got wrong value", k)
		}
	}
	if _, err = cfg.Get("no_key"); err != ErrKey {
		t.Error("expected to get ErrKey")
	}

	if err = cfg.Unmarshal(conf_ok); err != nil {
		t.Error(err)
	}
	if err = cfg.Unmarshal(conf_bad); err != ErrStructPtr {
		t.Error("expected to get ErrStructPtr")
	}

	if separator[0] != cfg.separator[0] {
		t.Errorf("separator: expected %q, got %q", separator, cfg.separator)
	}

	// == Editing
	if err = cfg.Set("STRING", "big"); err != nil {
		t.Fatal(err)
	}
	if cfg.data["STRING"] != "big" {
		t.Errorf("edit: value %q could not be set in key %q", "big", "STRING")
	}

	if err = cfg.Set("Not", ""); err == nil {
		t.Errorf("edit: key %q should not exist", "Not")
	}
	// ==

	if testing.Verbose() {
		cfg.Print()
	}
}