normatron 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. data/.yardopts +2 -0
  2. data/lib/normatron/filters.rb +42 -24
  3. data/lib/normatron/filters/ascii_filter.rb +18 -12
  4. data/lib/normatron/filters/blank_filter.rb +19 -15
  5. data/lib/normatron/filters/camelize_filter.rb +39 -34
  6. data/lib/normatron/filters/capitalize_filter.rb +19 -13
  7. data/lib/normatron/filters/chomp_filter.rb +27 -21
  8. data/lib/normatron/filters/dasherize_filter.rb +16 -10
  9. data/lib/normatron/filters/downcase_filter.rb +19 -13
  10. data/lib/normatron/filters/dump_filter.rb +20 -14
  11. data/lib/normatron/filters/keep_filter.rb +101 -84
  12. data/lib/normatron/filters/remove_filter.rb +26 -21
  13. data/lib/normatron/filters/squeeze_filter.rb +24 -16
  14. data/lib/normatron/filters/squish_filter.rb +20 -13
  15. data/lib/normatron/filters/strip_filter.rb +25 -17
  16. data/lib/normatron/filters/swapcase_filter.rb +19 -13
  17. data/lib/normatron/filters/titleize_filter.rb +19 -13
  18. data/lib/normatron/filters/underscore_filter.rb +28 -22
  19. data/lib/normatron/filters/upcase_filter.rb +19 -13
  20. data/lib/normatron/version.rb +1 -1
  21. data/spec/normatron/configuration_spec.rb +0 -1
  22. data/spec/normatron/extensions/active_record_spec.rb +1 -0
  23. data/spec/normatron/filters/ascii_filter_spec.rb +15 -9
  24. data/spec/normatron/filters/blank_filter_spec.rb +10 -11
  25. data/spec/normatron/filters/camelize_filter_spec.rb +94 -33
  26. data/spec/normatron/filters/capitalize_filter_spec.rb +8 -7
  27. data/spec/normatron/filters/squeeze_filter_spec.rb +2 -2
  28. data/spec/spec_helper.rb +6 -1
  29. data/spec/support/matchers/evaluate_matcher.rb +75 -0
  30. metadata +5 -2
@@ -2,25 +2,31 @@ require 'normatron/filters/helpers'
2
2
 
3
3
  module Normatron
4
4
  module Filters
5
+
6
+ ##
7
+ # Capitalizes the first character of each word.
8
+ #
9
+ # @example Out of box
10
+ # TitleizeFilter.evaluate("at your will!") #=> "At Your Will!"
11
+ #
12
+ # @example Using as ActiveRecord::Base normalizer
13
+ # normalize :attribute_a, :with => :titleize
14
+ # normalize :attribute_b, :with => [:custom_filter, :titleize]
15
+ #
16
+ # @see http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Chars.html#method-i-titleize ActiveSupport::Multibyte::Chars#titleize
17
+ # @see DownFilter Normatron::Filters::DownFilter
18
+ # @see SwapcaseFilter Normatron::Filters::SwapcaseFilter
19
+ # @see UpcaseFilter Normatron::Filters::UpcaseFilter
5
20
  module TitleizeFilter
6
21
  extend Helpers
7
22
 
8
23
  ##
9
- # Capitalizes the first character of each word.
10
- #
11
- # @example
12
- # TitleizeFilter.evaluate("at your will!") #=> "At Your Will!"
24
+ # Performs input conversion according to filter requirements.
13
25
  #
14
- # @example Using as ActiveRecord::Base normalizer
15
- # normalize :attribute_a, :with => :titleize
16
- # normalize :attribute_b, :with => [:custom_filter, :titleize]
26
+ # This method returns the object itself when the first argument is not a String.
17
27
  #
18
- # @param [String] input A character sequence
19
- # @return [String] The titleized character sequence or the object itself
20
- # @see http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Chars.html#method-i-titleize ActiveSupport::Multibyte::Chars#titleize
21
- # @see DownFilter Normatron::Filters::DownFilter
22
- # @see SwapcaseFilter Normatron::Filters::SwapcaseFilter
23
- # @see UpcaseFilter Normatron::Filters::UpcaseFilter
28
+ # @param input [String] The String to be filtered
29
+ # @return [String] A new titleized String
24
30
  def self.evaluate(input)
