Skip to content

Commit

Permalink
Merge branch 'master' of github.com:venmo/VENCalculatorInputView
Browse files Browse the repository at this point in the history
  • Loading branch information
ayanonagon committed Jul 11, 2014
2 parents e4eca00 + e2ab504 commit e2a1f51
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
1 change: 1 addition & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ platform :ios, '7.0'
target 'VENCalculatorInputViewTests', :exclusive => true do
pod 'Specta', '~> 0.2.1'
pod 'Expecta', '~> 0.2.2'
pod 'OCMock', '~>3.0.0'
end
7 changes: 5 additions & 2 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
PODS:
- Expecta (0.2.3)
- Expecta (0.2.4)
- OCMock (3.0.2)
- Specta (0.2.1)

DEPENDENCIES:
- Expecta (~> 0.2.2)
- OCMock (~> 3.0.0)
- Specta (~> 0.2.1)

SPEC CHECKSUMS:
Expecta: 578e0c29df79a96a159187599e2def686ef6a66c
Expecta: 112bcafa2304ee0f3c5e586505f24555a47b25d5
OCMock: b9836ab89d8d5e66cbe6333f93857037c310ee62
Specta: 9141310f46b1f68b676650ff2854e1ed0b74163a

COCOAPODS: 0.33.1
10 changes: 10 additions & 0 deletions VENCalculatorInputView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
2F74D09D196F533B0040C816 /* UITextField+VENCalculatorInputView.m in Sources */ = {isa = PBXBuildFile; fileRef = 2F74D09C196F533B0040C816 /* UITextField+VENCalculatorInputView.m */; };
2F74D09F196F53E20040C816 /* UITextField+VENCalculatorInputViewSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 2F74D09E196F53E20040C816 /* UITextField+VENCalculatorInputViewSpec.m */; };
40BED60EE864401D8AFB2433 /* libPods-VENCalculatorInputViewTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 142758DF9FEC443BA2D635DE /* libPods-VENCalculatorInputViewTests.a */; };
B6F22CFD18AC031100543816 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B6F22CFC18AC031100543816 /* Foundation.framework */; };
B6F22D0C18AC031100543816 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B6F22CFC18AC031100543816 /* Foundation.framework */; };
Expand Down Expand Up @@ -47,6 +49,9 @@

