Skip to content

Commit

Permalink
Merge pull request #27 from instedd/regression-tests
Browse files Browse the repository at this point in the history
Add some regression tests

For #25
  • Loading branch information
Andrés Atencio authored Nov 20, 2020
2 parents d72ed4f + a02c53d commit 7608feb
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 12 deletions.
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ version: "3.8"
services:
web:
image: crystallang/crystal:0.35.1
command: crystal run app/twiliosim.cr
command: crystal run src/twiliosim.cr
ports:
- 3000
volumes:
- ./src:/app
- .:/app
working_dir: /app
ngrok:
image: wernight/ngrok
ports:
Expand Down
1 change: 1 addition & 0 deletions pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose run --rm web crystal spec
32 changes: 32 additions & 0 deletions spec/models/ao_message_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "spec"
require "../../src/models/ao_message"

include Twiliosim

describe AOMessage do
describe "#message" do
it "initializes OK" do
ao_message = AOMessage.new("foo", "bar")

ao_message.message.should eq "foo"
end
end

describe "#redirect_url" do
it "initializes OK" do
ao_message = AOMessage.new("foo", "bar")

ao_message.redirect_url.should eq "bar"
end
end

describe "#to_s" do
it "returns the string representation" do
ao_message = AOMessage.new("foo", "bar")

str = ao_message.to_s

str.should eq "AOMessage - bar - foo"
end
end
end
74 changes: 74 additions & 0 deletions spec/models/call_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require "spec"
require "../../src/models/call"

include Twiliosim

describe Call do
describe "#status" do
it "initializes created" do
call = Call.new("foo", "bar", "baz")

call.status.should eq "created"
end

it "starts in-progress" do
call = Call.new("foo", "bar", "baz")

call.start

call.status.should eq "in-progress"
end

it "finished completed" do
call = Call.new("foo", "bar", "baz")

call.finish

call.status.should eq "completed"
end
end

describe "#id" do
it "initializes with a new (random) UUID" do
call_0 = Call.new("foo_0", "bar_1", "baz_2")
call_1 = Call.new("foo_1", "bar_1", "baz_1")
uuid_size = 36

call_0.id.size.should eq uuid_size
call_1.id.size.should eq uuid_size
call_0.id.should_not eq call_1.id
end
end

describe "#to" do
it "initializes OK" do
call = Call.new("foo", "bar", "baz")

call.to.should eq "foo"
end
end

describe "#from" do
it "initializes OK" do
call = Call.new("foo", "bar", "baz")

call.from.should eq "bar"
end
end

describe "#account_sid" do
it "initializes OK" do
call = Call.new("foo", "bar", "baz")

call.account_sid.should eq "baz"
end
end

describe "#no_reply" do
it "initializes OK" do
call = Call.new("foo", "bar", "baz")

call.no_reply.should eq false
end
end
end
65 changes: 65 additions & 0 deletions spec/models/reply_command_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "spec"
require "../../src/models/ao_message"
require "../../src/models/reply_command"

include Twiliosim

describe PressDigits do
describe "#ao_message" do
it "initializes OK" do
ao_message = AOMessage.new("foo", "bar")
foo = 28

reply_command = PressDigits.new(ao_message, foo)

reply_command.ao_message.should eq ao_message
end
end

describe "#digits" do
it "initializes OK" do
ao_message = AOMessage.new("foo", "bar")
foo = 53

reply_command = PressDigits.new(ao_message, foo)

reply_command.digits.should eq foo
end
end

describe "#to_s" do
it "returns the string representation" do
ao_message = AOMessage.new("foo", "bar")
foo = 95
reply_command = PressDigits.new(ao_message, foo)

str = reply_command.to_s

str.should eq "PressDigits < ReplyCommand #{foo}"
end
end
end

describe HangUp do
describe "#ao_message" do
it "initializes OK" do
ao_message = AOMessage.new("foo", "bar")
foo = 28

reply_command = HangUp.new(ao_message)

reply_command.ao_message.should eq ao_message
end
end

describe "#to_s" do
it "returns the string representation" do
ao_message = AOMessage.new("foo", "bar")
reply_command = HangUp.new(ao_message)

str = reply_command.to_s

str.should eq "HangUp < ReplyCommand"
end
end
end
140 changes: 140 additions & 0 deletions spec/models/simulator_command_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
require "spec"
require "../../src/models/simulator_command"
require "../../src/config"

include Twiliosim

