Skip to content

Commit

Permalink
Merge branch 'yaml-spec' of github.com:jwaldrip/crystal into yaml-spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jwaldrip committed Aug 20, 2017
2 parents e6f99a8 + 964438a commit bee14b7
Show file tree
Hide file tree
Showing 52 changed files with 686 additions and 172 deletions.
2 changes: 1 addition & 1 deletion bin/ci
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ prepare_system() {

build() {
with_build_env 'make std_spec clean'
with_build_env 'make crystal spec doc'
with_build_env 'make crystal std_spec compiler_spec doc'
with_build_env 'find samples -name "*.cr" | xargs -L 1 ./bin/crystal build --no-codegen'
with_build_env './bin/crystal tool format --check samples spec src'
}
Expand Down
12 changes: 12 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ describe Crystal::Formatter do
assert_format "Foo( x: Int32 )", "Foo(x: Int32)"
assert_format "Foo( x: Int32 , y: Float64 )", "Foo(x: Int32, y: Float64)"

assert_format "NamedTuple(a: Int32,)", "NamedTuple(a: Int32)"
assert_format "NamedTuple(\n a: Int32,\n)"
assert_format "NamedTuple(\n a: Int32,)", "NamedTuple(\n a: Int32,\n)"
assert_format "class Foo\n NamedTuple(\n a: Int32,\n )\nend"

assert_format "::Tuple(T)"
assert_format "::NamedTuple(T)"
assert_format "::Pointer(T)"
Expand Down Expand Up @@ -634,6 +639,13 @@ describe Crystal::Formatter do
assert_format %(asm("nop" :::: "volatile" , "alignstack" , "intel" )), %(asm("nop" :::: "volatile", "alignstack", "intel"))
assert_format %(asm("nop" ::: "eax" , "ebx" : "volatile" , "alignstack" )), %(asm("nop" ::: "eax", "ebx" : "volatile", "alignstack"))
assert_format %(asm("a" : "b"(c) : "d"(e) :: "volatile"))
assert_format %(asm("a" : "b"(c)\n)), %(asm("a" : "b"(c)))
assert_format %(asm("a" :: "d"(e)\n)), %(asm("a" :: "d"(e)))
assert_format %(asm("a" ::: "f"\n)), %(asm("a" ::: "f"))
assert_format %(asm("a" :::: "volatile"\n)), %(asm("a" :::: "volatile"))
assert_format %(asm("a" : "b"(c) : "d"(e)\n : "f"))
assert_format %(asm("a" : "b"(c) : "d"(e)\n : "f",\n "g"))
assert_format %(asm("a" ::: "a"\n : "volatile",\n "intel"))

assert_format "1 # foo\n1234 # bar", "1 # foo\n1234 # bar"
assert_format "1234 # foo\n1 # bar", "1234 # foo\n1 # bar"
Expand Down
4 changes: 1 addition & 3 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,7 @@ describe "Parser" do

it_parses "$~", Global.new("$~")
it_parses "$~.foo", Call.new(Global.new("$~"), "foo")
it_parses "$0", Call.new(Global.new("$~"), "[]", 0.int32)
it_parses "$1", Call.new(Global.new("$~"), "[]", 1.int32)
it_parses "$1?", Call.new(Global.new("$~"), "[]?", 1.int32)
it_parses "foo $1", Call.new(nil, "foo", Call.new(Global.new("$~"), "[]", 1.int32))
Expand All @@ -874,9 +875,6 @@ describe "Parser" do
it_parses "foo $?", Call.new(nil, "foo", Global.new("$?"))
it_parses "$? = 1", Assign.new("$?".var, 1.int32)

it_parses "$0", Path.global("PROGRAM_NAME")
it_parses "foo $0", Call.new(nil, "foo", Path.global("PROGRAM_NAME"))

it_parses "foo out x; x", [Call.new(nil, "foo", Out.new("x".var)), "x".var]
it_parses "foo(out x); x", [Call.new(nil, "foo", Out.new("x".var)), "x".var]
it_parses "foo out @x; @x", [Call.new(nil, "foo", Out.new("@x".instance_var)), "@x".instance_var]
Expand Down
29 changes: 29 additions & 0 deletions spec/compiler/semantic/block_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,21 @@ describe "Block inference" do
"type must be Float64"
end

it "reports error on method instantiate (#4543)" do
assert_error %(
class Foo
@foo = 42
def initialize(&block : -> Int32)
@foo = yield
end
end
Foo.new { 42u32 }
),
"expected block to return Int32, not UInt32"
end

it "matches block arg return type" do
assert_type("
class Foo(T)
Expand Down Expand Up @@ -766,6 +781,20 @@ describe "Block inference" do
)) { int32 }
end