25
31
  input.kind_of?(String) ? mb_send(:titleize, input) : input
26
32
  end
@@ -2,34 +2,40 @@ require 'normatron/filters/helpers'
2
2
 
3
3
  module Normatron
4
4
  module Filters
5
+
6
+ ##
7
+ # Makes an underscored lowercase form from the expression in the string.
8
+ #
9
+ # Changes ‘::’ to ‘/’ to convert namespaces to paths.
10
+ #
11
+ # As a rule of thumb you can think of underscore as the inverse of camelize, though there are cases where that does
12
+ # not hold:
13
+ #
14
+ # "SSLError".underscore.camelize # => "SslError"
15
+ #
16
+ # @example Out of box
17
+ # UnderscoreFilter.evaluate("ActiveRecord::Errors") #=> "active_record/errors"
18
+ #
19
+ # @example Using as ActiveRecord::Base normalizer
20
+ # normalize :attribute_a, :with => :underscore
21
+ # normalize :attribute_b, :with => [:custom_filter, :underscore]
22
+ # normalize :attribute_c, :with => [[:underscore, :lower]]
23
+ # normalize :attribute_d, :with => [{:underscore => :lower}]
24
+ # normalize :attribute_e, :with => [:custom_filter, [:underscore, :lower]]
25
+ # normalize :attribute_f, :with => [:custom_filter, {:underscore => :lower}]
26
+ #
27
+ # @see http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore ActiveSupport::Inflector#underscore
28
+ # @see CamelizeFilter Normatron::Filters::CamelizeFilter
5
29
  module UnderscoreFilter
6
30
  extend Helpers
7
31
 
8
32
  ##
9
- # Makes an underscored, lowercase form from the expression in the string.
33
+ # Performs input conversion according to filter requirements.
10
34
  #
11
- # Changes ‘::’ to ‘/’ to convert namespaces to paths.
35
+ # This method returns the object itself when the first argument is not a String.
12
36
  #
13
- # As a rule of thumb you can think of underscore as the inverse of camelize, though there are cases where that does not hold:
14
- # "SSLError".underscore.camelize # => "SslError"
15
- #
16
- # @example
17
- # UnderscoreFilter.evaluate("ActiveRecord::Errors") #=> "active_record/errors"
18
- #
19
- # @example Using as ActiveRecord::Base normalizer
20
- # normalize :attribute_a, :with => :underscore
21
- # normalize :attribute_b, :with => [:custom_filter, :underscore]
22
- # normalize :attribute_c, :with => [[:underscore, :lower]]
23
- # normalize :attribute_d, :with => [{:underscore => :lower}]
24
- # normalize :attribute_e, :with => [:custom_filter, [:underscore, :lower]]
25
- # normalize :attribute_f, :with => [:custom_filter, {:underscore => :lower}]
26
- #
27
- # @param [String] input A character sequence
28
- # @return [String] The underscored character sequence or the object itself
29
- # @see http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore ActiveSupport::Inflector#underscore
30
- # @see CamelizeFilter Normatron::Filters::CamelizeFilter
31
- # @todo Performance tests
32
- # @todo Exception class
37
+ # @param input [String] The String to be filtered
38
+ # @return [String] A new underscored String
33
39
  def self.evaluate(input)
34
40
  return input unless input.kind_of?(String)
35
41
 
@@ -2,25 +2,31 @@ require 'normatron/filters/helpers'
2
2
 
3
3
  module Normatron
4
4
  module Filters
5
+
6
+ ##
7
+ # Uppercase all characters.
8
+ #
9
+ # @example Out of box
10
+ # UpcaseFilter.evaluate("borderlands") #=> "BORDERLANDS"
11
+ #
12
+ # @example Using as ActiveRecord::Base normalizer
13
+ # normalize :attribute_a, :with => :upcase
14
+ # normalize :attribute_b, :with => [:custom_filter, :upcase]
15
+ #
16
+ # @see http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Chars.html#method-i-upcase ActiveSupport::Multibyte::Chars#upcase
17
+ # @see DownFilter Normatron::Filters::DownFilter
18
+ # @see TitleizeFilter Normatron::Filters::TitleizeFilter
19
+ # @see SwapcaseFilter Normatron::Filters::SwapcaseFilter
5
20
  module UpcaseFilter
