-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
91 lines (70 loc) · 2.29 KB
/
main_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
package main
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
"clockapp/config"
"os"
"strings"
"log"
"bufio"
)
type timerconfig map[string]string
func TestClockLogic(t *testing.T) {
createdAt := time.Now()
rounded := time.Date(createdAt.Year(), createdAt.Month(), createdAt.Day(), 0, 0, 10, 0, createdAt.Location())
outtimestr := ClockLogic(rounded)
timerenv, err := readEnvFile("timer.env")
if err != nil {
t.Error("Error while reading timer.env file")
}
config.Loadconfiguration()
assert.Equal(t, timerenv["second"],outtimestr)
assert := assert.New(t)
assert.NotEqual(timerenv["minute"],outtimestr)
assert.NotEqual(timerenv["hour"],outtimestr)
rounded = time.Date(createdAt.Year(), createdAt.Month(), createdAt.Day(), 0, 0, 59, 0, createdAt.Location())
outtimestr = ClockLogic(rounded)
assert.NotEqual(timerenv["second"],outtimestr)
assert.Equal(timerenv["minute"],outtimestr)
assert.NotEqual(timerenv["hour"],outtimestr)
rounded = time.Date(createdAt.Year(), createdAt.Month(), createdAt.Day(), 0, 59, 59, 0, createdAt.Location())
outtimestr = ClockLogic(rounded)
assert.NotEqual(timerenv["second"],outtimestr)
assert.NotEqual(timerenv["minute"],outtimestr)
assert.Equal(timerenv["hour"],outtimestr)
}
func readEnvFile(filename string) (timerconfig, error) {
config := timerconfig{}
if len(filename) == 0 {
return config, nil
}
file, err := os.Open(filename)
if err != nil {
log.Println("Error here while opening")
log.Fatal(err)
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if equal := strings.Index(line, "="); equal >= 0 {
if key := strings.TrimSpace(line[:equal]); len(key) > 0 {
value := ""
if len(line) > equal {
value = strings.TrimSpace(line[equal+1:])
strings.Trim(value, "\"")
}
value = strings.TrimRight(value, "\r\"")
value = strings.TrimLeft(value, "\r\"")
config[key] = value
}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
return nil, err
}
return config, nil
}