tcravit_ruby_lib 0.0.8 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,115 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : app_banner_spec.rb
5
+ # Specs for : TcravitRubyLib::AppBanner
6
+ ############################################################################
7
+ # Copyright 2011-2018, Tammy Cravit.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ ############################################################################
21
+
22
+ require 'spec_helper'
23
+
24
+ RSpec.describe "TcravitRubyLib::AppBanner" do
25
+
26
+ context "the basics" do
27
+ it "to format the app banner correctly" do
28
+ ab = TcravitRubyLib::Banner(name: "MyApp")
29
+ lines = ab.split("\n")
30
+ expect(lines[0]).to be == ("*" * 76)
31
+ expect(lines[2]).to be == ("*" * 76)
32
+ end
33
+ end
34
+
35
+ context "app banner components" do
36
+ it "to allow you to make a banner with just a name" do
37
+ ab = TcravitRubyLib::Banner(name: "MyApp")
38
+ lines = ab.split("\n")
39
+ expect(lines[1]).to match(/^\*\s+MyApp/)
40
+ expect(lines[1]).to match(/\s+\*$/)
41
+ expect(lines[1].length).to be == 76
42
+ end
43
+
44
+ it "to allow you to make a banner with a name and description" do
45
+ ab = TcravitRubyLib::Banner(name: "MyApp", description: "Do some super cool stuff")
46
+ lines = ab.split("\n")
47
+ expect(lines[1]).to match(/^\*\s+MyApp: Do some super cool stuff/)
48
+ expect(lines[1]).to match(/\s+\*$/)
49
+ expect(lines[1].length).to be == 76
50
+ end
51
+
52
+ it "to allow you to make a banner with a name and date" do
53
+ ab = TcravitRubyLib::Banner(name: "MyApp", date: "2018-01-01")
54
+ lines = ab.split("\n")
55
+ expect(lines[1]).to match(/^\*\s+MyApp/)
56
+ expect(lines[1]).to match(/\s+\*$/)
57
+ expect(lines[1].length).to be == 76
58
+ expect(lines[2]).to match(/^\*\s+2018-01-01\s+\*$/)
59
+ expect(lines[2].length).to be == 76
60
+ end
61
+
62
+ it "to allow you to make a banner with a name, date, description" do
63
+ ab = TcravitRubyLib::Banner(name: "MyApp", description: "Do some super cool stuff", date: "2018-01-01")
64
+ lines = ab.split("\n")
65
+ expect(lines[1]).to match(/^\*\s+MyApp: Do some super cool stuff/)
66
+ expect(lines[1]).to match(/\s+\*$/)
67
+ expect(lines[1].length).to be == 76
68
+ expect(lines[2]).to match(/^\*\s+2018-01-01\s+\*$/)
69
+ expect(lines[2].length).to be == 76
70
+ end
71
+
72
+ it "to allow you to make a banner with a name, date, description, version" do
73
+ ab = TcravitRubyLib::Banner(name: "MyApp", description: "Do some super cool stuff", date: "2018-01-01", version: "1.0.1")
74
+ lines = ab.split("\n")
75
+ expect(lines[1]).to match(/^\*\s+MyApp: Do some super cool stuff/)
76
+ expect(lines[1]).to match(/\s+\*$/)
77
+ expect(lines[1].length).to be == 76
78
+ expect(lines[2]).to match(/^\*\s+Version 1.0.1, 2018-01-01\s+\*$/)
79
+ expect(lines[2].length).to be == 76
80
+ end
81
+
82
+ it "to allow you to make a banner with a name, date, description, version, author" do
83
+ ab = TcravitRubyLib::Banner(name: "MyApp", description: "Do some super cool stuff", date: "2018-01-01", version: "1.0.1", author: "John Doe")
84
+ lines = ab.split("\n")
85
+ expect(lines[1]).to match(/^\*\s+MyApp: Do some super cool stuff/)
86
+ expect(lines[1]).to match(/\s+\*$/)
87
+ expect(lines[1].length).to be == 76
88
+ expect(lines[2]).to match(/^\*\s+Version 1.0.1, 2018-01-01, John Doe\s+\*$/)
89
+ expect(lines[2].length).to be == 76
90
+ end
91
+ end
92
+
93
+ context "error handling" do
94
+ it "to raise an error if the name is not provided" do
95
+ expect {TcravitRubyLib::Banner(description: "Foo")}.to raise_error(ArgumentError)
96
+ end
97
+
98
+ it "to raise an error if the description is too long" do
99
+ expect {TcravitRubyLib::Banner(description: ("*" * 133), line_length: 76)}.to raise_error(ArgumentError)
100
+ expect {TcravitRubyLib::Banner(description: ("*" * 128), line_length: 133)}.to raise_error(ArgumentError)
101
+ end
102
+ end
103
+
104
+ context "formatting" do
105
+ it "to allow you to change the output line length" do
106
+ ab = TcravitRubyLib::Banner(name: "MyApp", description: "Do some super cool stuff", line_length: 133)
107
+ lines = ab.split("\n")
108
+ expect(lines[0]).to be == ("*" * 133)
109
+ expect(lines[1]).to match(/^\*\s+MyApp: Do some super cool stuff/)
110
+ expect(lines[1]).to match(/\s+\*$/)
111
+ expect(lines[1].length).to be == 133
112
+ expect(lines[2]).to be == ("*" * 133)
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,70 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : app_config_spec.rb
5
+ # Specs for : TcravitRubyLib::AppConfig
6
+ ############################################################################
7
+ # Copyright 2011-2018, Tammy Cravit.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ ############################################################################
21
+
22
+ require 'spec_helper'
23
+ require 'fileutils'
24
+
25
+ RSpec.describe "TcravitRubyLib::AppConfig" do
26
+
27
+ STRING_VAL = "string"
28
+ NUMERIC_VAL = 18293
29
+ SYMBOLIC_VAL = :squid
30
+
31
+ before(:all) do
32
+ TcravitRubyLib::AppConfig.configure do
33
+ string_value STRING_VAL
34
+ numeric_value NUMERIC_VAL
35
+ symbol_value SYMBOLIC_VAL
36
+ end
37
+ end
38
+
39
+ it "should return the values for defined properties" do
40
+ expect(TcravitRubyLib::AppConfig.string_value).to be == STRING_VAL
41
+ expect(TcravitRubyLib::AppConfig.numeric_value).to be == NUMERIC_VAL
42
+ expect(TcravitRubyLib::AppConfig.symbol_value).to be == SYMBOLIC_VAL
43
+ end
44
+
45
+ it "should raise an error for undefined properties" do
46
+ expect { TcravitRubyLib::AppConfig.fishpaste_value }.to raise_error
47
+ end
48
+
49
+ it "should allow me to change a configuration property" do
50
+ TcravitRubyLib::AppConfig.string_value = "new value"
51
+ expect(TcravitRubyLib::AppConfig.string_value).to be == "new value"
52
+ end
53
+
54
+ it "should allow me to remove a configuration property" do
55
+ TcravitRubyLib::AppConfig.remove!(:string_value)
56
+ expect { TcravitRubyLib::AppConfig.string_value }.to raise_error
57
+ end
58
+
59
+ it "should allow additional properties to be added" do
60
+ TcravitRubyLib::AppConfig.configure do
61
+ another_value 192
62
+ end
63
+ TcravitRubyLib::AppConfig.configure do
64
+ and_another_value 384
65
+ end
66
+ expect(TcravitRubyLib::AppConfig.another_value).to be == 192
67
+ expect(TcravitRubyLib::AppConfig.and_another_value).to be == 384
68
+ end
69
+
70
+ end
@@ -0,0 +1,71 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : config_searcher_spec.rb
5
+ # Specs for : TcravitRubyLib::ConfigSearcher
6
+ ############################################################################
7
+ # Copyright 2011-2018, Tammy Cravit.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ ############################################################################
21
+
22
+ require 'spec_helper'
23
+ require 'fileutils'
24
+
25
+ RSpec.describe "TcravitRubyLib::ConfigSearcher" do
26
+
27
+ BASE_DIR = '/tmp/config_searcher_test'
28
+ DEEP_DIR = "#{BASE_DIR}/foo/bar/baz"
29
+ CONFIG_DIR = "#{BASE_DIR}/.config"
30
+
31
+ before(:all) do
32
+ FileUtils.mkdir_p DEEP_DIR
33
+ FileUtils.mkdir_p CONFIG_DIR
34
+ end
35
+
36
+ after(:all) do
37
+ FileUtils.remove_dir BASE_DIR, true
38
+ end
39
+
40
+ context "the basics" do
41
+ it "should successfully find a directory which exists" do
42
+ dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: DEEP_DIR, look_for: ".config")
43
+ expect(dir_path.to_s).to_not be_nil
44
+ expect(dir_path.to_s).to be == CONFIG_DIR
45
+ end
46
+
47
+ it "should not find a directory when one doesn't exist" do
48
+ dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: DEEP_DIR, look_for: ".snausages")
49
+ expect(dir_path.to_s).to be == ""
50
+ end
51
+
52
+ it "should return the container dir when the only_container_dir option is provided" do
53
+ dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: DEEP_DIR, look_for: ".config", only_container_dir: true)
54
+ expect(dir_path.to_s).to be == BASE_DIR
55
+ end
56
+ end
57
+
58
+ context "error handling" do
59
+ it "should raise an exception when the start_in directory doesn't exist" do
60
+ expect { TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: "#{DEEP_DIR}xxxxxxx", look_for: ".snausages") }.to raise_error
61
+ end
62
+ end
63
+
64
+ context "alternative option names" do
65
+ it "should behave the same for the alternative option names" do
66
+ dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_dir: DEEP_DIR, config_dir: ".config")
67
+ expect(dir_path.to_s).to_not be_nil
68
+ expect(dir_path.to_s).to be == CONFIG_DIR
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,58 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : configurable_spec.rb
5
+ # Specs for : TcravitRubyLib::Configurable
6
+ ############################################################################
7
+ # Copyright 2011-2018, Tammy Cravit.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ ############################################################################
21
+
22
+ require 'spec_helper'
23
+
24
+ class ConfigurableTest
25
+ include TcravitRubyLib::Configurable.with(:attr_a, :attr_b, :attr_c)
26
+ end
27
+
28
+ RSpec.describe "TCravitRubyLib::Configurable" do
29
+ it "should respond to the config method and provide its configuration" do
30
+ expect(ConfigurableTest).to respond_to(:config)
31
+ end
32
+
33
+ it "should respond to the Configure method and provide a space for configuration" do
34
+ expect(ConfigurableTest).to respond_to(:configure)
35
+ end
36
+
37
+ it "should respond to attributes for the configuration accessors" do
38
+ ["attr_a", "attr_b", "attr_c"].each do |a|
39
+ expect(ConfigurableTest.config).to respond_to(a.to_sym)
40
+ expect(ConfigurableTest.config).to respond_to("#{a}=".to_sym)
41
+ end
42
+ ["nonexistent_attr"].each do |a|
43
+ expect(ConfigurableTest.config).not_to respond_to(a.to_sym)
44
+ expect(ConfigurableTest.config).not_to respond_to("#{a}=".to_sym)
45
+ end
46
+ end
47
+
48
+ it "should allow configuration" do
49
+ ConfigurableTest.configure do
50
+ attr_a 42
51
+ attr_b "Squid"
52
+ attr_c :FishPaste
53
+ end
54
+ expect(ConfigurableTest.config.attr_a).to be == 42
55
+ expect(ConfigurableTest.config.attr_b).to be == "Squid"
56
+ expect(ConfigurableTest.config.attr_c).to be == :FishPaste
57
+ end
58
+ end
@@ -0,0 +1,38 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : global_spec.rb
5
+ # Specs for : Global aspects of the tcravit_ruby_lib library (version
6
+ # number etc.)
7
+ ############################################################################
8
+ # Copyright 2011-2018, Tammy Cravit.
9
+ #
10
+ # Licensed under the Apache License, Version 2.0 (the "License");
11
+ # you may not use this file except in compliance with the License.
12
+ # You may obtain a copy of the License at
13
+ #
14
+ # http://www.apache.org/licenses/LICENSE-2.0
15
+ #
16
+ # Unless required by applicable law or agreed to in writing, software
17
+ # distributed under the License is distributed on an "AS IS" BASIS,
18
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ # See the License for the specific language governing permissions and
20
+ # limitations under the License.
21
+ ############################################################################
22
+
23
+ require 'spec_helper'
24
+
25
+ RSpec.describe "TcravitRubyLib" do
26
+ it "should define a VERSION constant" do
27
+ expect(TcravitRubyLib::VERSION).to_not be_nil
28
+ expect(TcravitRubyLib::VERSION).to match(/\d+\.\d+\.\d+/)
29
+ end
30
+
31
+ it "should define a VERSION_DATA array" do
32
+ expect(TcravitRubyLib::VERSION_DATA).not_to be_nil
33
+ expect(TcravitRubyLib::VERSION_DATA.length).to be == 3
34
+ TcravitRubyLib::VERSION_DATA.each do |i|
35
+ expect(i).to be_a_kind_of(Numeric)
36
+ end
37
+ end
38
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,42 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : spec_helper.rb
5
+ # Decription : RSpec configuration and global helper functions for testing
6
+ ############################################################################
7
+ # Copyright 2011-2018, Tammy Cravit.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ ############################################################################
21
+
1
22
  require 'rspec'