describe SimulatorCommand do
describe "#parse" do
it "returns nil when invalid" do
simulator_command = SimulatorCommand.parse("foo")

simulator_command.should eq nil
end

it "returns a OneOfCommand" do
message = "#oneof:1,2"

simulator_command = SimulatorCommand.parse(message)

simulator_command.is_a?(OneOfCommand).should eq true
simulator_command.should eq OneOfCommand.parse(message)
end

it "returns a NumericCommand" do
message = "#numeric:1-2"

simulator_command = SimulatorCommand.parse(message)

simulator_command.is_a?(NumericCommand).should eq true
simulator_command.should eq NumericCommand.parse(message)
end
end

describe OneOfCommand do
describe "#choices" do
it "initializes OK" do
foo = [1, 2]

simulator_command = OneOfCommand.new(foo)

simulator_command.choices.should eq foo
end
end

describe "#parse" do
it "returns a OneOfCommand initialized OK" do
foo = [1, 2]
control_command = OneOfCommand.new(foo)
message = "#oneof:1,2"

parsed_command = OneOfCommand.parse(message)

parsed_command.should eq control_command
end
end

describe "#valid_sample" do
it "returns a sample in choices" do
choices = [1, 2]
simulator_command = OneOfCommand.new(choices)

valid_sample = simulator_command.valid_sample

choices.includes?(valid_sample).should eq true
end
end

describe "#invalid_sample" do
it "returns a sample not in choices" do
choices = [1, 2]
simulator_command = OneOfCommand.new(choices)

invalid_sample = simulator_command.invalid_sample(Config.load)

choices.includes?(invalid_sample).should eq false
end
end
end

describe NumericCommand do
describe "#min" do
it "initializes OK" do
min = 1
max = 2

simulator_command = NumericCommand.new(min, max)

simulator_command.min.should eq min
end
end

describe "#max" do
it "initializes OK" do
min = 1
max = 2

simulator_command = NumericCommand.new(min, max)

simulator_command.max.should eq max
end
end

describe "#parse" do
it "returns a NumericCommand initialized OK" do
min = 1
max = 2
control_command = NumericCommand.new(min, max)
message = "#numeric:#{min}-#{max}"

parsed_command = NumericCommand.parse(message)

parsed_command.should eq control_command
end
end

describe "#valid_sample" do
it "returns a sample in range" do
min = 1
max = 2
simulator_command = NumericCommand.new(min, max)

valid_sample = simulator_command.valid_sample

(min..max).to_a.includes?(valid_sample).should eq true
end
end

describe "#invalid_sample" do
it "returns a sample not in range" do
min = 1
max = 2
simulator_command = NumericCommand.new(min, max)

invalid_sample = simulator_command.invalid_sample(Config.load)

(min..max).to_a.includes?(invalid_sample).should eq false
end
end
end
end
2 changes: 0 additions & 2 deletions spec/spec_helper.cr

This file was deleted.

4 changes: 0 additions & 4 deletions spec/twiliosim_spec.cr

This file was deleted.

8 changes: 4 additions & 4 deletions src/models/simulator_command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ struct Twiliosim::OneOfCommand < Twiliosim::SimulatorCommand

def invalid_sample(config : Twiliosim::Config) : Int32 | Nil
# restrict candidates by config
invalid_candidates = (0..config.max_incorrect_reply_value)
invalid_candidates = (0..config.max_incorrect_reply_value).to_a
# restrict candidates by command
invalid_candidates = invalid_candidates.to_a.reject! { |x| @choices.includes?(x) }
invalid_candidates = invalid_candidates.reject! { |x| @choices.includes?(x) }
# pick a random candidate
invalid_candidates.sample unless invalid_candidates.empty?
end
Expand All @@ -59,9 +59,9 @@ struct Twiliosim::NumericCommand < Twiliosim::SimulatorCommand

def invalid_sample(config : Twiliosim::Config) : Int32 | Nil
# restrict candidates by config
invalid_candidates = (0..config.max_incorrect_reply_value)
invalid_candidates = (0..config.max_incorrect_reply_value).to_a
# restrict candidates by command
invalid_candidates = invalid_candidates.to_a.reject!(@min..@max)
invalid_candidates = invalid_candidates.reject!(@min..@max)
# pick a random candidate
invalid_candidates.sample unless invalid_candidates.empty?
end
Expand Down

0 comments on commit 7608feb

Please sign in to comment.