File: registry_test.js

package info (click to toggle)
aseba-plugin-blockly 20180211%2Bgit-2
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (143 lines) | stat: -rw-r--r-- 5,300 bytes parent folder | download | duplicates (2)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

goog.provide('goog.tweak.RegistryTest');
goog.setTestOnly('goog.tweak.RegistryTest');

goog.require('goog.asserts.AssertionError');
goog.require('goog.testing.jsunit');
goog.require('goog.tweak');
/** @suppress {extraRequire} needed for createRegistryEntries. */
goog.require('goog.tweak.testhelpers');

var registry;

function setUp() {
  createRegistryEntries('');
  registry = goog.tweak.getRegistry();
}

function tearDown() {
  goog.tweak.registry_ = null;
}

function testGetBaseEntry() {
  // Initial values
  assertEquals(
      'wrong bool1 object', boolEntry, registry.getBooleanSetting('Bool'));
  assertEquals(
      'wrong string object', strEntry, registry.getStringSetting('Str'));
  assertEquals(
      'wrong numeric object', numEntry, registry.getNumericSetting('Num'));
  assertEquals('wrong button object', buttonEntry, registry.getEntry('Button'));
  assertEquals(
      'wrong button object', boolGroup, registry.getEntry('BoolGroup'));
}

function testInitializeFromQueryParams() {
  var testCase = 0;
  function assertQuery(
      queryStr, boolValue, enumValue, strValue, subBoolValue, subBoolValue2) {
    createRegistryEntries(queryStr);
    assertEquals(
        'Wrong bool value for query: ' + queryStr, boolValue,
        boolEntry.getValue());
    assertEquals(
        'Wrong enum value for query: ' + queryStr, enumValue,
        strEnumEntry.getValue());
    assertEquals(
        'Wrong str value for query: ' + queryStr, strValue,
        strEntry.getValue());
    assertEquals(
        'Wrong BoolOne value for query: ' + queryStr, subBoolValue,
        boolOneEntry.getValue());
    assertEquals(
        'Wrong BoolTwo value for query: ' + queryStr, subBoolValue2,
        boolTwoEntry.getValue());
  }
  assertQuery('?dummy=1&bool=&enum=&s=', false, '', '', false, true);
  assertQuery('?bool=0&enum=A&s=a', false, 'A', 'a', false, true);
  assertQuery('?bool=1&enum=A', true, 'A', '', false, true);
  assertQuery('?bool=true&enum=B&s=as+df', true, 'B', 'as df', false, true);
  assertQuery('?enum=C', false, 'C', '', false, true);
  assertQuery('?enum=C&boolgroup=-booltwo', false, 'C', '', false, false);
  assertQuery('?enum=C&boolgroup=b1,-booltwo', false, 'C', '', true, false);
  assertQuery('?enum=C&boolgroup=b1', false, 'C', '', true, true);
  assertQuery('?s=a+b%20c%26', false, 'A', 'a b c&', false, true);
}

function testMakeUrlQuery() {
  assertEquals('All values are default.', '', registry.makeUrlQuery(''));
  assertEquals(
      'All values are default - with existing params.', '?super=pudu',
      registry.makeUrlQuery('?super=pudu'));

  boolEntry.setValue(true);
  numEnumEntry.setValue(2);
  strEntry.setValue('f o&o');
  assertEquals(
      'Wrong query string 1.', '?bool=1&enum2=2&s=f+o%26o',
      registry.makeUrlQuery('?bool=1'));
  assertEquals(
      'Wrong query string 1 - with existing params.',
      '?super=pudu&bool=1&enum2=2&s=f+o%26o',
      registry.makeUrlQuery('?bool=0&s=g&super=pudu'));

  boolOneEntry.setValue(true);
  assertEquals(
      'Wrong query string 2.', '?bool=1&boolgroup=B1&enum2=2&s=f+o%26o',
      registry.makeUrlQuery(''));

  boolTwoEntry.setValue(false);
  assertEquals(
      'Wrong query string 3.',
      '?bool=1&boolgroup=B1,-booltwo&enum2=2&s=f+o%26o',
      registry.makeUrlQuery(''));
}

function testOverrideDefaultValue_calledBefore() {
  registry.overrideDefaultValue('b', false);
  registry.overrideDefaultValue('b', true);
  goog.tweak.registerBoolean('b', 'b desc');
  var bEntry = registry.getEntry('b');
  assertTrue('Default value should be true.', bEntry.getDefaultValue());
  assertTrue('Value should be true.', bEntry.getValue());
}

function testOverrideDefaultValue_calledAfter() {
  var exception = assertThrows('Should assert.', function() {
    registry.overrideDefaultValue('Bool2', false);
  });
  assertTrue(
      'Wrong exception', exception instanceof goog.asserts.AssertionError);
}

function testCompilerOverrideDefaultValue() {
  createRegistryEntries('', {'b': true});
  registry = goog.tweak.getRegistry();
  goog.tweak.registerBoolean('b', 'b desc');
  var bEntry = registry.getEntry('b');
  assertTrue('Default value should be true.', bEntry.getDefaultValue());
  assertTrue('Value should be true.', bEntry.getValue());
}

function testCompilerAndJsOverrideDefaultValue() {
  createRegistryEntries('', {'b': false});
  registry = goog.tweak.getRegistry();
  registry.overrideDefaultValue('b', true);
  goog.tweak.registerBoolean('b', 'b desc', true);
  var bEntry = registry.getEntry('b');
  assertFalse('Default value should be false.', bEntry.getDefaultValue());
  assertFalse('Value should be false.', bEntry.getValue());
}