it "ignores void return type (4)" do
assert_type(%(
alias Alias = Void
def foo(&block : -> Alias)
yield
end
foo do
1
end
)) { int32 }
end

it "uses block return type as return type, even if can't infer block type" do
assert_type(%(
class Foo
Expand Down
10 changes: 5 additions & 5 deletions spec/compiler/semantic/instance_var_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,7 @@ describe "Semantic: instance var" do
@x : Int32
end
),
"instance variable '@x' of Foo was not initialized in all of the 'initialize' methods, rendering it nilable"
"instance variable '@x' of Foo was not initialized directly in all of the 'initialize' methods, rendering it nilable. Indirect initialization is not supported."
end

it "doesn't if declaring nilable instance var and turns out to be nilable" do
Expand All @@ -1884,7 +1884,7 @@ describe "Semantic: instance var" do
@x : T
end
),
"instance variable '@x' of Foo(T) was not initialized in all of the 'initialize' methods, rendering it nilable"
"instance variable '@x' of Foo(T) was not initialized directly in all of the 'initialize' methods, rendering it nilable. Indirect initialization is not supported."
end

it "errors if declaring instance var and turns out to be nilable, in generic module type" do
Expand All @@ -1897,7 +1897,7 @@ describe "Semantic: instance var" do
include Moo(Int32)
end
),
"instance variable '@x' of Foo was not initialized in all of the 'initialize' methods, rendering it nilable"
"instance variable '@x' of Foo was not initialized directly in all of the 'initialize' methods, rendering it nilable. Indirect initialization is not supported."
end

it "doesn't error if declaring instance var and doesn't out to be nilable, in generic module type" do
Expand Down Expand Up @@ -1932,7 +1932,7 @@ describe "Semantic: instance var" do
include Moo(T)
end
),
"instance variable '@x' of Foo(T) was not initialized in all of the 'initialize' methods, rendering it nilable"
"instance variable '@x' of Foo(T) was not initialized directly in all of the 'initialize' methods, rendering it nilable. Indirect initialization is not supported."
end

it "doesn't error if not initializing variables but calling super" do
Expand Down Expand Up @@ -4467,7 +4467,7 @@ describe "Semantic: instance var" do
Foo.new
),
"instance variable '@y' of Foo was not initialized in all of the 'initialize' methods, rendering it nilable"
"instance variable '@y' of Foo was not initialized directly in all of the 'initialize' methods, rendering it nilable. Indirect initialization is not supported."
end

it "errors if finally not initialized in macro def" do
Expand Down
47 changes: 47 additions & 0 deletions spec/compiler/semantic/macro_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1122,4 +1122,51 @@ describe "Semantic: macro" do
Moo.bar
), inject_primitives: false) { int32 }
end

it "finds metaclass instance of instance method (#4739)" do
assert_type(%(
class Parent
macro foo
def self.bar
1
end
end
end
class Child < Parent
def foo
end
end
class GrandChild < Child
foo
end
GrandChild.bar
)) { int32 }
end

it "finds metaclass instance of instance method (#4639)" do
assert_type(%(
module Include
macro foo
def foo
1
end
end
end
class Parent
include Include
foo
end
class Foo < Parent
foo
end
Foo.new.foo
)) { int32 }
end
end
6 changes: 3 additions & 3 deletions spec/compiler/semantic/new_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe "Semantic: new" do
My.new("foo")
),
"instance variable '@caps' of My was not initialized in all of the 'initialize' methods, rendering it nilable"
"instance variable '@caps' of My was not initialized directly in all of the 'initialize' methods, rendering it nilable. Indirect initialization is not supported."
end

it "errors if using self call in default argument (2)" do
Expand All @@ -170,7 +170,7 @@ describe "Semantic: new" do
My.new("foo")
),
"instance variable '@caps' of My was not initialized in all of the 'initialize' methods, rendering it nilable"
"instance variable '@caps' of My was not initialized directly in all of the 'initialize' methods, rendering it nilable. Indirect initialization is not supported."
end

