Skip to content

Commit

Permalink
Add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Sep 7, 2019
1 parent 5c0a31e commit 6bd47f5
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ install:
- shards install

script:
- DEBUG=1 crystal spec
- crystal spec
- crystal tool format --check
- bin/ameba
91 changes: 88 additions & 3 deletions spec/debug_spec.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,94 @@
require "./spec_helper"

class Foo
end

module Bar
end

enum Baz
One
Two
Three
end

describe Debug do
# TODO: Write tests
context "with literals" do
it "works with Nil" do
assert_debug nil
end

it "works with Int" do
assert_debug 1
assert_debug 1_u64
end

it "works with Float" do
assert_debug 1.23
end

it "works with Bool" do
assert_debug true
assert_debug false
end

it "works with Char" do
assert_debug '.'
assert_debug '…'
end

it "works with String" do
assert_debug "foo"
end
end

context "with containers" do
it "works with Tuple" do
assert_debug({1, 2, 3})
end

it "works with Array" do
assert_debug [1, 2, 3]
end
end

context "with expressions" do
it do
assert_debug(1 + 2).should eq(3)
assert_debug("foo" + "bar").should eq("foobar")
end
end

context "with side-effects" do
it do
i = 1
assert_debug(i += 1)
i.should eq(2)
end

it do
arr = [:foo]
assert_debug(arr << :bar)
arr.should eq([:foo, :bar])
end
end

context "with user defined classes" do
it "works for classes" do
assert_debug Foo
end

it "works for objects" do
assert_debug Foo.new
end

it "works for modules" do
assert_debug Bar
end

it "works" do
false.should eq(true)
it "works for enums" do
assert_debug Baz
assert_debug Baz::One
end
end
end
38 changes: 38 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,2 +1,40 @@
require "spec"
require "../src/debug"

Debug.configure do |settings|
settings.location_detection = :compile
settings.max_path_length = nil
end

macro assert_debug(exp, *, file = __FILE__, line = __LINE__)
%ret = nil

%previous_logger = Debug.logger
begin
IO::Memory.new.tap do |io|
Debug.logger = Debug::Logger.new(io)
Debug.logger.tap do |logger|
%ret = debug!(value = {{ exp }})

case value
when Value then %ret.should eq(value)
when Reference then %ret.should be(value)
end
%ret.should be_a(typeof(value))

if Debug::ACTIVE && Debug.enabled?
relative_filename = {{ file }}.lchop(Dir.current + "/")

io.to_s.should contain "#{relative_filename}:{{ line }}"
io.to_s.should contain {{ exp.stringify }}
else
io.to_s.should be_empty
end
end
end
ensure
Debug.logger = %previous_logger
end

%ret
end

0 comments on commit 6bd47f5

Please sign in to comment.