File: path.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (152 lines) | stat: -rw-r--r-- 3,388 bytes parent folder | download | duplicates (2)
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
#import "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSKeyValueCoding.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSDecimalNumber.h>
#import <Foundation/NSDictionary.h>

@interface Tester : NSObject
{
  int num1;
  double num2;
  id child;
  NSMutableDictionary * dict;
}

@end

@implementation Tester
- (void) dealloc
{
  DESTROY(child);
  DESTROY(dict);
  DEALLOC
}
@end

@interface CustomKVC : NSObject
{
  int num1;
  NSString * string;
}
@end

@implementation CustomKVC
- (void)dealloc
{
  [string release];
  [super dealloc];
}

- (void)setValue:(id)value forKey:(NSString *)key
{
  if ([key isEqualToString:@"num1"]) {
    num1 = [value intValue];
  } else if ([key isEqualToString:@"Lücke"]) {
    [string release];
    string = [value copy];
  }
}

- (id)valueForKey:(NSString *)key
{
  if ([key isEqualToString:@"num1"]) {
    return [NSNumber numberWithInt:num1];
  } else if ([key isEqualToString:@"Lücke"]) {
    return string;
  }
  return nil;
}

@end

@interface DeprecatedCustomKVC : NSObject
{
  NSMutableDictionary * storage;
}

- (id)init;
- (id)valueForKey:(NSString *)key;
- (void)takeValue:(id)value forKey:(NSString *)key;
@end

@implementation DeprecatedCustomKVC

- (void) dealloc
{
  RELEASE(storage);
  DEALLOC
}

- (id)init
{
  self = [super init];
  storage = [[NSMutableDictionary alloc] init];

  return self;
}

- (id)valueForKey:(NSString *)key
{
  if ([key isEqualToString:@"dict"]) {
    return storage;
  }
  return nil;
}

- (void)takeValue:(id)value forKey:(NSString *)key
{
  if ([key isEqualToString:@"dict"]) {
    [storage release];
    storage = [value retain];
  }
}

@end

int main(void) {
  NSAutoreleasePool * arp = [NSAutoreleasePool new];

  NSString * string = @"GNUstep";

  Tester * tester = [[[Tester alloc] init] autorelease];
  [tester setValue:[NSMutableDictionary dictionary] forKey:@"dict"];
  [tester setValue:[[[Tester alloc] init] autorelease] forKey:@"child"];
  [tester setValue:[[CustomKVC new] autorelease]
        forKeyPath:@"child.child"];
  DeprecatedCustomKVC * deprecated = [[[DeprecatedCustomKVC alloc] init]
    autorelease];
  NSNumber * n = [NSNumber numberWithInt:8];
  NSNumber * n2 = [NSNumber numberWithDouble:87.999];

  [tester setValue:n2 forKeyPath:@"child.num2"];
  PASS([[tester valueForKeyPath:@"child.num2"] isEqualToNumber:n2],
      "KVC works with simple paths");

  [deprecated takeValue:[NSDictionary dictionaryWithObject:@"test"
                          forKey:@"key"]
                forKey:@"dict"];

  PASS_RUNS(
      [tester setValue:n forKeyPath:@"child.child.num1"],
      "KVC appears to work with key path");
  PASS([[tester valueForKeyPath:@"child.child.num1"] isEqualToNumber:n],
      "KVC works with key paths");

  NSLog(@"tester.child.child = %@", [tester valueForKeyPath:
      @"child.child"]);
  PASS_RUNS(
      [tester setValue:string forKeyPath:@"child.child.Lücke"],
      "KVC appears to work with a unicode key path");
  PASS([[tester valueForKeyPath:@"child.child.Lücke"] isEqualToString:string],
      "KVC works with unicode path");

  PASS_RUNS(
      [tester setValue:string forKeyPath:@"dict.Lücke"],
      "KVC appears to work with a unicode key path (test2)");
  PASS([[tester valueForKeyPath:@"dict.Lücke"] isEqualToString:string],
      "KVC works with unicode path (test2)");

  [arp release];
  return 0;
}