6
21
  extend Helpers
7
22
 
8
23
  ##
9
- # Uppercase all characters.
10
- #
11
- # @example
12
- # UpcaseFilter.evaluate("borderlands") #=> "BORDERLANDS"
24
+ # Performs input conversion according to filter requirements.
13
25
  #
14
- # @example Using as ActiveRecord::Base normalizer
15
- # normalize :attribute_a, :with => :upcase
16
- # normalize :attribute_b, :with => [:custom_filter, :upcase]
26
+ # This method returns the object itself when the first argument is not a String.
17
27
  #
18
- # @param [String] input A character sequence
19
- # @return [String] The uppercased character sequence or the object itself
20
- # @see http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Chars.html#method-i-upcase ActiveSupport::Multibyte::Chars#upcase
21
- # @see DownFilter Normatron::Filters::DownFilter
22
- # @see TitleizeFilter Normatron::Filters::TitleizeFilter
23
- # @see SwapcaseFilter Normatron::Filters::SwapcaseFilter
28
+ # @param input [String] The String to be filtered
29
+ # @return [String] A new uppercased String
24
30
  def self.evaluate(input)
25
31
  input.kind_of?(String) ? mb_send(:upcase, input) : input
26
32
  end
@@ -1,3 +1,3 @@
1
1
  module Normatron
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'normatron/configuration'
3
2
 
4
3
  describe Normatron::Configuration do
5
4
 
@@ -15,6 +15,7 @@ describe Normatron::Extensions::ActiveRecord do
15
15
  ActiveRecord.send(:remove_const, :Base)
16
16
  load 'active_record/base.rb'
17
17
  end
18
+ pending "'after' is causing a problem to test configuration file --seed 52000"
18
19
 
19
20
  describe :normalize do
20
21
  subject { model }
@@ -1,14 +1,20 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  require 'spec_helper'
4
- require 'normatron/filters/ascii_filter'
5
4
 
6
- describe Normatron::Filters::AsciiFilter do
7
- it_should_behave_like "string processor"
8
- it_should_behave_like "evaluable filter", ["ÉBRIO" ], "EBRIO"
9
- it_should_behave_like "evaluable filter", ["até" ], "ate"
10
- it_should_behave_like "evaluable filter", ["cirurgião" ], "cirurgiao"
11
- it_should_behave_like "evaluable filter", ["email@domain.com" ], "email@domain.com"
12
- it_should_behave_like "evaluable filter", ["éçü&! *¬¬" ], "ecu&! *!!"
13
- it_should_behave_like "evaluable filter", ["⠋⠗⠁⠝⠉⠑" ], "france"
5
+ module Normatron
6
+ module Filters
7
+ describe AsciiFilter do
8
+ it { should evaluate("ÉBRIO" ).to("EBRIO" ) }
9
+ it { should evaluate("até" ).to("ate" ) }
10
+ it { should evaluate("cirurgião" ).to("cirurgiao" ) }
11
+ it { should evaluate("email@domain.com").to("email@domain.com") }
12
+ it { should evaluate("éçü&! *¬¬" ).to("ecu&! *!!" ) }
13
+ it { should evaluate("⠋⠗⠁⠝⠉⠑" ).to("france" ) }
14
+ it { should evaluate(100 ).to(100 ) }
15
+ it { should evaluate(nil ).to(nil ) }
16
+
17
+ pending "Open a pull request to Stringex.\n'¬' (not sign) character is not evaluating as expected (minus sign or tilde)."
18
+ end
19
+ end
14
20
  end
@@ -1,15 +1,14 @@
1
1
  require 'spec_helper'
2
- require 'normatron/filters/blank_filter'
3
2
 
