Basic tests for core functionality

* ScreenModule & ViewController methods
* ViewHelper module
* SystemHelper module
This commit is contained in:
Matt Brewer
2013-03-19 16:50:43 -04:00
parent 4db0b141ba
commit 3eb331a37a
9 changed files with 207 additions and 4 deletions

View File

@@ -1,5 +1,4 @@
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
return true if RUBYMOTION_ENV == 'test'
class AppDelegate < ProMotion::AppDelegateParent
def on_load(application, launch_options)
end
end

View File

@@ -8,15 +8,18 @@ module ProMotion
class << self
def log(log, with_color:color)
return if RUBYMOTION_ENV == "test"
puts color[0] + NAME + log + color[1]
end
def log(log, withColor:color)
return if RUBYMOTION_ENV == "test"
warn "[DEPRECATION] `log(log, withColor:color)` is deprecated. Use `log(log, with_color:color)`"
self.log(log, with_color:color)
end
def log(log)
return if RUBYMOTION_ENV == "test"
log(log, with_color: DEFAULT_COLOR)
end
end

View File

@@ -19,7 +19,7 @@ module ProMotion
def frame_from_array(array)
return CGRectMake(array[0], array[1], array[2], array[3]) if array.length == 4
Console.log(" - frame_from_array expects an array with four elements: [x, y, width, height]", withColor: Console::RED_COLOR)
Console.log(" - frame_from_array expects an array with four elements: [x, y, width, height]", with_color: Console::RED_COLOR)
CGRectZero
end

View File

@@ -0,0 +1,3 @@
class DummyClass
# will dynamically do things with this thing
end

View File

@@ -0,0 +1,10 @@
class HomeScreen < ProMotion::Screen
title "Home"
def on_load
set_nav_bar_right_button "Save", action: :save_something, type: UIBarButtonItemStyleDone
set_nav_bar_left_button "Cancel", action: :return_to_some_other_screen, type: UIBarButtonItemStylePlain
end
end

28
spec/ios_version_spec.rb Normal file
View File

@@ -0,0 +1,28 @@
describe "ios version" do
before do
@dummy = DummyClass.new
@dummy.extend ProMotion::SystemHelper
end
it "#ios_version_is?" do
@dummy.ios_version_is?(@dummy.ios_version).should == true
end
it "#ios_version_greater?" do
@dummy.ios_version_greater?('1.0').should == true
end
it "#ios_version_greater_eq?" do
@dummy.ios_version_greater_eq?(@dummy.ios_version).should == true
end
it "#ios_version_less?" do
@dummy.ios_version_less?('9.0').should == true
end
it "#ios_version_less_eq?" do
@dummy.ios_version_less_eq?(@dummy.ios_version).should == true
end
end

View File

@@ -4,4 +4,10 @@ describe "pro motion module" do
should.not.raise(NameError) { PM }
end
it "should not allow screen inclusion into just any class" do
dummy = DummyClass.new
dummy.extend ProMotion::ScreenModule
should.raise(StandardError) { dummy.on_create }
end
end

View File

@@ -0,0 +1,111 @@
describe "screen properties" do
before do
# Simulate AppDelegate setup of main screen
@screen = HomeScreen.new modal: true, nav_bar: true
@screen.on_load
end
it "should store title" do
HomeScreen.get_title.should == 'Home'
end
it "should let the instance reset the title" do
@screen.title = "instance method"
HomeScreen.get_title.should == 'instance method'
end
it "should store debug mode" do
HomeScreen.debug_mode = true
HomeScreen.debug_mode.should == true
end
it "#is_modal? should be true" do
@screen.is_modal?.should == true
end
it "should know it is the first screen" do
@screen.first_screen?.should == true
end
it "#should_autorotate should default to 'true'" do
@screen.should_autorotate.should == true
end
describe "iOS lifecycle methods" do
it "-viewDidLoad" do
@screen.mock!(:view_did_load) { true }
@screen.viewDidLoad.should == true
end
it "-viewWillAppear" do
@screen.mock!(:view_will_appear) { |animated| animated.should == true }
@screen.viewWillAppear(true)
end
it "-viewDidAppear" do
@screen.mock!(:view_did_appear) { |animated| animated.should == true }
@screen.viewDidAppear(true)
end
it "-viewWillDisappear" do
@screen.mock!(:view_will_disappear) { |animated| animated.should == true }
@screen.viewWillDisappear(true)
end
it "-viewDidDisappear" do
@screen.mock!(:view_did_disappear) { |animated| animated.should == true }
@screen.viewDidDisappear(true)
end
it "-shouldAutorotateToInterfaceOrientation" do
@screen.mock!(:should_rotate) { |o| o.should == UIInterfaceOrientationPortrait }
@screen.shouldAutorotateToInterfaceOrientation(UIInterfaceOrientationPortrait)
end
it "-shouldAutorotate" do
@screen.mock!(:should_autorotate) { true }
@screen.shouldAutorotate.should == true
end
it "-willRotateToInterfaceOrientation" do
@screen.mock! :will_rotate do |orientation, duration|
orientation.should == UIInterfaceOrientationPortrait
duration.should == 0.5
end
@screen.willRotateToInterfaceOrientation(UIInterfaceOrientationPortrait, duration: 0.5)
end
it "-didRotateFromInterfaceOrientation" do
@screen.mock!(:on_rotate) { true }
@screen.didRotateFromInterfaceOrientation(UIInterfaceOrientationPortrait).should == true
end
end
describe "navigation controller behavior" do
it "should have a nav bar" do
@screen.has_nav_bar?.should == true
end
it "#main_controller should return a navigation controller" do
@screen.main_controller.should.be.instance_of ProMotion::NavigationController
end
it "have a right bar button item" do
@screen.navigationItem.rightBarButtonItem.should.not == nil
end
it "should have a left bar button item" do
@screen.navigationItem.leftBarButtonItem.should.not == nil
end
end
end

43
spec/view_helper_spec.rb Normal file
View File

@@ -0,0 +1,43 @@
describe "view helpers" do
def equal_rect(rect)
->(obj) { CGRectEqualToRect obj, rect }
end
before do
@dummy = UIView.alloc.initWithFrame CGRectZero
@dummy.extend ProMotion::ViewHelper
end
it "#frame_from_array should return zero rect for bad input" do
@dummy.frame_from_array([]).should equal_rect(CGRectZero)
end
it "#frame_from_array should return a valid CGRect" do
@dummy.frame_from_array([0,0,320,480]).should equal_rect(CGRectMake(0,0,320,480))
end
it "should allow you to set attributes" do
@dummy.set_attributes @dummy, backgroundColor: UIColor.redColor
@dummy.backgroundColor.should == UIColor.redColor
end
describe "content height" do
before do
@child = UIView.alloc.initWithFrame([[20,100],[300,380]])
@dummy.addSubview @child
end
it "should return content height" do
@dummy.content_height(@dummy).should == 480
end
it "should ignore hidden subviews" do
@child.hidden = true
@dummy.content_height(@dummy).should == 0
end
end
end