Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Last active December 10, 2015 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-hiroshi/d32967a5438b32512ea0 to your computer and use it in GitHub Desktop.
Save s-hiroshi/d32967a5438b32512ea0 to your computer and use it in GitHub Desktop.
JavaScript > PhantomJS + Qunitのサンプル
module("Validation");
test("string a is true", function() {
var value = 'a';
equal(Validation.isNotScalar(value), true, 'isNotScaler return ture when argument is string not empty');
});
test("undefined is false", function() {
var value;
equal(Validation.isNotScalar(value), false, 'isNotScaler return false when argument is undefined');
});
test("empty string is false", function() {
var value = {};
equal(Validation.isNotScalar(value), false, 'isNotScaler return false when argument is empty string');
});
test("empty string is false", function() {
var value = '';
equal(Validation.isNotScalar(value), false, 'isNotScaler return false when argument is empty string');
});
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="keywords" content="">
<link rel="stylesheet" href="qunit.css" media="all">
<script src="qunit.js"></script>
<script src="../validation/validation.js"></script>
<script src="test.js"></script>
</head>
<body>
<h1 id="qunit-header">QUnit example</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</form>
</body>
</html>
/**
* 入力チェック
*
*/
function Validation() {}
/**
* String型(空文字''を除く)、Number型以外はfalse。
*
* @method isNotScalar
* @param value
* @return {Boolean}
* 引数なしで呼び出されたときはfalseを返す。
* Number, String出ないときはfaliseを返す。
*/
Validation.isNotScalar = function (value) {
// Boolean
if (typeof value === 'boolean') {
return false;
}
// Object
if (typeof value === 'object') {
return false;
}
// Undefined
if (typeof value === 'undefined') {
return false;
}
// Null
if (value === null) {
return false;
}
// 空文字
if (value === '') {
return false;
}
return true;
};
/**
* 0より大きいかをチェック
*
* @method isMoreThan0
* @param {Number} num
* @return {Boolean}
* 0より大きいときはtrue。それ以外はfalseを返す。
* 引数なしで呼び出したときはfalse
*/
Validation.isMoreThan0 = function(num) {
// キャスト失敗はNaNを返す(エラーではない)
// undefined, 空文字, nullはNaNを返す
var result = parseInt(num, 10);
// 数字以外
if (isNaN(result)) {
return false;
}
// 0以下
if ((num <= 0) === true) {
return false;
}
// 0より大きい
return true;
};
/**
* 0以上かをチェック
*
* @method is0AndMore
* @param {Number} num
* 0以上はtrue。それ以外はfalseを返す。
* 引数なしで呼び出したときはfalse
*/
Validation.is0AndMore = function(num) {
// キャスト失敗はNaNを返す(エラーではない)
// undefined, 空文字, nullはNaNを返す
var result = parseInt(num, 10);
// 数字以外
if (isNaN(result)) {
return false;
}
// 0未満
if ((num < 0) === true) {
return false;
}
// 0以上
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment