Skip to content

Commit 9c0c590

Browse files
committed
Was made refactoring and updated tests
1 parent 28554cb commit 9c0c590

File tree

9 files changed

+93
-20
lines changed

9 files changed

+93
-20
lines changed

src/JavaScriptEngineSwitcher.ChakraCore/Helpers/ReflectionHelpers.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public static bool IsFullyFledgedMethod(MethodInfo method)
4343

4444
public static void FixFieldValueType(ref object value, FieldInfo field)
4545
{
46+
if (value == null)
47+
{
48+
return;
49+
}
50+
4651
Type valueType = value.GetType();
4752
Type fieldType = field.FieldType;
4853

@@ -59,7 +64,11 @@ public static void FixFieldValueType(ref object value, FieldInfo field)
5964

6065
public static void FixPropertyValueType(ref object value, PropertyInfo property)
6166
{
62-
if (value == null) return;
67+
if (value == null)
68+
{
69+
return;
70+
}
71+
6372
Type valueType = value.GetType();
6473
Type propertyType = property.PropertyType;
6574

@@ -81,7 +90,11 @@ public static void FixArgumentTypes(ref object[] argValues, ParameterInfo[] para
8190
for (int argIndex = 0; argIndex < argCount; argIndex++)
8291
{
8392
object argValue = argValues[argIndex];
84-
if (argValue == null) continue;
93+
if (argValue == null)
94+
{
95+
continue;
96+
}
97+
8598
Type argType = argValue.GetType();
8699

87100
ParameterInfo parameter = parameters[argIndex];
@@ -124,17 +137,7 @@ public static MethodBase GetBestFitMethod(MethodBase[] methods, object[] argValu
124137
}
125138

126139
Type[] argTypes = argValues
127-
.Select(a =>
128-
{
129-
if (a == null)
130-
{
131-
return null;
132-
}
133-
else
134-
{
135-
return a.GetType();
136-
}
137-
})
140+
.Select(a => a != null ? a.GetType() : typeof(object))
138141
.ToArray()
139142
;
140143
var compatibleMethods = new List<MethodWithMetadata>();
@@ -234,4 +237,4 @@ public ushort CompatibilityScore
234237
}
235238
}
236239
}
237-
}
240+
}

src/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This package does not contain the native implementations of ChakraCore. Therefor
2222
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</Description>
2323
<PackageIconUrl>https://raw.githubusercontent.com/Taritsyn/JavaScriptEngineSwitcher/master/Icons/JavaScriptEngineSwitcher_ChakraCore_Logo128x128.png</PackageIconUrl>
2424
<PackageTags>JavaScriptEngineSwitcher;JavaScript;ECMAScript;ChakraCore</PackageTags>
25-
<PackageReleaseNotes>ChakraCore was updated to version 1.11.15.</PackageReleaseNotes>
25+
<PackageReleaseNotes>Fixed a errors leading to null reference exceptions in the `ReflectionHelpers` class. Special thanks to Vanjoge.</PackageReleaseNotes>
2626
</PropertyGroup>
2727

2828
<Import Project="../../build/common.props" />

src/JavaScriptEngineSwitcher.ChakraCore/readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
=============
3131
RELEASE NOTES
3232
=============
33-
ChakraCore was updated to version 1.11.15.
33+
Fixed a errors leading to null reference exceptions in the `ReflectionHelpers`
34+
class. Special thanks to Vanjoge.
3435

3536
=============
3637
DOCUMENTATION

test/JavaScriptEngineSwitcher.Tests/Interop/FileManager.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
using System;
22
using System.IO;
3+
using System.Text;
34

45
namespace JavaScriptEngineSwitcher.Tests.Interop
56
{
67
public sealed class FileManager
78
{
89
public string ReadFile(string path)
10+
{
11+
return ReadFile(path, null);
12+
}
13+
14+
public string ReadFile(string path, Encoding encoding)
915
{
1016
if (path == null)
1117
{
1218
throw new ArgumentNullException("path");
1319
}
1420

15-
string content = File.ReadAllText(path);
21+
encoding = encoding ?? Encoding.UTF8;
22+
23+
string content = File.ReadAllText(path, encoding);
1624

1725
return content;
1826
}

test/JavaScriptEngineSwitcher.Tests/Interop/Person.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,26 @@ public string LastName
1414
set;
1515
}
1616

17+
public string Patronymic
18+
{
19+
get;
20+
set;
21+
}
22+
1723

1824
public Person()
1925
: this(string.Empty, string.Empty)
2026
{ }
2127

2228
public Person(string firstName, string lastName)
29+
: this(firstName, lastName, string.Empty)
30+
{ }
31+
32+
public Person(string firstName, string lastName, string patronymic)
2333
{
2434
FirstName = firstName;
2535
LastName = lastName;
36+
Patronymic = patronymic;
2637
}
2738

2839

test/JavaScriptEngineSwitcher.Tests/Interop/Product.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public sealed class Product
44
{
55
public string Name;
6+
public string Description;
67
public double Price;
78
}
89
}

