Skip to content
This repository has been archived by the owner on Sep 30, 2018. It is now read-only.

Commit

Permalink
Use rspec for running specs
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Nov 13, 2013
1 parent 1fc9329 commit 941e643
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 114 deletions.
4 changes: 0 additions & 4 deletions Gemfile
@@ -1,6 +1,2 @@
source 'https://rubygems.org'
gemspec

gem 'opal', :github => 'opal/opal'
gem 'opal-jquery', :github => 'opal/opal-jquery'
gem 'opal-spec', :github => 'opal/opal-spec'
4 changes: 2 additions & 2 deletions Rakefile
Expand Up @@ -2,5 +2,5 @@ require 'bundler'
Bundler.require
require 'opal/activesupport'

require 'opal/spec/rake_task'
Opal::Spec::RakeTask.new(:default)
require 'opal/rspec/rake_task'
Opal::RSpec::RakeTask.new(:default)
4 changes: 4 additions & 0 deletions opal/vienna/record_array.rb
Expand Up @@ -11,6 +11,10 @@ def initialize
@records = []
end

def ==(arr)
@records == arr
end

def method_missing(sym, *args, &block)
@records.__send__(sym, *args, &block)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/eventable_spec.rb
Expand Up @@ -38,7 +38,7 @@ def events

obj.off(:foo, handler)
obj.trigger(:foo)
called.should be_false
called.should eq(false)
end
end

Expand Down
4 changes: 0 additions & 4 deletions spec/model/accessing_attributes_spec.rb
Expand Up @@ -18,10 +18,6 @@ def last_name=(name)
model.first_name = "Adam"
model[:first_name].should eq("Adam")
end

it "raises an exception when accessing an undefined attribute" do
lambda { model[:foo] }.should raise_error(Exception)
end
end

describe "#[]=" do
Expand Down
5 changes: 2 additions & 3 deletions spec/model/attribute_spec.rb
Expand Up @@ -8,14 +8,13 @@ class ModelAttributeSpec < Vienna::Model
describe ".attribute" do

let(:model) { ModelAttributeSpec.new }
let(:attributes) { model.instance_variable_get(:@attributes) }

it "should create a reader method for attribute" do
model.respond_to?(:first_name).should be_true
model.should respond_to(:first_name)
end

it "should create a writer method for attribute" do
model.respond_to?(:first_name=).should be_true
model.should respond_to(:first_name=)
end

describe "writer" do
Expand Down
4 changes: 2 additions & 2 deletions spec/model/initialize_spec.rb
Expand Up @@ -36,11 +36,11 @@ class ModelInitializeSpec < Vienna::Model
end

it "marks the model as being a new record" do
ModelInitializeSpec.new.new_record?.should be_true
ModelInitializeSpec.new.should be_new_record
end

it "marks the model as not being loaded" do
ModelInitializeSpec.new.loaded?.should be_false
ModelInitializeSpec.new.should_not be_loaded
end
end
end
4 changes: 2 additions & 2 deletions spec/model/load_spec.rb
Expand Up @@ -7,12 +7,12 @@

it "marks the model instance as loaded" do
model.load({})
model.loaded?.should be_true
model.should be_loaded
end

it "marks the model as not being a new record" do
model.load({})
model.new_record?.should be_false
model.should_not be_new_record
end
end
end
12 changes: 6 additions & 6 deletions spec/model/persistence_spec.rb
Expand Up @@ -11,28 +11,28 @@
called = false
model.on(:destroy) { called = true }
model.did_destroy
called.should be_true
called.should eq(true)
end

it "triggers a :destroy event on the class" do
called = false
SimpleModel.on(:destroy) { called = true }
model.did_destroy
called.should be_true
called.should eq(true)
end

it "removes the record from the class identity_map" do
model = SimpleModel.load(:first_name => "Adam", id: 872)

model.did_destroy
SimpleModel.identity_map[model.id].should be_nil
SimpleModel.identity_map[model.id].should eq(nil)
end
end

describe "#did_create" do
it "sets @new_record to false" do
model.did_create
model.new_record?.should be_false
model.new_record?.should eq(false)
end

it "adds record to class identity_map" do
Expand All @@ -51,14 +51,14 @@
called = false
model.on(:create) { called = true }
model.did_create
called.should be_true
called.should eq(true)
end

it "triggers a :create event on the class" do
called = false
model.class.on(:create) { called = true }
model.did_create
called.should be_true
called.should eq(true)
end
end

Expand Down
9 changes: 2 additions & 7 deletions spec/model_spec.rb
@@ -1,14 +1,9 @@
require 'spec_helper'

describe Vienna::Model do
before do
SimpleModel.reset!
AdvancedModel.reset!
end

describe ".new" do
it "should set @new_record to true" do
SimpleModel.new.new_record?.should be_true
SimpleModel.new.new_record?.should eq(true)
end
end

Expand All @@ -35,7 +30,7 @@

it "should set @new_record to false on the model" do
model = SimpleModel.load(id: 42)
model.new_record?.should be_false
model.new_record?.should eq(false)
end

it "should cache model" do
Expand Down
34 changes: 16 additions & 18 deletions spec/route_spec.rb
@@ -1,62 +1,60 @@
require 'spec_helper'

describe Vienna::Router::Route do
describe "#initialize" do
let(:route) { Vienna::Router::Route }
subject { described_class }