4
- describe Normatron::Filters::BlankFilter do
5
- it_should_behave_like "string processor"
6
- it_should_behave_like "evaluable filter", ["18pm"], "18pm"
7
-
8
- describe :blank do
9
- let(:word) { "phase" }
10
- it { subject.send(:evaluate, " ").should be_nil }
11
- it { subject.send(:evaluate, "\n \t \r \f").should be_nil }
12
- it { subject.send(:evaluate, "\n\t\r\f" ).should be_nil }
13
- it { subject.send(:evaluate, word ).should equal word }
3
+ module Normatron
4
+ module Filters
5
+ describe BlankFilter do
6
+ it { subject.send(:evaluate, " ").should be_nil }
7
+ it { subject.send(:evaluate, "\n \t \r \f").should be_nil }
8
+ it { subject.send(:evaluate, "\n\t\r\f" ).should be_nil }
9
+ it { subject.send(:evaluate, "phase" ).should eq("phase") }
10
+ it { subject.send(:evaluate, 100 ).should eq(100) }
11
+ it { subject.send(:evaluate, nil ).should be_nil }
12
+ end
14
13
  end
15
14
  end
@@ -3,40 +3,101 @@
3
3
  require 'spec_helper'
4
4
  require 'normatron/filters/camelize_filter'
5
5
 
6
- describe Normatron::Filters::CamelizeFilter do
7
- it_should_behave_like "string processor"
8
- it_should_behave_like "evaluable filter", ["active_record/errors" ], "ActiveRecord::Errors"
9
- it_should_behave_like "evaluable filter", ["active_record/errors", :upper ], "ActiveRecord::Errors"
10
- it_should_behave_like "evaluable filter", ["active_record/errors", :lower ], "activeRecord::Errors"
11
- it_should_behave_like "evaluable filter", ["dados_históricos/período/empresa" ], "DadosHistóricos::Período::Empresa"
12
- it_should_behave_like "evaluable filter", ["dados_históricos/período/empresa", :upper], "DadosHistóricos::Período::Empresa"
13
- it_should_behave_like "evaluable filter", ["dados_históricos/período/empresa", :lower], "dadosHistóricos::Período::Empresa"
14
- it_should_behave_like "evaluable filter", ["ímpar_par/norte_sul" ], "ÍmparPar::NorteSul"
15
- it_should_behave_like "evaluable filter", ["ímpar_par/norte_sul", :upper ], "ÍmparPar::NorteSul"
16
- it_should_behave_like "evaluable filter", ["ímpar_par/norte_sul", :lower ], "ímparPar::NorteSul"
17
-
18
- context "should affect acronyms" do
19
- let(:inflections) { ActiveSupport::Inflector::Inflections.instance }
20
-
21
- before(:all) do
22
- inflections.acronym 'HTTP'
23
- inflections.acronym 'SSL'
24
- inflections.acronym 'Xml'
25
- inflections.acronym 'docType'
26
- end
6
+ module Normatron
7
+ module Filters
8
+ describe CamelizeFilter do
9
+ it { should evaluate("aint_no/rest_for/the_wicked").to("AintNo::RestFor::TheWicked" ) }
10
+ it { should evaluate("AINT_NO/REST_FOR/THE_WICKED").to("AintNo::RestFor::TheWicked" ) }
11
+ it { should evaluate("Aint_no/Rest_for/The_wicked").to("AintNo::RestFor::TheWicked" ) }
12
+ it { should evaluate("Aint_No/Rest_For/The_Wicked").to("AintNo::RestFor::TheWicked" ) }
13
+ it { should evaluate("aInT_No/rEsT_FoR/ThE_WiCkEd").to("AintNo::RestFor::TheWicked" ) }
14
+ it { should evaluate("AiNt_nO/ReSt_fOr/tHe_wIcKeD").to("AintNo::RestFor::TheWicked" ) }
27
15
 