2
23
  require 'tcravit_ruby_lib'
3
24
 
25
+ # A quick-and-dirty method to load RSpec helper files in spec/support
26
+ def require_helper_named(helper_name)
27
+ require "#{File.join(File.dirname(__FILE__), "support", "#{helper_name}.rb")}"
28
+ end
29
+
30
+ # A quick-and-dirty method to load RSpec matchers in spec/support/matchers
31
+ def require_custom_matcher_named(matcher_name)
32
+ require "#{File.join(File.dirname(__FILE__), "support", "matchers", "#{matcher_name}_matcher.rb")}"
33
+ end
34
+
4
35
  RSpec.configure do |config|
5
- config.color_enabled = true
36
+ # config.formatter = 'progress'
6
37
  config.formatter = 'documentation'
7
- end
38
+ config.expect_with :rspec do |c|
39
+ c.syntax = :expect
40
+ c.on_potential_false_positives = :nothing
41
+ end
42
+ end
@@ -0,0 +1,49 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : be_a_rake_task_named_matcher.rb
5
+ # Description : RSpec custom matcher to ensure the subject is a Rake task
6
+ #
7
+ # This matcher tests the following aspects of the supplied subject:
8
+ #
9
+ # - it is of type Rake::Taskj
10
+ # - it responds to the :name method
11
+ # - its name matches the expected value
12
+ #
13
+ # If the matcher fails, it returns a list of which test conditions were not
14
+ # successful.
15
+ ############################################################################
16
+ # Copyright 2011-2018, Tammy Cravit.
17
+ #
18
+ # Licensed under the Apache License, Version 2.0 (the "License");
19
+ # you may not use this file except in compliance with the License.
20
+ # You may obtain a copy of the License at
21
+ #
22
+ # http://www.apache.org/licenses/LICENSE-2.0
23
+ #
24
+ # Unless required by applicable law or agreed to in writing, software
25
+ # distributed under the License is distributed on an "AS IS" BASIS,
26
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27
+ # See the License for the specific language governing permissions and
28
+ # limitations under the License.
29
+ ############################################################################
30
+
31
+ require 'rspec/expectations'
32
+ require File.join(File.dirname(__FILE__), "matcher_helpers.rb")
33
+
34
+ RSpec::Matchers.define :be_a_rake_task_named do |expected|
35
+ match do |actual|
36
+ @errors = []
37
+ @errors.push("expected a Rake task, but got a \"#{actual.class.name}\" instead") unless (actual.class.name == "Rake::Task")
38
+ begin
39
+ @errors.push("expected the task to be named \"#{expected}\", but got name \"#{actual.name}\" instead") unless (actual.name == expected)
40
+ rescue
41
+ @errors.push("expected the task to respond to \"name\"")
42
+ end
43
+ @errors.empty?
44
+ end
45
+
46
+ failure_message do |actual|
47
+ @errors.join("\n")
48
+ end
49
+ end
@@ -0,0 +1,57 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : be_a_valid_gem_version_file_for_matcher.rb
5
+ # Description : RSpec custom matcher for Gem version files.
6
+ #
7
+ # This matcher validates the format of a Gem version.rb file. It checks the
8
+ # following:
9
+ #
10
+ # - The specified file exists
11
+ # - The specified file matches the format:
12
+ #
13
+ # module GemName
14
+ # VERSION_DATA = [x, y, z]
15
+ # VERSION = VERSION_DATA.join(".")
16
+ # end
17
+ #
18
+ # If the matcher fails, a list of the failing conditions is returned.
19
+ ############################################################################
20
+ # Copyright 2011-2018, Tammy Cravit.
21
+ #
22
+ # Licensed under the Apache License, Version 2.0 (the "License");
23
+ # you may not use this file except in compliance with the License.
24
+ # You may obtain a copy of the License at
25
+ #
26
+ # http://www.apache.org/licenses/LICENSE-2.0
27
+ #
28
+ # Unless required by applicable law or agreed to in writing, software
29
+ # distributed under the License is distributed on an "AS IS" BASIS,
30
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31
+ # See the License for the specific language governing permissions and
32
+ # limitations under the License.
33
+ ############################################################################
34
+
35
+ require 'rspec/expectations'
36
+ require File.join(File.dirname(__FILE__), "matcher_helpers.rb")
37
+
38
+ RSpec::Matchers.define :be_a_valid_gem_version_file_for do |module_name|
39
+ match do |actual|
40
+ @errors = []
41
+ if File.exist?(actual) then
42
+ contents = TcravitRubyLib::MatchHelpers.read_output_file(actual)
43
+ @errors.push("expected the file to start with a module declaration") unless contents[0].match(/^\s*module/)
44
+ @errors.push("expected the file to declare a module named \"#{module_name}\"") unless contents[0].match(/^\s*module #{module_name}/)
45
+ @errors.push("expected the file to declare VERSION_DATA") unless contents[1].match(/^\s*VERSION_DATA =/)
46
+ @errors.push("expected VERSION_DATA to be a [major,minor,build] array") unless contents[1].match(/^\s+VERSION_DATA = \[\d+, \d+, \d+\]\s*$/)
47
+ @errors.push("expected the file to end with an \"end\" line") unless contents[3] == "end"
48
+ else
49
+ @errors.push("expected the file #{actual} to exist")
50
+ end
51
+ @errors.empty?
52
+ end
53
+
54
+ failure_message do |actual|
55
+ @errors.join("\n")
56
+ end
57
+ end
@@ -0,0 +1,54 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : declare_the_gem_version_to_be_matcher.rb
5
+ # Description : RSpec custom matcher to check the version declared in a
6
+ # Rubygem version.rb file.
7
+ #
8
+ # This matcher assumes the file format conforms to the format described in
9
+ # be_a_valid_gem_version_file_for_matcher.rb and checks that it declares
10
+ # itself to be of the specified version. If you want to skip checking any
11
+ # component of the version number, pass a nil value for the expected
12
+ # versions.
13
+ #
14
+ # If the matcher fails, a list of which components mismatched is returned.
15
+ ############################################################################
16
+ # Copyright 2011-2018, Tammy Cravit.
17
+ #
18
+ # Licensed under the Apache License, Version 2.0 (the "License");
19
+ # you may not use this file except in compliance with the License.
20
+ # You may obtain a copy of the License at
21
+ #
22
+ # http://www.apache.org/licenses/LICENSE-2.0
23
+ #
24
+ # Unless required by applicable law or agreed to in writing, software
25
+ # distributed under the License is distributed on an "AS IS" BASIS,
26
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27
+ # See the License for the specific language governing permissions and
28
+ # limitations under the License.
29
+ ############################################################################
30
+
31
+ require 'rspec/expectations'
32
+ require File.join(File.dirname(__FILE__), "matcher_helpers.rb")
33
+
34
+ RSpec::Matchers.define :declare_the_gem_version_to_be do |expected_major, expected_minor, expected_build|
35
+ match do |actual|
36
+ @errors = []
37
+ contents = TcravitRubyLib::MatchHelpers.read_output_file(actual)
38
+ v = eval(contents[1].downcase)
39
+ unless expected_major.nil?
40
+ @errors.push("expected major version #{expected_major} but got #{v[0]}") unless (v[0] == expected_major)
41
+ end
42
+ unless expected_minor.nil?
43
+ @errors.push("expected minor version #{expected_minor} but got #{v[1]}") unless (v[1] == expected_minor)
44
+ end
45
+ unless expected_build.nil?
46
+ @errors.push("expected build version #{expected_build} but got #{v[2]}") unless (v[2] == expected_build)
47
+ end
48
+ @errors.empty?
49
+ end
50
+
51
+ failure_message do |actual|
52
+ @errors.join("\n")
53
+ end
54
+ end
@@ -0,0 +1,48 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : matcher_helpers.rb
5
+ # Description : Helper functions for the RSpec custom matchers in this
6
+ # directory.
7
+ ############################################################################
8
+ # Copyright 2011-2018, Tammy Cravit.
9
+ #
10
+ # Licensed under the Apache License, Version 2.0 (the "License");
11
+ # you may not use this file except in compliance with the License.
12
+ # You may obtain a copy of the License at
13
+ #
14
+ # http://www.apache.org/licenses/LICENSE-2.0
15
+ #
16
+ # Unless required by applicable law or agreed to in writing, software
17
+ # distributed under the License is distributed on an "AS IS" BASIS,
18
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ # See the License for the specific language governing permissions and
20
+ # limitations under the License.
21
+ ############################################################################
22
+
23
+ require 'rspec/expectations'
24
+
25
+ module TcravitRubyLib
26
+ module MatchHelpers
27
+
28
+ ##
29
+ # Read a text file and return its contents as an array of strings. If
30
+ # the file is not found or an error occurs, returns an empty array.
31
+
32
+ def self.read_output_file(f)
33
+ lines = []
34
+
35
+ if File.exist?(f)
36
+ begin
37
+ fh = open(f)
38
+ fh.each { |l| lines.push(l.chomp) }
39
+ fh.close
40
+ rescue
41
+ return []
42
+ end
43
+ end
44
+ return lines
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,46 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : utility_spec.rb
5
+ # Specs for : TcravitRubyLib::Utility
6
+ ############################################################################
7
+ # Copyright 2011-2018, Tammy Cravit.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ ############################################################################
21
+
22
+ require 'spec_helper'
23
+ require "tcravit_ruby_lib/utility"
24
+
25
+ RSpec.describe "TcravitRubyLib::Utility" do
26
+ context "random_alphanumeric()" do
27
+ it "should return a random string of the correct length" do
28
+ s = TcravitRubyLib::Utility.random_alphanumeric(16)
29
+ expect(s).not_to be_nil
30
+ expect(s.length).to be == 16
31
+ end
32
+
33
+ it "should return a relatively random string" do
34
+ s = TcravitRubyLib::Utility.random_alphanumeric(256)
35
+ frequencies = {}
36
+ s.chars.each {|c| frequencies[c] ||= 0; frequencies[c] = frequencies[c] + 1}
37
+ expect(frequencies.values.max_by { |x| x}).to be <= 25 # There's probably a better way to do this
38
+ end
39
+
40
+ it "should allow you to generate pronounceable passwords" do
41
+ s = TcravitRubyLib::Utility.random_alphanumeric(32, true)
42
+ expect(s.length).to be == 32
43
+ expect(s).to match(/^[A-Za-z]+/)
44
+ end
45
+ end
46
+ end