@@ -16,49 +16,76 @@ function Test() {
1616 if ( ! document . getElementById ( "test-results" ) ) {
1717 document . body . appendChild ( resultsContainer ) ;
1818 }
19- cases . forEach ( ( testCase ) => {
19+ for ( let i = 0 ; i < cases . length ; i ++ ) {
20+ const testCase = cases [ i ] ;
2021 const describeText = testCase . description . split ( " - " ) [ 0 ] ;
2122 const itText = testCase . description . split ( " - " ) [ 1 ] ;
2223 if ( currentDescribe !== describeText ) {
2324 currentDescribe = describeText ;
2425 const groupHeader = document . createElement ( "h2" ) ;
25- groupHeader . textContent = currentDescribe ;
26+ setTextContent ( groupHeader , currentDescribe ) ;
2627 resultsContainer . appendChild ( groupHeader ) ;
2728 }
2829 const resultElement = document . createElement ( "p" ) ;
2930 try {
3031 testCase . fn ( ) ;
31- resultElement . textContent = `✔ ${ itText } `;
32+ setTextContent ( resultElement , getPassSymbol ( ) + ` ${ itText } `) ;
3233 resultElement . style . color = "green" ;
3334 } catch ( error ) {
34- resultElement . textContent = `✖ ${ itText } - ${ error . message } `;
35+ setTextContent ( resultElement , getFailSymbol ( ) + ` ${ itText } - ${ error . message } `) ;
3536 resultElement . style . color = "red" ;
3637 }
3738 resultsContainer . appendChild ( resultElement ) ;
38- } ) ;
39+ }
3940 return ;
40- }
41- cases . forEach ( ( testCase ) => {
42- if ( currentDescribe !== testCase . description . split ( " - " ) [ 0 ] ) {
43- if ( currentDescribe !== null ) {
44- console . groupEnd ( ) ;
41+ } else {
42+ for ( let i = 0 ; i < cases . length ; i ++ ) {
43+ var testCase = cases [ i ] ;
44+ if ( currentDescribe !== testCase . description . split ( " - " ) [ 0 ] ) {
45+ if ( currentDescribe !== null && typeof console !== 'undefined' ) {
46+ console . groupEnd ( ) ;
47+ }
48+ currentDescribe = testCase . description . split ( " - " ) [ 0 ] ;
49+ if ( typeof console !== 'undefined' ) {
50+ console . group ( currentDescribe ) ;
51+ }
52+ }
53+ try {
54+ testCase . fn ( ) ;
55+ if ( typeof console !== 'undefined' ) {
56+ console . log ( '\x1b[32m' + getPassSymbol ( ) + ' ' + testCase . description + '\x1b[0m' ) ;
57+ }
58+ } catch ( error ) {
59+ if ( typeof console !== 'undefined' ) {
60+ console . error (
61+ '\x1b[31m' + getFailSymbol ( ) + ' ' + testCase . description + ' - ' + error . message + '\x1b[0m'
62+ ) ;
63+ }
4564 }
46- currentDescribe = testCase . description . split ( " - " ) [ 0 ] ;
47- console . group ( currentDescribe ) ;
4865 }
49- try {
50- testCase . fn ( ) ;
51- console . log ( `\x1b[32m✔ ${ testCase . description } \x1b[0m` ) ;
52- } catch ( error ) {
53- console . error (
54- `\x1b[31m✖ ${ testCase . description } - ${ error . message } \x1b[0m`
55- ) ;
66+ if ( currentDescribe !== null && typeof console !== 'undefined' ) {
67+ console . groupEnd ( ) ;
5668 }
57- } ) ;
58- if ( currentDescribe !== null ) {
59- console . groupEnd ( ) ;
6069 }
6170 }
71+
72+ // Helper function to set text content
73+ function setTextContent ( element , text ) {
74+ if ( typeof element . textContent !== 'undefined' ) {
75+ element . textContent = text ;
76+ } else {
77+ element . innerText = text ;
78+ }
79+ }
80+
81+ function getPassSymbol ( ) {
82+ return '>' ;
83+ }
84+
85+ function getFailSymbol ( ) {
86+ return '!' ;
87+ }
88+
6289 function deepCompare ( obj1 , obj2 ) {
6390 if (
6491 typeof obj1 !== "object" ||
@@ -68,16 +95,32 @@ function Test() {
6895 ) {
6996 return obj1 === obj2 ;
7097 }
71- const keys1 = Object . keys ( obj1 ) ;
72- const keys2 = Object . keys ( obj2 ) ;
98+
99+ var keys1 = [ ] ;
100+ for ( var key in obj1 ) {
101+ if ( Object . prototype . hasOwnProperty . call ( obj1 , key ) ) {
102+ keys1 . push ( key ) ;
103+ }
104+ }
105+
106+ var keys2 = [ ] ;
107+ for ( var key in obj2 ) {
108+ if ( Object . prototype . hasOwnProperty . call ( obj2 , key ) ) {
109+ keys2 . push ( key ) ;
110+ }
111+ }
112+
73113 if ( keys1 . length !== keys2 . length ) {
74114 return false ;
75115 }
76- for ( let key of keys1 ) {
77- if ( ! obj2 . hasOwnProperty ( key ) || ! deepCompare ( obj1 [ key ] , obj2 [ key ] ) ) {
116+
117+ for ( var i = 0 ; i < keys1 . length ; i ++ ) {
118+ var key = keys1 [ i ] ;
119+ if ( ! Object . prototype . hasOwnProperty . call ( obj2 , key ) || ! deepCompare ( obj1 [ key ] , obj2 [ key ] ) ) {
78120 return false ;
79121 }
80122 }
123+
81124 return true ;
82125 }
83126 function describe ( description , fn ) {
@@ -89,35 +132,35 @@ function Test() {
89132 function it ( description , fn ) {
90133 cases . push ( { description : currentDescription + " - " + description , fn } ) ;
91134 }
92- function assert ( condition , message = "" ) {
135+ function assert ( condition , message ) {
93136 if ( typeof window !== "undefined" ) {
94- if ( Array . isArray ( condition ) && condition . length === 2 ) {
137+ if ( typeof condition === 'object' && condition !== null && typeof condition . length === 'number' && condition . length === 2 ) {
95138 if ( ! deepCompare ( condition [ 0 ] , condition [ 1 ] ) ) {
96139 throw new Error (
97140 message +
98- ` Expected ${ JSON . stringify (
141+ ' Expected ' + JSON . stringify (
99142 condition [ 0 ]
100- ) } equals ${ JSON . stringify ( condition [ 1 ] ) } `
143+ ) + ' equals ' + JSON . stringify ( condition [ 1 ] )
101144 ) ;
102145 }
103146 } else {
104147 if ( ! condition ) {
105- throw new Error ( message + ` Expected ${ String ( condition ) } ` ) ;
148+ throw new Error ( message + ' Expected ' + String ( condition ) ) ;
106149 }
107150 }
108151 } else {
109- if ( Array . isArray ( condition ) && condition . length === 2 ) {
152+ if ( typeof condition === 'object' && condition !== null && typeof condition . length === 'number' && condition . length === 2 ) {
110153 if ( ! deepCompare ( condition [ 0 ] , condition [ 1 ] ) ) {
111154 throw new Error (
112155 message +
113- ` Expected ${ JSON . stringify (
156+ ' Expected ' + JSON . stringify (
114157 condition [ 0 ]
115- ) } equals ${ JSON . stringify ( condition [ 1 ] ) } `
158+ ) + ' equals ' + JSON . stringify ( condition [ 1 ] )
116159 ) ;
117160 }
118161 } else {
119162 if ( ! condition ) {
120- throw new Error ( message + ` Expected ${ String ( condition ) } ` ) ;
163+ throw new Error ( message + ' Expected ' + String ( condition ) ) ;
121164 }
122165 }
123166 }
@@ -126,4 +169,5 @@ function Test() {
126169 return { describe, it, assert } ;
127170}
128171
172+
129173module . exports = Test
0 commit comments