@@ -2,30 +2,74 @@ module( "CmdSetScriptValue" );
22
33test ( "Test CmdSetScriptValue for source (Undo and Redo)" , function ( ) {
44
5- var editor = new Editor ( ) ;
65
6+ // setup
7+ var editor = new Editor ( ) ;
78 var box = aBox ( "The scripted box" ) ;
8- var xMove = { name : "" , source : "function update( event ) { this.position.x = this.position.x + 1; }" } ;
9- var yMove = { name : "" , source : "function update( event ) { this.position.y = this.position.y + 1; }" } ;
10- var scripts = [ xMove , yMove ] ;
9+ var cmd = new CmdAddObject ( box ) ;
10+ cmd . updatable = false ;
11+ editor . execute ( cmd ) ;
12+
13+ var translateScript = { name : "Translate" , source : "function( update ) {}" } ;
14+ cmd = new CmdAddScript ( box , translateScript ) ;
15+ cmd . updatable = false ;
16+ editor . execute ( cmd ) ;
17+
18+
19+ var testSourceData = [
20+
21+ { name : "Translate" , source : "function update( event ) { this.position.x = this.position.x + 1; }" } ,
22+ { name : "Translate" , source : "function update( event ) { this.position.y = this.position.y + 1; }" } ,
23+ { name : "Translate" , source : "function update( event ) { this.position.z = this.position.z + 1; }" }
24+
25+ ] ;
26+
27+
28+ // test source
29+
30+ testSourceData . map ( function ( script ) {
31+
32+ var cmd = new CmdSetScriptValue ( box , translateScript , 'source' , script . source , 0 ) ;
33+ cmd . updatable = false ;
34+ editor . execute ( cmd ) ;
35+
36+ } ) ;
37+
38+ var length = testSourceData . length ;
39+ ok ( editor . scripts [ box . uuid ] [ 0 ] [ 'source' ] == testSourceData [ length - 1 ] . source ,
40+ "OK, 'source' was set correctly to the last value (expected: '" + testSourceData [ length - 1 ] . source + "', actual: '" + editor . scripts [ box . uuid ] [ 0 ] [ 'source' ] + "')" ) ;
41+
42+ editor . undo ( ) ;
43+ ok ( editor . scripts [ box . uuid ] [ 0 ] [ 'source' ] == testSourceData [ length - 2 ] . source ,
44+ "OK, 'source' was set correctly to the second to the last value after undo (expected: '" + testSourceData [ length - 2 ] . source + "', actual: '" + editor . scripts [ box . uuid ] [ 0 ] [ 'source' ] + "')" ) ;
45+
46+ editor . redo ( ) ;
47+ ok ( editor . scripts [ box . uuid ] [ 0 ] [ 'source' ] == testSourceData [ length - 1 ] . source ,
48+ "OK, 'source' was set correctly to the last value again after redo (expected: '" + testSourceData [ length - 1 ] . source + "', actual: '" + editor . scripts [ box . uuid ] [ 0 ] [ 'source' ] + "')" ) ;
49+
50+
51+
52+ // test name
53+
54+ var names = [ "X Script" , "Y Script" , "Z Script" ] ;
1155
12- editor . execute ( new CmdAddObject ( box ) ) ;
56+ names . map ( function ( name ) {
1357
14- var cmd = new CmdAddScript ( box , scripts [ 0 ] ) ;
15- cmd . updatable = false ;
16- editor . execute ( cmd ) ;
58+ cmd = new CmdSetScriptValue ( box , translateScript , 'name' , name ) ;
59+ cmd . updatable = false ;
60+ editor . execute ( cmd ) ;
1761
18- cmd = new CmdSetScriptValue ( box , xMove , 'source' , yMove [ 'source' ] , 0 ) ;
19- cmd . updatable = false ;
20- editor . execute ( cmd ) ;
21- ok ( editor . scripts [ box . uuid ] [ 0 ] [ 'source' ] == yMove [ 'source' ] , "OK, script source has been set successfully" ) ;
62+ } ) ;
2263
23- console . log ( editor . scripts ) ;
64+ var scriptName = editor . scripts [ box . uuid ] [ 0 ] [ "name" ] ;
65+ ok ( scriptName == names [ names . length - 1 ] , "OK, the script name corresponds to the last script name that was assigned" ) ;
2466
2567 editor . undo ( ) ;
26- ok ( editor . scripts [ box . uuid ] [ 0 ] [ 'source' ] == xMove [ 'source' ] , "OK, script source has been set to previous state" ) ;
68+ scriptName = editor . scripts [ box . uuid ] [ 0 ] [ "name" ] ;
69+ ok ( scriptName == names [ names . length - 2 ] , "OK, the script name corresponds to the second last script name that was assigned" ) ;
2770
2871 editor . redo ( ) ;
29- ok ( editor . scripts [ box . uuid ] [ 0 ] [ 'source' ] == yMove [ 'source' ] , "OK, script source has been reverted successfully" ) ;
72+ scriptName = editor . scripts [ box . uuid ] [ 0 ] [ "name" ] ;
73+ ok ( scriptName == names [ names . length - 1 ] , "OK, the script name corresponds to the last script name that was assigned, again" ) ;
3074
3175} ) ;
0 commit comments