1+ using NUnit . Framework ;
2+
3+ using MsieJavaScriptEngine . Helpers ;
4+
5+ namespace MsieJavaScriptEngine . Test . Common
6+ {
7+ [ TestFixture ]
8+ public class ErrorFormattingTests
9+ {
10+ [ Test ]
11+ public void GettingSourceFragmentFromLineIsCorrect ( )
12+ {
13+ // Arrange
14+ const string input1 = "" ;
15+ const string targetOutput1 = input1 ;
16+
17+ const string input2 = " \n " ;
18+ const string targetOutput2 = input2 ;
19+
20+ const string input3 = "var @variable3 = 678;" ;
21+ const string targetOutput3 = input3 ;
22+
23+ const string input4 = " Math.hasOwnProperty(\" log2\" )||(Math.log2=function(n){" +
24+ "return Math.log(@n)*Math.LOG2E});" ;
25+ const string targetOutput4 = "…Math.hasOwnProperty(\" log2\" )||(Math.log2=function(n){" +
26+ "return Math.log(@n)*Math.LOG2E});" ;
27+
28+ const string input5 = "function mix(destination,source){var propertyName;destination=destination||{};" +
29+ "for(propertyName in source){if(source.hasOwnProperty(propertyName){" +
30+ "destination[propertyName]=source[propertyName]}}return destination}"
31+ ;
32+ const string targetOutput5 = "… in source){if(source.hasOwnProperty(propertyName){" +
33+ "destination[propertyName]=source[propertyName]}}r…" ;
34+
35+ const string input6 = "Object.hasOwnProperty(\" assign)||(Object.assign=function(n){" +
36+ "var u,i,f,t,r;if(typeof n==\" undefined\" ||n===null)" +
37+ "throw new TypeError(\" Object.assign: argument is not an Object.\" );" +
38+ "for(u=Object(n),f=arguments.length,i=1;i<f;i++)" +
39+ "if(t=arguments[i],typeof t!=\" undefined\" &&t!==null)for(r in t)" +
40+ "Object.prototype.hasOwnProperty.call(t,r)&&(u[r]=t[r]);return u});"
41+ ;
42+ const string targetOutput6 = "Object.hasOwnProperty(\" assign)||(Object.assign=function(n){" +
43+ "var u,i,f,t,r;if(typeof n==\" undefined\" ||n…" ;
44+
45+ const string input7 = "function base64_encode(a){" +
46+ "var b=\" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\" ;" +
47+ "var c,o2,o3,h1,h2,h3,h4,bits,i=0,enc='';do{c=a.charCodeAt(i++);o2=a.charCodeAt(i++);" +
48+ "o3=a.charCodeAt(i++);bits=c<<16|o2<<8|o3;h1=bits>>18&0x3f;h2=bits>>12&0x3f;" +
49+ "h3=bits>>6&0x3f;h4=bits&0x3f;enc+=b.charAt(h1)+b.charAt(h2)+b.charAt(h3)+b.charAt(h4)}" +
50+ "while(i<a.length);switch(a.length%3){case 1:enc=enc.slice(0,-2)+'==';break;" +
51+ "case 2:enc=enc.slice(0,-1)+'=';break}return @enc}"
52+ ;
53+ const string targetOutput7 = "…(a.length%3){case 1:enc=enc.slice(0,-2)+'==';break;" +
54+ "case 2:enc=enc.slice(0,-1)+'=';break}return @enc}" ;
55+
56+ // Act
57+ string output1 = JsErrorHelpers . GetSourceFragmentFromLine ( input1 , 1 , 100 ) ;
58+ string output2 = JsErrorHelpers . GetSourceFragmentFromLine ( input2 , 1 , 100 ) ;
59+ string output3 = JsErrorHelpers . GetSourceFragmentFromLine ( input3 , 5 , 100 ) ;
60+ string output4 = JsErrorHelpers . GetSourceFragmentFromLine ( input4 , 70 , 85 ) ;
61+ string output5 = JsErrorHelpers . GetSourceFragmentFromLine ( input5 , 145 , 100 ) ;
62+ string output6 = JsErrorHelpers . GetSourceFragmentFromLine ( input6 , 23 , 100 ) ;
63+ string output7 = JsErrorHelpers . GetSourceFragmentFromLine ( input7 , 465 , 100 ) ;
64+
65+ // Assert
66+ Assert . AreEqual ( targetOutput1 , output1 ) ;
67+ Assert . AreEqual ( targetOutput2 , output2 ) ;
68+ Assert . AreEqual ( targetOutput3 , output3 ) ;
69+ Assert . AreEqual ( targetOutput4 , output4 ) ;
70+ Assert . AreEqual ( targetOutput5 , output5 ) ;
71+ Assert . AreEqual ( targetOutput6 , output6 ) ;
72+ Assert . AreEqual ( targetOutput7 , output7 ) ;
73+ }
74+ }
75+ }
0 commit comments