This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathbehavior.livecodescript
More file actions
175 lines (143 loc) · 6.04 KB
/
behavior.livecodescript
File metadata and controls
175 lines (143 loc) · 6.04 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
script "CoreEngineBehavior"
/*
Copyright (C) 2016 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
----------------------------------------------------------------
-- Errors
----------------------------------------------------------------
on _TestCyclicBehavior pStack
set the behavior of (the behavior of pStack) to pStack
end _TestCyclicBehavior
on TestCyclicBehavior
create stack "Behavior"
create stack
set the behavior of it to the long id of stack "Behavior"
TestAssertThrow "cycle in behavior script hierarchy throws", \
"_TestCyclicBehavior", the long id of me, \
"EE_PARENTSCRIPT_CYCLICOBJECT", it
end TestCyclicBehavior
on TestInvalidBehaviorChange
local tScript
put "on changeBehavior" & return into tScript
put "set the behavior of me to the long id of stack" \
&& quote & "NewBehavior" & quote & return after tScript
put "end changeBehavior" after tScript
create stack "NewBehavior"
create stack "Behavior"
set the script of it to tScript
create stack
set the behavior of it to the long id of stack "Behavior"
TestAssertThrow "behavior change while parent script is executing throws", \
"changeBehavior", it, "EE_PARENTSCRIPT_EXECUTING"
end TestInvalidBehaviorChange
on _TestInvalidObjectBehavior pStack
create field
set the behavior of pStack to it
end _TestInvalidObjectBehavior
on TestInvalidObjectTypeBehavior
local tStack, tField
create stack
put it into tStack
TestAssertThrow "set behavior to non-button control throws", \
"_TestInvalidObjectBehavior", the long id of me, \
"EE_PARENTSCRIPT_BADOBJECT", tStack
end TestInvalidObjectTypeBehavior
----------------------------------------------------------------
-- Functionality
----------------------------------------------------------------
on TestSetBehaviorNoChange
local tScript
put "on noChangeBehavior" & return into tScript
put "set the behavior of me to the long id of this me" & return after tScript
put "end noChangeBehavior" after tScript
local tBehavior
create stack "Behavior"
put it into tBehavior
set the script of tBehavior to tScript
local tTarget
create stack
put it into tTarget
set the behavior of tTarget to tBehavior
dispatch "noChangeBehavior" to tTarget
TestAssert "set behavior to current behavior object", \
the behavior of tTarget is tBehavior
end TestSetBehaviorNoChange
private command _TestSetBehaviorParent pType
local tParentScript
put "on parentHandler; end parentHandler" into tParentScript
local tParentObject
if pType is "button" then
create stack
create button "Behavior"
else
create stack "Behavior"
end if
put it into tParentObject
set the script of tParentObject to tParentScript
local tTarget
create stack
put it into tTarget
set the behavior of tTarget to tParentObject
dispatch "parentHandler" to tTarget
TestAssert pType && "behavior handles dispatched message", \
it is "handled"
set the behavior of tTarget to empty
TestAssert pType && "behavior set to empty", \
the behavior of tTarget is empty
dispatch function "returnValue" to tTarget
TestAssert pType && "behavior no longer in message path", \
it is "unhandled"
end _TestSetBehaviorParent
on TestSetBehavior
repeat for each item tType in "button,stack"
_TestSetBehaviorParent tType
end repeat
end TestSetBehavior
on TestBehaviorHandlerResolveOrder
local tScript
put "function returnValue; return false; end returnValue" into tScript
create stack "Behavior"
set the script of it to tScript
local tTarget
create stack
put it into tTarget
put "function returnValue; return true; end returnValue" into tScript
set the script of tTarget to tScript
set the behavior of tTarget to the long id of stack "Behavior"
dispatch function "returnValue" to tTarget
TestAssert "dispatched function handled before behavior", \
the result is true
put "function returnValue; pass returnValue; end returnValue" into tScript
set the script of tTarget to tScript
dispatch function "returnValue" to tTarget
TestAssert "behavior handles passed dispatched function", \
the result is false
end TestBehaviorHandlerResolveOrder
on TestChangeBehaviorWhileExecuting
create stack "test"
set the defaultStack to "test"
create button "child"
set the script of button "child" to "on Foo;Bar;end Foo"
create button "parent"
set the script of button "parent" to "on Bar;Baz;end Bar"
set the script of stack "test" to "on Bar;set the behavior of button " & quote & "child" & quote & " to the long id of button " & quote & "parent"& quote &";end Bar"
TestAssertDoesNotThrow "can set behavior while executing if it is empty", "Foo", \
the long id of button "child"
set the script of stack "test" to "on Baz;set the behavior of button " & quote & "child" & quote & " to the long id of button " & quote & "parent"& quote &";end Baz"
TestAssertDoesNotThrow "can set behavior while executing if it is the same", "Foo", \
the long id of button "child"
-- wait 0 milliseconds;throw 0 added to test the crash if the error is not thrown
set the script of stack "test" to "on Baz;set the behavior of button " & quote & "child" & quote & " to empty;wait 0 milliseconds;throw 0;end Baz"
TestAssertThrow "set behavior to empty while it is executing throws", "Foo", \
the long id of button "child", "EE_PARENTSCRIPT_EXECUTING"
delete stack "test"
end TestChangeBehaviorWhileExecuting