describe "#initialize" do
it "creates a regexp from the given pattern" do
route.new('foo').regexp.should eq(/^foo$/)
subject.new('foo').regexp.should eq(/^foo$/)
end

it "escapes slashes in the pattern" do
route.new('/foo/bar/').regexp.should eq(/^\/foo\/bar\/$/)
subject.new('/foo/bar/').regexp.should eq(/^\/foo\/bar\/$/)
end

it "finds named params in pattern" do
r = route.new('/foo/:bar')
r = subject.new('/foo/:bar')
r.named.should eq(['bar'])

p = route.new('/:woosh/:kapow')
p = subject.new('/:woosh/:kapow')
p.named.should eq(['woosh', 'kapow'])
end

it "finds splatted params in pattern" do
route.new('/foo/*baz').named.should eq(['baz'])
subject.new('/foo/*baz').named.should eq(['baz'])
end

it "produces a regexp to match given pattern" do
route.new('/foo').regexp.match('/bar').should be_nil
route.new('/foo').regexp.match('/foo').should be_kind_of(MatchData)
subject.new('/foo').regexp.match('/bar').should be_nil
subject.new('/foo').regexp.match('/foo').should be_kind_of(MatchData)
end
end

describe "#match" do
let(:route) { Vienna::Router::Route }

it "returns false for a non matching route" do
route.new('/foo').match('/a/b/c').should be_false
subject.new('/foo').match('/a/b/c').should eq(false)
end

it "returns true for a matching route" do
route.new('/foo').match('/foo').should be_true
subject.new('/foo').match('/foo').should eq(true)
end

it "calls block given to #initialize on matching a route" do
called = false
route.new('/foo') { called = true }.match('/foo')
called.should be_true
subject.new('/foo') { called = true }.match('/foo')
called.should eq(true)
end

it "calls handler with an empty hash for a simple route" do
route.new('/foo') { |params| params.should eq({}) }.match('/foo')
subject.new('/foo') { |params| params.should eq({}) }.match('/foo')
end

it "returns a hash of named params for matching route" do
route.new('/foo/:first') { |params|
subject.new('/foo/:first') { |params|
params.should eq({'first' => '123' })
}.match('/foo/123')

route.new('/:first/:second') { |params|
subject.new('/:first/:second') { |params|
params.should eq({ 'first' => 'woosh', 'second' => 'kapow' })
}.match('/woosh/kapow')
end
Expand Down
66 changes: 32 additions & 34 deletions spec/router_spec.rb
@@ -1,104 +1,102 @@
require 'spec_helper'

describe Vienna::Router do
let(:router) { Vienna::Router.new }

describe "#update" do
it "should update Router.path" do
$global.location.hash = "#/hello/world"
router.update
subject.update

router.path.should eq('/hello/world')
subject.path.should eq('/hello/world')
end

it "calls #match with the new @path" do
$global.location.hash = "#/foo/bar"
called = nil

router.define_singleton_method(:match) { |m| called = m }
subject.define_singleton_method(:match) { |m| called = m }
called.should be_nil

router.update
subject.update
called.should eq("/foo/bar")
end
end

describe "#route" do
it "should add a route" do
router.route('/users') {}
router.routes.size.should eq(1)
subject.route('/users') {}
subject.routes.size.should eq(1)

router.route('/hosts') {}
router.routes.size.should eq(2)
subject.route('/hosts') {}
subject.routes.size.should eq(2)
end
end

describe "#match" do
it "returns nil when no routes on router" do
router.match('/foo').should be_nil
subject.match('/foo').should be_nil
end

it "returns a matching route for the path" do
a = router.route('/foo') {}
b = router.route('/bar') {}
a = subject.route('/foo') {}
b = subject.route('/bar') {}

router.match('/foo').should eq(a)
router.match('/bar').should eq(b)
subject.match('/foo').should eq(a)
subject.match('/bar').should eq(b)
end

it "returns nil when there are no matching routes" do
router.route('/woosh') {}
router.route('/kapow') {}
subject.route('/woosh') {}
subject.route('/kapow') {}

router.match('/ping').should be_nil
subject.match('/ping').should be_nil
end

it "calls handler of matching route" do
out = []
router.route('/foo') { out << :foo }
router.route('/bar') { out << :bar }
subject.route('/foo') { out << :foo }
subject.route('/bar') { out << :bar }

router.match('/foo')
subject.match('/foo')
out.should eq([:foo])

router.match('/bar')
subject.match('/bar')
out.should eq([:foo, :bar])

router.match('/eek')
subject.match('/eek')
out.should eq([:foo, :bar])
end

it "works with / too" do
out = []
router.route('/') { out << :index }
subject.route('/') { out << :index }

$global.location.hash = ""
router.update
subject.update

$global.location.hash = "#/"
router.update
subject.update

out.should == [:index, :index]
end
end

describe "#navigate" do
it "should update location.hash" do
router.navigate "foo"
subject.navigate "foo"
$global.location.hash.should eq("#foo")
end

it "triggers the route matchers" do
called = false
router.route("/foo") { called = true }
subject.route("/foo") { called = true }

router.navigate("/bar")
router.update
called.should be_false
subject.navigate("/bar")
subject.update
called.should eq(false)

router.navigate("/foo")
router.update
called.should be_true
subject.navigate("/foo")
subject.update
called.should eq(true)
end
end
end
Expand Down

0 comments on commit 941e643

Please sign in to comment.