File: data_iterator_spec.rb

package info (click to toggle)
jruby 9.4.8.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 89,244 kB
  • sloc: ruby: 548,574; java: 276,189; yacc: 25,873; ansic: 6,178; xml: 6,111; sh: 1,855; sed: 94; makefile: 78; jsp: 48; tcl: 40; exp: 12
file content (149 lines) | stat: -rw-r--r-- 4,743 bytes parent folder | download | duplicates (4)
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
import 'org.jruby.ir.util.DirectedGraph'
import 'org.jruby.ir.util.DataIterator'
import 'java.util.NoSuchElementException'

describe "DataIterator" do

  before do
    @graph = DirectedGraph.new
    @graph.addEdge(1, 2, "foo")
    @graph.addEdge(2, 3, "foo")
  end

  # hasNext method doesn't use the source or destination of iterator at all
  # So specs are not written for iterator having source set to false
  describe "hasNext" do

    context "edges of given type" do

      it "returns true if the iterator contains an edge of given type" do
        iterator = DataIterator.new(@graph.edges(), "foo", true, false)
        expect(iterator.hasNext).to eq true
      end

      it "returns false if the iterator does not contain any edge of given type" do
        iterator = DataIterator.new(@graph.edges(), "bar", true, false)
        expect(iterator.hasNext).to eq false
      end

    end

    context "edges not of given type" do

      it "returns true if the iterator contains an edge not of given type" do
        iterator = DataIterator.new(@graph.edges(), "bar", true, true)
        expect(iterator.hasNext).to eq true
      end

      it "returns false if the iterator contains an edge of given type" do
        iterator = DataIterator.new(@graph.edges(), "foo", true, true)
        expect(iterator.hasNext).to eq false
      end

    end

    context "when iterator type is null" do

      context "edges of given type" do

        it "returns true if the iterator contains an edge of type nil" do
          # add an edge of type nil
          @graph.addEdge(4,1,nil)
          iterator = DataIterator.new(@graph.edges(), nil, true, false)
          expect(iterator.hasNext).to eq true
        end

        it "returns false if the iterator does not contain any edge of type nil" do
          iterator = DataIterator.new(@graph.edges(), nil, true, false)
          expect(iterator.hasNext).to eq false
        end

      end

      context "edges not of given type" do

        it "returns true if the iterator contains an edge not of type nil" do
          iterator = DataIterator.new(@graph.edges(), nil, true, true)
          expect(iterator.hasNext).to eq true
        end

        it "returns false if the iterator contains all edges of type nil" do
          # remove existing edges not of type nil
          @graph.removeEdge(1,2)
          @graph.removeEdge(2,3)
          # add an edge of type nil
          @graph.addEdge(4,1,nil)
          iterator = DataIterator.new(@graph.edges(), nil, true, true)
          expect(iterator.hasNext).to eq false
        end

      end

    end

    context "when edge type is nil and iterator type is not nil" do

      it "returns true if the iterator contains an edge not of type nil" do
        # remove existing edges not of type nil
        @graph.removeEdge(1,2)
        @graph.removeEdge(2,3)
        # add an edge of type nil
        @graph.addEdge(4,1,nil)
        iterator = DataIterator.new(@graph.edges(), "foo", true, true)
        expect(iterator.hasNext).to eq true
      end

      it "returns false if the iterator contains all edges not of type nil" do
        iterator = DataIterator.new(@graph.edges(), "foo", true, true)
        expect(iterator.hasNext).to eq false
      end
    end

  end

  describe "next" do

    context "when the iterator has next edge" do

      context "when asked for data of source vertex" do
        it "returns the data of the source of the edge" do
          iterator = DataIterator.new(@graph.edges(), "foo", true, false)
          expect([1, 2]).to include iterator.next
        end
      end

      context "when asked for data of destination vertex" do
        it "returns the data of the destination of the edge" do
          iterator = DataIterator.new(@graph.edges(), "foo", false, false)
          expect([2, 3]).to include iterator.next
        end
      end
    end

    context "when the iterator does not have next edge" do

      before do
        @empty_graph = DirectedGraph.new
      end

      it "throws NoSuchElementException for source data" do
        iterator = DataIterator.new(@empty_graph.edges(), "foo", true, false)
        expect { iterator.next }.to raise_error NoSuchElementException
      end

      it "throws NoSuchElementException for destination data" do
        iterator = DataIterator.new(@empty_graph.edges(), "foo", false, false)
        expect { iterator.next }.to raise_error NoSuchElementException
      end
    end
  end

  describe "remove" do

    it "throws UnsupportedOperationException exception" do
      iterator = DataIterator.new(@graph.edges(), "foo", true, false)
      expect { iterator.remove }.to raise_error Java::JavaLang::UnsupportedOperationException
    end
  end

end