class_spec_helper 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b8638213951339c30d2d985e7cda10f3857c41575c2ba9409c0d3ef166468b92
4
+ data.tar.gz: 64299b1761a7d59974b3582266f7304b968408d204c18406c7c01b550045d244
5
+ SHA512:
6
+ metadata.gz: 6defe9ad81d470ad0a6e0e0d20cbb6cc016b0155a9389ebf386b97b3dd82526d52a37be92431b78a335b14ef1d89adf44a4fda14f98d2b00aaa4aa7c0526c8e0
7
+ data.tar.gz: 96db5f7019a128090a84505e0e3a7a8562a02f7cb3f38ba24e1dcd556db01f1546a6c8a94db368c667c2d32be0040a45d0cf7738ade6c73c98f3a89358804b3b
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0 (2023-07-13)
4
+
5
+
6
+ ### Features
7
+
8
+ * dynamically create named classes from within your test suite and have them destroyed automatically between each test ([8918b35](https://github.com/craigulliott/class_spec_helper/commit/8918b35e8a57527c9b95a3ca02d9c3d8526253f7))
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at craigulliott@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Craig Ulliott
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ # ClassSpecHelper
2
+
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/class_spec_helper.svg)](https://badge.fury.io/rb/class_spec_helper)
5
+ [![Specs](https://github.com/craigulliott/class_spec_helper/actions/workflows/specs.yml/badge.svg)](https://github.com/craigulliott/class_spec_helper/actions/workflows/specs.yml)
6
+ [![Types](https://github.com/craigulliott/class_spec_helper/actions/workflows/types.yml/badge.svg)](https://github.com/craigulliott/class_spec_helper/actions/workflows/types.yml)
7
+ [![Coding Style](https://github.com/craigulliott/class_spec_helper/actions/workflows/linter.yml/badge.svg)](https://github.com/craigulliott/class_spec_helper/actions/workflows/linter.yml)
8
+
9
+
10
+ ### What this gem is
11
+
12
+ A helper class for creating up and easily deleting (cleaning up) classes within a testing environment.
13
+
14
+ If you are building something which uses metaprogramming to dynamically create classes, then this gem will be a useful tool within your test suite.
15
+
16
+ ## Key Features
17
+
18
+ * Easily create named classes within your test suite
19
+ * Optionally provide base a base class for your new class to extend from
20
+ * Optionally provide a block which will be called within your new class via class_eval
21
+ * Handles class namespacing automatically
22
+ * Have those classes destroyed automatically between tests
23
+
24
+ ## Installation
25
+
26
+ Add the gem to your Gemfile:
27
+
28
+ ```ruby
29
+ gem "class_spec_helper"
30
+ ```
31
+
32
+ Or to your `*.gemspec`
33
+
34
+ ```ruby
35
+ spec.add_development_dependency "class_spec_helper"
36
+ ```
37
+
38
+ And run bundle install
39
+
40
+ $ bundle install
41
+
42
+
43
+ ## Getting Started
44
+
45
+ #### Setting up rspec
46
+
47
+ Install ClassSpecHelper into your `spec/spec_helper.rb`
48
+
49
+ ```ruby
50
+ require "class_spec_helper"
51
+
52
+ RSpec.configure do |config|
53
+
54
+ # make class_spec_helper conveniently accessable within your test suite
55
+ config.add_setting :class_spec_helper
56
+ config.class_spec_helper = ClassSpecHelper.new
57
+
58
+ # destroy these dyanmically created classes after each test
59
+ config.after(:each) do
60
+ config.class_spec_helper.remove_all_dynamically_created_classes
61
+ end
62
+ end
63
+
64
+ ```
65
+
66
+
67
+ #### An example test which creates classes dynamically
68
+
69
+ Note, that these dynamically created classes will be destroyed automatically after each test has been run
70
+
71
+ ```ruby
72
+ RSpec.describe ClassSpecHelper do
73
+ let(:class_spec_helper) { RSpec.configuration.class_spec_helper }
74
+
75
+ describe "for a class and a child class" do
76
+ before(:each) do
77
+ # create the class named MyClass
78
+ class_spec_helper.create_class :MyClass
79
+ # note than MyClass will be available here
80
+ class_spec_helper.create_class :MyChildClass, MyClass
81
+ end
82
+
83
+ it "created a class which correctly extends the base class" do
84
+ expect(MyClass < MyChildClass).to be true
85
+ end
86
+
87
+ end
88
+ end
89
+ ```
90
+
91
+
92
+ ## Development
93
+
94
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
95
+
96
+ We use [Conventional Commit Messages](https://gist.github.com/qoomon/5dfcdf8eec66a051ecd85625518cfd13).
97
+
98
+ Code should be linted and formatted according to [Ruby Standard](https://github.com/standardrb/standard).
99
+
100
+ Publishing is automated via github actions and Googles [Release Please](https://github.com/google-github-actions/release-please-action) github action
101
+
102
+ We prefer using squash-merges when merging pull requests because it helps keep a linear git history and allows more fine grained control of commit messages which get sent to release-please and ultimately show up in the changelog.
103
+
104
+ Type checking is enabled for this project. You can find the corresponding `rbs` files in the sig folder.
105
+
106
+ Install types for the packages used in development (such as `rspec`) by running
107
+
108
+ $ rbs collection install
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome on GitHub at https://github.com/craigulliott/class_spec_helper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/craigulliott/class_spec_helper/blob/master/CODE_OF_CONDUCT.md).
113
+
114
+ ## License
115
+
116
+ This software is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
117
+
118
+ ## Code of Conduct
119
+
120
+ Everyone interacting in the ClassSpecHelper project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/craigulliott/class_spec_helper/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ClassSpecHelper
4
+ module CreateClasses
5
+ # create a new class with the provided name, if the provided name is
6
+ # namespaced, then assume each part of the namespace is a module and
7
+ # create create modules as needed to satistfy the fully qualified name
8
+ #
9
+ # if base_class is provided, then the newly created class will extend it
10
+ #
11
+ # if block is provided, then the newly created class will execute
12
+ # it via class_eval
13
+ def create_class fully_qualified_class_name, base_class = nil, &block
14
+ # because we're using eval, we validate the class name to ensure it is not mallicious
15
+ unless /\A[A-Z][a-zA-Z0-9_]*(::[A-Z][a-zA-Z0-9_]*)*\z/.match?(fully_qualified_class_name)
16
+ raise ClassNameError, "`#{fully_qualified_class_name}` is not a valid class name"
17
+ end
18
+
19
+ # ensure it was not already created here
20
+ if Object.const_defined? fully_qualified_class_name
21
+ raise ClassAlreadyExistsError, "Class `#{fully_qualified_class_name}` already exists within this application"
22
+ end
23
+
24
+ # ensure it does not already exist
25
+ if @classes.key? fully_qualified_class_name.to_sym
26
+ raise ClassAlreadyDynamicallyCreatedError, "Class `#{fully_qualified_class_name}` was already dynamically created with this helper"
27
+ end
28
+
29
+ # We prepend "::" to the class name to ensure the class is created at the top most scope
30
+ class_name_parts = fully_qualified_class_name.to_s.split("::")
31
+ class_name = class_name_parts.pop
32
+ module_names = class_name_parts
33
+
34
+ eval_code_lines = []
35
+
36
+ # is this class nested within a namespace?
37
+ is_namespaced = module_names.any?
38
+ if is_namespaced
39
+ # first module is always prepended by a "::" to ensure it is at the top most level
40
+ eval_code_lines << "module ::#{module_names.shift}"
41
+ # each remaining module name is just nested within this top most module
42
+ module_names.each do |module_name|
43
+ eval_code_lines << "module #{module_name}"
44
+ end
45
+ end
46
+ # the class definition
47
+ eval_code_lines << "class #{is_namespaced ? nil : "::"}#{class_name} #{base_class && "< #{base_class.name}"}"
48
+ # add the expected number of "ends" to close the class and any nested modules
49
+ end_count = eval_code_lines.count
50
+ end_count.times do
51
+ eval_code_lines << "end"
52
+ end
53
+
54
+ eval_code = eval_code_lines.join("\n")
55
+
56
+ # we use `eval` because we want to create a class which immediately has
57
+ # the expected name, unlike an anonymous class which is initially created
58
+ # without the expected name and only receives the expected name once assigned
59
+ # to a constant.
60
+ eval eval_code # standard:disable Security/Eval
61
+
62
+ # get the new class from the constant
63
+ klass = Object.const_get fully_qualified_class_name
64
+
65
+ # remember the full class name with namespace, so we can remove it again later
66
+ @classes[fully_qualified_class_name.to_sym] = klass
67
+
68
+ # finish building the class
69
+ klass.class_eval(&block) if block
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ClassSpecHelper
4
+ module DestroyClasses
5
+ # remove any classes (constants) which were
6
+ # created by the class helper
7
+ def remove_all_dynamically_created_classes
8
+ @classes.each do |fully_qualified_class_name, klass|
9
+ # destroy the class by removing the constant
10
+ destroy_class klass
11
+ # remove the class from the list of classes
12
+ @classes.delete fully_qualified_class_name.to_sym
13
+ end
14
+ end
15
+
16
+ # destroy the class by removing the constant
17
+ def destroy_class klass
18
+ fully_qualified_class_name = klass.name
19
+ # get the namespace and class name from the fully qualified class name
20
+ namespace = namespace_from_name fully_qualified_class_name
21
+ class_name = class_name_from_name fully_qualified_class_name
22
+ # remove the constant from the namespace
23
+ namespace.send :remove_const, class_name
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ClassSpecHelper
4
+ module Naming
5
+ # provided with a fully qualified class name, return the namespace
6
+ #
7
+ # For example:
8
+ # namespace_from_name "Foo::Bar::Baz" will return the constant Foo
9
+ # namespace_from_name "Foo" will return the constant Object
10
+ def namespace_from_name fully_qualified_class_name
11
+ namespace_parts = fully_qualified_class_name.split("::")[0..-2]
12
+ namespace_name = namespace_parts.join("::")
13
+
14
+ # if the model was namespaced, then return the coresponding constant
15
+ # if there is no namespace, then use `Object` which is the top most
16
+ # namespace in ruby
17
+ if namespace_name == ""
18
+ Object
19
+ else
20
+ Object.const_get namespace_name
21
+ end
22
+ end
23
+
24
+ # provided with a fully qualified class name, return the class name
25
+ #
26
+ # For example:
27
+ # namespace_from_name "Foo::Bar::Baz" will return the string "Baz"
28
+ # namespace_from_name "Foo" will return the string "Foo"
29
+ def class_name_from_name name
30
+ name.split("::").last
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ClassSpecHelper
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "class_spec_helper/version"
4
+
5
+ require "class_spec_helper/create_classes"
6
+ require "class_spec_helper/destroy_classes"
7
+ require "class_spec_helper/naming"
8
+
9
+ class ClassSpecHelper
10
+ class ClassAlreadyDynamicallyCreatedError < StandardError
11
+ end
12
+
13
+ class ClassAlreadyExistsError < StandardError
14
+ end
15
+
16
+ class ClassNameError < StandardError
17
+ end
18
+
19
+ def initialize
20
+ @classes = {}
21
+ end
22
+
23
+ include CreateClasses
24
+ include DestroyClasses
25
+ include Naming
26
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: class_spec_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Craig Ulliott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby gem to create named classes for use within your specs, and then
14
+ clear them out automatically between specs.
15
+ email:
16
+ - craigulliott@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - lib/class_spec_helper.rb
26
+ - lib/class_spec_helper/create_classes.rb
27
+ - lib/class_spec_helper/destroy_classes.rb
28
+ - lib/class_spec_helper/naming.rb
29
+ - lib/class_spec_helper/version.rb
30
+ homepage:
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ source_code_uri: https://github.com/craigulliott/class_spec_helper/
35
+ changelog_uri: https://github.com/craigulliott/class_spec_helper/blob/main/CHANGELOG.md
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 3.0.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.2.3
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Easily create and destroy named classes for use within your specs.
55
+ test_files: []