aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/debug/reflect_test.html
blob: 1fa4c8eea5a9da429fd07fc7cd4c591001dbdaad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<html>
<!--
Copyright 2011 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.debug.reflect</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.debug.reflect');
  goog.require('goog.testing.jsunit');
  goog.require('goog.userAgent');
</script>
</head>
<body>
<script>

var typeOf = goog.debug.reflect.typeOf;
var className = goog.debug.reflect.className;

function testBasicTypes() {
  assertEquals('undefined', typeOf(undefined));
  assertEquals('null', typeOf(null));
  assertEquals('boolean', typeOf(false));
  assertEquals('number', typeOf(0));
  assertEquals('string', typeOf(''));

  assertUndefined(className(undefined));
  assertUndefined(className(null));
  assertUndefined(className(false));
  assertUndefined(className(0));
  assertUndefined(className(''));
}

function testWrapperTypes() {
  assertEquals('Boolean', typeOf(new Boolean(false)));
  assertEquals('Number', typeOf(new Number(0)));
  assertEquals('String', typeOf(new String('')));

  assertEquals('Boolean', className(Boolean));
  assertEquals('Number', className(Number));
  assertEquals('String', className(String));
}

function testBuiltInTypes() {
  assertEquals('Array', typeOf([]));
  assertEquals('Date', typeOf(new Date));
  assertEquals('Error', typeOf(new Error));
  assertEquals('Function', typeOf(function() {}));
  assertEquals('Object', typeOf({}));
  assertEquals('RegExp', typeOf(/./));

  assertEquals('Array', className(Array));
  assertEquals('Date', className(Date));
  assertEquals('Error', className(Error));
  assertEquals('Object', className(Object));
}

function testGoogProvidedTypes() {
  // Force type map reinitialization with the goog.provided types.
  delete goog.debug.reflect.typeMap_;

  goog.provide('x.a');
  x.a = function() {};
  x.a.b = function() {};

  assertEquals('Object', typeOf(x));
  assertEquals('x.a', typeOf(new x.a));
  assertEquals('Object', typeOf(new x.a.b));

  assertUndefined(className(x));
  assertEquals('x.a', className(x.a));
  assertUndefined(className(x.a.b));
}

function testUnregisteredTypes() {
  var oldIE = goog.userAgent.IE && !goog.userAgent.isVersion(8);
  assertEquals(oldIE ? 'ActiveXObject' : 'HTMLDocument', typeOf(document));

  var expectedTypeOfBody = oldIE ? 'ActiveXObject' : 'HTMLBodyElement';
  assertEquals(expectedTypeOfBody, typeOf(document.body));

  var expectedTypeOfWindow = 'Window';
  if (goog.userAgent.WEBKIT || goog.userAgent.OPERA) {
    // Note: window.constructor.name is 'DOMWindow' in Chrome but the type
    // DOMWindow is not exposed to JavaScript.
    expectedTypeOfWindow = 'Object';
  }
  if (oldIE) {
    expectedTypeOfWindow = 'ActiveXObject';
  }
  assertEquals(expectedTypeOfWindow, typeOf(window));
}

</script>
</body>
</html>