packages feed

jsonlogic-hs-0.1.0.0: test/community/control/if.json

[
    "Boolean checks",
    {
        "description": "true is true, and uses the then",
        "rule": { "if": [true, "apple", "banana"] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "false is false, and uses the fallback",
        "rule": { "if": [false, "apple", "banana"] },
        "data": null,
        "result": "banana"
    },
    "Null check",
    {
        "description": "null is falsy",
        "rule": { "if": [[], "apple", "banana"] },
        "data": null,
        "result": "banana"
    },
    "Object checks",
    {
        "description": "Objects are always truthy, even without keys.",
        "rule": { "if": [{}, "apple", "banana"] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "Objects are always truthy, with keys defined",
        "rule": { "if": [{ "val": [] }, "apple", "banana"] },
        "data": { "some": "value" },
        "result": "apple"
    },
    "Array checks",
    {
        "description": "Empty array is falsy",
        "rule": { "if": [[], "apple", "banana"] },
        "data": null,
        "result": "banana"
    },
    {
        "description": "Array with single element is truthy",
        "rule": { "if": [[1], "apple", "banana" ] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "Array with multiple elements is truthy",
        "rule": { "if": [[1, 2, 3, 4], "apple", "banana" ] },
        "data": null,
        "result": "apple"
    },
    "String checks",
    {
        "description": "Empty string is falsy",
        "rule": { "if": ["", "apple", "banana"] },
        "data": null,
        "result": "banana"
    },
    {
        "description": "Any non-empty string is truthy",
        "rule": { "if": ["zucchini", "apple", "banana"] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "Any non-empty string is truthy, even 0",
        "rule": { "if": ["0", "apple", "banana"] },
        "data": null,
        "result": "apple"
    },
    "Too few arguments",
    { 
        "description": "No arguments is just null.", 
        "rule": { "if": [] }, 
        "data": null, 
        "result": null 
    },
    {
        "description": "If no then is defined when true, it'll just return null.",
        "rule": { "if": [true] },
        "data": null,
        "result": true
    },
    {
        "description": "If no fallback is defined when falsy, it'll just return null.",
        "rule": { "if": [false] },
        "data": null,
        "result": false
    },
    {
        "description": "If no then is defined when truthy, it'll just return null.",
        "rule": { "if": ["apple"] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "When truthy, it'll return the 'then' even if no fallback is defined.",
        "rule": { "if": [true, "apple"] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "When falsy, it'll return null if the fallback is not defined, even if the 'then' is defined.",
        "rule": { "if": [false, "apple"] },
        "data": null,
        "result": null
    },
    "Variadic Tests",
    {
        "description": "Returns the first truthy if condition",
        "rule": { "if": [true, "apple", false, "banana"] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "Returns the first truthy then condition, even if multiple conditions are true",
        "rule": { "if": [true, "apple", true, "banana"] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "Retuns the first truthy condition, with the fallback defined.",
        "rule": { "if": [true, "apple", false, "banana", "carrot"] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "Returns the first truthy condition, with the fallback defined; multiple true conditions.",
        "rule": { "if": [true, "apple", true, "banana", "carrot"] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "Returns the first truthy condition, with the first condition falsy. Fallback defined.",
        "rule": { "if": [false, "apple", true, "banana", "carrot"] },
        "data": null,
        "result": "banana"
    },
    {
        "description": "Returns the first truthy condition, in this case it's the 2nd one.",
        "rule": { "if": [false, "apple", true, "banana"] },
        "data": null,
        "result": "banana"
    },
    {
        "description": "When none of the conditions are true, it returns the fallback.",
        "rule": { "if": [false, "apple", false, "banana", "carrot"] },
        "data": null,
        "result": "carrot"
    },
    {
        "description": "When none of the conditions are true, it returns the fallback. In this case the fallback is not defined, so it is null.",
        "rule": { "if": [false, "apple", false, "banana"] },
        "data": null,
        "result": null
    },
    {
        "description": "3 Conditions, all falsy. Returns fallback.",
        "rule": { "if": [false, "apple", false, "banana", false, "carrot", "date"] },
        "data": null,
        "result": "date"
    },
    {
        "description": "3 Conditions, all falsy, no fallback defined. Returns null.",
        "rule": { "if": [false, "apple", false, "banana", false, "carrot"] },
        "data": null,
        "result": null
    },
    {
        "description": "First condition truthy, 3 conditions.",
        "rule": { "if": [true, "apple", false, "banana", false, "carrot", "date"] },
        "data": null,
        "result": "apple"
    },

    {
        "description": "2nd Condition is Truthy",
        "rule": { "if": [false, "apple", true, "banana", false, "carrot", "date"] },
        "data": null,
        "result": "banana"
    },
    {
        "description": "Third Condition is Truthy",
        "rule": { "if": [false, "apple", false, "banana", true, "carrot", "date"] },
        "data": null,
        "result": "carrot"
    },
    {
        "description": "Returns first truthy value, some conditions truthy",
        "rule": { "if": [false, "apple", true, "banana", true, "carrot", "date"] },
        "data": null,
        "result": "banana"
    },
    {
        "description": "Returns first truthy value, some conditions truthy (2)",
        "rule": { "if": [true, "apple", false, "banana", true, "carrot", "date"] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "Returns first truthy value, some conditions truthy (3)",
        "rule": { "if": [true, "apple", true, "banana", false, "carrot", "date"] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "Returns first truthy value, all conditions truthy",
        "rule": { "if": [true, "apple", true, "banana", true, "carrot", "date"] },
        "data": null,
        "result": "apple"
    },
    "Some Variadic Checks with Other Types",
    {
        "description": "Returns first truthy value, some conditions truthy (4)",
        "rule": { "if": [false, "apple", [1], "banana", true, "carrot", "date"] },
        "data": null,
        "result": "banana"
    },   
    {
        "description": "Returns first truthy value, some conditions truthy (5)",
        "rule": { "if": [false, "apple", "This is true", "banana", true, "carrot", "date"] },
        "data": null,
        "result": "banana"
    },   
    {
        "description": "Returns first truthy value, some conditions truthy (6)",
        "rule": { "if": [null, "apple", 0, "banana", 7, "carrot", "date"] },
        "data": null,
        "result": "carrot"
    },
    {
        "description": "Returns first truthy value, some conditions truthy (7)",
        "rule": { "if": ["0", "apple", 0, "banana", 7, "carrot", "date"] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "Returns first truthy value, some conditions truthy (8)",
        "rule": { "if": [{}, "apple", 0, "banana", 7, "carrot", "date"] },
        "data": null,
        "result": "apple"
    },
    "Bad Arguments",
    {
        "description": "If with non-array arguments throws",
        "rule": { "if": "apple" },
        "data": null,
        "error": { "type": "Invalid Arguments" }
    },
    "Short circuiting check",
    {
        "description": "If will not evaluate then branch if the condition is false",
        "rule": { "if": [false, { "throw": "Not Lazy" }, "banana"] },
        "data": null,
        "result": "banana"
    },
    {
        "description": "If will not evaluate fallback branch if the condition is true",
        "rule": { "if": [true, "apple", { "throw": "Not Lazy" }] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "If will not evaluate other branches or checks if a prior condition is true",
        "rule": { "if": [false, { "throw": "Not Lazy" }, true, "apple", { "throw": "Not Lazy" }, { "throw": "Not Lazy" }, { "throw": "Not Lazy" }] },
        "data": null,
        "result": "apple"
    },
    {
        "description": "No branches except the fallback check will be evaluated if the condition is false",
        "rule": { "if": [false, { "throw": "Not Lazy" }, false, { "throw": "Not Lazy" }, false, { "throw": "Not Lazy" }, "carrot"] },
        "data": null,
        "result": "carrot"
    }
]