1+ /**
2+ * Copyright 2018 Google Inc. All Rights Reserved.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the 'License');
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an 'AS IS' BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
117import * as assert from 'assert' ;
218import * as mocha from 'mocha' ;
3-
419import * as logger from '../src/common/consolelogger' ;
520
21+ const LEVELS = [ 'error' , 'warn' , 'info' , 'debug' , 'silly' ] ;
22+ let consoleTxt = '' ;
23+
24+
25+
626
727describe ( 'ConsoleLogger' , ( ) => {
8- describe ( 'new RootSpan()' , ( ) => {
9- it ( 'should create a RootSpan instance' , ( ) => {
10- const consoleLogger = logger ( { level : 'error' } ) ;
11-
12- consoleLogger . info ( "info" ) ;
13- consoleLogger . error ( "error" ) ;
14-
15- assert . ok ( true ) ;
16- } ) ;
28+ const intercept = require ( 'intercept-stdout' ) ;
29+ const unhookIntercept = intercept ( ( txt ) => {
30+ consoleTxt = txt ;
31+ return txt ;
32+ } ) ;
33+ describe ( 'new ConsoleLogger()' , ( ) => {
34+ it ( 'should levels from consoleLogger equals default levels' , ( ) => {
35+ const consoleLogger = logger ( ) ;
36+ assert . equal ( LEVELS . length , consoleLogger . logger . levels . length ) ;
37+ } ) ;
38+
39+ it ( 'should level from consoleLogger equal error' , ( ) => {
40+ const consoleLogger = logger ( LEVELS [ 0 ] ) ;
41+ assert . strictEqual ( LEVELS [ 0 ] , consoleLogger . logger . level ) ;
42+ } ) ;
43+ } ) ;
44+
45+ /**
46+ *
47+ */
48+ describe ( 'error logger' , ( ) => {
49+ const consoleLogger = logger ( LEVELS [ 0 ] ) ;
50+
51+ it ( 'should logger error in console' , ( ) => {
52+ consoleTxt = '' ;
53+ consoleLogger . error ( 'error test logger in console' ) ;
54+ unhookIntercept ( ) ;
55+ const validateString = consoleTxt . indexOf ( 'error' ) ;
56+
57+ assert . ok ( validateString >= 0 ) ;
58+ } ) ;
59+
60+ it ( 'should not logger warn in console' , ( ) => {
61+ consoleTxt = '' ;
62+ consoleLogger . warn ( 'warn test logger in console' ) ;
63+ unhookIntercept ( ) ;
64+
65+ const validateString = consoleTxt . indexOf ( 'warn' ) ;
66+
67+ assert . equal ( validateString , - 1 ) ;
68+ } ) ;
69+
70+ it ( 'should not logger info in console' , ( ) => {
71+ consoleTxt = '' ;
72+ consoleLogger . info ( 'info test logger in console' ) ;
73+ unhookIntercept ( ) ;
74+
75+ const validateString = consoleTxt . indexOf ( 'info' ) ;
76+
77+ assert . equal ( validateString , - 1 ) ;
78+ } ) ;
79+
80+ it ( 'should not logger debug in console' , ( ) => {
81+ consoleTxt = '' ;
82+ consoleLogger . debug ( 'debug test logger in console' ) ;
83+ unhookIntercept ( ) ;
84+ const validateString = consoleTxt . indexOf ( 'debug' ) ;
85+
86+ assert . equal ( validateString , - 1 ) ;
87+ } ) ;
88+
89+ it ( 'should not logger silly in console' , ( ) => {
90+ consoleTxt = '' ;
91+ consoleLogger . silly ( 'silly test logger in console' ) ;
92+ unhookIntercept ( ) ;
93+ const validateString = consoleTxt . indexOf ( 'silly' ) ;
94+
95+ assert . equal ( validateString , - 1 ) ;
96+ } ) ;
97+ } ) ;
98+
99+ describe ( 'info logger' , ( ) => {
100+
101+ const consoleLogger = logger ( LEVELS [ 2 ] ) ;
102+
103+ it ( 'should logger error in console' , ( ) => {
104+ const intercept = require ( 'intercept-stdout' ) ;
105+ const unhookIntercept = intercept ( ( txt ) => {
106+ consoleTxt = txt ;
107+ return txt ;
108+ } ) ;
109+
110+ consoleTxt = '' ;
111+ consoleLogger . error ( 'error test logger in console' ) ;
112+ unhookIntercept ( ) ;
113+ const validateString = consoleTxt . indexOf ( 'error' ) ;
114+
115+ assert . ok ( validateString >= 0 ) ;
116+ } ) ;
117+
118+ it ( 'should not logger warn in console' , ( ) => {
119+ const intercept = require ( 'intercept-stdout' ) ;
120+ const unhookIntercept = intercept ( ( txt ) => {
121+ consoleTxt = txt ;
122+ return txt ;
123+ } ) ;
124+
125+ consoleTxt = '' ;
126+ consoleLogger . warn ( 'warn test logger in console' ) ;
127+ unhookIntercept ( ) ;
128+ const validateString = consoleTxt . indexOf ( 'warn' ) ;
129+
130+ assert . ok ( validateString >= 0 ) ;
131+ } ) ;
132+
133+ it ( 'should logger info in console' , ( ) => {
134+ const intercept = require ( 'intercept-stdout' ) ;
135+ const unhookIntercept = intercept ( ( txt ) => {
136+ consoleTxt = txt ;
137+ return txt ;
138+ } ) ;
139+
140+ consoleTxt = '' ;
141+ consoleLogger . info ( 'info test logger in console' ) ;
142+ unhookIntercept ( ) ;
143+ const validateString = consoleTxt . indexOf ( 'info' ) ;
144+
145+ assert . ok ( validateString >= 0 ) ;
146+ } ) ;
147+
148+ it ( 'should not logger debug in console' , ( ) => {
149+ const intercept = require ( 'intercept-stdout' ) ;
150+ const unhookIntercept = intercept ( ( txt ) => {
151+ consoleTxt = txt ;
152+ return txt ;
153+ } ) ;
154+
155+ consoleTxt = '' ;
156+ consoleLogger . debug ( 'debug test logger in console' ) ;
157+ unhookIntercept ( ) ;
158+ const validateString = consoleTxt . indexOf ( 'debug' ) ;
159+
160+ assert . equal ( validateString , - 1 ) ;
161+ } ) ;
162+
163+ it ( 'should not logger silly in console' , ( ) => {
164+ const intercept = require ( 'intercept-stdout' ) ;
165+ const unhookIntercept = intercept ( ( txt ) => {
166+ consoleTxt = txt ;
167+ return txt ;
17168 } ) ;
18- } ) ;
169+
170+ consoleTxt = '' ;
171+ consoleLogger . silly ( 'silly test logger in console' ) ;
172+ unhookIntercept ( ) ;
173+ const validateString = consoleTxt . indexOf ( 'silly' ) ;
19174
175+ assert . equal ( validateString , - 1 ) ;
176+ } ) ;
177+ } ) ;
178+
179+ } ) ;
0 commit comments