@@ -7,26 +7,102 @@ var suite = vows.describe("d3.interpolate");
77suite . addBatch ( {
88 "interpolate" : {
99 topic : load ( "interpolate/interpolate" ) . document ( ) ,
10- "interpolates numbers" : function ( d3 ) {
11- assert . equal ( d3 . interpolate ( 2 , 12 ) ( .4 ) , 6 ) ;
12- assert . equal ( d3 . interpolate ( "2px" , 12 ) ( .4 ) , 6 ) ;
10+
11+ "when a and b are numbers" : {
12+ "interpolates numbers" : function ( d3 ) {
13+ assert . strictEqual ( d3 . interpolate ( 2 , 12 ) ( .4 ) , 6 ) ;
14+ }
15+ } ,
16+
17+ "when a and b are color strings" : {
18+ "interpolates RGB values and returns a hexadecimal string" : function ( d3 ) {
19+ assert . strictEqual ( d3 . interpolate ( "#ff0000" , "#008000" ) ( .4 ) , "#993300" ) ;
20+ } ,
21+ "interpolates named colors in RGB" : function ( d3 ) {
22+ assert . strictEqual ( d3 . interpolate ( "red" , "green" ) ( .4 ) , "#993300" ) ;
23+ } ,
24+ "interpolates decimal RGB colors in RGB" : function ( d3 ) {
25+ assert . strictEqual ( d3 . interpolate ( "rgb(255,0,0)" , "rgb(0,128,0)" ) ( .4 ) , "#993300" ) ;
26+ } ,
27+ "interpolates decimal HSL colors in RGB" : function ( d3 ) {
28+ assert . strictEqual ( d3 . interpolate ( "hsl(0,100%,50%)" , "hsl(120,100%,25%)" ) ( .4 ) , "#993300" ) ;
29+ }
1330 } ,
14- "interpolates colors" : function ( d3 ) { // beware instanceof d3_Color
15- assert . equal ( d3 . interpolate ( "#abcdef" , "#fedcba" ) ( .4 ) , "#ccd3da" ) ;
16- assert . equal ( d3 . interpolate ( "#abcdef" , d3 . rgb ( "#fedcba" ) ) ( .4 ) , "#ccd3da" ) ;
17- assert . equal ( d3 . interpolate ( "#abcdef" , d3 . hsl ( "#fedcba" ) ) ( .4 ) , "#ccd3da" ) ;
18- assert . equal ( d3 . interpolate ( "#abcdef" , d3 . lab ( "#fedcba" ) ) ( .4 ) , "#ccd3da" ) ;
31+
32+ "when a and b are color objects" : {
33+ "interpolates RGB values and returns a hexadecimal string" : function ( d3 ) {
34+ assert . strictEqual ( d3 . interpolate ( d3 . rgb ( 255 , 0 , 0 ) , d3 . rgb ( 0 , 128 , 0 ) ) ( .4 ) , "#993300" ) ;
35+ } ,
36+ "interpolates d3.hsl in RGB" : function ( d3 ) {
37+ assert . strictEqual ( d3 . interpolate ( d3 . hsl ( "red" ) , d3 . hsl ( "green" ) ) ( .4 ) , "#993300" ) ;
38+ } ,
39+ "interpolates d3.lab in RGB" : function ( d3 ) {
40+ assert . strictEqual ( d3 . interpolate ( d3 . lab ( "red" ) , d3 . lab ( "green" ) ) ( .4 ) , "#993300" ) ;
41+ } ,
42+ "interpolates d3.hcl in RGB" : function ( d3 ) {
43+ assert . strictEqual ( d3 . interpolate ( d3 . hcl ( "red" ) , d3 . hcl ( "green" ) ) ( .4 ) , "#993300" ) ;
44+ }
45+ } ,
46+
47+ "when a and b are strings" : {
48+ "interpolates matching numbers in both strings" : function ( d3 ) {
49+ assert . strictEqual ( d3 . interpolate ( " 10/20 30" , "50/10 100 " ) ( .4 ) , "26/16 58 " ) ;
50+ } ,
51+ "if a and b are coercible to numbers, interpolates numbers rather than strings" : function ( d3 ) {
52+ assert . strictEqual ( d3 . interpolate ( "1." , "2." ) ( .5 ) , 1.5 ) ;
53+ assert . strictEqual ( d3 . interpolate ( "1e+3" , "1e+4" ) ( .5 ) , 5500 ) ;
54+ } ,
55+ "preserves non-numbers in string b" : function ( d3 ) {
56+ assert . strictEqual ( d3 . interpolate ( " 10/20 30" , "50/10 foo " ) ( .4 ) , "26/16 foo " ) ;
57+ } ,
58+ "preserves non-matching numbers in string b" : function ( d3 ) {
59+ assert . strictEqual ( d3 . interpolate ( " 10/20 bar" , "50/10 100 " ) ( .4 ) , "26/16 100 " ) ;
60+ } ,
61+ "preserves equal-value numbers in both strings" : function ( d3 ) {
62+ assert . strictEqual ( d3 . interpolate ( " 10/20 100 20" , "50/10 100, 20 " ) ( .4 ) , "26/16 100, 20 " ) ;
63+ }
1964 } ,
20- "interpolates strings" : function ( d3 ) {
21- assert . equal ( d3 . interpolate ( "width:10px;" , "width:50px;" ) ( .2 ) , "width:18px;" ) ;
22- assert . equal ( d3 . interpolate ( 2 , "12px" ) ( .4 ) , "6px" ) ;
65+
66+ "when a and b are arrays" : {
67+ "interpolates each element in b" : function ( d3 ) {
68+ assert . strictEqual ( JSON . stringify ( d3 . interpolate ( [ 2 , 4 ] , [ 12 , 24 ] ) ( .4 ) ) , "[6,12]" ) ;
69+ } ,
70+ "interpolates arrays, even when both a and b are coercible to numbers" : function ( d3 ) {
71+ assert . strictEqual ( JSON . stringify ( d3 . interpolate ( [ 2 ] , [ 12 ] ) ( .4 ) ) , "[6]" ) ;
72+ assert . strictEqual ( JSON . stringify ( d3 . interpolate ( [ [ 2 ] ] , [ [ 12 ] ] ) ( .4 ) ) , "[[6]]" ) ;
73+ } ,
74+ "reuses the returned array during interpolation" : function ( d3 ) {
75+ var i = d3 . interpolate ( [ 2 ] , [ 12 ] ) ;
76+ assert . strictEqual ( i ( .2 ) , i ( .4 ) ) ;
77+ }
2378 } ,
24- "interpolates arrays" : function ( d3 ) {
25- assert . deepEqual ( d3 . interpolate ( [ 2 , 4 ] , [ 12 , 24 ] ) ( .4 ) , [ 6 , 12 ] ) ;
79+
80+ "when a and b are objects" : {
81+ "interpolates each property in b" : function ( d3 ) {
82+ assert . deepEqual ( d3 . interpolate ( { foo : 2 , bar : 4 } , { foo : 12 , bar : 24 } ) ( .4 ) , { foo : 6 , bar : 12 } ) ;
83+ } ,
84+ "interpolates arrays, even when both a and b are coercible to numbers" : function ( d3 ) {
85+ var two = new Number ( 2 ) , twelve = new Number ( 12 ) ;
86+ two . foo = "2px" ;
87+ twelve . foo = "12px" ;
88+ assert . deepEqual ( d3 . interpolate ( two , twelve ) ( .4 ) , { foo : "6px" } ) ;
89+ } ,
90+ "reuses the returned object during interpolation" : function ( d3 ) {
91+ var i = d3 . interpolate ( { foo : 2 , bar : 4 } , { foo : 12 , bar : 24 } ) ;
92+ assert . strictEqual ( i ( .2 ) , i ( .4 ) ) ;
93+ }
2694 } ,
27- "interpolates objects" : function ( d3 ) {
28- assert . deepEqual ( d3 . interpolate ( { foo : 2 } , { foo : 12 } ) ( .4 ) , { foo : 6 } ) ;
95+
96+ "when a and b are different types" : {
97+ "coerces both types to strings" : function ( d3 ) {
98+ assert . strictEqual ( d3 . interpolate ( "2" , 12 ) ( .4 ) , 6 ) ;
99+ assert . strictEqual ( d3 . interpolate ( "2px" , 12 ) ( .4 ) , 6 ) ;
100+ assert . strictEqual ( d3 . interpolate ( [ 2 ] , 12 ) ( .4 ) , 6 ) ;
101+ assert . strictEqual ( d3 . interpolate ( { valueOf : function ( ) { return 2 ; } } , 12 ) ( .4 ) , 6 ) ;
102+ assert . strictEqual ( d3 . interpolate ( { toString : function ( ) { return 2 ; } } , 12 ) ( .4 ) , 6 ) ;
103+ }
29104 } ,
105+
30106 "may or may not interpolate between enumerable and non-enumerable properties" : function ( d3 ) {
31107 var a = Object . create ( { } , { foo : { value : 1 , enumerable : true } } ) ,
32108 b = Object . create ( { } , { foo : { value : 2 , enumerable : false } } ) ;
@@ -50,17 +126,20 @@ suite.addBatch({
50126 assert . equal ( d3 . interpolate ( "hasOwnProperty" , "hasOwnProperty" ) ( 0 ) , "hasOwnProperty" ) ;
51127 }
52128 } ,
129+
53130 "interpolators" : {
54131 topic : load ( "interpolate/interpolate" ) . document ( ) ,
55132 "can register a custom interpolator" : function ( d3 ) {
56- d3 . interpolators . push ( function ( a , b ) {
57- return a == "one" && b == "two" && d3 . interpolateNumber ( 1 , 2 ) ;
58- } ) ;
59- assert . equal ( d3 . interpolate ( "one" , "two" ) ( - .5 ) , .5 ) ;
60- assert . equal ( d3 . interpolate ( "one" , "two" ) ( 0 ) , 1 ) ;
61- assert . equal ( d3 . interpolate ( "one" , "two" ) ( .5 ) , 1.5 ) ;
62- assert . equal ( d3 . interpolate ( "one" , "two" ) ( 1 ) , 2 ) ;
63- assert . equal ( d3 . interpolate ( "one" , "two" ) ( 1.5 ) , 2.5 ) ;
133+ d3 . interpolators . push ( function ( a , b ) { return a == "one" && b == "two" && d3 . interpolateNumber ( 1 , 2 ) ; } ) ;
134+ try {
135+ assert . equal ( d3 . interpolate ( "one" , "two" ) ( - .5 ) , .5 ) ;
136+ assert . equal ( d3 . interpolate ( "one" , "two" ) ( 0 ) , 1 ) ;
137+ assert . equal ( d3 . interpolate ( "one" , "two" ) ( .5 ) , 1.5 ) ;
138+ assert . equal ( d3 . interpolate ( "one" , "two" ) ( 1 ) , 2 ) ;
139+ assert . equal ( d3 . interpolate ( "one" , "two" ) ( 1.5 ) , 2.5 ) ;
140+ } finally {
141+ d3 . interpolators . pop ( ) ;
142+ }
64143 }
65144 }
66145} ) ;
0 commit comments