test/JavaScriptEngineSwitcher.Tests/InteropTestsBase.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ public virtual void EmbeddingOfInstanceOfCustomReferenceTypeWithFieldsIsCorrect(
7070
var product = new Product
7171
{
7272
Name = "Red T-shirt",
73+
Description = string.Empty,
7374
Price = 995.00
7475
};
7576

76-
const string updateCode = "product.Price *= 1.15;";
77+
const string updateCode = @"product.Description = null;
78+
product.Price *= 1.15;";
7779

7880
const string input1 = "product.Name";
7981
const string targetOutput1 = "Red T-shirt";
@@ -219,7 +221,8 @@ public virtual void EmbeddingOfInstanceOfCustomReferenceTypeWithPropertiesIsCorr
219221
{
220222
// Arrange
221223
var person = new Person("Vanya", "Ivanov");
222-
const string updateCode = "person.LastName = person.LastName.substr(0, 5) + 'ff';";
224+
const string updateCode = @"person.LastName = person.LastName.substr(0, 5) + 'ff';
225+
person.Patronymic = null;";
223226

224227
const string input1 = "person.FirstName";
225228
const string targetOutput1 = "Vanya";
@@ -406,7 +409,7 @@ public virtual void EmbeddingOfInstanceOfCustomReferenceTypeWithMethodIsCorrect(
406409
var fileManager = new FileManager();
407410
const string filePath = "Files/link.txt";
408411

409-
string input = string.Format("fileManager.ReadFile('{0}')", filePath.Replace(@"\", @"\\"));
412+
string input = string.Format("fileManager.ReadFile('{0}', null)", filePath.Replace(@"\", @"\\"));
410413
const string targetOutput = "http://www.panopticoncentral.net/2015/09/09/the-two-faces-of-jsrt-in-windows-10/";
411414

412415
// Act

test/JavaScriptEngineSwitcher.Tests/Jint/InteropTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
using JavaScriptEngineSwitcher.Core;
88

9+
using JavaScriptEngineSwitcher.Tests.Interop;
10+
911
namespace JavaScriptEngineSwitcher.Tests.Jint
1012
{
1113
public class InteropTests : InteropTestsBase
@@ -18,6 +20,47 @@ protected override string EngineName
1820

1921
#region Embedding of objects
2022

23+
#region Objects with fields
24+
25+
[Fact]
26+
public override void EmbeddingOfInstanceOfCustomReferenceTypeWithFieldsIsCorrect()
27+
{
28+
// Arrange
29+
var product = new Product
30+
{
31+
Name = "Red T-shirt",
32+
Description = string.Empty,
33+
Price = 995.00
34+
};
35+
36+
const string updateCode = "product.Price *= 1.15;";
37+
38+
const string input1 = "product.Name";
39+
const string targetOutput1 = "Red T-shirt";
40+
41+
const string input2 = "product.Price";
42+
const double targetOutput2 = 1144.25;
43+
44+
// Act
45+
string output1;
46+
double output2;
47+
48+
using (var jsEngine = CreateJsEngine())
49+
{
50+
jsEngine.EmbedHostObject("product", product);
51+
jsEngine.Execute(updateCode);
52+
53+
output1 = jsEngine.Evaluate<string>(input1);
54+
output2 = jsEngine.Evaluate<double>(input2);
55+
}
56+
57+
// Assert
58+
Assert.Equal(targetOutput1, output1);
59+
Assert.Equal(targetOutput2, output2);
60+
}
61+
62+
#endregion
63+
2164
#region Recursive calls
2265

2366
#region Mapping of errors

test/JavaScriptEngineSwitcher.Tests/Vroom/InteropTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public override void EmbeddingOfInstanceOfCustomReferenceTypeWithFieldsIsCorrect
2525
public override void EmbeddingOfInstanceOfBuiltinReferenceTypeWithMethodIsCorrect()
2626
{ }
2727

28+
public override void EmbeddingOfInstanceOfCustomReferenceTypeWithMethodIsCorrect()
29+
{ }
30+
2831
#endregion
2932

3033
#region Delegates

0 commit comments

Comments
 (0)