tspec 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 83c4d9398a6b867d88b048d7e04490339fcfb83f
4
+ data.tar.gz: '09f87a6d7814aa3314e4a8420556052ed25e0cfd'
5
+ SHA512:
6
+ metadata.gz: fd0096b8d732ff9c56fa41c1a7abfd02b46eab64063fe9680b9b688e6db9a44f899f16b795008f502f1d79ed370bb3117176a4e30a4883069ea804ab0d03dabc
7
+ data.tar.gz: e1a10ffc54d218e3025ec18927db331027a0d0cf609bd451235b971e14946dab9cdc32d2384623ef78f07406d95d209a2f7d86702386fd0b18fd3fcc9f991057
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /vendor/bundle
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ # RubyMine
16
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2
5
+ - 2.3.3
6
+ - 2.4.0
7
+ before_install: gem install bundler -v 1.14.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at k128585@ie.u-ryukyu.ac.jp. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tspec.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 siman-man
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,139 @@
1
+ # TSpec
2
+
3
+ TSpec add simple type check of method into Ruby.
4
+
5
+ :construction: **Recommended for use only in hobby programming. Do not use this in production apps.** :construction:
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tspec'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tspec
22
+
23
+ ## Usage
24
+
25
+ tspec can use `#receive` and `#return` method.
26
+
27
+
28
+ ### receive
29
+
30
+ `receive` define the type of method arguments.
31
+
32
+ ```ruby
33
+ require 'tspec'
34
+
35
+ def echo(str)
36
+ puts str
37
+ end.receive(str: String)
38
+ ```
39
+
40
+ specify multiple type.
41
+
42
+ ```ruby
43
+ require 'tspec'
44
+
45
+ def echo(val)
46
+ puts val
47
+ end.receive(val: [String, Float])
48
+
49
+ echo('hello')
50
+ echo(3.14)
51
+ ```
52
+
53
+ if method arguments is single. it can skip keyword.
54
+
55
+ ```ruby
56
+ require 'tspec'
57
+
58
+ def join_array(arr)
59
+ arr.join(' ')
60
+ end.receive([String])
61
+
62
+ puts join_array(%w(hello world))
63
+ ```
64
+
65
+ if specify Array content type. writing is strange.
66
+
67
+ ```ruby
68
+ require 'tspec'
69
+
70
+ def receive_string_array(arr)
71
+ arr.join
72
+ end.receive(arr: [[String]])
73
+
74
+ puts receive_string_array(['hello', 'world'])
75
+ ```
76
+
77
+
78
+ ### return
79
+
80
+ `return` define the type of method return value.
81
+
82
+ ```ruby
83
+ require 'tspec'
84
+
85
+ def message
86
+ 'hello world'
87
+ end.return(String)
88
+ ```
89
+
90
+ multiple return value.
91
+
92
+ ```ruby
93
+ require 'tspec'
94
+
95
+ def random_val
96
+ [1.0, '1', :hello].sample
97
+ end.return(Float, String, Symbol)
98
+
99
+ 10.times do
100
+ v = random_val
101
+ end
102
+ ```
103
+
104
+ specify Array content type.
105
+
106
+ ```ruby
107
+ require 'tspec'
108
+
109
+ def message_list
110
+ %w(hello ruby world)
111
+ end.return([String])
112
+
113
+ p message_list
114
+ ```
115
+
116
+ ## Example
117
+
118
+ combination `receive` and `return` method.
119
+
120
+ ```ruby
121
+ require 'tspec'
122
+
123
+ def string2symbol(str)
124
+ str.to_sym
125
+ end.receive(str: String).return(Symbol)
126
+
127
+ p string2symbol('hello') #=> :hello
128
+ p string2symbol(123) #=> TSpec::ArgumentTypeError
129
+ ```
130
+
131
+ ## Contributing
132
+
133
+ Bug reports and pull requests are welcome on GitHub at https://github.com/siman-man/tspec. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
134
+
135
+
136
+ ## License
137
+
138
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
139
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tspec"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ require 'tspec'
2
+
3
+ def say_hello(str)
4
+ puts str
5
+ end.receive(st: String) #=> undefined `st' variable name
6
+
7
+ def echo(val)
8
+ puts val
9
+ end.receive(val: String)
10
+
11
+ echo(3.14) #=> echo need String parameter
@@ -0,0 +1,8 @@
1
+ require 'tspec'
2
+
3
+ def echo(val)
4
+ puts val
5
+ end.receive(val: [String, Float])
6
+
7
+ echo('hello')
8
+ echo(3.14)
@@ -0,0 +1,15 @@
1
+ require 'tspec'
2
+
3
+ def message
4
+ 'hello'
5
+ end.return(Float)
6
+
7
+ def string_or_float
8
+ ['hello', 3.14].sample
9
+ end.return(String, Regexp)
10
+
11
+ def message_list
12
+ %w(hello ruby world)
13
+ end.return(String)
14
+
15
+ p message_list
@@ -0,0 +1,15 @@
1
+ require 'tspec'
2
+
3
+ def message
4
+ 'hello'
5
+ end.return(String)
6
+
7
+ def string_or_float
8
+ ['hello', 3.14].sample
9
+ end.return(String, Float)
10
+
11
+ def message_list
12
+ %w(hello ruby world)
13
+ end.return([String])
14
+
15
+ p message_list
data/lib/tspec.rb ADDED
@@ -0,0 +1,107 @@
1
+ module TSpec
2
+ class ReturnValueTypeError < StandardError
3
+ end
4
+
5
+ class NotFoundArgumentNameError < StandardError
6
+ end
7
+
8
+ class ArgumentTypeError < StandardError
9
+ end
10
+ end
11
+
12
+ class Symbol
13
+ def return(*types)
14
+ self
15
+ end
16
+
17
+ def receive(*type, **types)
18
+ self
19
+ end
20
+ end
21
+
22
+ module TSpec
23
+ @method_return_type_table = {}
24
+ @method_arguments_type_table = {}
25
+ @before_trace = {}
26
+ @type_error_flag = false
27
+
28
+ def self.value_type_check(value, *types)
29
+ types.any? do |type|
30
+ if type.instance_of?(Array)
31
+ return false unless value.instance_of?(Array)
32
+
33
+ value.all? do |v|
34
+ type.any? do |t|
35
+ value_type_check(v, t)
36
+ end
37
+ end
38
+ else
39
+ value.instance_of?(type)
40
+ end
41
+ end
42
+ end
43
+
44
+ TracePoint.trace do |tp|
45
+ case tp.event
46
+ when :call
47
+ if %i(return receive).include?(tp.method_id) && %i(method_added singleton_method_added define_method).include?(@before_trace[:method_id])
48
+ klass = (@before_trace[:method_id] == :singleton_method_added) ? @before_trace[:self].singleton_class : @before_trace[:self]
49
+ ctx = tp.binding
50
+ key = "#{klass}::#{ctx.receiver}"
51
+
52
+ case tp.method_id
53
+ when :return
54
+ @method_return_type_table[key] = ctx.local_variable_get(:types)
55
+ when :receive
56
+ @method_arguments_type_table[key] = ctx.local_variable_get(:types)
57
+
58
+ if @method_arguments_type_table[key].empty?
59
+ @method_arguments_type_table[key] = {ctx.local_variable_get(:type).__id__ => ctx.local_variable_get(:type)}
60
+ end
61
+ end
62
+ else
63
+ key = "#{tp.defined_class}::#{tp.method_id}"
64
+
65
+ if types = @method_arguments_type_table[key]
66
+ arguments = tp.binding.eval("method(:#{tp.method_id}).parameters.map(&:last)")
67
+
68
+ types.each do |name, type|
69
+
70
+ if name != type.__id__
71
+ unless arguments.include?(name)
72
+ @type_error_flag = true
73
+ raise NotFoundArgumentNameError, "undefined arguments `#{name}' for #{key}"
74
+ end
75
+
76
+ value = tp.binding.local_variable_get(name)
77
+ else
78
+ value = tp.binding.local_variable_get(arguments.first)
79
+ end
80
+
81
+ unless value_type_check(value, *type)
82
+ @type_error_flag = true
83
+ raise ArgumentTypeError, "##{tp.method_id} '#{name}' variable should be #{type.inspect}, but actual '#{value.inspect}' - #{value.class}"
84
+ end
85
+ end
86
+ end
87
+ end
88
+ when :return
89
+ if !@type_error_flag
90
+ key = "#{tp.defined_class}::#{tp.method_id}"
91
+
92
+ if types = @method_return_type_table[key]
93
+ unless value_type_check(tp.return_value, *types)
94
+ @type_error_flag = true
95
+ raise ReturnValueTypeError, "`#{tp.method_id}' expected return #{types.map(&:inspect).join(' or ')}, but actual `#{tp.return_value.inspect}' - #{tp.return_value.class}"
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ @type_error_flag = false
102
+
103
+ if tp.defined_class != Symbol || !%i(return receive).include?(tp.method_id)
104
+ @before_trace = {self: tp.self, method_id: tp.method_id}
105
+ end
106
+ end
107
+ end
data/tspec.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "tspec"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["siman-man"]
9
+ spec.email = ["k128585@ie.u-ryukyu.ac.jp"]
10
+
11
+ spec.summary = %q{Add type check method}
12
+ spec.description = %q{Add type check method}
13
+ spec.homepage = "https://github.com/siman-man/tspec"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = '>= 2.2.0'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.14"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tspec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - siman-man
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Add type check method
56
+ email:
57
+ - k128585@ie.u-ryukyu.ac.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - examples/receive_failed.rb
73
+ - examples/receive_success.rb
74
+ - examples/return_failed.rb
75
+ - examples/return_success.rb
76
+ - lib/tspec.rb
77
+ - tspec.gemspec
78
+ homepage: https://github.com/siman-man/tspec
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 2.2.0
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.10
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Add type check method
102
+ test_files: []