mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-23 23:15:52 +00:00
Merge branch 'fix-cpuserdefaults-protocol' of https://github.com/primalmotion/cappuccino into primalmotion-cpuserdefaults-fixes
This commit is contained in:
commit
5078c3e9cf
2 files changed
+336
-20
No files matched your search
+181
-20
@@ -361,7 +361,8 @@ var StandardUserDefaults;
|
||||
}
|
||||
}
|
||||
|
||||
// Value accessors
|
||||
#pragma mark -
|
||||
#pragma mark Values Accessors
|
||||
|
||||
/*!
|
||||
Returns the Boolean value associated with the specified key.
|
||||
@@ -369,10 +370,37 @@ var StandardUserDefaults;
|
||||
- (BOOL)boolForKey:(CPString)aKey
|
||||
{
|
||||
var value = [self objectForKey:aKey];
|
||||
if ([value respondsToSelector:@selector(boolValue)])
|
||||
return [value boolValue]
|
||||
if (typeof(value) == "boolean")
|
||||
return value;
|
||||
else if ([value respondsToSelector:@selector(boolValue)])
|
||||
return [value boolValue];
|
||||
|
||||
return value;
|
||||
return NO;
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
Returns the data value associated with the specified key.
|
||||
*/
|
||||
- (CPData)dataForKey:(CPString)aKey
|
||||
{
|
||||
var value = [self objectForKey:aKey];
|
||||
if ([value class] === CPData)
|
||||
return value;
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the Boolean value associated with the specified key.
|
||||
*/
|
||||
- (CPDictionary)dictionaryForKey:(CPString)aKey
|
||||
{
|
||||
var value = [self objectForKey:aKey];
|
||||
if ([value class] === CPDictionary)
|
||||
return value;
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -381,22 +409,12 @@ var StandardUserDefaults;
|
||||
- (float)floatForKey:(CPString)aKey
|
||||
{
|
||||
var value = [self objectForKey:aKey];
|
||||
if ([value respondsToSelector:@selector(floatValue)])
|
||||
if (typeof(value) == "number")
|
||||
return parseFloat(value);
|
||||
else if ([value respondsToSelector:@selector(floatValue)])
|
||||
return [value floatValue];
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the double value associated with the specified key.
|
||||
*/
|
||||
- (double)doubleForKey:(CPString)aKey
|
||||
{
|
||||
var value = [self objectForKey:aKey];
|
||||
if ([value respondsToSelector:@selector(doubleValue)])
|
||||
return [value doubleValue];
|
||||
|
||||
return value;
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -405,12 +423,155 @@ var StandardUserDefaults;
|
||||
- (int)integerForKey:(CPString)aKey
|
||||
{
|
||||
var value = [self objectForKey:aKey];
|
||||
if ([value respondsToSelector:@selector(intValue)])
|
||||
if (typeof(value) == "number")
|
||||
return parseInt(value);
|
||||
else if ([value respondsToSelector:@selector(intValue)])
|
||||
return [value intValue];
|
||||
|
||||
return value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the double value associated with the specified key.
|
||||
*/
|
||||
- (double)doubleForKey:(CPString)aKey
|
||||
{
|
||||
var value = [self objectForKey:aKey];
|
||||
if (typeof(value) == "number")
|
||||
return parseFloat(value);
|
||||
else if ([value respondsToSelector:@selector(doubleValue)])
|
||||
return [value doubleValue];
|
||||
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the string value associated with the specified key.
|
||||
*/
|
||||
- (CPString)stringForKey:(CPString)aKey
|
||||
{
|
||||
var value = [self objectForKey:aKey];
|
||||
|
||||
if ([value class] === CPString)
|
||||
return value;
|
||||
else if ([value respondsToSelector:@selector(stringValue)])
|
||||
return [value stringValue];
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the CPURL value associated with the specified key.
|
||||
*/
|
||||
- (CPURL)URLForKey:(CPString)aKey
|
||||
{
|
||||
var value = [self objectForKey:aKey];
|
||||
switch ([value class])
|
||||
{
|
||||
case CPURL:
|
||||
return value;
|
||||
break;
|
||||
case CPString:
|
||||
return [CPURL URLWithString:value];
|
||||
break;
|
||||
default:
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the array value associated with the specified key.
|
||||
*/
|
||||
- (CPArray)arrayForKey:(CPString)aKey
|
||||
{
|
||||
var value = [self objectForKey:aKey];
|
||||
if ([value class] === CPArray)
|
||||
return value;
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the string array value associated with the specified key.
|
||||
*/
|
||||
- (CPArray)stringArrayForKey:(CPString)aKey
|
||||
{
|
||||
var value = [self objectForKey:aKey];
|
||||
if ([value class] === CPArray)
|
||||
{
|
||||
for (var i = 0; i < [value count]; i++)
|
||||
if ([[value objectAtIndex:i] class] !== CPString)
|
||||
return nil;
|
||||
return value;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Values Setters
|
||||
|
||||
/*!
|
||||
Set a boolean in your application domain.
|
||||
*/
|
||||
- (void)setBool:(BOOL)aBool forKey:(CPString)aKey
|
||||
{
|
||||
if (typeof(aBool) !== "boolean")
|
||||
[CPException raise:CPInvalidArgumentException reason:aBool + @" is not a BOOL"];
|
||||
[self setObject:aBool forKey:aKey];
|
||||
}
|
||||
|
||||
/*!
|
||||
Set a float in your application domain.
|
||||
*/
|
||||
- (void)setFloat:(float)aFloat forKey:(CPString)aKey
|
||||
{
|
||||
if (typeof(parseFloat(aFloat)) !== "number")
|
||||
[CPException raise:CPInvalidArgumentException reason:aFloat + @" is not a valid number"];
|
||||
[self setObject:aFloat forKey:aKey];
|
||||
}
|
||||
|
||||
/*!
|
||||
Set a double in your application domain.
|
||||
*/
|
||||
- (void)setDouble:(double)aDouble forKey:(CPString)aKey
|
||||
{
|
||||
if (typeof(parseFloat(aDouble)) !== "number")
|
||||
[CPException raise:CPInvalidArgumentException reason:aDouble + @" is not a valid number"];
|
||||
[self setObject:aDouble forKey:aKey];
|
||||
}
|
||||
|
||||
/*!
|
||||
Set a integer in your application domain.
|
||||
*/
|
||||
- (void)setInteger:(int)anInteger forKey:(CPString)aKey
|
||||
{
|
||||
if (typeof(parseInt(anInteger)) !== "number")
|
||||
[CPException raise:CPInvalidArgumentException reason:anInteger + @" is not a valid number"];
|
||||
[self setObject:anInteger forKey:aKey];
|
||||
}
|
||||
|
||||
/*!
|
||||
Set a string in your application domain.
|
||||
*/
|
||||
- (void)setString:(int)aString forKey:(CPString)aKey
|
||||
{
|
||||
if ([aString class] !== CPString)
|
||||
[CPException raise:CPInvalidArgumentException reason:aString + @" is not a valid string"];
|
||||
[self setObject:aString forKey:aKey];
|
||||
}
|
||||
|
||||
/*!
|
||||
Set a URL in your application domain.
|
||||
*/
|
||||
- (void)setURL:(CPURL)anURL forKey:(CPString)aKey
|
||||
{
|
||||
if ([anURL class] !== CPURL && [anURL class] !== CPString)
|
||||
[CPException raise:CPInvalidArgumentException reason:anURL + @" is not a valid CPURL"];
|
||||
[self setObject:anURL forKey:aKey];
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
|
||||
@implementation CPUserDefaultsStore : CPObject
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* CPUserDefaultsTest.j
|
||||
*
|
||||
* Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
@import <Foundation/Foundation.j>
|
||||
@import <AppKit/AppKit.j>
|
||||
@import "../../Foundation/CPUserDefaults.j"
|
||||
|
||||
CPUserDefaultsTestKey1 = @"KEY1";
|
||||
CPUserDefaultsTestKey2 = @"KEY2";
|
||||
|
||||
@implementation CPUserDefaultsCookieStore (test)
|
||||
|
||||
- (void)setData:(CPData)aData
|
||||
{
|
||||
// deactive real storage
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
//CPLogRegister(CPLogPrint)
|
||||
|
||||
@implementation CPUserDefaultsTest : OJTestCase
|
||||
{
|
||||
CPUserDefaults target;
|
||||
}
|
||||
|
||||
- (void)setUp
|
||||
{
|
||||
target = [CPUserDefaults standardUserDefaults];
|
||||
}
|
||||
|
||||
- (void)tearDown
|
||||
{
|
||||
[CPUserDefaults resetStandardUserDefaults];
|
||||
}
|
||||
|
||||
- (void)testAppDefault
|
||||
{
|
||||
var appDefaults = [CPDictionary dictionaryWithObjectsAndKeys:YES, CPUserDefaultsTestKey1, @"Hello!", CPUserDefaultsTestKey2];
|
||||
[target registerDefaults:appDefaults];
|
||||
|
||||
[self assertTrue:[target boolForKey:CPUserDefaultsTestKey1]];
|
||||
[self assert:[target objectForKey:CPUserDefaultsTestKey2] equals:@"Hello!"];
|
||||
|
||||
}
|
||||
|
||||
|
||||
- (void)testSetObjectForKey
|
||||
{
|
||||
[target setObject:[CPArray arrayWithObjects:@"cell1", @"cell2"] forKey:CPUserDefaultsTestKey1];
|
||||
|
||||
var cell1 = [[target objectForKey:CPUserDefaultsTestKey1] objectAtIndex:0],
|
||||
cell2 = [[target objectForKey:CPUserDefaultsTestKey1] objectAtIndex:1];
|
||||
|
||||
[self assert:cell1 equals:@"cell1"];
|
||||
[self assert:cell2 equals:@"cell2"];
|
||||
}
|
||||
|
||||
- (void)testBool
|
||||
{
|
||||
[target setBool:YES forKey:CPUserDefaultsTestKey1];
|
||||
[self assertTrue:[target boolForKey:CPUserDefaultsTestKey1]];
|
||||
|
||||
[target removeObjectForKey:CPUserDefaultsTestKey1];
|
||||
[self assertFalse:[target boolForKey:CPUserDefaultsTestKey1]];
|
||||
}
|
||||
|
||||
- (void)testInteger
|
||||
{
|
||||
[target setInteger:1 forKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[target integerForKey:CPUserDefaultsTestKey1] equals:1];
|
||||
|
||||
[target removeObjectForKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[target integerForKey:CPUserDefaultsTestKey1] equals:0];
|
||||
}
|
||||
|
||||
- (void)testFloat
|
||||
{
|
||||
[target setFloat:1.0 forKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[target floatForKey:CPUserDefaultsTestKey1] equals:1.0];
|
||||
|
||||
[target removeObjectForKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[target floatForKey:CPUserDefaultsTestKey1] equals:0.0];
|
||||
}
|
||||
|
||||
- (void)testDouble
|
||||
{
|
||||
[target setDouble:1.0 forKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[target doubleForKey:CPUserDefaultsTestKey1] equals:1.0];
|
||||
|
||||
[target removeObjectForKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[target doubleForKey:CPUserDefaultsTestKey1] equals:0.0];
|
||||
}
|
||||
|
||||
- (void)testString
|
||||
{
|
||||
[target setString:@"Hello World" forKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[target stringForKey:CPUserDefaultsTestKey1] equals:@"Hello World"];
|
||||
|
||||
[target removeObjectForKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[target stringForKey:CPUserDefaultsTestKey1] equals:nil];
|
||||
}
|
||||
|
||||
- (void)testURL
|
||||
{
|
||||
[target setURL:[CPURL URLWithString:@"http://cappuccino.org"] forKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[[target URLForKey:CPUserDefaultsTestKey1] class] equals:CPURL];
|
||||
[self assert:[[target URLForKey:CPUserDefaultsTestKey1] absoluteString] equals:@"http://cappuccino.org"];
|
||||
|
||||
[target setURL:@"http://cappuccino.org" forKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[[target URLForKey:CPUserDefaultsTestKey1] class] equals:CPURL];
|
||||
[self assert:[[target URLForKey:CPUserDefaultsTestKey1] absoluteString] equals:@"http://cappuccino.org"];
|
||||
|
||||
[target removeObjectForKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[target URLForKey:CPUserDefaultsTestKey1] equals:nil];
|
||||
}
|
||||
|
||||
- (void)testArray
|
||||
{
|
||||
[target setObject:[@"a", @"b", 3] forKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[[target arrayForKey:CPUserDefaultsTestKey1] class] equals:CPArray];
|
||||
[self assert:[[target arrayForKey:CPUserDefaultsTestKey1] objectAtIndex:1] equals:@"b"];
|
||||
[self assert:[[target arrayForKey:CPUserDefaultsTestKey1] objectAtIndex:2] equals:3];
|
||||
[self assert:[target stringArrayForKey:CPUserDefaultsTestKey1] equals:nil];
|
||||
|
||||
[target removeObjectForKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[target arrayForKey:CPUserDefaultsTestKey1] equals:nil];
|
||||
}
|
||||
|
||||
- (void)testData
|
||||
{
|
||||
[target setObject:[CPData dataWithRawString:@"data"] forKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[[target dataForKey:CPUserDefaultsTestKey1] class] equals:CPData];
|
||||
|
||||
[target removeObjectForKey:CPUserDefaultsTestKey1];
|
||||
[self assert:[target dataForKey:CPUserDefaultsTestKey1] equals:nil];
|
||||
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user