This repository was archived by the owner on Jun 5, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathAssert.cs
More file actions
157 lines (137 loc) · 6.89 KB
/
Copy pathAssert.cs
File metadata and controls
157 lines (137 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright 2014 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Jurassic;
using Jurassic.Library;
namespace SharpJS.Libraries
{
public class Assert : /* Constructor-less */ Module
{
public readonly AssertionErrorConstructor AssertionError;
public Assert(Context context) : base(context) {
AssertionError = new AssertionErrorConstructor(context);
Populate();
SetPropertyValue("AssertionArray", AssertionError, true);
}
private static string truncate(string s, int n) {
if (s == null)
return null;
return s.Length < n ? s : s.Substring(0, n);
}
public static ErrorInstance MakeError(Assert self, object actual, object expected, object message, object operatr) {
bool generated = false;
if (message == null || TypeComparer.Equals(message, Undefined.Value)) {
message = truncate(JSONObject.Stringify(self.Engine, actual), 128)
+ ' '
+ operatr
+ ' '
+ truncate(JSONObject.Stringify(self.Engine, expected), 128);
generated = true;
}
ErrorInstance err = self.Engine.Error.Construct(message as string);
err.SetPropertyValue("message", message, false);
err.SetPropertyValue("name", "AssertionError", false);
err.SetPropertyValue("actual", actual, false);
err.SetPropertyValue("expected", expected, false);
err.SetPropertyValue("operator", operatr, false);
err.SetPropertyValue("generatedMessage", generated, false);
return err;
}
[JSFunction(Name = "fail", IsEnumerable = true)]
public void Fail(object actual, object expected, object message, object operatr) {
throw new JavaScriptException(MakeError(this, actual, expected, message, operatr), -1, "Assert.cs");
}
[JSFunction(Name = "ok", IsEnumerable = true)]
public void Ok(object value, object message) {
if (TypeComparer.Equals(value, false))
Fail(value, true, message, "==");
}
[JSFunction(Name = "ifError", IsEnumerable = true)]
public void IfError(object value, object message) {
if (TypeComparer.Equals(value, true))
throw new JavaScriptException(value, 49, "Assert.cs");
}
[JSFunction(Name = "equal", IsEnumerable = true)]
public void Equal(object actual, object expected, object message) {
if (!TypeComparer.Equals(actual, expected))
Fail(actual, expected, message, "==");
}
[JSFunction(Name = "notEqual", IsEnumerable = true)]
public void NotEqual(object actual, object expected, object message) {
if (TypeComparer.Equals(actual, expected))
Fail(actual, expected, message, "!=");
}
[JSFunction(Name = "strictEqual", IsEnumerable = true)]
public void StrictEqual(object actual, object expected, object message) {
if (!TypeComparer.StrictEquals(actual, expected))
Fail(actual, expected, message, "===");
}
[JSFunction(Name = "notStrictEqual", IsEnumerable = true)]
public void NotStrictEqual(object actual, object expected, object message) {
if (TypeComparer.StrictEquals(actual, expected))
Fail(actual, expected, message, "!==");
}
private bool _ExpectedException(object actual, object expected) {
if (actual == null || expected == null)
return false;
if (actual is RegExpInstance)
return (actual as RegExpInstance).Test(actual as string);
if (actual is FunctionInstance && (actual as FunctionInstance).HasInstance(expected)) {
return true;
}
if (expected is FunctionInstance && (expected as FunctionInstance).Call(Engine.Object.Construct(), actual) == (object)true)
return true;
return false;
}
private void _Throws(bool shouldThrow, FunctionInstance block, object expected, object message) {
if (expected is string) {
message = expected;
expected = null;
}
JavaScriptException actual = null;
try {
block.Call(null);
} catch (JavaScriptException e) {
actual = e;
}
message = (expected != null && expected is FunctionInstance ? " (" + (expected as FunctionInstance).Name + ")." : ".") + (message != null ? " " + message : ".");
if (shouldThrow && actual == null)
Fail(actual, expected, "Missing expected exception" + message, null);
if (!shouldThrow && _ExpectedException(actual, expected))
Fail(actual, expected, "Got unwanted exception" + message, null);
if ((shouldThrow && actual != null && expected != null && !_ExpectedException(actual, expected)) || (!shouldThrow && actual != null))
throw new JavaScriptException(actual, -1, "Assert.cs");
}
[JSFunction(Name = "throws", IsEnumerable = true)]
public void Throws(FunctionInstance block, object expected, object message) {
_Throws(true, block, expected, message);
}
[JSFunction(Name = "doesNotThrow", IsEnumerable = true)]
public void DowsNotThrow(FunctionInstance block, object message) {
_Throws(false, block, null, message);
}
public class AssertionErrorConstructor : ClrFunction
{
private Context Context;
public AssertionErrorConstructor(Context context) : base(context.Engine.Function.InstancePrototype, "AssertionError", context.Engine.Error.InstancePrototype) {
Context = context;
}
[JSConstructorFunction]
public ErrorInstance Construct(object options_) {
ObjectInstance options = (options_ as ObjectInstance);
if (options == null)
options = Context.Engine.Object.Construct();
return MakeError(Context.GetModule("assert") as Assert, options.GetPropertyValue("actual"), options.GetPropertyValue("expected"), options.GetPropertyValue("message"), options.GetPropertyValue("operator"));
}
}
}
}