1
1
var stylize = require ( '../vows/console' ) . stylize ;
2
2
var inspect = require ( '../vows/console' ) . inspect ;
3
+ var diff = require ( 'diff' ) ;
4
+
5
+ /**
6
+ * Pad the given `str` to `len`.
7
+ *
8
+ * @param {String } str
9
+ * @param {String } len
10
+ * @return {String }
11
+ * @api private
12
+ */
13
+
14
+ function pad ( str , len ) {
15
+ str = String ( str ) ;
16
+ return Array ( len - str . length + 1 ) . join ( ' ' ) + str ;
17
+ }
18
+
19
+ /**
20
+ * Color lines for `str`, using the color `name`.
21
+ *
22
+ * @param {String } name
23
+ * @param {String } str
24
+ * @return {String }
25
+ * @api private
26
+ */
27
+
28
+ function styleLines ( str , name ) {
29
+ return str . split ( '\n' ) . map ( function ( str ) {
30
+ return stylize ( str , name ) ;
31
+ } ) . join ( '\n' ) ;
32
+ }
33
+
34
+ /**
35
+ * Return a character diff for `err`.
36
+ *
37
+ * @param {Error } err
38
+ * @return {String }
39
+ * @api private
40
+ */
41
+
42
+ function errorDiff ( err , type ) {
43
+ return diff [ 'diff' + type ] ( err . actual , err . expected ) . map ( function ( str ) {
44
+ if ( / ^ ( \n + ) $ / . test ( str . value ) ) str . value = Array ( ++ RegExp . $1 . length ) . join ( '<newline>' ) ;
45
+ if ( str . added ) return styleLines ( str . value , 'green' ) ;
46
+ if ( str . removed ) return styleLines ( str . value , 'red' ) ;
47
+ return str . value ;
48
+ } ) . join ( '' ) ;
49
+ }
3
50
4
51
require ( 'assert' ) . AssertionError . prototype . toString = function ( ) {
5
52
var that = this ,
@@ -10,14 +57,50 @@ require('assert').AssertionError.prototype.toString = function () {
10
57
}
11
58
12
59
function parse ( str ) {
13
- var actual = inspect ( that . actual , { showHidden : that . actual instanceof Error } ) ,
14
- expected ;
60
+ var actual = that . actual ,
61
+ expected = that . expected ,
62
+ msg , len ;
63
+
64
+ if (
65
+ 'string' === typeof actual &&
66
+ 'string' === typeof expected
67
+ ) {
68
+ len = Math . max ( actual . length , expected . length ) ;
69
+
70
+ if ( len < 20 ) msg = errorDiff ( that , 'Chars' ) ;
71
+ else msg = errorDiff ( that , 'Words' ) ;
72
+
73
+ // linenos
74
+ var lines = msg . split ( '\n' ) ;
75
+ if ( lines . length > 4 ) {
76
+ var width = String ( lines . length ) . length ;
77
+ msg = lines . map ( function ( str , i ) {
78
+ return pad ( ++ i , width ) + ' |' + ' ' + str ;
79
+ } ) . join ( '\n' ) ;
80
+ }
81
+
82
+ // legend
83
+ msg = '\n'
84
+ + stylize ( 'actual' , 'green' )
85
+ + ' '
86
+ + stylize ( 'expected' , 'red' )
87
+ + '\n\n'
88
+ + msg
89
+ + '\n' ;
90
+
91
+ // indent
92
+ msg = msg . replace ( / ^ / gm, ' ' ) ;
93
+
94
+ return msg ;
95
+ }
96
+
97
+ actual = inspect ( actual , { showHidden : actual instanceof Error } ) ;
15
98
16
- if ( that . expected instanceof Function ) {
17
- expected = that . expected . name ;
99
+ if ( expected instanceof Function ) {
100
+ expected = expected . name ;
18
101
}
19
102
else {
20
- expected = inspect ( that . expected , { showHidden : that . actual instanceof Error } ) ;
103
+ expected = inspect ( expected , { showHidden : actual instanceof Error } ) ;
21
104
}
22
105
23
106
return str . replace ( / { a c t u a l } / g, actual ) .
0 commit comments