field_number_test.js
2.56 KB
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
/**
* @license
* Visual Blocks Editor
*
* Copyright 2017 Google Inc.
* https://developers.google.com/blockly/
*
* 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.
*/
/**
* @fileoverview Tests for Blockly.FieldNumber
* @author Anm@anm.me (Andrew n marshall)
*/
'use strict';
function test_fieldnumber_constructor() {
// No arguments
var field = new Blockly.FieldNumber();
assertEquals(field.getValue(), '0');
// Unlike blockly, scratch-blocks doesn't store min, max, and precision.
// TODO: Update this to check the restrictor, based on min, max, and precision.
assertEquals(field.min_, undefined);
assertEquals(field.max_, undefined);
assertEquals(field.precision_, undefined);
// Numeric values
field = new Blockly.FieldNumber(1);
assertEquals(field.getValue(), '1');
field = new Blockly.FieldNumber(1.5);
assertEquals(field.getValue(), '1.5');
// String value
field = new Blockly.FieldNumber('2');
assertEquals(field.getValue(), '2');
field = new Blockly.FieldNumber('2.5');
assertEquals(field.getValue(), '2.5');
// All values
field = new Blockly.FieldNumber(
/* value */ 0,
/* min */ -128,
/* max */ 127,
/* precision */ 1);
// Unlike blockly and scratch-blocks, openblock-blocks doesn't store recision,
// but store min and max
assertEquals(field.getValue(), '0');
assertEquals(field.min_, -128);
assertEquals(field.max_, 127);
assertEquals(field.precision_, undefined);
// Bad value defaults to '0'
field = new Blockly.FieldNumber('bad');
assertEquals(field.getValue(), '0');
field = new Blockly.FieldNumber(NaN);
assertEquals(field.getValue(), '0');
}
function test_fieldnumber_fromJson() {
assertEquals(Blockly.FieldNumber.fromJson({}).getValue(), '0');
assertEquals(Blockly.FieldNumber.fromJson({ value: 1 }).getValue(), '1');
// All options, but scratch-blocks parses min/max/precision differently from
// Blockly. See notes in field_number.js.
var field = Blockly.FieldNumber.fromJson({
value: 0,
min: -128,
max: 127,
precision: 1
});
assertEquals(field.getValue(), '0');
}