| 
									
										
										
										
											2011-10-02 00:36:05 +02:00
										 |  |  | var assert = require('assert'), | 
					
						
							|  |  |  |     colors = require('./colors'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // This is a pretty nice example on how tests shouldn't be written. However,
 | 
					
						
							|  |  |  | // it's more about API stability than about really testing it (although it's
 | 
					
						
							|  |  |  | // a pretty complete test suite).
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var s = 'string'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function a(s, code) { | 
					
						
							| 
									
										
										
										
											2012-08-08 13:10:01 +02:00
										 |  |  |   return '\x1B[' + code.toString() + 'm' + s + '\x1B[39m'; | 
					
						
							| 
									
										
										
										
											2011-10-02 00:36:05 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function aE(s, color, code) { | 
					
						
							|  |  |  |   assert.equal(s[color], a(s, code)); | 
					
						
							|  |  |  |   assert.equal(colors[color](s), a(s, code)); | 
					
						
							|  |  |  |   assert.equal(s[color], colors[color](s)); | 
					
						
							|  |  |  |   assert.equal(s[color].stripColors, s); | 
					
						
							|  |  |  |   assert.equal(s[color].stripColors, colors.stripColors(s)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function h(s, color) { | 
					
						
							|  |  |  |   return '<span style="color:' + color + ';">' + s + '</span>'; | 
					
						
							|  |  |  |   // that's pretty dumb approach to testing it
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var stylesColors = ['white', 'grey', 'black', 'blue', 'cyan', 'green', 'magenta', 'red', 'yellow']; | 
					
						
							|  |  |  | var stylesAll = stylesColors.concat(['bold', 'italic', 'underline', 'inverse', 'rainbow']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | colors.mode = 'console'; | 
					
						
							| 
									
										
										
										
											2012-08-08 13:10:01 +02:00
										 |  |  | assert.equal(s.bold, '\x1B[1m' + s + '\x1B[22m'); | 
					
						
							|  |  |  | assert.equal(s.italic, '\x1B[3m' + s + '\x1B[23m'); | 
					
						
							|  |  |  | assert.equal(s.underline, '\x1B[4m' + s + '\x1B[24m'); | 
					
						
							| 
									
										
										
										
											2012-11-20 18:11:16 +01:00
										 |  |  | assert.equal(s.strikethrough, '\x1B[9m' + s + '\x1B[29m'); | 
					
						
							| 
									
										
										
										
											2012-08-08 13:10:01 +02:00
										 |  |  | assert.equal(s.inverse, '\x1B[7m' + s + '\x1B[27m'); | 
					
						
							| 
									
										
										
										
											2011-10-02 00:36:05 +02:00
										 |  |  | assert.ok(s.rainbow); | 
					
						
							|  |  |  | aE(s, 'white', 37); | 
					
						
							|  |  |  | aE(s, 'grey', 90); | 
					
						
							|  |  |  | aE(s, 'black', 30); | 
					
						
							|  |  |  | aE(s, 'blue', 34); | 
					
						
							|  |  |  | aE(s, 'cyan', 36); | 
					
						
							|  |  |  | aE(s, 'green', 32); | 
					
						
							|  |  |  | aE(s, 'magenta', 35); | 
					
						
							|  |  |  | aE(s, 'red', 31); | 
					
						
							|  |  |  | aE(s, 'yellow', 33); | 
					
						
							|  |  |  | assert.equal(s, 'string'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-19 18:51:41 +10:00
										 |  |  | colors.setTheme({error:'red'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | assert.equal(typeof("astring".red),'string'); | 
					
						
							|  |  |  | assert.equal(typeof("astring".error),'string'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-02 00:36:05 +02:00
										 |  |  | colors.mode = 'browser'; | 
					
						
							|  |  |  | assert.equal(s.bold, '<b>' + s + '</b>'); | 
					
						
							|  |  |  | assert.equal(s.italic, '<i>' + s + '</i>'); | 
					
						
							|  |  |  | assert.equal(s.underline, '<u>' + s + '</u>'); | 
					
						
							| 
									
										
										
										
											2012-11-20 18:11:16 +01:00
										 |  |  | assert.equal(s.strikethrough, '<del>' + s + '</del>'); | 
					
						
							| 
									
										
										
										
											2011-10-02 00:36:05 +02:00
										 |  |  | assert.equal(s.inverse, '<span style="background-color:black;color:white;">' + s + '</span>'); | 
					
						
							|  |  |  | assert.ok(s.rainbow); | 
					
						
							|  |  |  | stylesColors.forEach(function (color) { | 
					
						
							|  |  |  |   assert.equal(s[color], h(s, color)); | 
					
						
							|  |  |  |   assert.equal(colors[color](s), h(s, color)); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-19 18:51:41 +10:00
										 |  |  | assert.equal(typeof("astring".red),'string'); | 
					
						
							|  |  |  | assert.equal(typeof("astring".error),'string'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-02 00:36:05 +02:00
										 |  |  | colors.mode = 'none'; | 
					
						
							|  |  |  | stylesAll.forEach(function (style) { | 
					
						
							|  |  |  |   assert.equal(s[style], s); | 
					
						
							|  |  |  |   assert.equal(colors[style](s), s); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-19 18:51:41 +10:00
										 |  |  | assert.equal(typeof("astring".red),'string'); | 
					
						
							|  |  |  | assert.equal(typeof("astring".error),'string'); |