如何在YUI3中使用Assert.isUndefine()和Assert.isNotUndefine()?

发布于 2024-10-11 02:00:14 字数 1572 浏览 3 评论 0原文

我正在尝试使用 YUI3 测试框架测试是否存在已声明、已定义的函数。在 Safari 和 FireFox 中,尝试使用 isNotUndefined、isUndefined 或 isFunction 失败。我希望它们抛出可由测试框架处理的异常。

 Y
 Object
 Y.Assert
 Object
 Y.Assert.isNotUndefined(x, "fail message")
 ReferenceError: Can't find variable: x
 Y.Assert.isUndefined(x, "fail message")
 ReferenceError: Can't find variable: x
 Y.Assert.isFunction(x, "fail message")
 ReferenceError: Can't find variable: x

但是,相反,我从来没有看到失败消息,并且由于解释器的阻碍,其余的测试也不会运行......这是否破坏了这些函数的目的,或者我误解了框架?

我的直觉告诉我,给定上面的代码并且只有上面的代码,

  Y.Assert.isUndefined(x, "fail message")

应该继续执行而不会出现错误(因为 x 未声明),并且

  Y.Assert.isNotUndefined(x, "fail message")

应该记录消息“失败消息”(因为 x 未声明)。

但是,由于 ReferenceError,无法(使用那些 YUI3 方法)测试未声明的对象。相反,我留下了一些非常丑陋的断言代码。我不能使用

 Y.Assert.isNotUndefined(x)
 ReferenceError: Can't find variable: x

or ,

 Y.assert( x !== undefined )
 ReferenceError: Can't find variable: x

这让我的

 Y.assert( typeof(x) !== "undefined" ) // the only working option I've found
 Assert Error: Assertion failed.

可读性比 更差

 Y.Assert.isNotUndefined(x)

再次,我问:这不会破坏这些函数的目的,还是我误解了框架?

so

 x

未声明,因此不可直接测试,而 while

 var x;

声明了它,但未定义。最后

 var x = function() {};

既被声明又被定义。

能力

 Y.Assert.isNotUndeclared(x);

我认为我缺少的是轻松地说出“-Wil”的

I'm trying to test for the presence of a declared, defined function using the YUI3 Test framework. In Safari and FireFox, trying to use isNotUndefined, isUndefined, or isFunction fails. I expect those to throw an exception that can be handled by the test framework.

 Y
 Object
 Y.Assert
 Object
 Y.Assert.isNotUndefined(x, "fail message")
 ReferenceError: Can't find variable: x
 Y.Assert.isUndefined(x, "fail message")
 ReferenceError: Can't find variable: x
 Y.Assert.isFunction(x, "fail message")
 ReferenceError: Can't find variable: x

But, instead, I never get to see the failure messages, and the remainder of the tests do not run, because of the interpreter getting in the way... Doesn't that undermine the purpose of these functions, or am I misunderstanding the framework?

My intuition tells me that, given the code above and only the code above,

  Y.Assert.isUndefined(x, "fail message")

should continue on without an error (because x is undeclared) and

  Y.Assert.isNotUndefined(x, "fail message")

should log the message "fail message" (because x is undeclared).

However, because of the ReferenceError, there's no way (using those YUI3 methods) to test for undeclared objects. Instead, I'm left with some pretty ugly assertion code. I can't use

 Y.Assert.isNotUndefined(x)
 ReferenceError: Can't find variable: x

or

 Y.assert( x !== undefined )
 ReferenceError: Can't find variable: x

which leaves me with

 Y.assert( typeof(x) !== "undefined" ) // the only working option I've found
 Assert Error: Assertion failed.

which is much less readable than

 Y.Assert.isNotUndefined(x)

Again, I ask: Doesn't that undermine the purpose of these functions, or am I misunderstanding the framework?

So

 x

is undeclared, and so not directly testable, while

 var x;

declares it, but leaves it undefined. Finally

 var x = function() {};

is both declared and defined.

I think that what's missing for me is the ability to easily say

 Y.Assert.isNotUndeclared(x);

-Wil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-10-18 02:00:14

好的,昨天有点晚了,我现在明白你的问题了,你想做的是检查变量是否被定义,对吧?

执行此操作的唯一方法是 typeof x === 'undefined'

typeof 运算符允许使用不存在的变量。

因此,为了使其工作,您需要上述表达式并将其插入到正常的 true/false 断言中。

例如(没有使用过YUI3):

Y.Assert.isTrue(typeof x === 'undefined', "fail message"); // isUndefined
Y.Assert.isFalse(typeof x === 'undefined', "fail message"); // isNotUndefined

OK, was a bit late yesterday guess I understand you question now, what you want to do is to check whether a variable was defined at all, right?

The only way to do this is typeof x === 'undefined'.

The typeof operator allows for non-existent variables to be used with it.

So in order to it to work, you need the above expression and plug that into a normal true/false assert.

For example (haven't used YUI3):

Y.Assert.isTrue(typeof x === 'undefined', "fail message"); // isUndefined
Y.Assert.isFalse(typeof x === 'undefined', "fail message"); // isNotUndefined
随风而去 2024-10-18 02:00:14

上下文:我希望能够区分:

x // undeclared && undefined
var x; // declared && undefined
var x = 5; // declared && defined

因此,这里的挑战是 JavaScript 无法轻易区分前两种情况,而我希望能够出于教学目的做到这一点。经过大量的玩耍和阅读后,似乎确实有一种方法可以做到这一点,至少在网络浏览器的上下文中和全局变量(没有很大的限制,但是......):

function isDeclared(objName) {
  return ( window.hasOwnProperty(objName) ) ? true : false;
}

function isDefined(objName) {
  return ( isDeclared(objName) && ("undefined" !== typeof eval(objName)) ) ? true : false;
}

我意识到使用 eval可能不安全,但对于我使用这些函数的严格控制的上下文来说,这是可以的。所有其他人请注意并参阅 http://www.jslint.com/lint.html

isDeclared("x")  // false
isDefined("x")   // false

var x;
isDeclared("x")  // true
isDefined("x")   // false

var x = 5;
isDeclared("x")  // true
isDefined("x")   // true

CONTEXT: I want to be able to distinguish among:

x // undeclared && undefined
var x; // declared && undefined
var x = 5; // declared && defined

So, the challenge here is that JavaScript doesn't readily distinguish between the first 2 cases, which I wanted to be able to do for teaching purposes. After a lot of playing and reading, there does seem to be a way to do it, at least within the context of a web browser and for global variables (not great restrictions, but...):

function isDeclared(objName) {
  return ( window.hasOwnProperty(objName) ) ? true : false;
}

function isDefined(objName) {
  return ( isDeclared(objName) && ("undefined" !== typeof eval(objName)) ) ? true : false;
}

I realize that the use of eval could be unsafe, but for the tightly controlled context in which I would use these functions, it's OK. All others, beware and see http://www.jslint.com/lint.html

isDeclared("x")  // false
isDefined("x")   // false

var x;
isDeclared("x")  // true
isDefined("x")   // false

var x = 5;
isDeclared("x")  // true
isDefined("x")   // true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文