28
- after(:all) do
29
- inflections.acronyms.delete("http")
30
- inflections.acronyms.delete("ssl")
31
- inflections.acronyms.delete("xml")
32
- inflections.acronyms.delete("doctype")
33
- end
16
+ it { should evaluate("assets/stocks/companies" ).to("Assets::Stocks::Companies" ) }
17
+ it { should evaluate("ASSETS/STOCKS/COMPANIES" ).to("Assets::Stocks::Companies" ) }
18
+ it { should evaluate("Assets/Stocks/Companies" ).to("Assets::Stocks::Companies" ) }
19
+ it { should evaluate("aSsEtS/StOcKs/cOmPaNiEs" ).to("Assets::Stocks::Companies" ) }
20
+ it { should evaluate("AsSeTs/sToCkS/CoMpAnIeS" ).to("Assets::Stocks::Companies" ) }
21
+
22
+ it { should evaluate("bank/account" ).to("Bank::Account" ) }
23
+ it { should evaluate("BANK/ACCOUNT" ).to("Bank::Account" ) }
24
+ it { should evaluate("Bank/Account" ).to("Bank::Account" ) }
25
+ it { should evaluate("bAnK/AcCoUnT" ).to("Bank::Account" ) }
26
+ it { should evaluate("BaNk/aCcOuNt" ).to("Bank::Account" ) }
27
+
28
+ it { should evaluate("for_sale" ).to("ForSale" ) }
29
+ it { should evaluate("FOR_SALE" ).to("ForSale" ) }
30
+ it { should evaluate("For_sale" ).to("ForSale" ) }
31
+ it { should evaluate("For_Sale" ).to("ForSale" ) }
32
+ it { should evaluate("fOr_sAlE" ).to("ForSale" ) }
33
+ it { should evaluate("FoR_SaLe" ).to("ForSale" ) }
34
+
35
+ it { should evaluate("high_voltage/musics" ).to("HighVoltage::Musics" ) }
36
+ it { should evaluate("HIGH_VOLTAGE/MUSICS" ).to("HighVoltage::Musics" ) }
37
+ it { should evaluate("High_voltage/Musics" ).to("HighVoltage::Musics" ) }
38
+ it { should evaluate("High_Voltage/Musics" ).to("HighVoltage::Musics" ) }
39
+ it { should evaluate("hIgH_VoLtAgE/MuSiCs" ).to("HighVoltage::Musics" ) }
40
+ it { should evaluate("HiGh_vOlTaGe/mUsIcS" ).to("HighVoltage::Musics" ) }
41
+
42
+ it { should evaluate("note_book/black_piano" ).to("NoteBook::BlackPiano" ) }
43
+ it { should evaluate("NOTE_BOOK/BLACK_PIANO" ).to("NoteBook::BlackPiano" ) }
44
+ it { should evaluate("Note_book/Black_piano" ).to("NoteBook::BlackPiano" ) }
45
+ it { should evaluate("Note_Book/Black_Piano" ).to("NoteBook::BlackPiano" ) }
46
+ it { should evaluate("nOtE_BoOk/bLaCk_pIaNo" ).to("NoteBook::BlackPiano" ) }
47
+ it { should evaluate("NoTe_bOoK/BlAcK_PiAnO" ).to("NoteBook::BlackPiano" ) }
48
+
49
+ it { should evaluate("product" ).to("Product" ) }
50
+ it { should evaluate("PRODUCT" ).to("Product" ) }
51
+ it { should evaluate("Product" ).to("Product" ) }
52
+ it { should evaluate("pRoDuCt" ).to("Product" ) }
53
+ it { should evaluate("PrOdUcT" ).to("Product" ) }
34
54
 
