-
Notifications
You must be signed in to change notification settings - Fork 0
/
envie_test.go
106 lines (98 loc) · 2.56 KB
/
envie_test.go
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
//
// envie_test.go
// olog
//
// Copyright 2020 Daher Alfawares
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2
//
// Unless required by applicable law or agreed to in writing,
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions
// limitations under the License
package envie
import (
"os"
"testing"
)
type fail struct {
V1 string `envie:"TEST_VARIABLE_x"`
V2 string `envie:"TEST_VARIABLE_y"`
}
func TestAutoFails(t *testing.T) {
AutoPanic = false
var e fail
Auto(&e)
empty := empty(&e)
if len(empty) != 2 {
t.Errorf("expected 2 errors, found %v", len(empty))
}
}
type entity struct {
V1 string `envie:"TEST_VARIABLE_1"`
V2 string `envie:"TEST_VARIABLE_2"`
}
func TestUnmarshalFromFile(t *testing.T) {
hello := "hello"
world := "world"
var e entity
err := UnmarshalFromFile(".env", &e)
empty := empty(&e)
if len(empty) != 0 {
t.Errorf("expected 0 errors, found %v", len(empty))
}
if err != nil {
t.Errorf("Error %v", err)
}
if e.V1 != hello {
t.Errorf("incorrect environment variable:\nexpected:%v\nfound:%v", hello, e.V1)
}
if e.V2 != world {
t.Errorf("incorrect environment variable:\nexpected:%v\nfound:%v", world, e.V2)
}
}
func TestAuto(t *testing.T) {
var e entity
hello := "hello"
world := "world"
Auto(&e)
if e.V1 != hello {
t.Errorf("incorrect environment variable:\nexpected:%v\nfound:%v", hello, e.V1)
}
if e.V2 != world {
t.Errorf("incorrect environment variable:\nexpected:%v\nfound:%v", world, e.V2)
}
hello = "Hello"
world = "World"
os.Setenv("TEST_VARIABLE_1", hello)
os.Setenv("TEST_VARIABLE_2", world)
Auto(&e)
if e.V1 != hello {
t.Errorf("incorrect environment variable:\nexpected:%v\nfound:%v", hello, e.V1)
}
if e.V2 != world {
t.Errorf("incorrect environment variable:\nexpected:%v\nfound:%v", world, e.V2)
}
}
func TestUnmarshalFromSystem(t *testing.T) {
hello := "hello"
world := "world"
os.Setenv("TEST_VARIABLE_1", hello)
os.Setenv("TEST_VARIABLE_2", world)
var e entity
err := UnmarshalFromSystem(&e)
if err != nil {
t.Errorf("Error %v", err)
}
if e.V1 != hello {
t.Errorf("incorrect environment variable:\nexpected:%v\nfound:%v", hello, e.V1)
}
if e.V2 != world {
t.Errorf("incorrect environment variable:\nexpected:%v\nfound:%v", world, e.V2)
}
}