|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | require_relative "../data-structures/LinkedList/linked_list"
|
2 |
| -require "byebug" |
| 4 | + |
3 | 5 | RSpec.describe LinkedList do
|
4 |
| - let(:linked_list) { LinkedList.new } |
| 6 | + context "with an empty list" do |
| 7 | + describe "#append" do |
| 8 | + it "adds a value to the enf of the list" do |
| 9 | + expect(subject.append("First Value")).to eq "Success: Value 'First Value' was appended" |
| 10 | + expect(subject.head.value).to eq "First Value" |
| 11 | + expect(subject.tail.value).to eq "First Value" |
| 12 | + expect(subject.size).to eq 1 |
| 13 | + end |
| 14 | + end |
5 | 15 |
|
6 |
| - describe "#add" do |
7 |
| - it "adds a value to the linked list" do |
8 |
| - expect(linked_list.add("First Value")).to eq "success" |
9 |
| - expect(linked_list.head.value).to eq "First Value" |
10 |
| - expect(linked_list.tail.value).to eq "First Value" |
| 16 | + describe "#prepend" do |
| 17 | + it "adds a value at the start of the list" do |
| 18 | + expect(subject.prepend("New first value")).to eq "Success: Value 'New first value' was prepended" |
| 19 | + expect(subject.head.value).to eq "New first value" |
| 20 | + expect(subject.tail.value).to eq "New first value" |
| 21 | + end |
11 | 22 | end
|
12 |
| - end |
13 | 23 |
|
14 |
| - describe "#get" do |
15 |
| - it "gets the value at a given index" do |
16 |
| - linked_list.add("First Value") |
17 |
| - linked_list.add("Second Value") |
18 |
| - expect(linked_list.get(1)).to eq "Second Value" |
| 24 | + describe "#isert_at" do |
| 25 | + it "show an error" do |
| 26 | + expect { subject.insert_at(0, "First value") }.to output("Error: The list is empty\n").to_stderr |
| 27 | + end |
19 | 28 | end
|
20 | 29 |
|
21 |
| - it "returns error message if index out of bounds" do |
22 |
| - expect(linked_list.get(10)).to eq "INDEX 10 OUT OF BOUNDS" |
| 30 | + describe "#get" do |
| 31 | + it "returns error message if list is empty" do |
| 32 | + expect { subject.get(0) }.to output("Error: The list is empty\n").to_stderr |
| 33 | + end |
23 | 34 | end
|
24 |
| - end |
25 | 35 |
|
26 |
| - describe "#replace" do |
27 |
| - before do |
28 |
| - linked_list.add("First Value") |
29 |
| - linked_list.add("Second Value") |
| 36 | + describe "#replace" do |
| 37 | + it "returns error message if list is empty" do |
| 38 | + expect { subject.replace(0, "New Value") }.to output("Error: The list is empty\n").to_stderr |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + describe "#remove" do |
| 43 | + it "returns error message if the list is empty" do |
| 44 | + expect { subject.remove(0) }.to output("Error: The list is empty\n").to_stderr |
| 45 | + end |
30 | 46 | end
|
31 | 47 |
|
32 |
| - it "replaces the value at a given index" do |
33 |
| - linked_list.replace(1, "New First Value") |
34 |
| - expect(linked_list.get(1)).to eq "New First Value" |
| 48 | + describe "#inspect" do |
| 49 | + it "prints the linked list" do |
| 50 | + expect(subject.inspect).to eq [] |
| 51 | + end |
35 | 52 | end
|
36 | 53 |
|
37 |
| - it "returns error message if index out of bounds" do |
38 |
| - expect(linked_list.replace(10, "New Value")).to eq "INDEX 10 OUT OF BOUNDS" |
| 54 | + describe "#size" do |
| 55 | + it "returns 0 if the linked list is empty" do |
| 56 | + expect(subject.size).to eq 0 |
| 57 | + end |
39 | 58 | end
|
40 | 59 | end
|
41 | 60 |
|
42 |
| - describe "#remove" do |
| 61 | + context "whit nodes already in the list" do |
43 | 62 | before do
|
44 |
| - linked_list.add("First Value") |
45 |
| - linked_list.add("Second Value") |
| 63 | + subject.append("First Value") |
| 64 | + subject.append("Second Value") |
46 | 65 | end
|
47 | 66 |
|
48 |
| - it "removes the value at a given index" do |
49 |
| - linked_list.remove(0) |
50 |
| - expect(linked_list.get(0)).to eq "Second Value" |
| 67 | + describe "#append" do |
| 68 | + it "adds a value to the enf of the list" do |
| 69 | + expect(subject.append("Third Value")).to eq "Success: Value 'Third Value' was appended" |
| 70 | + expect(subject.head.value).to eq "First Value" |
| 71 | + expect(subject.tail.value).to eq "Third Value" |
| 72 | + expect(subject.size).to eq 3 |
| 73 | + end |
51 | 74 | end
|
52 | 75 |
|
53 |
| - it "returns error message if index out of bounds" do |
54 |
| - expect(linked_list.remove(10)).to eq "INDEX 10 OUT OF BOUNDS" |
| 76 | + describe "#prepend" do |
| 77 | + it "adds a value at the start of the list" do |
| 78 | + expect(subject.prepend("New first value")).to eq "Success: Value 'New first value' was prepended" |
| 79 | + expect(subject.head.value).to eq "New first value" |
| 80 | + expect(subject.tail.value).to eq "Second Value" |
| 81 | + end |
55 | 82 | end
|
56 |
| - end |
57 | 83 |
|
58 |
| - describe "#inspect" do |
59 |
| - before do |
60 |
| - linked_list.add("First Value") |
61 |
| - linked_list.add("Second Value") |
| 84 | + describe "#isert_at" do |
| 85 | + it "inserts the node on index 0" do |
| 86 | + expect(subject.size).to eq 2 |
| 87 | + expect(subject.insert_at(0, "New node")).to eq "Success: Value 'New node' was inserted at index 0" |
| 88 | + expect(subject.head.value).to eq "New node" |
| 89 | + expect(subject.size).to eq 3 |
| 90 | + end |
| 91 | + |
| 92 | + it "inserts the node on the last index" do |
| 93 | + expect(subject.size).to eq 2 |
| 94 | + expect(subject.insert_at(2, "New node")).to eq "Success: Value 'New node' was inserted at index 2" |
| 95 | + expect(subject.tail.value).to eq "New node" |
| 96 | + expect(subject.size).to eq 3 |
| 97 | + end |
| 98 | + |
| 99 | + it "inserts the new node in the middle" do |
| 100 | + expect(subject.size).to eq 2 |
| 101 | + expect(subject.insert_at(1, "New node")).to eq "Success: Value 'New node' was inserted at index 1" |
| 102 | + expect(subject.head.value).to eq "First Value" |
| 103 | + expect(subject.tail.value).to eq "Second Value" |
| 104 | + expect(subject.size).to eq 3 |
| 105 | + end |
62 | 106 | end
|
63 | 107 |
|
64 |
| - it "prints the linked list" do |
65 |
| - expect(linked_list.inspect).to eq ["First Value", "Second Value"] |
| 108 | + describe "#get" do |
| 109 | + it "gets the value at a given index" do |
| 110 | + expect(subject.get(1)).to eq "Second Value" |
| 111 | + end |
| 112 | + |
| 113 | + it "returns error message if index out of bounds" do |
| 114 | + expect { subject.get(10) }.to output("Index 10 out of bounds\n").to_stderr |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + describe "#replace" do |
| 119 | + it "replaces the value at a given index" do |
| 120 | + subject.replace(1, "New First Value") |
| 121 | + expect(subject.get(1)).to eq "New First Value" |
| 122 | + end |
| 123 | + |
| 124 | + it "returns error message if index out of bounds" do |
| 125 | + expect { subject.replace(10, "New Value") }.to output("Index 10 out of bounds\n").to_stderr |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + describe "#remove" do |
| 130 | + it "removes the value at a given index" do |
| 131 | + subject.remove(0) |
| 132 | + expect(subject.get(0)).to eq "Second Value" |
| 133 | + end |
| 134 | + |
| 135 | + it "returns error message if index out of bounds" do |
| 136 | + expect { subject.remove(10) }.to output("Index 10 out of bounds\n").to_stderr |
| 137 | + end |
66 | 138 | end
|
67 |
| - end |
68 | 139 |
|
69 |
| - describe "#size" do |
70 |
| - it "returns the size of the linked list" do |
71 |
| - linked_list.add("First Value") |
72 |
| - linked_list.add("Second Value") |
73 |
| - linked_list.add("Third Value") |
74 |
| - expect(linked_list.size).to eq 3 |
| 140 | + describe "#inspect" do |
| 141 | + it "prints the linked list" do |
| 142 | + expect(subject.inspect).to eq ["First Value", "Second Value"] |
| 143 | + end |
75 | 144 | end
|
76 | 145 |
|
77 |
| - it "returns 0 if the linked list is empty" do |
78 |
| - expect(linked_list.size).to eq 0 |
| 146 | + describe "#size" do |
| 147 | + it "returns the size of the linked list" do |
| 148 | + subject.append("Third Value") |
| 149 | + expect(subject.size).to eq 3 |
| 150 | + end |
79 | 151 | end
|
80 | 152 | end
|
81 | 153 | end
|
0 commit comments