35
- it_should_behave_like "evaluable filter", ["http_address/ssl/xml_file/doctype" ], "HTTPAddress::SSL::XmlFile::docType"
36
- it_should_behave_like "evaluable filter", ["http_address/ssl/xml_file/doctype", :upper], "HTTPAddress::SSL::XmlFile::docType"
37
- it_should_behave_like "evaluable filter", ["http_address/ssl/xml_file/doctype", :lower], "httpAddress::SSL::XmlFile::docType"
38
- it_should_behave_like "evaluable filter", ["doctype_stop/run_ssl/xml/mix_http" ], "docTypeStop::RunSSL::Xml::MixHTTP"
39
- it_should_behave_like "evaluable filter", ["doctype_stop/run_ssl/xml/mix_http", :upper], "docTypeStop::RunSSL::Xml::MixHTTP"
40
- it_should_behave_like "evaluable filter", ["doctype_stop/run_ssl/xml/mix_http", :lower], "doctypeStop::RunSSL::Xml::MixHTTP"
55
+ it { should evaluate("south_america/brazil/paraná").to("SouthAmerica::Brazil::Paraná") }
56
+ it { should evaluate("SOUTH_AMERICA/BRAZIL/PARANÁ").to("SouthAmerica::Brazil::Paraná") }
57
+ it { should evaluate("South_america/Brazil/Paraná").to("SouthAmerica::Brazil::Paraná") }
58
+ it { should evaluate("South_America/Brazil/Paraná").to("SouthAmerica::Brazil::Paraná") }
59
+ it { should evaluate("sOuTh_aMeRiCa/bRaZiL/PaRaNá").to("SouthAmerica::Brazil::Paraná") }
60
+ it { should evaluate("SoUtH_AmErIcA/BrAzIl/pArAnÁ").to("SouthAmerica::Brazil::Paraná") }
61
+
62
+ it { should evaluate("you_cannot/steal_my/wallet" ).to("YouCannot::StealMy::Wallet" ) }
63
+ it { should evaluate("YOU_CANNOT/STEAL_MY/WALLET" ).to("YouCannot::StealMy::Wallet" ) }
64
+ it { should evaluate("You_cannot/Steal_my/Wallet" ).to("YouCannot::StealMy::Wallet" ) }
65
+ it { should evaluate("You_Cannot/Steal_My/Wallet" ).to("YouCannot::StealMy::Wallet" ) }
66
+ it { should evaluate("yOu_cAnNoT/StEaL_My/wAlLeT" ).to("YouCannot::StealMy::Wallet" ) }
67
+ it { should evaluate("YoU_CaNnOt/sTeAl_mY/WaLlEt" ).to("YouCannot::StealMy::Wallet" ) }
68
+
69
+ it { should evaluate("aint_no/rest_for/the_wicked").to("aintNo::RestFor::TheWicked" ).with(:lower) }
70
+ it { should evaluate("AINT_NO/REST_FOR/THE_WICKED").to("aintNo::RestFor::TheWicked" ).with(:lower) }
71
+ it { should evaluate("Aint_no/Rest_for/The_wicked").to("aintNo::RestFor::TheWicked" ).with(:lower) }
72
+ it { should evaluate("Aint_No/Rest_For/The_Wicked").to("aintNo::RestFor::TheWicked" ).with(:lower) }
73
+ it { should evaluate("aInT_No/rEsT_FoR/ThE_WiCkEd").to("aintNo::RestFor::TheWicked" ).with(:lower) }
74
+ it { should evaluate("AiNt_nO/ReSt_fOr/tHe_wIcKeD").to("aintNo::RestFor::TheWicked" ).with(:lower) }
75
+
76
+ it { should evaluate(100).to(100) }
77
+ it { should evaluate(nil).to(nil) }
78
+
79
+ context "with acronyms setted" do
80
+ let(:inflections) { ActiveSupport::Inflector::Inflections.instance }
81
+
82
+ before(:all) do
83
+ inflections.acronym 'HTTP'
84
+ inflections.acronym 'SSL'
85
+ inflections.acronym 'Xml'
86
+ inflections.acronym 'docType'
87
+ end
88
+
89
+ after(:all) do
90
+ inflections.acronyms.delete("http")
91
+ inflections.acronyms.delete("ssl")
92
+ inflections.acronyms.delete("xml")
93
+ inflections.acronyms.delete("doctype")
94
+ end
95
+
96
+ it { should evaluate("http_address/ssl/xml_file/doctype").to("HTTPAddress::SSL::XmlFile::docType") }
97
+ it { should evaluate("http_address/ssl/xml_file/doctype").to("httpAddress::SSL::XmlFile::docType").with(:lower) }
98
+ it { should evaluate("doctype_stop/run_ssl/xml/mix_http").to("docTypeStop::RunSSL::Xml::MixHTTP" ) }
99
+ it { should evaluate("doctype_stop/run_ssl/xml/mix_http").to("doctypeStop::RunSSL::Xml::MixHTTP" ).with(:lower) }
100
+ end
101
+ end
41
102
  end
42
103
  end
@@ -4,11 +4,12 @@ require 'spec_helper'
4
4
  require 'normatron/filters/capitalize_filter'
5
5
 
6
6
  describe Normatron::Filters::CapitalizeFilter do