it "errors if using self call in default argument (3)" do
Expand All @@ -189,7 +189,7 @@ describe "Semantic: new" do
My.new("foo")
),
"instance variable '@caps' of My was not initialized in all of the 'initialize' methods, rendering it nilable"
"instance variable '@caps' of My was not initialized directly in all of the 'initialize' methods, rendering it nilable. Indirect initialization is not supported."
end

it "inherits initialize and new methods if doesn't define new (#3238)" do
Expand Down
2 changes: 1 addition & 1 deletion spec/compiler/semantic/nilable_instance_var_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe "Semantic: nilable instance var" do
Foo.new.foo
),
"instance variable '@foo' of Foo was not initialized in all of the 'initialize' methods, rendering it nilable"
"instance variable '@foo' of Foo was not initialized directly in all of the 'initialize' methods, rendering it nilable. Indirect initialization is not supported."
end

it "says instance var was used before initialized" do
Expand Down
4 changes: 4 additions & 0 deletions spec/std/big/big_float_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ describe "BigFloat" do
it { "12345678901234567".to_big_f.to_s.should eq("12345678901234567") }
end

describe "#inspect" do
it { "2.3".to_big_f.inspect.should eq("2.3_big_f") }
end

it "#hash" do
b = 123.to_big_f
b.hash.should eq(b.to_f64.hash)
Expand Down
4 changes: 4 additions & 0 deletions spec/std/big/big_int_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ describe "BigInt" do
a.to_s(32).should eq(d)
end

describe "#inspect" do
it { "2".to_big_i.inspect.should eq("2_big_i") }
end

it "does gcd and lcm" do
# 3 primes
a = BigInt.new("48112959837082048697")
Expand Down
20 changes: 20 additions & 0 deletions spec/std/float_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ describe "Float" do
end
end

describe "#inspect" do
it "does inspect for f64" do
3.2.inspect.should eq("3.2")
end

it "does inspect for f32" do
3.2_f32.inspect.should eq("3.2_f32")
end

it "does inspect for f64 with IO" do
str = String.build { |io| 3.2.inspect(io) }
str.should eq("3.2")
end

it "does inspect for f32" do
str = String.build { |io| 3.2_f32.inspect(io) }
str.should eq("3.2_f32")
end
end

describe "hash" do
it "does for Float32" do
1.2_f32.hash.should_not eq(0)
Expand Down
21 changes: 21 additions & 0 deletions spec/std/int_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,27 @@ describe "Int" do
end
end

describe "#inspect" do
it "appends the type" do
23.inspect.should eq("23")
23_i8.inspect.should eq("23_i8")
23_i16.inspect.should eq("23_i16")
-23_i64.inspect.should eq("-23_i64")
23_u8.inspect.should eq("23_u8")
23_u16.inspect.should eq("23_u16")
23_u32.inspect.should eq("23_u32")
23_u64.inspect.should eq("23_u64")
end

it "appends the type using IO" do
str = String.build { |io| 23.inspect(io) }
str.should eq("23")

str = String.build { |io| -23_i64.inspect(io) }
str.should eq("-23_i64")
end
end

describe "bit" do
it { 5.bit(0).should eq(1) }
it { 5.bit(1).should eq(0) }
Expand Down
27 changes: 27 additions & 0 deletions spec/std/json/builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ private def assert_built(expected)
string.should eq(expected)
end

private class TestObject
def to_json(builder)
{"int" => 12}.to_json(builder)
end
end

describe JSON::Builder do
it "writes null" do
assert_built("null") do
Expand Down Expand Up @@ -251,4 +257,25 @@ describe JSON::Builder do
json.end_document
end
end

it "writes field with scalar in object" do
assert_built(%<{"int":42,"float":0.815,"null":null,"bool":true,"string":"string"}>) do
object do
field "int", 42
field "float", 0.815
field "null", nil
field "bool", true
field "string", "string"
end
end
end

it "writes field with arbitrary value in object" do
assert_built(%<{"hash":{"hash":"value"},"object":{"int":12}}>) do
object do
field "hash", {"hash" => "value"}
field "object", TestObject.new
end
end
end
end
Loading

0 comments on commit bee14b7

Please sign in to comment.