Skip to content

Commit b5483c1

Browse files
committed
fix broken tests
1 parent 539971a commit b5483c1

File tree

9 files changed

+60
-20
lines changed

9 files changed

+60
-20
lines changed

R/entity_class.R

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ entity<-setClass(
3737
value='',
3838
type='character',
3939
max_length=1
40-
),
40+
),
4141
validity = function(object) {
4242
check_length=length(value(object)) <= max_length(object)
4343
check_type=class(value(object))[1] %in% type(object)
@@ -56,6 +56,38 @@ entity<-setClass(
5656
}
5757
)
5858

59+
## initialise parameters on object creation
60+
setMethod(f="initialize",
61+
signature="entity",
62+
definition=function(.Object,...)
63+
{
64+
L=list(...)
65+
SN=slotNames(.Object)
66+
if (length(L)>0)
67+
{
68+
for (i in seq_len(length(L)))
69+
{
70+
if (names(L)[[i]] %in% SN) {
71+
slot(.Object,names(L)[[i]])=L[[names(L)[[i]]]]
72+
}
73+
}
74+
}
75+
76+
if (!('value' %in% names(L))) {
77+
if (isVirtualClass(.Object@type)) {
78+
# create a spoof object until a real one is generated
79+
x=numeric(0)
80+
class(x)=.Object@type
81+
.Object@value=x
82+
} else {
83+
.Object@value=new(.Object@type)
84+
}
85+
}
86+
validObject(.Object)
87+
return(.Object)
88+
}
89+
)
90+
5991
#' @describeIn entity get the value for an entity
6092
#' @export
6193
setMethod(f="value",

R/generics.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#' obj = param.obj(M, 'value_0')
2222
#'
2323
#' # set a parameter as an object
24-
#' param.obj(M, 'value_0') = entity(value = 15)
24+
#' param.obj(M, 'value_0') = entity(value = 15,type='numeric')
2525
#'
2626
setGeneric("param.obj",function(obj,name)standardGeneric("param.obj"))
2727

@@ -152,7 +152,7 @@ setGeneric("output.value",function(obj,name)standardGeneric("output.value"))
152152
#' @return struct object
153153
#' @examples
154154
#' M = example_model()
155-
#' output.value(M,'result_1') = 0.95
155+
#' output.value(M,'result_1') = 'example'
156156
#'
157157
setGeneric("output.value<-",
158158
function(obj,name,value)standardGeneric("output.value<-"))
@@ -176,7 +176,7 @@ setGeneric("output.value<-",
176176
#' obj = output.obj(M, 'result_1')
177177
#'
178178
#' # set a output as an object
179-
#' output.obj(M, 'result_1') = entity(value = 15)
179+
#' output.obj(M, 'result_1') = entity(value = 15,type='numeric')
180180
#'
181181
setGeneric("output.obj",
182182
function(obj,name)standardGeneric("output.obj"))

R/method_class.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ method<-setClass(
2626
#' @export
2727
#' @examples
2828
#' D = iris_dataset() # example dataset
29+
#' M = method() # create a method object
2930
#' M = method.apply(M,D) # apply method to data
3031
#'
3132
setMethod(f="method.apply",
@@ -59,7 +60,8 @@ setMethod(f='predicted',
5960
#' \dontrun{
6061
#'
6162
#' M = method()
62-
#' predicted.name(M)
63+
#' predicted.name(M) = 'example' # set
64+
#' predicted.name(M) # get
6365
#' }
6466
#'
6567
setMethod(f='predicted.name',

man/method-class.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/output.obj.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/output.value.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/param.obj.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-params-outputs.R

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ test_that('params and outputs',{
1010
outputs.result_1='entity',
1111
outputs.result_2='numeric'
1212
),
13-
prototype=list(predicted='result_1')
13+
prototype=list(
14+
predicted='result_1',
15+
outputs.result_1=entity(name='result_1',type='numeric',value=0))
1416
)
1517

1618
## test return objects
@@ -21,7 +23,7 @@ test_that('params and outputs',{
2123
expect_identical(obj,numeric(0))
2224
# outputs
2325
obj = output.obj(test_model(),'result_1')
24-
expect_identical(obj,entity())
26+
expect_true(is(obj,'entity'))
2527
obj = output.obj(test_model(),'result_2')
2628
expect_identical(obj,numeric(0))
2729

@@ -40,31 +42,32 @@ test_that('params and outputs',{
4042
expect_equal(param.name(TM,'value_1'),'pickle')
4143
expect_equal(param.name(TM,'value_2'),'value_2')
4244
# outputs
43-
output.obj(TM,'result_1')=entity(name='carrot')
45+
output.obj(TM,'result_1')=entity(name='carrot',type='numeric',value=10)
4446
expect_equal(output.name(TM,'result_1'),'carrot')
4547
expect_equal(output.name(TM,'result_2'),'result_2')
4648

4749
## test set lists
48-
L=list('value_1'=10,value_2=20)
50+
L=list('value_1'='banana',value_2=20)
4951
param.list(TM)=L
50-
expect_equal(param.value(TM,'value_1'),10)
52+
expect_equal(param.value(TM,'value_1'),'banana')
5153
expect_equal(param.value(TM,'value_2'),20)
5254
names(L)=c('result_1','result_2')
55+
L$result_1=10
5356
output.list(TM)=L
5457
expect_equal(output.value(TM,'result_1'),10)
5558
expect_equal(output.value(TM,'result_2'),20)
5659

5760
## get lists
5861
K=param.list(TM)
59-
expect_identical(K,list(value_1=10,value_2=20))
62+
expect_identical(K,list(value_1='banana',value_2=20))
6063
# output
6164
J=output.list(TM)
6265
expect_identical(J,list(result_1=10,result_2=20))
6366

6467
## $ and $<-
6568
#params
66-
TM$value_1=777
67-
expect_equal(TM$value_1,777)
69+
TM$value_1='cabbage'
70+
expect_equal(TM$value_1,'cabbage')
6871
expect_error(TM$value_3)
6972
expect_error({TM$value_3=999})
7073

@@ -74,7 +77,8 @@ test_that('params and outputs',{
7477
slots=c(
7578
outputs.result_1='entity',
7679
outputs.result_2='numeric'
77-
)
80+
),
81+
prototype = list(outputs.result_1=entity(type='numeric',value=1))
7882
)
7983
TM=test_model2()
8084
# outputs

tests/testthat/test-stato.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ test_that('stato object',{
66
slots=c(params.value_1='entity.stato',
77
outputs.value_1='entity.stato'),
88
prototype=list(stato.id='OBI:0200201',
9-
params.value_1=entity.stato(value=1,stato.id='OBI:0000175'),
10-
outputs.value_1=entity.stato(value=1,stato.id='OBI:0000175'))
9+
params.value_1=entity.stato(value=1,stato.id='OBI:0000175',type='numeric'),
10+
outputs.value_1=entity.stato(value=1,stato.id='OBI:0000175',type='numeric'))
1111
)
1212
S=test_class()
1313
expect_equal(stato.id(S),'OBI:0200201')

0 commit comments

Comments
 (0)