Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 105 lines (85 sloc) 3.258 kb
abf3083 @jamesgolick add the rest of the plugin
authored
1 $LOAD_PATH.reject! { |path| path.include?('TextMate') }
45a7f34 @jamesgolick add plugin skeleton
authored
2 require 'test/unit'
abf3083 @jamesgolick add the rest of the plugin
authored
3 require 'rubygems'
4 require 'active_support'
5 require 'mocha'
23ed346 @webmat Straight ports of the tests to Shoulda
webmat authored
6 require 'shoulda'
abf3083 @jamesgolick add the rest of the plugin
authored
7 require 'active_record'
8 require File.dirname(__FILE__)+'/../lib/enum_field'
9 require File.dirname(__FILE__)+'/../init'
10
11 class MockedModel; include EnumField; end;
45a7f34 @jamesgolick add plugin skeleton
authored
12
13 class EnumFieldTest < Test::Unit::TestCase
23ed346 @webmat Straight ports of the tests to Shoulda
webmat authored
14 context "with a simple gender enum" do
15 setup do
16 @possible_values = %w( male female )
17 MockedModel.expects(:validates_inclusion_of).with(:gender, :in => @possible_values, :message => "invalid gender")
18 MockedModel.send(:enum_field, :gender, @possible_values)
19 end
abf3083 @jamesgolick add the rest of the plugin
authored
20
23ed346 @webmat Straight ports of the tests to Shoulda
webmat authored
21 should "create constant with possible values named as pluralized field" do
22 assert_equal @possible_values, MockedModel::GENDERS
23 end
abf3083 @jamesgolick add the rest of the plugin
authored
24
23ed346 @webmat Straight ports of the tests to Shoulda
webmat authored
25 should "create query methods for each enum type" do
26 model = MockedModel.new
27
28 model.stubs(:gender).returns("male")
29 assert model.male?
98bd8aa @webmat Remove unnecessary operations from one of the tests
webmat authored
30 assert !model.female?
23ed346 @webmat Straight ports of the tests to Shoulda
webmat authored
31 model.stubs(:gender).returns("female")
32 assert !model.male?
33 assert model.female?
34 end
abf3083 @jamesgolick add the rest of the plugin
authored
35
23ed346 @webmat Straight ports of the tests to Shoulda
webmat authored
36 should "extend active record base with method" do
efb5eea Added support for Mixed Case terms.
Tammer Saleh authored
37 assert_respond_to ActiveRecord::Base, :enum_field
23ed346 @webmat Straight ports of the tests to Shoulda
webmat authored
38 end
45a7f34 @jamesgolick add plugin skeleton
authored
39 end
e3255c7 @webmat Fix query method names for multiple word choices, e.g. 'full time' =>…
webmat authored
40
0046839 @webmat Add the possibility to specify the error message
webmat authored
41 context "Specifying a message" do
42 setup do
43 @possible_values = %w(on off)
44 MockedModel.expects(:validates_inclusion_of).with(:status, :in => @possible_values, :message => "custom insult")
45 end
46
47 should "override the default message" do
48 MockedModel.send(:enum_field, :status, @possible_values, :message => 'custom insult')
49 end
50 end
51
e3255c7 @webmat Fix query method names for multiple word choices, e.g. 'full time' =>…
webmat authored
52 context "With an enum containing multiple word choices" do
53 setup do
40de85c Added tests for unicode and special chars
Tammer Saleh authored
54 MockedModel.stubs(:validates_inclusion_of)
e3255c7 @webmat Fix query method names for multiple word choices, e.g. 'full time' =>…
webmat authored
55 MockedModel.send :enum_field, :field, ['choice one', 'choice-two', 'other']
56 @model = MockedModel.new
57 end
58
59 should "define an underscored query method for the multiple word choice" do
efb5eea Added support for Mixed Case terms.
Tammer Saleh authored
60 assert_respond_to @model, :choice_one?
e3255c7 @webmat Fix query method names for multiple word choices, e.g. 'full time' =>…
webmat authored
61 end
62
63 should "define an underscored query method for the dasherized choice" do
efb5eea Added support for Mixed Case terms.
Tammer Saleh authored
64 assert_respond_to @model, :choice_two?
65 end
66 end
67
68 context "With an enum containing mixed case choices" do
69 setup do
70 MockedModel.stubs(:validates_inclusion_of)
71 MockedModel.send :enum_field, :field, ['Choice One', 'ChoiceTwo', 'Other']
72 @model = MockedModel.new
73 end
74
75 should "define a lowercase, underscored query method for the multiple word choice" do
76 assert_respond_to @model, :choice_one?
77 end
78
79 should "define a lowercase query method for the camelcase choice" do
80 assert_respond_to @model, :choicetwo?
e3255c7 @webmat Fix query method names for multiple word choices, e.g. 'full time' =>…
webmat authored
81 end
82 end
40de85c Added tests for unicode and special chars
Tammer Saleh authored
83
84 context "With an enum containing strange characters" do
85 setup do
86 MockedModel.stubs(:validates_inclusion_of)
87 MockedModel.send :enum_field, :field, ['choice%one', 'choice☺two', 'other.']
88 @model = MockedModel.new
89 end
90
91 should "define a normal query method for the unicode choice" do
92 assert_respond_to @model, :choice_two?
93 end
94
95 should "define a normal query method for the % choice" do
96 assert_respond_to @model, :choice_one?
97 end
98
99 should "define a query method without the trailing punctuation for the other choice" do
100 assert_respond_to @model, :other?
101 end
102 end
45a7f34 @jamesgolick add plugin skeleton
authored
103 end
23ed346 @webmat Straight ports of the tests to Shoulda
webmat authored
104
Something went wrong with that request. Please try again.