-
Notifications
You must be signed in to change notification settings - Fork 11
/
i2cdevice_spec.rb
executable file
·89 lines (64 loc) · 1.82 KB
/
i2cdevice_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!rspec
$LOAD_PATH.unshift "lib"
require "i2c"
require "i2c/driver/i2c-dev"
require "tempfile"
describe I2CDevice do
before do
@i2cout = ""
@i2cin = ""
@ioctl = nil
ioctl = proc do |cmd, arg|
@ioctl = [ cmd, arg ]
end
syswrite = proc do |str|
@i2cout << str
end
sysread = proc do |n|
@i2cin
end
@temp = Tempfile.new("i2c")
file = nil
open = File.method(:open)
allow(File).to receive(:open) do
file = open.call(@temp.path, "r+")
file.define_singleton_method(:ioctl) {|cmd,arg| ioctl.call(cmd, arg) }
file.define_singleton_method(:syswrite) {|str| syswrite.call(str) }
file.define_singleton_method(:sysread) {|n| sysread.call(n) }
file
end
@driver = I2CDevice::Driver::I2CDev.new(@temp.path)
end
describe "#i2cset" do
it "should be write 1 byte" do
i2c = I2CDevice.new(address: 0x10, driver: @driver)
i2c.i2cset(0x00)
expect(@ioctl).to eq([ I2CDevice::Driver::I2CDev::I2C_SLAVE, 0x10 ])
expect(@i2cout).to eq("\x00")
end
it "should be write multi bytes" do
i2c = I2CDevice.new(address: 0x10, driver: @driver)
i2c.i2cset(0x00, 0x01, 0x02)
expect(@ioctl).to eq([ I2CDevice::Driver::I2CDev::I2C_SLAVE, 0x10 ])
expect(@i2cout).to eq("\x00\x01\x02")
end
end
describe "#i2cget" do
it "should be read 1 byte" do
i2c = I2CDevice.new(address: 0x10, driver: @driver)
@i2cin = "\x01"
ret = i2c.i2cget(0x00)
expect(ret).to eq("\x01")
expect(@ioctl).to eq([ I2CDevice::Driver::I2CDev::I2C_SLAVE, 0x10 ])
expect(@i2cout).to eq("\x00")
end
it "should be read multi byte" do
i2c = I2CDevice.new(address: 0x10, driver: @driver)
@i2cin = "\x01\x02\x03"
ret = i2c.i2cget(0x00)
expect(ret).to eq("\x01\x02\x03")
expect(@ioctl).to eq([ I2CDevice::Driver::I2CDev::I2C_SLAVE, 0x10 ])
expect(@i2cout).to eq("\x00")
end
end
end