assertions.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. require('utils/errors/definitions');
  20. /**
  21. * Method to check custom statement and
  22. * throws custom error if statement is false
  23. *
  24. * @param desc
  25. * @param test
  26. * @param ErrorObject
  27. */
  28. App.assert = function(desc, test, ErrorObject) {
  29. if (!test) {
  30. ErrorObject = ErrorObject || Error;
  31. throw new ErrorObject(desc ? ' Info: ' + desc : '');
  32. }
  33. };
  34. /**
  35. * Check if passed variable should be not null
  36. *
  37. * @param object
  38. * @param desc
  39. */
  40. App.assertExists = function(object, desc) {
  41. App.assert(desc, !Em.isNone(object), App.NotNullTypeError);
  42. };
  43. /**
  44. * Check if passed variable is object
  45. *
  46. * @param object
  47. * @param desc
  48. */
  49. App.assertObject = function(object, desc) {
  50. App.assert(desc, (typeof object === 'object') && object, App.ObjectTypeError);
  51. };
  52. /**
  53. * Check if variable is instance of ember object
  54. *
  55. * @param object
  56. * @param desc
  57. */
  58. App.assertEmberObject = function(object, desc) {
  59. App.assert(desc, Em.typeOf(object) === 'instance', App.EmberObjectTypeError);
  60. };
  61. /**
  62. * Check if variable is array
  63. *
  64. * @param object
  65. * @param desc
  66. */
  67. App.assertArray = function(object, desc) {
  68. App.assert(desc, Em.isArray(object), App.ArrayTypeError);
  69. };
  70. /**
  71. * Check if variable is function
  72. *
  73. * @param object
  74. * @param desc
  75. */
  76. App.assertFunction = function(object, desc) {
  77. App.assert(desc, Em.typeOf(object) === 'function', App.FunctionTypeError);
  78. };