File: bit_mask_spec.rb

package info (click to toggle)
ruby-ffi-bit-masks 0.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 148 kB
  • sloc: ruby: 232; makefile: 3
file content (157 lines) | stat: -rw-r--r-- 3,979 bytes parent folder | download | duplicates (3)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require 'spec_helper'
require 'ffi/bit_masks/bit_mask'

describe FFI::BitMasks::BitMask do
  let(:flags) { {:foo => 0x1, :bar => 0x2, :baz => 0x4} }

  subject { described_class.new(flags) }

  describe "#initialize" do
    subject { described_class.new(flags) }

    it "should initialize the flags" do
      expect(subject.flags).to eq(flags)
    end

    it "should invert the flags into bitmasks" do
      expect(subject.bitmasks).to eq(flags.invert)
    end

    it "should default type to uint" do
      expect(subject.native_type).to eq(FFI::Type::UINT)
    end

    context "when given a custom type" do
      subject { described_class.new(flags,:ushort) }

      it "should set native type" do
        expect(subject.native_type).to eq(FFI::Type::USHORT)
      end
    end
  end

  describe "#symbols" do
    it "should return the names of the flags" do
      expect(subject.symbols).to eq(flags.keys)
    end
  end

  describe "#symbol_map" do
    it "should return the flags" do
      expect(subject.symbol_map).to eq(flags)
    end
  end

  describe "#to_h" do
    it "should return the flags" do
      expect(subject.to_h).to eq(flags)
    end
  end

  describe "#to_hash" do
    it "should return the flags" do
      expect(subject.to_hash).to eq(flags)
    end
  end

  describe "#[]" do
    context "when given a Symbol" do
      it "should lookup the bitmask" do
        expect(subject[:bar]).to eq(flags[:bar])
      end
    end

    context "when given an Integer" do
      it "should lookup the flag" do
        expect(subject[flags[:bar]]).to eq(:bar)
      end
    end

    context "otherwise" do
      it "should return nil" do
        expect(subject[Object.new]).to be_nil
      end
    end
  end

  describe "#to_native" do
    context "when given a Hash" do
      let(:hash) { {:foo => true, :bar => true, :baz => false} }

      it "should bitwise or together the flag masks" do
        expect(subject.to_native(hash)).to eq(flags[:foo] | flags[:bar])
      end

      context "when one of the keys does not correspond to a flag" do
        let(:hash) { {:foo => true, :bug => true, :baz => true} }

        it "should ignore the key" do
          expect(subject.to_native(hash)).to eq(flags[:foo] | flags[:baz])
        end
      end
    end

    context "when an Object that respnds to #to_int" do
      let(:int) { 0x3 }

      it "should call #to_int" do
        expect(subject.to_native(int)).to eq(0x3)
      end

      context "when given a bitmask that contains unknown masks" do
        let(:int) { flags[:foo] | flags[:bar] | 0x8 | 0x10 }

        it "should filter out the unknown masks" do
          expect(subject.to_native(int)).to eq(
            flags[:foo] | flags[:bar]
          )
        end
      end
    end

    context "when given an Object that does not respond to #to_int" do
      it "should raise an ArgumentError" do
        expect {
          subject.to_native(Object.new)
        }.to raise_error(ArgumentError)
      end
    end
  end

  describe "#from_native" do
    let(:value) { flags[:foo] | flags[:baz] }

    it "should set the flags from the value" do
      expect(subject.from_native(value)).to eq({
        :foo => true,
        :bar => false,
        :baz => true
      })
    end

    context "when one flag is a combination of other flags" do
      let(:flags) { {:foo => 0x1, :bar => 0x2, :baz => 0x3} }
      let(:value) { flags[:foo] | flags[:bar]      }

      it "should set all flags whose bitmasks are present" do
        expect(subject.from_native(value)).to eq({
          :foo => true,
          :bar => true,
          :baz => true
        })
      end
    end

    context "when given a value that contains unknown masks" do
      let(:value) { flags[:foo] | flags[:baz] | 0x8 | 0x10 }

      it "should ignore the unknown flags" do
        expect(subject.from_native(value)).to eq({
          :foo => true,
          :bar => false,
          :baz => true
        })
      end
    end
  end
end