-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
112 lines (101 loc) · 2.97 KB
/
tests.js
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
"use strict"
test("name property", function() {
let result = async(function*something() { })
if(result.name != "something") {
throw('should be "something" but is ' + result.name)
} })
test("length property", function() {
let result = async(function*(first, second, ...others) { })
if(result.length != 2) {
throw('should be "2" but is ' + result.length)
} })
test("toString method", function() {
let result1 = async(function*() { })
let result2 = result1.toString()
if(result2 != "async function () { }") {
throw('should return "async function () { }" but returns ' + result2)
} })
test("returns promise", function() {
let result1 = async(function*() { })
let result2 = result1()
if(typeof(result2.then) != "function" || typeof(result2.catch) != "function") {
throw('should return a promise but returns ' + result2)
} })
test("returning values resolves promise", function(pass, fail) {
let result1 = async(function*() {
return("something")
})
result1().then(function(result2) {
if(result2 == "something") {
pass()
} else {
fail('result should be "something" but is ' + result2)
} }) })
test("throwing errors rejects promise", function(pass, fail) {
let result = async(function*() {
throw("something")
})
result().then(function(result) {
fail('should reject but resolved with ' + result)
}).catch(function(error) {
if(error == "something") {
pass()
} else {
fail('error should be "something" but is ' + error)
} }) })
test("this value", function(pass, fail) {
let result = async(function*() {
if(this == "something") {
pass()
} else {
fail('should be "something" but is ' + this)
} })
result.call("something")
})
test("yield awaits promise", function(pass, fail) {
let result1 = async(function*() {
let result2 = yield(Promise.resolve("something"))
if(result2 == "something") {
pass()
} else {
fail('result should be "something" but is ' + result2)
} })
result1()
})
test("multiple yields", function(pass, fail) {
let result1 = async(function*() {
let result2 = yield(Promise.resolve("something"))
let result3 = yield(Promise.resolve("something"))
if(result2 == "something" && result3 == "something") {
pass()
} else {
fail('results should be "something" but are ' + result2 + " and " + result3)
} })
result1()
})
test("yield and throw", function(pass, fail) {
let result1 = async(function*() {
let result2 = yield(Promise.resolve("something"))
throw(result2)
})
result1().then(function(result) {
fail('should reject but resolved with ' + result)
}).catch(function(error) {
if(error == "something") {
pass()
} else {
fail('error should be "something" but is ' + error)
} }) })
test("try and catch", function(pass, fail) {
let result1 = async(function*() {
try {
let result2 = yield(Promise.reject("something"))
fail('should reject but resolved with ' + result2)
} catch(error) {
if(error == "something") {
pass()
} else {
fail('error should be "something" but is ' + error)
} } })
result1()
})