} else if (is_val<value_string>(property)) {
auto key = property->as_string().str();
JJ_DEBUG("Accessing %s built-in '%s'", is_val<value_array>(object) ? "array" : "string", key.c_str());
- val = try_builtin_func(ctx, key, object);
+ val = try_builtin_func(ctx, key, object, true);
} else {
throw std::runtime_error("Cannot access property with non-string/non-number: got " + property->type());
}
throw std::runtime_error("Cannot access property with non-string: got " + property->type());
}
auto key = property->as_string().str();
- val = try_builtin_func(ctx, key, object);
+ val = try_builtin_func(ctx, key, object, true);
}
if (ctx.is_get_stats && val && object && property) {
virtual int64_t as_int() const override { return val_int; }
virtual double as_float() const override { return static_cast<double>(val_int); }
virtual string as_string() const override { return std::to_string(val_int); }
+ virtual bool as_bool() const override {
+ return val_int != 0;
+ }
virtual const func_builtins & get_builtins() const override;
};
using value_int = std::shared_ptr<value_int_t>;
if (out.back() == '.') out.push_back('0'); // leave one zero if no decimals
return out;
}
+ virtual bool as_bool() const override {
+ return val_flt != 0.0;
+ }
virtual const func_builtins & get_builtins() const override;
};
using value_float = std::shared_ptr<value_float_t>;
json::object(),
"yes"
);
+
+ test_template(t, "is undefined falsy",
+ "{{ 'yes' if not y else 'no' }}",
+ json::object(),
+ "yes"
+ );
+
+ test_template(t, "is undefined attribute falsy",
+ "{{ 'yes' if not y.x else 'no' }}",
+ {{"y", true}},
+ "yes"
+ );
+
+ test_template(t, "is undefined key falsy",
+ "{{ 'yes' if not y['x'] else 'no' }}",
+ {{"y", {{}}}},
+ "yes"
+ );
+
+ test_template(t, "is empty array falsy",
+ "{{ 'yes' if not y else 'no' }}",
+ {{"y", json::array()}},
+ "yes"
+ );
+
+ test_template(t, "is empty object falsy",
+ "{{ 'yes' if not y else 'no' }}",
+ {{"y", json::object()}},
+ "yes"
+ );
+
+ test_template(t, "is empty string falsy",
+ "{{ 'yes' if not y else 'no' }}",
+ {{"y", ""}},
+ "yes"
+ );
+
+ test_template(t, "is 0 falsy",
+ "{{ 'yes' if not y else 'no' }}",
+ {{"y", 0}},
+ "yes"
+ );
+
+ test_template(t, "is 0.0 falsy",
+ "{{ 'yes' if not y else 'no' }}",
+ {{"y", 0.0}},
+ "yes"
+ );
+
+ test_template(t, "is non-empty array truthy",
+ "{{ 'yes' if y else 'no' }}",
+ {{"y", json::array({""})}},
+ "yes"
+ );
+
+ test_template(t, "is non-empty object truthy",
+ "{{ 'yes' if y else 'no' }}",
+ {{"y", {"x", false}}},
+ "yes"
+ );
+
+ test_template(t, "is non-empty string truthy",
+ "{{ 'yes' if y else 'no' }}",
+ {{"y", "0"}},
+ "yes"
+ );
+
+ test_template(t, "is 1 truthy",
+ "{{ 'yes' if y else 'no' }}",
+ {{"y", 1}},
+ "yes"
+ );
+
+ test_template(t, "is 1.0 truthy",
+ "{{ 'yes' if y else 'no' }}",
+ {{"y", 1.0}},
+ "yes"
+ );
}
static void test_loops(testing & t) {