7
- it_should_behave_like "string processor"
8
- it_should_behave_like "evaluable filter", ["i love winter" ], "I love winter"
9
- it_should_behave_like "evaluable filter", ["I LOVE WINTER" ], "I love winter"
10
- it_should_behave_like "evaluable filter", ["ó, vida cruel!"], "Ó, vida cruel!"
11
- it_should_behave_like "evaluable filter", ["Ó, VIDA CRUEL!"], "Ó, vida cruel!"
12
- it_should_behave_like "evaluable filter", ["1 minute" ], "1 minute"
13
- it_should_behave_like "evaluable filter", ["1 MINUTE" ], "1 minute"
7
+ it { should evaluate("i love winter" ).to("I love winter" ) }
8
+ it { should evaluate("I LOVE WINTER" ).to("I love winter" ) }
9
+ it { should evaluate("ó, vida cruel!").to("Ó, vida cruel!") }
10
+ it { should evaluate("Ó, VIDA CRUEL!").to("Ó, vida cruel!") }
11
+ it { should evaluate("1 minute" ).to("1 minute" ) }
12
+ it { should evaluate("1 MINUTE" ).to("1 minute" ) }
13
+ it { should evaluate(100 ).to(100 ) }
14
+ it { should evaluate(nil ).to(nil ) }
14
15
  end
@@ -5,6 +5,6 @@ require 'normatron/filters/squeeze_filter'
5
5
 
6
6
  describe Normatron::Filters::SqueezeFilter do
7
7
  it_should_behave_like "string processor"
8
- it_should_behave_like "evaluable filter", ["1 22 333 4444 " ], "1 2 3 4 "
9
- it_should_behave_like "evaluable filter", ["1 22 333 4444 ", "2-3 "], "1 2 3 4444 "
8
+ it { should evaluate("squeezing: hells bells").to("squezing: hels bels" ) }
9
+ it { should evaluate("squeezing: hells bells").to("squeezing: hels bels").with("l") }
10
10
  end
@@ -1,8 +1,13 @@
1
1
  # encoding: UTF-8
2
2
 
3
- require "active_record"
3
+ require "normatron"
4
4
  Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
5
5
 
6
+ RSpec.configure do |config|
7
+ config.include(FilterMatchers)
8
+ config.order = "random"
9
+ end
10
+
6
11
  shared_examples "evaluable filter" do |args, expected|
7
12
  if args.size == 1
8
13
  it { subject.send(:evaluate, args[0]).should_not equal expected }
@@ -0,0 +1,75 @@
1
+ module FilterMatchers
2
+ def evaluate(input)
3
+ EvaluateMatcher.new(input)
4
+ end
5
+
6
+ class EvaluateMatcher
7
+ def initialize(input)
8
+ @input = input
9
+ self
10
+ end
11
+
12
+ def to(output)
13
+ @expected = output
14
+ self
15
+ end
16
+
17
+ def with(*args)
18
+ @args = args
19
+ self
20
+ end
21
+
22
+ def matches?(filter_module)
23
+ @filter = filter_module
24
+ @got = get_evaluation
25
+ @reason = get_failure_reason
26
+ @reason.nil?
27
+ end
28
+
29
+ def description
30
+ case @reason
31
+ when :value
32
+ "eq #{@expected.inspect}"
33
+ when :identity
34
+ "not equal #{@expected.inspect}"
35
+ when :type
36
+ "be a kind of #{@input.class}"
37
+ else
38
+ "evaluate #{@input.inspect} to #{@expected.inspect}"
39
+ end
40
+ end
41
+
42
+ def failure_message
43
+ case @reason
44
+ when :value
45
+ ["input: #{@input.inspect}",
46
+ "expected: #{@expected.inspect}",
47
+ "got: #{@got.inspect}"] * "\n"
48
+ when :identity
49
+ "expected #{@filter.name} evaluate and returns a different object_id from input object."
50
+ when :type
51
+ "expected #{@filter.name} evaluate and returns the same object type of his input."
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def get_evaluation
58
+ if @args.nil?
59
+ @filter.evaluate(@input)
60
+ else
61
+ @filter.evaluate(@input, *@args)
62
+ end
63
+ end
64
+
65
+ def get_failure_reason
66
+ if @got != @expected
67
+ :value
68
+ elsif !@got.kind_of?(@input.class)
69
+ :type
70
+ elsif @got.equal?(@input) && @got.kind_of?(String)
71
+ :identity
72
+ end
73
+ end
74
+ end
75
+ end