Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:patrickt/yamlkit

  • Loading branch information...
commit 33ece65071a28a707e648c8c6c14a754a4cb103c 2 parents 0285182 + c1cf850
Patrick Thomson authored
View
2  src/YKEmitter.h
@@ -5,7 +5,7 @@
// Created by Patrick Thomson on 12/29/08.
//
-#import <Cocoa/Cocoa.h>
+#import <Foundation/Foundation.h>
#import "yaml.h"
@interface YKEmitter : NSObject {
View
2  src/YKEmitter.m
@@ -71,7 +71,7 @@ - (int)_writeItem:(id)item toDocument:(yaml_document_t *)doc
} else {
// TODO: Add optional support for tagging emitted items.
// TODO: Wrap long lines.
- nodeID = yaml_document_add_scalar(doc, (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG, (yaml_char_t*)[[item description] UTF8String], strlen([[item description] UTF8String]), YAML_ANY_SCALAR_STYLE);
+ nodeID = yaml_document_add_scalar(doc, (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG, (yaml_char_t*)[[item description] UTF8String], [[item description] length], YAML_ANY_SCALAR_STYLE);
}
return nodeID;
}
View
2  src/YKParser.h
@@ -5,7 +5,7 @@
// Created by Patrick Thomson on 12/29/08.
//
-#import <Cocoa/Cocoa.h>
+#import <Foundation/Foundation.h>
#import "yaml.h"
@interface YKParser : NSObject {
View
51 src/YKParser.m
@@ -8,6 +8,10 @@
#import "YKParser.h"
#import "YKConstants.h"
+
+static BOOL _isBooleanTrue(NSString *aString);
+static BOOL _isBooleanFalse(NSString *aString);
+
@interface YKParser (YKParserPrivateMethods)
- (id)_interpretObjectFromEvent:(yaml_event_t)event;
@@ -144,20 +148,21 @@ - (NSArray *)parseWithError:(NSError **)e
- (id)_interpretObjectFromEvent:(yaml_event_t)event
{
- id obj = [NSString stringWithUTF8String:(const char *)event.data.scalar.value];
+ NSString *stringValue = [NSString stringWithUTF8String:(const char *)event.data.scalar.value];
+ id obj = stringValue;
if(event.data.scalar.style == YAML_PLAIN_SCALAR_STYLE) {
NSScanner *scanner = [NSScanner scannerWithString:obj];
// Integers are automatically casted unless given a !!str tag. I think.
- if([scanner scanInt:NULL]) {
- obj = [NSNumber numberWithInt:[obj intValue]];
- } else if([scanner scanDouble:NULL]) {
+ if([scanner scanDouble:NULL] && [scanner scanLocation] == [stringValue length]) {
obj = [NSNumber numberWithDouble:[obj doubleValue]];
+ } else if([scanner scanInt:NULL] && [scanner scanLocation] == [stringValue length]) {
+ obj = [NSNumber numberWithInt:[obj intValue]];
// FIXME: Boolean parsing here is not in accordance with the YAML standards.
- } else if([obj caseInsensitiveCompare:@"true"] == NSOrderedSame) {
+ } else if(_isBooleanTrue((NSString *)obj)) {
obj = [NSNumber numberWithBool:YES];
- } else if([obj caseInsensitiveCompare:@"false"] == NSOrderedSame) {
+ } else if(_isBooleanFalse((NSString *)obj)) {
obj = [NSNumber numberWithBool:NO];
} else if([obj isEqualToString:@"~"]) {
obj = [NSNull null];
@@ -228,3 +233,37 @@ - (void)dealloc
}
@end
+
+static BOOL _isBooleanFalse(NSString *aString)
+{
+ BOOL isFalse = NO;
+ const char *cstr = [aString UTF8String];
+ char *falseValues[] = {
+ "false", "False", "FALSE",
+ "n", "N", "NO", "No", "no",
+ "off", "Off", "OFF"
+ };
+ size_t length = sizeof(falseValues) / sizeof(*falseValues);
+ int index;
+ for(index = 0; index < length && !isFalse; index++) {
+ isFalse = strcmp(cstr, falseValues[index]) == 0;
+ }
+ return isFalse;
+}
+
+static BOOL _isBooleanTrue(NSString *aString)
+{
+ BOOL isTrue = NO;
+ const char *cstr = [aString UTF8String];
+ char *trueValues[] = {
+ "true", "TRUE", "True",
+ "y", "Y", "Yes", "yes", "YES",
+ "on", "On", "ON"
+ };
+ size_t length = sizeof(trueValues) / sizeof(*trueValues);
+ int index;
+ for(index = 0; index < length && !isTrue; index++) {
+ isTrue = strcmp(cstr, trueValues[index]) == 0;
+ }
+ return isTrue;
+}
View
1  test/TestParsing.h
@@ -16,6 +16,7 @@
- (void)testVerySimpleStringParsing;
- (void)testModerateLoadingFromFile;
- (void)testAutomaticIntegerCasting;
+- (void)testAutomaticBooleanCasting;
- (void)testWithNonexistentFile;
- (void)testWithMalformedStringInput;
- (void)testSuccessfulLoadingUsingErrors;
View
39 test/TestParsing.m
@@ -32,6 +32,15 @@ - (void)testVerySimpleStringParsing
STAssertEqualObjects(o, needed, @"#parse returned an incorrect object");
}
+- (void)testDigitPrefixedStringParsing
+{
+ [p readString:@"- 325de3fa"];
+ id o = [p parse];
+ STAssertNotNil(o, @"#parse method failed to return anything.");
+ NSArray *needed = [NSArray arrayWithObject: [NSArray arrayWithObjects:@"325de3fa", nil]];
+ STAssertEqualObjects(o, needed, @"#parse returned an incorrect object");
+}
+
- (void)testModerateLoadingFromFile
{
[p readFile:@"test/moderate.yaml"];
@@ -50,6 +59,36 @@ - (void)testAutomaticIntegerCasting
STAssertEquals(1, [[o objectAtIndex:0] intValue], @"was not equal to 1");
}
+- (void)testAutomaticDoubleCasting
+{
+ [p readString:@"- 1.5\n"];
+ NSArray *o = [[p parse] objectAtIndex:0];
+ STAssertTrue([[o objectAtIndex:0] isKindOfClass:[NSNumber class]], @"was not a number");
+ STAssertEqualObjects([o objectAtIndex:0], [NSNumber numberWithDouble:1.5], @"incorrectly cast to NSNumber");
+}
+
+- (void)testAutomaticBooleanCasting
+{
+ [p readString:@"- true\n- True\n- TRUE\n- y\n- Y\n- Yes\n- YES\n- yes\n- on\n- On\n- ON\n"];
+ NSArray *o = [[p parse] objectAtIndex:0];
+ for(id value in o) {
+ if([value isKindOfClass:[NSNumber class]]) {
+ STAssertTrue([value boolValue], @"boolean value was not true");
+ } else {
+ STFail(@"was not a boolean");
+ }
+ }
+ [p readString:@"- false\n- False\n- FALSE\n- n\n- N\n- No\n- NO\n- off\n- Off\n- OFF\n"];
+ o = [[p parse] objectAtIndex:0];
+ for(id value in o) {
+ if([value isKindOfClass:[NSNumber class]]) {
+ STAssertFalse([value boolValue], @"boolean value was not false");
+ } else {
+ STFail(@"was not a boolean");
+ }
+ }
+}
+
- (void)testWithNonexistentFile
{
STAssertFalse([p readFile:@"test/doesnotexist"], @"#readFile returned true when given a nonexistent file");
Please sign in to comment.
Something went wrong with that request. Please try again.