battman 0.0.1

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.
@@ -0,0 +1,173 @@
1
+ require 'battman/smapi_battery'
2
+
3
+ module Battman
4
+
5
+ describe SmapiBattery do
6
+
7
+ it 'is a subclass of Battery' do
8
+ expect(SmapiBattery.new).to be_a Battery
9
+ end
10
+
11
+ describe '#path' do
12
+
13
+ let(:path_prefix) { '/sys/devices/platform/smapi' }
14
+
15
+ it 'builds the path for a smapi battery with the correct index' do
16
+ battery = SmapiBattery.new
17
+
18
+ expect(battery.path).to eq File.join(path_prefix, 'BAT0')
19
+
20
+ battery = SmapiBattery.new(1)
21
+
22
+ expect(battery.path).to eq File.join(path_prefix, 'BAT1')
23
+ end
24
+
25
+ it 'sets the corresponding instance variable' do
26
+ battery = SmapiBattery.new
27
+ path = battery.path
28
+
29
+ expect(battery.instance_variable_get(:@path)).to eq path
30
+ end
31
+ end
32
+
33
+ let(:battery) { SmapiBattery.new }
34
+
35
+ before(:each) do
36
+ allow(File).to receive(:join).and_call_original
37
+ end
38
+
39
+ describe '#remaining_percent' do
40
+
41
+ let(:percent_file) { File.join(battery.path, 'remaining_percent') }
42
+
43
+ before(:each) do
44
+ allow(File).to receive(:read).with(percent_file).and_return("100\n")
45
+ end
46
+
47
+ it 'reads the value from the correct file' do
48
+ battery.remaining_percent
49
+
50
+ expect(File).to have_received(:read).with(percent_file)
51
+ end
52
+
53
+ it 'returns the integer value of the read file contents' do
54
+ expect(battery.remaining_percent).to eq 100
55
+ end
56
+
57
+ end
58
+
59
+ describe '#remaining_running_time' do
60
+ let(:running_time_file) { File.join(battery.path, 'remaining_running_time') }
61
+
62
+ it 'reads the value from the correct file' do
63
+ allow(File).to receive(:read).with(running_time_file).and_return("100\n")
64
+ battery.remaining_running_time
65
+
66
+ expect(File).to have_received(:read).with(running_time_file)
67
+ end
68
+
69
+ it 'returns the number of seconds remaining' do
70
+ allow(File).to receive(:read).with(running_time_file).and_return("100\n")
71
+
72
+ expect(battery.remaining_running_time).to eq 6000
73
+ end
74
+
75
+ it 'raises a WrongStateError if the battery is not discharging' do
76
+ allow(File).to receive(:read).with(running_time_file).and_return("not_discharging\n")
77
+
78
+ expect { battery.remaining_running_time }.to raise_error(WrongStateError)
79
+ end
80
+
81
+ end
82
+
83
+ describe '#remaining_charging_time' do
84
+ let(:charging_time_file) { File.join(battery.path, 'remaining_charging_time') }
85
+
86
+ it 'reads the value from the correct file' do
87
+ allow(File).to receive(:read).with(charging_time_file).and_return("100\n")
88
+ battery.remaining_charging_time
89
+
90
+ expect(File).to have_received(:read).with(charging_time_file)
91
+ end
92
+
93
+ it 'returns the number of seconds remaining' do
94
+ allow(File).to receive(:read).with(charging_time_file).and_return("100\n")
95
+
96
+ expect(battery.remaining_charging_time).to eq 6000
97
+ end
98
+
99
+ it 'raises a WrongStateError if the battery is not charging' do
100
+ allow(File).to receive(:read).with(charging_time_file).and_return("not_charging\n")
101
+
102
+ expect { battery.remaining_charging_time }.to raise_error(WrongStateError)
103
+ end
104
+ end
105
+
106
+ describe '#power' do
107
+ let(:power_file) { File.join(battery.path, 'power_avg') }
108
+
109
+ it 'reads the value from the correct file' do
110
+ allow(File).to receive(:read).with(power_file).and_return("1000\n")
111
+ battery.power
112
+
113
+ expect(File).to have_received(:read).with(power_file)
114
+ end
115
+
116
+ it 'returns the power used in watt' do
117
+ allow(File).to receive(:read).with(power_file).and_return("-1000\n")
118
+
119
+ expect(battery.power).to eq(-1.0)
120
+ end
121
+
122
+ end
123
+
124
+ describe "#state" do
125
+ let(:state_file) { File.join(battery.path, 'state') }
126
+
127
+ it 'reads the state from the correct file' do
128
+ allow(File).to receive(:read).with(state_file).and_return("discharging\n")
129
+ battery.state
130
+
131
+ expect(File).to have_received(:read).with(state_file)
132
+ end
133
+
134
+ it 'returns the correct state as a symbol' do
135
+ allow(File).to receive(:read).with(state_file).and_return("discharging\n")
136
+
137
+ expect(battery.state).to eq :discharging
138
+ end
139
+ end
140
+
141
+ describe "#remaining_energy" do
142
+ let(:energy_file) { File.join(battery.path, 'remaining_capacity') }
143
+
144
+ it 'reads the value from the correct file' do
145
+ allow(File).to receive(:read).with(energy_file).and_return("100000\n")
146
+ battery.remaining_energy
147
+
148
+ expect(File).to have_received(:read).with(energy_file)
149
+ end
150
+
151
+ it 'returns the remaining energy in Wh' do
152
+ allow(File).to receive(:read).with(energy_file).and_return("100000\n")
153
+ expect(battery.remaining_energy).to eq 100.0
154
+ end
155
+ end
156
+
157
+ describe "#full_energy" do
158
+ let(:energy_file) { File.join(battery.path, 'last_full_capacity') }
159
+
160
+ it 'reads the value from the correct file' do
161
+ allow(File).to receive(:read).with(energy_file).and_return("100000\n")
162
+ battery.full_energy
163
+
164
+ expect(File).to have_received(:read).with(energy_file)
165
+ end
166
+
167
+ it 'returns the remaining energy in Wh' do
168
+ allow(File).to receive(:read).with(energy_file).and_return("100000\n")
169
+ expect(battery.full_energy).to eq 100.0
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,83 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, make a
10
+ # separate helper file that requires this one and then use it only in the specs
11
+ # that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ require 'simplecov'
18
+ SimpleCov.start
19
+
20
+ $: << '../lib'
21
+
22
+ RSpec.configure do |config|
23
+ # The settings below are suggested to provide a good initial experience
24
+ # with RSpec, but feel free to customize to your heart's content.
25
+ =begin
26
+ # These two settings work together to allow you to limit a spec run
27
+ # to individual examples or groups you care about by tagging them with
28
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
29
+ # get run.
30
+ config.filter_run :focus
31
+ config.run_all_when_everything_filtered = true
32
+
33
+ # Many RSpec users commonly either run the entire suite or an individual
34
+ # file, and it's useful to allow more verbose output when running an
35
+ # individual spec file.
36
+ if config.files_to_run.one?
37
+ # Use the documentation formatter for detailed output,
38
+ # unless a formatter has already been configured
39
+ # (e.g. via a command-line flag).
40
+ config.default_formatter = 'doc'
41
+ end
42
+
43
+ # Print the 10 slowest examples and example groups at the
44
+ # end of the spec run, to help surface which specs are running
45
+ # particularly slow.
46
+ config.profile_examples = 10
47
+
48
+ # Run specs in random order to surface order dependencies. If you find an
49
+ # order dependency and want to debug it, you can fix the order by providing
50
+ # the seed, which is printed after each run.
51
+ # --seed 1234
52
+ config.order = :random
53
+
54
+ # Seed global randomization in this process using the `--seed` CLI option.
55
+ # Setting this allows you to use `--seed` to deterministically reproduce
56
+ # test failures related to randomization by passing the same `--seed` value
57
+ # as the one that triggered the failure.
58
+ Kernel.srand config.seed
59
+
60
+ # rspec-expectations config goes here. You can use an alternate
61
+ # assertion/expectation library such as wrong or the stdlib/minitest
62
+ # assertions if you prefer.
63
+ config.expect_with :rspec do |expectations|
64
+ # Enable only the newer, non-monkey-patching expect syntax.
65
+ # For more details, see:
66
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
67
+ expectations.syntax = :expect
68
+ end
69
+
70
+ # rspec-mocks config goes here. You can use an alternate test double
71
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
72
+ config.mock_with :rspec do |mocks|
73
+ # Enable only the newer, non-monkey-patching expect syntax.
74
+ # For more details, see:
75
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
76
+ mocks.syntax = :expect
77
+
78
+ # Prevents you from mocking or stubbing a method that does not exist on
79
+ # a real object. This is generally recommended.
80
+ mocks.verify_partial_doubles = true
81
+ end
82
+ =end
83
+ end
metadata ADDED
@@ -0,0 +1,204 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: battman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joakim Reinert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-25 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-doc
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activesupport
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Battman allows specifying actions to perform depending on the value of
140
+ some battery attribute
141
+ email:
142
+ - mail@jreinert.com
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - Gemfile
150
+ - Guardfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - battman.gemspec
155
+ - battman_example.rb
156
+ - lib/battman.rb
157
+ - lib/battman/acpi_battery.rb
158
+ - lib/battman/battery.rb
159
+ - lib/battman/dsl.rb
160
+ - lib/battman/dsl/every_block.rb
161
+ - lib/battman/dsl/watch_block.rb
162
+ - lib/battman/errors.rb
163
+ - lib/battman/smapi_battery.rb
164
+ - lib/battman/version.rb
165
+ - spec/lib/battman/acpi_battery_spec.rb
166
+ - spec/lib/battman/battery_spec.rb
167
+ - spec/lib/battman/dsl/every_block_spec.rb
168
+ - spec/lib/battman/dsl/watch_block_spec.rb
169
+ - spec/lib/battman/dsl_spec.rb
170
+ - spec/lib/battman/smapi_battery_spec.rb
171
+ - spec/spec_helper.rb
172
+ homepage: https://github.com/jreinert/battman
173
+ licenses:
174
+ - MIT
175
+ metadata: {}
176
+ post_install_message:
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubyforge_project:
192
+ rubygems_version: 2.2.2
193
+ signing_key:
194
+ specification_version: 4
195
+ summary: A simple dsl for polling battery info
196
+ test_files:
197
+ - spec/lib/battman/acpi_battery_spec.rb
198
+ - spec/lib/battman/battery_spec.rb
199
+ - spec/lib/battman/dsl/every_block_spec.rb
200
+ - spec/lib/battman/dsl/watch_block_spec.rb
201
+ - spec/lib/battman/dsl_spec.rb
202
+ - spec/lib/battman/smapi_battery_spec.rb
203
+ - spec/spec_helper.rb
204
+ has_rdoc: