]> err.no Git - sope/blob - Recycler/NGJavaScript/tests/JSArchivingTests.m
added Kolab sample data
[sope] / Recycler / NGJavaScript / tests / JSArchivingTests.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include "JSArchivingTests.h"
24 #include "Blah.h"
25 #include "MyNum.h"
26 #include "common.h"
27
28 #import <NGJavaScript/NGJavaScript.h>
29 #import <NGScripting/NGScriptLanguage.h>
30 #import <NGExtensions/NGExtensions.h>
31
32 @implementation JSArchivingTests
33
34 + (void)runSuite {
35   [self runTest:@"Archiving"];
36   [self runTest:@"JSArrayArchiveEvalProblem"];
37 }
38
39 - (void)testArchiving {
40   static NSString *testScript = 
41     @"var c = 30;\n"
42     @"var myArray=[1,2,3];\n"
43     @"function doIt(sender) {\n"
44     @"  print('doIt, a='+a+', b='+b+', c='+c);\n"
45     @"}\n"
46     ;
47   id original, copy;
48   NSData *archive;
49   
50   original = [[NGJavaScriptObject alloc] init];
51   [original setObject:@"10" forKey:@"a"];
52   [original setObject:@"20" forKey:@"b"];
53   [original evaluateScript:testScript language:@"javascript"];
54   [self print:@"  a: %@", [original objectForKey:@"a"]];
55   [self print:@"  b: %@", [original objectForKey:@"b"]];
56   [self print:@"  doIt: %@", [original objectForKey:@"doIt"]];
57   
58   [self print:@"archive object %@ (keys=%@) ...", 
59           original, 
60           [[original allKeys] componentsJoinedByString:@","]];
61   
62   archive = [NSArchiver archivedDataWithRootObject:original];
63   [self print:@"archived to data, size %i", [archive length]];
64   
65   copy = [NSUnarchiver unarchiveObjectWithData:archive];
66   [self print:@"unarchived object %@ (keys=%@)", 
67           copy,
68           [[copy allKeys] componentsJoinedByString:@","]];
69   [self print:@"  a: %@", [copy objectForKey:@"a"]];
70   [self print:@"  b: %@", [copy objectForKey:@"b"]];
71   [self print:@"  doIt: %@", [copy objectForKey:@"doIt"]];
72   [self print:@"  myArray: %@ (%@)", 
73         [copy objectForKey:@"myArray"],
74         [[copy objectForKey:@"myArray"] class]];
75   
76   [original release];
77 }
78
79
80 - (void)testJSArrayArchiveEvalProblem {
81   static NSString *testScriptA =
82     @"try {\n"
83     @"var counter=0;\n"
84     @"var dataRec = [\n"
85     @"  { 'a': 5, 'b': 10 }\n"
86     @"];\n"
87     @"} catch (exc) {\n"
88     @"  print('JS CODE CATCHED: '+exc);\n"
89     @"}\n"
90     ;
91   id wrapper, copy;
92   NSData *archive;
93   
94   /* create an object */
95   
96   wrapper = [[[NGJavaScriptObject alloc] init] autorelease];
97   [self print:@"object: %@ (keys=%@)", wrapper, [wrapper allKeys]];
98   
99   [wrapper evaluateScript:testScriptA language:@"javascript"];
100   [self print:@"object: %@ (keys=%@)", wrapper, [wrapper allKeys]];
101   [self printJavaScriptObjectInfo:wrapper];
102   
103   /* do the archiving/unarchiving transaction to create a copy */
104   
105   archive = [NSArchiver archivedDataWithRootObject:wrapper];
106   NSAssert1([archive length] > 0, 
107             @"archiver didn't generate a proper archive: %@",
108             archive);
109   
110   copy = [NSUnarchiver unarchiveObjectWithData:archive];
111   NSAssert(copy != nil, @"couldn't unarchive the object at all");
112   NSAssert1([copy isKindOfClass:[NGJavaScriptObject class]],
113             @"unexpected object class: %@", [copy class]);
114   
115   [self print:@"copy: %@ (keys=%@)", copy, [copy allKeys]];
116   [self printJavaScriptObjectInfo:wrapper];
117   
118   /*
119     the following broke with: "JS ERROR(<string>:1): null" which is
120     why we have this test
121   */
122   [copy evaluateScript:testScriptA language:@"javascript"];
123 }
124
125 @end /* JSArchivingTests */