alog.casesIoWrite   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
dl 0
loc 25
rs 9.45
c 0
b 0
f 0
nop 0
1
////////////////////////////////////////////////////////////////////////////////
2
// Author:   Nikita Koryabkin
3
// Email:    [email protected]
4
// Telegram: https://t.me/Apologiz
5
////////////////////////////////////////////////////////////////////////////////
6
7
package alog
8
9
import (
10
	"io"
11
	"testing"
12
13
	"github.com/mylockerteam/alog/strategy/file"
14
)
15
16
type argsLoggerWriteMessage struct {
17
	msg string
18
}
19
20
type testsLoggerWriteMessage struct {
21
	name   string
22
	fields Logger
23
	args   argsLoggerWriteMessage
24
}
25
26
func casesLoggerWriteMessage() []testsLoggerWriteMessage {
27
	l := loggerProvider()
28
	return []testsLoggerWriteMessage{
29
		{
30
			fields: Logger{
31
				Channel:    l.Channel,
32
				Strategies: l.Strategies,
33
			},
34
			args: argsLoggerWriteMessage{
35
				msg: testMsg,
36
			},
37
		},
38
		{
39
			fields: Logger{
40
				Channel: make(chan string),
41
				Strategies: []io.Writer{
42
					file.Get(""),
43
				},
44
			},
45
			args: argsLoggerWriteMessage{
46
				msg: testMsg,
47
			},
48
		},
49
	}
50
}
51
52
func TestLogger_writeMessage(t *testing.T) {
53
	tests := casesLoggerWriteMessage()
54
	for _, tt := range tests {
55
		t.Run(tt.name, func(t *testing.T) {
56
			l := &Logger{
57
				Channel:    tt.fields.Channel,
58
				Strategies: tt.fields.Strategies,
59
			}
60
			l.writeMessage(tt.args.msg)
61
		})
62
	}
63
}
64
65
type argsIoWrite struct {
66
	p []byte
67
}
68
69
type testsIoWrite struct {
70
	name    string
71
	fields  Logger
72
	args    argsIoWrite
73
	wantN   int
74
	wantErr bool
75
}
76
77
func casesIoWrite() []testsIoWrite {
78
	logger := loggerProvider()
79
	close(logger.Channel)
80
	return []testsIoWrite{
81
		{
82
			fields: Logger{
83
				Channel:    make(chan string, 1),
84
				Strategies: logger.Strategies,
85
			},
86
			args: argsIoWrite{
87
				p: []byte(testMsg),
88
			},
89
			wantErr: false,
90
			wantN:   12,
91
		},
92
		{
93
			fields: Logger{
94
				Channel:    logger.Channel,
95
				Strategies: logger.Strategies,
96
			},
97
			args: argsIoWrite{
98
				p: []byte(testMsg),
99
			},
100
			wantErr: true,
101
			wantN:   0,
102
		},
103
	}
104
}
105
106
func Test_io_Write(t *testing.T) {
107
	tests := casesIoWrite()
108
	for _, tt := range tests {
109
		t.Run(tt.name, func(t *testing.T) {
110
			l := &Logger{
111
				Channel:    tt.fields.Channel,
112
				Strategies: tt.fields.Strategies,
113
			}
114
			gotN, err := l.Write(tt.args.p)
115
			if (err != nil) != tt.wantErr {
116
				t.Errorf("Logger.Write() error = %v, wantErr %v", err, tt.wantErr)
117
				return
118
			}
119
			if gotN != tt.wantN {
120
				t.Errorf("Logger.Write() = %v, want %v", gotN, tt.wantN)
121
			}
122
		})
123
	}
124
}
125
126
type testsLoggerReader struct {
127
	name   string
128
	fields Logger
129
}
130
131
func casesLoggerReader() []testsLoggerReader {
132
	l := loggerProvider()
133
	l.Channel <- testMsg
134
	return []testsLoggerReader{
135
		{
136
			fields: Logger{
137
				Channel:    l.Channel,
138
				Strategies: l.Strategies,
139
			},
140
		},
141
	}
142
}
143
144
func TestLogger_reader(t *testing.T) {
145
	tests := casesLoggerReader()
146
	for _, tt := range tests {
147
		t.Run(tt.name, func(t *testing.T) {
148
			l := &Logger{
149
				Channel:    tt.fields.Channel,
150
				Strategies: tt.fields.Strategies,
151
			}
152
			go l.Reader()
153
		})
154
	}
155
}
156
157
func TestName(t *testing.T) {
158
	type args struct {
159
		code uint
160
	}
161
	tests := []struct {
162
		name string
163
		args args
164
		want string
165
	}{
166
		{
167
			args: args{
168
				code: Info,
169
			},
170
			want: "Info",
171
		},
172
		{
173
			args: args{
174
				code: Wrn,
175
			},
176
			want: "Warning",
177
		},
178
		{
179
			args: args{
180
				code: Err,
181
			},
182
			want: "Error",
183
		},
184
	}
185
	for _, tt := range tests {
186
		t.Run(tt.name, func(t *testing.T) {
187
			if got := Name(tt.args.code); got != tt.want {
188
				t.Errorf("Name() = %v, want %v", got, tt.want)
189
			}
190
		})
191
	}
192
}
193