inspect | = | "DefaultBehavior Case" |
contains behavior related to the case statement
-
none
- case(value, [elseCode] nil, +[whensAndThens])
- case:and(+args)
- case:else()
- case:nand(first, +args)
- case:nor(first, +args)
- case:not(other)
- case:or(+args)
- case:otherwise()
- case:xor(first, +args)
takes one argument that should evaluate to a value, zero or more whenAndThen pairs and one optional else clause. will first evaluate the initial value, then check each whenAndThen pair against this value. if the when part of a pair returns true, then return the result of evaluating the then part. if no pair matches and no else clause is present, returns nil. if an else clause is there, it should be the last one. each whenAndThen pair is comprised of two arguments, where the first is the when argument and the second is the then argument. the when part will be evaluated and the result of this evaluation will be sent a === message with the value as argument.
- - should only evaluate the first value once
[ show source ]
Ground caseTests = [] case((caseTests <<(:evaluated) 44), 1 ..(42), :one, 43 ..(45), :two) caseTests should ==([](:evaluated))
- - should only evaluate the when part of each entry until something returns true
[ show source ]
Ground caseTests = [] case(42, (caseTests <<(:one) 1 ..(30)), :onex, (caseTests <<(:two) 2 ..(40)), :twox, (caseTests <<(:three) 3 ..(44)), :threex, (caseTests <<(:four) 4 ..(45)), :fourx) caseTests should ==([](:one, :two, :three))
- - should return the value of the then part that succeeds
[ show source ]
case(42, 1 ..(10), :one, 10 ..(30), :two, 30 ..(50), :three, 50 ..(60), :four) should ==(:three)
- - should return nil if no part matches
[ show source ]
case(42) should be nil case(42, 1 ..(10), :one) should be nil
- - should execute the last statement if there is no when part for it
[ show source ]
case(42, 1 ..(10), :one, 42 +(2)) should ==(44)
- - should call === with the result of the condition on each when part
[ show source ]
Ground caseTests = [] obj = Origin mimic obj cell("===") = method(other, caseTests <<(other) false) case(44 *(2), obj, "blah", obj, "bluh" ) caseTests should ==([](88, 88))
- - should transform the when part to call cond:name if it exists
[ show source ]
Ground blargCalled = [] blarg = method(Ground blargCalled <<(:true), 1 ..(10)) case(5, blarg, :foo) should ==(:foo) blargCalled should ==([](:true)) Ground wowserCalled = [] DefaultBehavior Case cell("case:caseTestWowser") = method(+(args), wowserCalled <<(:mix) 1 ...(10)) case(5, caseTestWowser, :foo) should ==(:foo) case(5, caseTestWowser(1, 2, 3), :foo) should ==(:foo) wowserCalled should ==([](:mix, :mix)) case(5, caseTestWowser(1, caseTestWowser(44), 3), :foo) should ==(:foo) wowserCalled should ==([](:mix, :mix, :mix, :mix))
nil
[ show source ]
method(+args, DefaultBehavior Case AndCombiner with(components: args))
- - should return an object that calls === on each argument and returns true if all are true
[ show source ]
Ground calledWith = [] x = Origin mimic y = Origin mimic x === = method(other, Ground calledWith <<([](:x, other)) true) y === = method(other, Ground calledWith <<([](:y, other)) true) case:and(x, y) should ===(42) Ground calledWith should ==([]([](:x, 42), [](:y, 42))) case:and(1 ..(5), 3 ..(4)) should ===(4) x === = method(other, Ground calledWith <<([](:x, other)) false) Ground calledWith = [] (case:and(x, y) ===(43)) should be false Ground calledWith should ==([]([](:x, 43)))
- - should be possible to use within a case-expression
[ show source ]
case(42, and(1 ..(50), 40 ..(45)), :foo) should ==(:foo) case(42, and(and(40 ..(43)), 40 ..(45)), :foo) should ==(:foo) case(42, and(and(and(40 ..(43), 10 ...(12))), 40 ..(45)), :foo) should be nil
nil
[ show source ]
method( DefaultBehavior Case Else)
- - should not take any arguments
[ show source ]
fn(case:else(1)) should signal(Condition Error Invocation TooManyArguments)
- - should return an object that returns true from ===
[ show source ]
(case:else ===(:x)) should be true (case:else ===(nil)) should be true (case:else ===(42)) should be true
nil
[ show source ]
method(first, +args, DefaultBehavior Case NAndCombiner with(first: first, components: args))
- - should take at least one argument
[ show source ]
fn(case:nand) should signal(Condition Error Invocation TooFewArguments)
- - should return an object that fulfills the nand protocal when called with ===
[ show source ]
(case:nand(1 ..(5)) ===(2)) should be false (case:nand(1 ..(5)) ===(6)) should be true (case:nand(1 ..(5), 1 ...(3)) ===(2)) should be false (case:nand(1 ..(5), 1 ...(2)) ===(6)) should be true (case:nand(1 ..(5), 1 ..(7)) ===(6)) should be true (case:nand(1 ..(7), 1 ..(5)) ===(6)) should be true (case:nand(1 ..(5), 1 ...(3), 2 ..(3)) ===(2)) should be false (case:nand(1 ..(5), 1 ...(2), 1 ..(3)) ===(6)) should be true (case:nand(1 ..(5), 1 ..(7), 1 ..(3)) ===(6)) should be true (case:nand(1 ..(7), 1 ..(5), 1 ..(3)) ===(6)) should be true (case:nand(1 ..(5), 1 ...(2), 1 ..(100)) ===(6)) should be true (case:nand(1 ..(5), 1 ..(7), 1 ..(100)) ===(6)) should be true (case:nand(1 ..(7), 1 ..(5), 1 ..(100)) ===(6)) should be true
- - should be possible to use within a case-expression
[ show source ]
case(42, nand(1 ..(30)), :foo) should ==(:foo) case(42, nand(nand(1 ..(50), 43 ..(50)), 42 ..(43)), :foo) should be nil case(42, nand(nand(1 ..(50), 40 ..(50)), 43 ..(44)), :foo) should ==(:foo)
nil
[ show source ]
method(first, +args, DefaultBehavior Case NOrCombiner with(first: first, components: args))
- - should take at least one argument
[ show source ]
fn(case:nor) should signal(Condition Error Invocation TooFewArguments)
- - should return an object that fulfills the nor protocal when called with ===
[ show source ]
(case:nor(1 ..(5)) ===(2)) should be false (case:nor(1 ..(5)) ===(6)) should be true (case:nor(1 ..(5), 1 ...(3)) ===(2)) should be false (case:nor(1 ..(5), 1 ...(2)) ===(6)) should be true (case:nor(1 ..(5), 1 ..(7)) ===(6)) should be false (case:nor(1 ..(7), 1 ..(5)) ===(6)) should be false (case:nor(1 ..(5), 1 ...(3), 2 ..(3)) ===(2)) should be false (case:nor(1 ..(5), 1 ...(2), 1 ..(3)) ===(6)) should be true (case:nor(1 ..(5), 1 ..(7), 1 ..(3)) ===(6)) should be false (case:nor(1 ..(7), 1 ..(5), 1 ..(3)) ===(6)) should be false (case:nor(1 ..(5), 1 ...(2), 1 ..(100)) ===(6)) should be false (case:nor(1 ..(5), 1 ..(7), 1 ..(100)) ===(6)) should be false (case:nor(1 ..(7), 1 ..(5), 1 ..(100)) ===(6)) should be false
- - should be possible to use within a case-expression
[ show source ]
case(42, nor(1 ..(30)), :foo) should ==(:foo) case(42, nor(nor(1 ..(50), 43 ..(50)), 42 ..(43)), :foo) should be nil case(42, nor(nor(1 ..(50), 40 ..(50)), 43 ..(44)), :foo) should ==(:foo)
nil
[ show source ]
method(other, DefaultBehavior Case NotCombiner with(other: other))
- - should take exactly one argument
[ show source ]
fn(case:not) should signal(Condition Error Invocation TooFewArguments) fn(case:not(1, 2)) should signal(Condition Error Invocation TooManyArguments)
- - should return an object that when calling === on it will return the inverse of the argument to it
[ show source ]
(case:not(1 ..(5)) ===(0)) should be true (case:not(1 ..(5)) ===(1)) should be false (case:not(1 ..(5)) ===(2)) should be false (case:not(1 ..(5)) ===(3)) should be false (case:not(1 ..(5)) ===(4)) should be false (case:not(1 ..(5)) ===(5)) should be false (case:not(1 ..(5)) ===(6)) should be true (case:not(1 ...(5)) ===(0)) should be true (case:not(1 ...(5)) ===(1)) should be false (case:not(1 ...(5)) ===(2)) should be false (case:not(1 ...(5)) ===(3)) should be false (case:not(1 ...(5)) ===(4)) should be false (case:not(1 ...(5)) ===(5)) should be true (case:not(1 ...(5)) ===(6)) should be true
- - should be possible to use within a case-expression
[ show source ]
case(42, not(1 ..(30)), :foo) should ==(:foo) case(42, not(not(not(30 ..(33)))), :foo) should ==(:foo) case(42, not(not(30 ..(33))), :foo) should be nil
nil
[ show source ]
method(+args, DefaultBehavior Case OrCombiner with(components: args))
- - should return an object that calls === on each argument and returns true if at least one are true
[ show source ]
Ground calledWith = [] x = Origin mimic y = Origin mimic x === = method(other, Ground calledWith <<([](:x, other)) true) y === = method(other, Ground calledWith <<([](:y, other)) true) (case:or(x, y) ===(42)) should be true Ground calledWith should ==([]([](:x, 42))) (case:or(1 ..(3), 3 ..(4)) ===(4)) should be true x === = method(other, Ground calledWith <<([](:x, other)) false) (case:or(x, x) ===(43)) should be false Ground calledWith = [] (case:or(x, y) ===(43)) should be true Ground calledWith should ==([]([](:x, 43), [](:y, 43)))
- - should be possible to use within a case-expression
[ show source ]
case(42, or(1 ..(40), 40 ..(45)), :foo) should ==(:foo) case(42, or(or(50 ..(53)), 40 ..(45)), :foo) should ==(:foo) case(42, or(or(or(30 ..(33), 10 ...(12))), 40 ..(41)), :foo) should be nil
nil
[ show source ]
method( DefaultBehavior Case Else)
- - should not take any arguments
[ show source ]
fn(case:otherwise(1)) should signal(Condition Error Invocation TooManyArguments)
- - should return an object that returns true from ===
[ show source ]
(case:otherwise ===(:x)) should be true (case:otherwise ===(nil)) should be true (case:otherwise ===(42)) should be true
nil
[ show source ]
method(first, +args, DefaultBehavior Case XOrCombiner with(first: first, components: args))
- - should take at least one argument
[ show source ]
fn(case:xor) should signal(Condition Error Invocation TooFewArguments)
- - should return an object that fulfills the xor protocal when called with ===
[ show source ]
(case:xor(1 ..(5)) ===(2)) should be true (case:xor(1 ..(5)) ===(6)) should be false (case:xor(1 ..(5), 1 ...(3)) ===(2)) should be false (case:xor(1 ..(5), 1 ...(2)) ===(6)) should be false (case:xor(1 ..(5), 1 ..(7)) ===(6)) should be true (case:xor(1 ..(7), 1 ..(5)) ===(6)) should be true (case:xor(1 ..(5), 1 ...(3), 2 ..(3)) ===(2)) should be false (case:xor(1 ..(5), 1 ...(2), 1 ..(3)) ===(6)) should be false (case:xor(1 ..(5), 1 ..(7), 1 ..(3)) ===(6)) should be true (case:xor(1 ..(7), 1 ..(5), 1 ..(3)) ===(6)) should be true (case:xor(1 ..(5), 1 ...(2), 1 ..(100)) ===(6)) should be true (case:xor(1 ..(5), 1 ..(7), 1 ..(100)) ===(6)) should be false (case:xor(1 ..(7), 1 ..(5), 1 ..(100)) ===(6)) should be false
- - should be possible to use within a case-expression
[ show source ]
case(42, xor(1 ..(50)), :foo) should ==(:foo) case(42, xor(xor(1 ..(50), 43 ..(50)), 42 ..(43)), :foo) should be nil case(42, xor(xor(1 ..(50), 40 ..(50)), 42 ..(44)), :foo) should ==(:foo)