From 8df3b854939a63b14aab3fd4688b0caea3daf1dc Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 27 Jul 2022 11:39:50 +0800 Subject: adding test cases. --- spec/inputs/test/class_spec.yue | 418 ++++++++++++++--------------- spec/inputs/test/loops_spec.yue | 13 +- spec/inputs/test/table_spreading_spec.yue | 22 ++ spec/outputs/test/table_spreading_spec.lua | 36 +++ 4 files changed, 272 insertions(+), 217 deletions(-) create mode 100644 spec/inputs/test/table_spreading_spec.yue create mode 100644 spec/outputs/test/table_spreading_spec.lua (limited to 'spec') diff --git a/spec/inputs/test/class_spec.yue b/spec/inputs/test/class_spec.yue index 394c59b..df8bc96 100644 --- a/spec/inputs/test/class_spec.yue +++ b/spec/inputs/test/class_spec.yue @@ -1,297 +1,295 @@ - describe "class", -> - it "should make a class with constructor", -> - class Thing - new: => - @color = "blue" - - instance = Thing! + it "should make a class with constructor", -> + class Thing + new: => + @color = "blue" - assert.same instance, { color: "blue" } + instance = Thing! - it "should have instance methods", -> - class Thing - get_color: => @color + assert.same instance, { color: "blue" } - new: => - @color = "blue" + it "should have instance methods", -> + class Thing + get_color: => @color - instance = Thing! - assert.same instance\get_color!, "blue" + new: => + @color = "blue" - it "should have base properies from class", -> - class Thing - color: "blue" - get_color: => @color + instance = Thing! + assert.same instance\get_color!, "blue" - instance = Thing! - assert.same instance\get_color!, "blue" - assert.same Thing.color, "blue" + it "should have base properies from class", -> + class Thing + color: "blue" + get_color: => @color - it "should inherit another class", -> - class Base - get_property: => @[@property] + instance = Thing! + assert.same instance\get_color!, "blue" + assert.same Thing.color, "blue" - new: (@property) => + it "should inherit another class", -> + class Base + get_property: => @[@property] - class Thing extends Base - color: "green" + new: (@property) => - instance = Thing "color" - assert.same instance\get_property!, "green" + class Thing extends Base + color: "green" + instance = Thing "color" + assert.same instance\get_property!, "green" - it "should have class properties", -> - class Base - class Thing extends Base - instance = Thing! + it "should have class properties", -> + class Base + class Thing extends Base - assert.same Base.__name, "Base" - assert.same Thing.__name, "Thing" - assert.is_true Thing.__parent == Base + instance = Thing! - assert.is_true instance.__class == Thing + assert.same Base.__name, "Base" + assert.same Thing.__name, "Thing" + assert.is_true Thing.__parent == Base - it "should have name when assigned", -> - Thing = class - assert.same Thing.__name, "Thing" + assert.is_true instance.__class == Thing - it "should not expose class properties on instance", -> - class Thing - @height: 10 + it "should have name when assigned", -> + Thing = class + assert.same Thing.__name, "Thing" - Thing.color = "blue" + it "should not expose class properties on instance", -> + class Thing + @height: 10 - instance = Thing! - assert.same instance.color, nil - assert.same instance.height, nil + Thing.color = "blue" - it "should expose new things added to __base", -> - class Thing + instance = Thing! + assert.same instance.color, nil + assert.same instance.height, nil - instance = Thing! - Thing.__base.color = "green" + it "should expose new things added to __base", -> + class Thing - assert.same instance.color, "green" + instance = Thing! + Thing.__base.color = "green" - it "should call with correct receiver", -> - local instance + assert.same instance.color, "green" - class Thing - is_class: => assert.is_true @ == Thing - is_instance: => assert.is_true @ == instance + it "should call with correct receiver", -> + local instance - go: => - @@is_class! - @is_instance! + class Thing + is_class: => assert.is_true @ == Thing + is_instance: => assert.is_true @ == instance - instance = Thing! - instance\go! + go: => + @@is_class! + @is_instance! - it "should have class properies take precedence over base properties", -> - class Thing - @prop: "hello" - prop: "world" + instance = Thing! + instance\go! - assert.same "hello", Thing.prop + it "should have class properies take precedence over base properties", -> + class Thing + @prop: "hello" + prop: "world" - describe "super", -> - it "should call super constructor", -> - class Base - new: (@property) => + assert.same "hello", Thing.prop - class Thing extends Base - new: (@name) => - super "name" + describe "super", -> + it "should call super constructor", -> + class Base + new: (@property) => - instance = Thing "the_thing" + class Thing extends Base + new: (@name) => + super "name" - assert.same instance.property, "name" - assert.same instance.name, "the_thing" + instance = Thing "the_thing" - it "should call super method", -> - class Base - _count: 111 - counter: => @_count + assert.same instance.property, "name" + assert.same instance.name, "the_thing" - class Thing extends Base - counter: => "%08d"\format super! + it "should call super method", -> + class Base + _count: 111 + counter: => @_count - instance = Thing! - assert.same instance\counter!, "00000111" + class Thing extends Base + counter: => "%08d"\format super! - it "should call other method from super", -> - class Base - _count: 111 - counter: => - @_count + instance = Thing! + assert.same instance\counter!, "00000111" - class Thing extends Base - other_method: => super\counter! + it "should call other method from super", -> + class Base + _count: 111 + counter: => + @_count - instance = Thing! - assert.same instance\other_method!, 111 + class Thing extends Base + other_method: => super\counter! - it "should get super class", -> - class Base - class Thing extends Base - get_super: => super + instance = Thing! + assert.same instance\other_method!, 111 - instance = Thing! - assert.is_true instance\get_super! == Base + it "should get super class", -> + class Base + class Thing extends Base + get_super: => super - it "should get a bound method from super", -> - class Base - count: 1 - get_count: => @count + instance = Thing! + assert.is_true instance\get_super! == Base - class Thing extends Base - get_count: => "this is wrong" - get_method: => super\get_count + it "should get a bound method from super", -> + class Base + count: 1 + get_count: => @count - instance = Thing! - assert.same instance\get_method!!, 1 + class Thing extends Base + get_count: => "this is wrong" + get_method: => super\get_count - it "class properties take precedence in super class over base", -> - class Thing - @prop: "hello" - prop: "world" + instance = Thing! + assert.same instance\get_method!!, 1 - class OtherThing extends Thing + it "class properties take precedence in super class over base", -> + class Thing + @prop: "hello" + prop: "world" - assert.same "hello", OtherThing.prop + class OtherThing extends Thing - it "gets value from base in super class", -> - class Thing - prop: "world" + assert.same "hello", OtherThing.prop - class OtherThing extends Thing - assert.same "world", OtherThing.prop + it "gets value from base in super class", -> + class Thing + prop: "world" - it "should let parent be replaced on class", -> - class A - @prop: "yeah" - cool: => 1234 - plain: => "a" + class OtherThing extends Thing + assert.same "world", OtherThing.prop - class B - @prop: "okay" - cool: => 9999 - plain: => "b" + it "should let parent be replaced on class", -> + class A + @prop: "yeah" + cool: => 1234 + plain: => "a" - class Thing extends A - cool: => - super! + 1 + class B + @prop: "okay" + cool: => 9999 + plain: => "b" - get_super: => - super + class Thing extends A + cool: => + super! + 1 - instance = Thing! + get_super: => + super - assert.same "a", instance\plain! - assert.same 1235, instance\cool! - assert A == instance\get_super!, "expected super to be B" + instance = Thing! - Thing.__parent = B - setmetatable Thing.__base, B.__base + assert.same "a", instance\plain! + assert.same 1235, instance\cool! + assert A == instance\get_super!, "expected super to be B" - assert.same "b", instance\plain! - assert.same 10000, instance\cool! - assert B == instance\get_super!, "expected super to be B" + Thing.__parent = B + setmetatable Thing.__base, B.__base - it "should resolve many levels of super", -> - class One - a: => - 1 + assert.same "b", instance\plain! + assert.same 10000, instance\cool! + assert B == instance\get_super!, "expected super to be B" - class Two extends One - a: => - super! + 2 + it "should resolve many levels of super", -> + class One + a: => + 1 - class Three extends Two - a: => - super! + 3 + class Two extends One + a: => + super! + 2 - i = Three! + class Three extends Two + a: => + super! + 3 - assert.same 6, i\a! + i = Three! + assert.same 6, i\a! - it "should resolve many levels of super with a gap", -> - class One - a: => - 1 - class Two extends One + it "should resolve many levels of super with a gap", -> + class One + a: => + 1 - class Three extends Two - a: => - super! + 3 + class Two extends One - class Four extends Three - a: => - super! + 4 + class Three extends Two + a: => + super! + 3 - i = Four! + class Four extends Three + a: => + super! + 4 - assert.same 8, i\a! + i = Four! + assert.same 8, i\a! - it "should call correct class/instance super methods", -> - class Base - doit: => - "instance" - @doit: => - "class" + it "should call correct class/instance super methods", -> + class Base + doit: => + "instance" - class One extends Base - doit: => super! - @doit: => super! + @doit: => + "class" - assert.same "instance", One!\doit! - assert.same "class", One\doit! + class One extends Base + doit: => super! + @doit: => super! + assert.same "instance", One!\doit! + assert.same "class", One\doit! - it "should resolve many levels of super on class methods", -> - class One - @a: => - 1 - class Two extends One + it "should resolve many levels of super on class methods", -> + class One + @a: => + 1 - class Three extends Two - @a: => - super! + 3 + class Two extends One - class Four extends Three - @a: => - super! + 4 + class Three extends Two + @a: => + super! + 3 - assert.same 8, Four\a! + class Four extends Three + @a: => + super! + 4 - it "super should still work when method wrapped", -> - add_some = (opts) -> - => opts.amount + opts[1] @ + assert.same 8, Four\a! - class Base - value: => 1 + it "super should still work when method wrapped", -> + add_some = (opts) -> + => opts.amount + opts[1] @ - class Sub extends Base - value: add_some { - amount: 12 - => - super! + 100 - } + class Base + value: => 1 - class OtherSub extends Base - value: if true - => 5 + super! - else - => 2 + super! + class Sub extends Base + value: add_some { + amount: 12 + => + super! + 100 + } - assert.same 1 + 100 + 12, Sub!\value! - assert.same 6, OtherSub!\value! + class OtherSub extends Base + value: if true + => 5 + super! + else + => 2 + super! + assert.same 1 + 100 + 12, Sub!\value! + assert.same 6, OtherSub!\value! diff --git a/spec/inputs/test/loops_spec.yue b/spec/inputs/test/loops_spec.yue index 68b5f97..16531ef 100644 --- a/spec/inputs/test/loops_spec.yue +++ b/spec/inputs/test/loops_spec.yue @@ -1,9 +1,8 @@ - describe "loops", -> - it "should continue", -> - input = {1,2,3,4,5,6} - output = for x in *input - continue if x % 2 == 1 - x + it "should continue", -> + input = {1,2,3,4,5,6} + output = for x in *input + continue if x % 2 == 1 + x - assert.same output, { 2,4,6 } + assert.same output, { 2,4,6 } diff --git a/spec/inputs/test/table_spreading_spec.yue b/spec/inputs/test/table_spreading_spec.yue new file mode 100644 index 0000000..e7942cc --- /dev/null +++ b/spec/inputs/test/table_spreading_spec.yue @@ -0,0 +1,22 @@ +describe "table spreading", -> + it "list and dict", -> + template = { + foo: "Hello" + bar: "World" + "!" + } + + specialized = { + "a", "b", "c" + ...template + bar: "Bob" + } + + assert.same specialized, { + "a" + "b" + "c" + "!" + foo: "Hello" + bar: "Bob" + } diff --git a/spec/outputs/test/table_spreading_spec.lua b/spec/outputs/test/table_spreading_spec.lua new file mode 100644 index 0000000..fd90867 --- /dev/null +++ b/spec/outputs/test/table_spreading_spec.lua @@ -0,0 +1,36 @@ +return describe("table spreading", function() + return it("list and dict", function() + local template = { + foo = "Hello", + bar = "World", + "!" + } + local specialized + do + local _tab_0 = { + "a", + "b", + "c" + } + local _idx_0 = 1 + for _key_0, _value_0 in pairs(template) do + if _idx_0 == _key_0 then + _tab_0[#_tab_0 + 1] = _value_0 + _idx_0 = _idx_0 + 1 + else + _tab_0[_key_0] = _value_0 + end + end + _tab_0.bar = "Bob" + specialized = _tab_0 + end + return assert.same(specialized, { + "a", + "b", + "c", + "!", + foo = "Hello", + bar = "Bob" + }) + end) +end) -- cgit v1.2.3-55-g6feb