-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
customTests.R
161 lines (146 loc) · 4.58 KB
/
customTests.R
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
158
159
160
161
test_func1 <- function() {
try({
func <- get('boring_function', globalenv())
t1 <- identical(func(9), 9)
t2 <- identical(func(4), 4)
t3 <- identical(func(0), 0)
ok <- all(t1, t2, t3)
}, silent = TRUE)
exists('ok') && isTRUE(ok)
}
test_func2 <- function() {
try({
func <- get('my_mean', globalenv())
t1 <- identical(func(9), mean(9))
t2 <- identical(func(1:10), mean(1:10))
t3 <- identical(func(c(-5, -2, 4, 10)), mean(c(-5, -2, 4, 10)))
ok <- all(t1, t2, t3)
}, silent = TRUE)
exists('ok') && isTRUE(ok)
}
test_func3 <- function() {
try({
func <- get('remainder', globalenv())
t1 <- identical(func(9, 4), 9 %% 4)
t2 <- identical(func(divisor = 5, num = 2), 2 %% 5)
t3 <- identical(func(5), 5 %% 2)
ok <- all(t1, t2, t3)
}, silent = TRUE)
exists('ok') && isTRUE(ok)
}
test_func4 <- function() {
try({
func <- get('evaluate', globalenv())
t1 <- identical(func(sum, c(2, 4, 7)), 13)
t2 <- identical(func(median, c(9, 200, 100)), 100)
t3 <- identical(func(floor, 12.1), 12)
ok <- all(t1, t2, t3)
}, silent = TRUE)
exists('ok') && isTRUE(ok)
}
test_func5 <- function() {
try({
func <- get('telegram', globalenv())
t1 <- identical(func("Good", "morning"), "START Good morning STOP")
t2 <- identical(func("hello", "there", "sir"), "START hello there sir STOP")
t3 <- identical(func(), "START STOP")
ok <- all(t1, t2, t3)
}, silent = TRUE)
exists('ok') && isTRUE(ok)
}
test_func6 <- function() {
try({
func <- get('mad_libs', globalenv())
t1 <- identical(func(place = "Baltimore", adjective = "smelly", noun = "Roger Peng statue"), "News from Baltimore today where smelly students took to the streets in protest of the new Roger Peng statue being installed on campus.")
t2 <- identical(func(place = "Washington", adjective = "angry", noun = "Shake Shack"), "News from Washington today where angry students took to the streets in protest of the new Shake Shack being installed on campus.")
ok <- all(t1, t2)
}, silent = TRUE)
exists('ok') && isTRUE(ok)
}
test_func7 <- function() {
try({
func <- get('%p%', globalenv())
t1 <- identical(func("Good", "job!"), "Good job!")
t2 <- identical(func("one", func("two", "three")), "one two three")
ok <- all(t1, t2)
}, silent = TRUE)
exists('ok') && isTRUE(ok)
}
test_eval1 <- function(){
try({
e <- get("e", parent.frame())
expr <- e$expr
t1 <- identical(expr[[3]], 6)
expr[[3]] <- 7
t2 <- identical(eval(expr), 8)
ok <- all(t1, t2)
}, silent = TRUE)
exists('ok') && isTRUE(ok)
}
test_eval2 <- function(){
try({
e <- get("e", parent.frame())
expr <- e$expr
t1 <- identical(expr[[3]], quote(c(8, 4, 0)))
t2 <- identical(expr[[1]], quote(evaluate))
expr[[3]] <- c(5, 6)
t3 <- identical(eval(expr), 5)
ok <- all(t1, t2, t3)
}, silent = TRUE)
exists('ok') && isTRUE(ok)
}
test_eval3 <- function(){
try({
e <- get("e", parent.frame())
expr <- e$expr
t1 <- identical(expr[[3]], quote(c(8, 4, 0)))
t2 <- identical(expr[[1]], quote(evaluate))
expr[[3]] <- c(5, 6)
t3 <- identical(eval(expr), 6)
ok <- all(t1, t2, t3)
}, silent = TRUE)
exists('ok') && isTRUE(ok)
}
# Get the swirl state
getState <- function(){
# Whenever swirl is running, its callback is at the top of its call stack.
# Swirl's state, named e, is stored in the environment of the callback.
environment(sys.function(1))$e
}
# Get the value which a user either entered directly or was computed
# by the command he or she entered.
getVal <- function(){
getState()$val
}
# Get the last expression which the user entered at the R console.
getExpr <- function(){
getState()$expr
}
coursera_on_demand <- function(){
selection <- getState()$val
if(selection == "Yes"){
email <- readline("What is your email address? ")
token <- readline("What is your assignment token? ")
payload <- sprintf('{
"assignmentKey": "Q4-fkq8YEeW1-RKql4-XpQ",
"submitterEmail": "%s",
"secret": "%s",
"parts": {
"3MwYt": {
"output": "correct"
}
}
}', email, token)
url <- 'https://www.coursera.org/api/onDemandProgrammingScriptSubmissions.v1'
respone <- httr::POST(url, body = payload)
if(respone$status_code >= 200 && respone$status_code < 300){
message("Grade submission succeeded!")
} else {
message("Grade submission failed.")
message("Press ESC if you want to exit this lesson and you")
message("want to try to submit your grade at a later time.")
return(FALSE)
}
}
TRUE
}