mrubyc-test 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +52 -0
- data/LICENSE.txt +21 -0
- data/README.md +154 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/mrubyc-test +6 -0
- data/lib/mrubyc-ext/hash.rb +10 -0
- data/lib/mrubyc-ext/mock.rb +8 -0
- data/lib/mrubyc-ext/mrubyc_test_case.rb +72 -0
- data/lib/mrubyc-ext/object.rb +54 -0
- data/lib/mrubyc-test.rb +81 -0
- data/lib/mrubyc/test/config.rb +29 -0
- data/lib/mrubyc/test/generator/attribute.rb +49 -0
- data/lib/mrubyc/test/generator/double.rb +34 -0
- data/lib/mrubyc/test/generator/script.rb +21 -0
- data/lib/mrubyc/test/generator/test_case.rb +58 -0
- data/lib/mrubyc/test/init.rb +56 -0
- data/lib/mrubyc/test/version.rb +5 -0
- data/lib/mrubyc_test_case/mrubyc_test_case.rb +79 -0
- data/lib/templates/main.c.erb +39 -0
- data/lib/templates/sample_test.rb.erb +55 -0
- data/lib/templates/test.rb.erb +111 -0
- data/mrubyc-test.gemspec +33 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 160ab8e14780603229e59811a6dc2c387cacf4a9e9037d271d41b2e9746fea37
|
4
|
+
data.tar.gz: 598655a35c67729fe21fb90f4b9920428148eb59ed385ccfe32178b82cf38e65
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7a846f95b6cd76f38febef344e34ed912d396376e233c704f2a2bb4c436d30433524b01bcb860ec218cab9306f6afa1395d0e09c741bc72686ab27889d0751f2
|
7
|
+
data.tar.gz: 1ee0ffe96570e29fa18a6f668adbc7dc604d4087b2312c01852b8db0c9876f6548e4b4baa0cc08bccba16971964cd6a515a98d0ae2838e455f7553ab9baefdd6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 hitoshi.hasumi@monstar-lab.com. 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
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mrubyc-test (0.1.0)
|
5
|
+
activesupport (~> 5.2)
|
6
|
+
rufo (~> 0.4)
|
7
|
+
thor (~> 0.20)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activesupport (5.2.2)
|
13
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
+
i18n (>= 0.7, < 2)
|
15
|
+
minitest (~> 5.1)
|
16
|
+
tzinfo (~> 1.1)
|
17
|
+
concurrent-ruby (1.1.4)
|
18
|
+
diff-lcs (1.3)
|
19
|
+
i18n (1.5.1)
|
20
|
+
concurrent-ruby (~> 1.0)
|
21
|
+
minitest (5.11.3)
|
22
|
+
rake (10.5.0)
|
23
|
+
rspec (3.8.0)
|
24
|
+
rspec-core (~> 3.8.0)
|
25
|
+
rspec-expectations (~> 3.8.0)
|
26
|
+
rspec-mocks (~> 3.8.0)
|
27
|
+
rspec-core (3.8.0)
|
28
|
+
rspec-support (~> 3.8.0)
|
29
|
+
rspec-expectations (3.8.2)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.8.0)
|
32
|
+
rspec-mocks (3.8.0)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.8.0)
|
35
|
+
rspec-support (3.8.0)
|
36
|
+
rufo (0.4.1)
|
37
|
+
thor (0.20.3)
|
38
|
+
thread_safe (0.3.6)
|
39
|
+
tzinfo (1.2.5)
|
40
|
+
thread_safe (~> 0.1)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
bundler (~> 2.0)
|
47
|
+
mrubyc-test!
|
48
|
+
rake (~> 10.0)
|
49
|
+
rspec (~> 3.0)
|
50
|
+
|
51
|
+
BUNDLED WITH
|
52
|
+
2.0.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 HASUMI Hitoshi
|
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,154 @@
|
|
1
|
+
# mrubyc-test
|
2
|
+
|
3
|
+
mrubyc-test is an unit test framework for [mruby/c](https://github.com/mrubyc/mrubyc), supporting basic assertions, stub and mock.
|
4
|
+
|
5
|
+
## Acknowledgements
|
6
|
+
|
7
|
+
The API design and implementation of this gem is greatly inspired by [test-unit](https://github.com/test-unit/test-unit). Thank the great work.
|
8
|
+
|
9
|
+
## Features
|
10
|
+
|
11
|
+
- Tests are applicable to mruby code. C code will not be covered
|
12
|
+
- Tests will run on your PC (POSIX) hereby you can write *business logic* with mruby/c apart from C API matters like microcontroler peripherals
|
13
|
+
- Simple assertions ... enough for almost firmware development though, I will increase the number of assertion
|
14
|
+
- Stub ... You can write your mruby code without peripheral implementation by C
|
15
|
+
- Mock ... You can call any method sill doesn't exist
|
16
|
+
- The implementation of your application and test code will be analyzed by CRuby program, then comlpiled into mruby byte code and executed on mruby/c VM
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'mrubyc-test'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install mrubyc-test
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
Assuming you are using [mrubyc-utils](https://github.com/hasumikin/mrubyc-utils) to manage your project and [rbenv](https://github.com/rbenv/rbenv) to manage Ruby versions.
|
37
|
+
It means you have `.mrubycconfig` file in the top directory of your project.
|
38
|
+
|
39
|
+
Besides, you have to locate mruby model files that are the target of testing like `mrblib/models/class_name.rb`
|
40
|
+
|
41
|
+
This is an example of ESP32 project:
|
42
|
+
|
43
|
+
```
|
44
|
+
~/your_project $ tree
|
45
|
+
.
|
46
|
+
├── .mrubycconfig # Created by mrubyc-utils
|
47
|
+
├── Makefile
|
48
|
+
├── build
|
49
|
+
├── components
|
50
|
+
├── main
|
51
|
+
├── mrblib
|
52
|
+
│ └── models # Place your model class files here
|
53
|
+
│ ├── class_name.rb # The testing target `ClassName`
|
54
|
+
│ └── my_class.rb # The testing target `MyClass`
|
55
|
+
│ └── tasks
|
56
|
+
│ ├── main.rb # Loop script isn't covered by mrubyc-test. use mrubyc-debugger
|
57
|
+
│ └── sub.rb # Loop script isn't covered by mrubyc-test. use mrubyc-debugger
|
58
|
+
└── sdkconfig
|
59
|
+
```
|
60
|
+
|
61
|
+
In the same directory:
|
62
|
+
|
63
|
+
$ mrubyc-test init
|
64
|
+
|
65
|
+
Then, some directories and files will be created in your project.
|
66
|
+
Now you can run test because a sample test code was also created.
|
67
|
+
|
68
|
+
$ mrubyc-test test
|
69
|
+
|
70
|
+
You should get some assertion faulures.
|
71
|
+
Take a look at `test/sample_test.rb` to handle the failures and know how to write your own test.
|
72
|
+
|
73
|
+
### Asserions
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
def assertions
|
77
|
+
my_var = 1
|
78
|
+
assert_equal 1, my_var # => success
|
79
|
+
assert_not_equal 2, my_var # => success
|
80
|
+
assert_not_nil my_var # => success
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
### Stubs
|
85
|
+
|
86
|
+
Assuming you have a model file at `mrblib/models/sample.rb`
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
class Sample
|
90
|
+
def do_something
|
91
|
+
attr_accessor :result
|
92
|
+
def do_something(arg)
|
93
|
+
@result = arg + still_not_defined_method
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
Then you can test `#do_something` method without having `#still_not_defind_method` like this:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
def stub_case
|
103
|
+
sample_obj = Sample.new
|
104
|
+
stub(sample_obj).still_not_defined_method { ", so we are nice" }
|
105
|
+
sample_obj.do_something("Ruby is nice")
|
106
|
+
assert_equal 'Ruby is nice, so we are nice', sample_obj.result
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
### Mocks
|
111
|
+
|
112
|
+
`mrblib/models/sample.rb` looks like this time:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
class Sample
|
116
|
+
def do_other_thing
|
117
|
+
is_to_be_hit()
|
118
|
+
end
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
You can test whether `#is_to_be_hit` method will be called:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
def mock_case
|
126
|
+
sample_obj = Sample.new
|
127
|
+
mock(sample_obj).is_to_be_hit
|
128
|
+
sample_obj.do_other_thing
|
129
|
+
end
|
130
|
+
```
|
131
|
+
|
132
|
+
## Known problems
|
133
|
+
|
134
|
+
- `.ruby-version` should be set to a veriosn of CRuby to use mrubyc-test command although mruby/c developers want to set it like mruby-1.4.1
|
135
|
+
- You have to write stub or mock test fot all the methods still do not exist otherwise your test won't turn green
|
136
|
+
|
137
|
+
## TODO (possibly)
|
138
|
+
|
139
|
+
- Better solution of coexistence of CRuby and mruby. May be a kind of wrapper tool or rbenv plugin?
|
140
|
+
- Assertion against arguments of mock
|
141
|
+
- Other assertions like LT(<), GTE(>=), include?, ...etc.
|
142
|
+
- bla bla bla
|
143
|
+
|
144
|
+
## Contributing
|
145
|
+
|
146
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hasumikin/mrubyc-test. 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.
|
147
|
+
|
148
|
+
## License
|
149
|
+
|
150
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
151
|
+
|
152
|
+
## Code of Conduct
|
153
|
+
|
154
|
+
Everyone interacting in the Mrubyc::Test project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/mrubyc-test/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mrubyc/test"
|
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
data/exe/mrubyc-test
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
class MrubycTestCase
|
2
|
+
def initialize(information)
|
3
|
+
@information = information
|
4
|
+
$mock ||= Mock.new
|
5
|
+
@puts_success_message = true
|
6
|
+
@puts_failure_message = true
|
7
|
+
end
|
8
|
+
def puts_information
|
9
|
+
puts
|
10
|
+
puts @information[:test_class_name] + '#' + @information[:method_name]
|
11
|
+
end
|
12
|
+
def success(assertion, expected, actual)
|
13
|
+
puts_information
|
14
|
+
$success_count += 1
|
15
|
+
if @puts_success_message
|
16
|
+
puts $colors[:success] + ' assertion : ' + assertion.to_s + $colors[:normal]
|
17
|
+
puts $colors[:success] + ' result : ' + actual.to_ss + $colors[:normal]
|
18
|
+
else
|
19
|
+
print $colors[:success] + '.' + $colors[:normal]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
def failure(assertion, expected, actual, message)
|
23
|
+
puts_information
|
24
|
+
$failure_count += 1
|
25
|
+
if @puts_failure_message
|
26
|
+
puts $colors[:failure] + ' path : ' + @information[:path].to_s
|
27
|
+
puts $colors[:failure] + ' line : ' + @information[:line].to_s
|
28
|
+
puts $colors[:failure] + ' description: ' + @information[:description].to_s
|
29
|
+
puts $colors[:failure] + ' ' + message if message
|
30
|
+
puts $colors[:failure] + ' assertion : ' + assertion.to_s + $colors[:normal]
|
31
|
+
puts $colors[:failure] + ' expected : ' + expected.to_ss + $colors[:normal]
|
32
|
+
puts $colors[:failure] + ' actual : ' + actual.to_ss + $colors[:normal]
|
33
|
+
else
|
34
|
+
print $colors[:failure] + '.' + $colors[:normal]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
def assert_equal(expected, actual, message = nil)
|
38
|
+
assertion = :assert_equal
|
39
|
+
actual == expected ? success(assertion, expected, actual) : failure(assertion, expected, actual, message)
|
40
|
+
end
|
41
|
+
def assert_not_equal(expected, actual, message = nil)
|
42
|
+
assertion = :assert_not_equal
|
43
|
+
actual != expected ? success(assertion, expected, actual) : failure(assertion, expected, actual, message)
|
44
|
+
end
|
45
|
+
def assert_not_nil(expression, message = nil)
|
46
|
+
assertion = :assert_not_nil
|
47
|
+
expression != nil ? success(assertion, nil, expression) : failure(assertion, nil, expression, message)
|
48
|
+
end
|
49
|
+
def self.description(text)
|
50
|
+
end
|
51
|
+
def self.desc(text)
|
52
|
+
end
|
53
|
+
def setup
|
54
|
+
end
|
55
|
+
def teardown
|
56
|
+
end
|
57
|
+
def stub(object)
|
58
|
+
object
|
59
|
+
end
|
60
|
+
def check_mock
|
61
|
+
$mock.expected.keys.each do |key|
|
62
|
+
$mock.actual[key] = 0 unless $mock.actual[key]
|
63
|
+
if $mock.expected[key] > $mock.actual[key]
|
64
|
+
failure(:mock, $mock.expected[key], $mock.actual[key], key.to_s + ' shoud have been called at least expected times')
|
65
|
+
else
|
66
|
+
success(:mock, $mock.expected[key], $mock.actual[key])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|