/* Begin PBXFileReference section */
142758DF9FEC443BA2D635DE /* libPods-VENCalculatorInputViewTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-VENCalculatorInputViewTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
2F74D09B196F533B0040C816 /* UITextField+VENCalculatorInputView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITextField+VENCalculatorInputView.h"; sourceTree = "<group>"; };
2F74D09C196F533B0040C816 /* UITextField+VENCalculatorInputView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITextField+VENCalculatorInputView.m"; sourceTree = "<group>"; };
2F74D09E196F53E20040C816 /* UITextField+VENCalculatorInputViewSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITextField+VENCalculatorInputViewSpec.m"; sourceTree = "<group>"; };
8ECCE86895A54E0F8FA38DA9 /* Pods-VENCalculatorInputViewTests.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-VENCalculatorInputViewTests.xcconfig"; path = "Pods/Pods-VENCalculatorInputViewTests.xcconfig"; sourceTree = "<group>"; };
B6F22CF918AC031100543816 /* libVENCalculatorInputView.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libVENCalculatorInputView.a; sourceTree = BUILT_PRODUCTS_DIR; };
B6F22CFC18AC031100543816 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -135,6 +140,8 @@
B6F22D2B18AC039F00543816 /* VENCalculatorInputView.h */,
B6F22D2C18AC039F00543816 /* VENCalculatorInputView.m */,
B6F22D2D18AC039F00543816 /* VENCalculatorInputView.xib */,
2F74D09B196F533B0040C816 /* UITextField+VENCalculatorInputView.h */,
2F74D09C196F533B0040C816 /* UITextField+VENCalculatorInputView.m */,
B6FD6360196BAB8700D1913C /* NSString+VENCalculatorInputView.h */,
B6FD6361196BAB8700D1913C /* NSString+VENCalculatorInputView.m */,
B6F22D3118AC03C700543816 /* Images */,
Expand All @@ -154,6 +161,7 @@
B6F22D1218AC031100543816 /* VENCalculatorInputViewTests */ = {
isa = PBXGroup;
children = (
2F74D09E196F53E20040C816 /* UITextField+VENCalculatorInputViewSpec.m */,
B6F22D2218AC039100543816 /* VENMoneyCalculatorSpec.m */,
B6FD6363196BB09D00D1913C /* NSString+VENCalculatorInputViewSpec.m */,
B6F22D1318AC031100543816 /* Supporting Files */,
Expand Down Expand Up @@ -301,13 +309,15 @@
B6F22D3018AC039F00543816 /* VENCalculatorInputView.m in Sources */,
B6F22D2E18AC039F00543816 /* VENMoneyCalculator.m in Sources */,
B6F22D2F18AC039F00543816 /* VENCalculatorInputTextField.m in Sources */,
2F74D09D196F533B0040C816 /* UITextField+VENCalculatorInputView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
B6F22D0518AC031100543816 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2F74D09F196F53E20040C816 /* UITextField+VENCalculatorInputViewSpec.m in Sources */,
B6F22D2318AC039100543816 /* VENMoneyCalculatorSpec.m in Sources */,
B6FD6364196BB09D00D1913C /* NSString+VENCalculatorInputViewSpec.m in Sources */,
);
Expand Down
7 changes: 7 additions & 0 deletions VENCalculatorInputView/UITextField+VENCalculatorInputView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import <UIKit/UIKit.h>

@interface UITextField (VENCalculatorInputView)

- (NSRange)selectedNSRange;

@end
19 changes: 19 additions & 0 deletions VENCalculatorInputView/UITextField+VENCalculatorInputView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#import "UITextField+VENCalculatorInputView.h"

@implementation UITextField (VENCalculatorInputView)

- (NSRange)selectedNSRange
{
UITextPosition *beginning = self.beginningOfDocument;

UITextRange *selectedRange = self.selectedTextRange;
UITextPosition *selectionStart = selectedRange.start;
UITextPosition *selectionEnd = selectedRange.end;

NSInteger location = [self offsetFromPosition:beginning toPosition:selectionStart];
NSInteger length = [self offsetFromPosition:selectionStart toPosition:selectionEnd];

return NSMakeRange(location, length);
}

@end
12 changes: 10 additions & 2 deletions VENCalculatorInputView/VENCalculatorInputTextField.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import "VENCalculatorInputTextField.h"
#import "VENMoneyCalculator.h"
#import "UITextField+VENCalculatorInputView.h"

@interface VENCalculatorInputTextField ()
@property (strong, nonatomic) VENMoneyCalculator *moneyCalculator;
Expand Down Expand Up @@ -40,7 +41,7 @@ - (void)setUpInit {

- (void)setLocale:(NSLocale *)locale {
_locale = locale;
VENCalculatorInputView *inputView = self.inputView;
VENCalculatorInputView *inputView = (VENCalculatorInputView *)self.inputView;
inputView.locale = locale;
self.moneyCalculator.locale = locale;
}
Expand Down Expand Up @@ -83,7 +84,14 @@ - (void)venCalculatorTextFieldDidEndEditing {
#pragma mark - VENCalculatorInputViewDelegate

- (void)calculatorInputView:(VENCalculatorInputView *)inputView didTapKey:(NSString *)key {
[self insertText:key];
if ([self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
NSRange range = [self selectedNSRange];
if ([self.delegate textField:self shouldChangeCharactersInRange:range replacementString:key]) {
[self insertText:key];
}
} else {
[self insertText:key];
}
}

- (void)calculatorInputViewDidTapBackspace:(VENCalculatorInputView *)calculatorInputView {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#define EXP_SHORTHAND
#import <Expecta/Expecta.h>
#import <Specta/Specta.h>
#import <OCMock/OCMock.h>

#import "UITextField+VENCalculatorInputView.h"

SpecBegin(UITextRange_VENCalculatorInputView)

describe(@"selectedNSRange", ^{

it(@"should get UITextField's selectedText in an NSRange", ^{
id mockBeginningTextPosition = OCMClassMock([UITextPosition class]);
id mockSelectionStartTextPosition = OCMClassMock([UITextPosition class]);
id mockSelectionEndTextPosition = OCMClassMock([UITextPosition class]);

id mockTextField = OCMClassMock([UITextField class]);
id mockSelectedTextRange = OCMClassMock([UITextRange class]);

OCMStub([mockTextField beginningOfDocument]).andReturn(mockBeginningTextPosition);
OCMStub([mockTextField selectedTextRange]).andReturn(mockSelectedTextRange);
OCMStub([(UITextRange *)mockSelectedTextRange start]).andReturn(mockSelectionStartTextPosition);
OCMStub([(UITextRange *)mockSelectedTextRange end]).andReturn(mockSelectionEndTextPosition);

[[mockTextField expect] offsetFromPosition:mockBeginningTextPosition toPosition:mockSelectionStartTextPosition];
[[mockTextField expect] offsetFromPosition:mockSelectionStartTextPosition toPosition:mockSelectionEndTextPosition];

[mockTextField selectedNSRange];
});
});

SpecEnd

0 comments on commit e2a1f51

Please sign in to comment.