diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-05-10 10:56:07 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-02 14:23:21 +0000 |
| commit | 5bf873bef2e90110fc80039ef5ebe2d5b9d80b42 (patch) | |
| tree | c94b099d75ad27f75803fb2a115c0bd3186ce73a | |
| parent | make tostring Scope deterministic and test it (diff) | |
| download | alive-5bf873bef2e90110fc80039ef5ebe2d5b9d80b42.tar.gz alive-5bf873bef2e90110fc80039ef5ebe2d5b9d80b42.zip | |
test alv.type pp
| -rw-r--r-- | alv/type.moon | 11 | ||||
| -rw-r--r-- | spec/type_spec.moon | 18 |
2 files changed, 24 insertions, 5 deletions
diff --git a/alv/type.moon b/alv/type.moon index 5c76b86..4367bd2 100644 --- a/alv/type.moon +++ b/alv/type.moon @@ -52,7 +52,12 @@ class Type -- -- @type Primitive class Primitive extends Type - pp: (value) => tostring value + pp: (value) => + switch @name + when 'str' + string.format '%q', value + else + tostring value __eq: (other) => @name == other.name __tostring: => @name @@ -70,12 +75,12 @@ class Primitive extends Type -- @type Struct class Struct extends Type pp: (value) => - inner = table.concat ["#{k}: #{@types[k]\pp v}" for k, v in opairs value], ', ' + inner = table.concat ["#{k}: #{@types[k]\pp v}" for k, v in opairs value], ' ' "{#{inner}}" __eq: (other) => same @types, other.types __tostring: => - inner = table.concat ["#{k}: #{v}" for k, v in opairs @types], ', ' + inner = table.concat ["#{k}: #{v}" for k, v in opairs @types], ' ' "{#{inner}}" --- create a new struct type with a subset of keys. diff --git a/spec/type_spec.moon b/spec/type_spec.moon index d33fcb4..4ec5e7e 100644 --- a/spec/type_spec.moon +++ b/spec/type_spec.moon @@ -11,6 +11,11 @@ describe 'Primitive', -> assert.is.equal 'num', tostring num assert.is.equal 'str', tostring str + it 'pretty-prints values', -> + assert.is.equal 'true', bool\pp true + assert.is.equal '4.134', num\pp 4.134 + assert.is.equal '"hello"', str\pp "hello" + it 'implements __eq sensibly', -> assert.is.equal (Primitive 'bool'), bool assert.is.equal (Primitive 'num'), num @@ -25,6 +30,11 @@ describe 'Array', -> assert.is.equal 'num[3]', tostring vec3 assert.is.equal 'my-type[3][24]', tostring Array 24, Array 3, 'my-type' + it 'pretty-prints values', -> + assert.is.equal '[1 2 3]', vec3\pp { 1, 2, 3 } + assert.is.equal '[["a" "b" "c"] ["d" "e" "f"]]', + (Array 2, Array 3, str)\pp { {'a', 'b', 'c'}, {'d', 'e', 'f'} } + it 'implements __eq sensibly', -> assert.is.equal vec3, Array 3, num assert.not.equal vec3, Array 2, num @@ -35,8 +45,12 @@ describe 'Struct', -> abc = Struct { c: num, b: num, a: num } it 'stringifies well', -> - assert.is.equal '{dur: num, note: str}', tostring play - assert.is.equal '{a: num, b: num, c: num}', tostring abc + assert.is.equal '{dur: num note: str}', tostring play + assert.is.equal '{a: num b: num c: num}', tostring abc + + it 'pretty-prints values', -> + assert.is.equal '{dur: 0.5 note: "a"}', play\pp { dur: 0.5, note: 'a' } + assert.is.equal '{a: 1 b: 2 c: 3}', abc\pp { a: 1, b: 2, c: 3 } it 'implements __eq sensibly', -> assert.is.equal play, Struct { note: str, dur: num } |
