@@ -3,40 +3,41 @@ import { describe, it, expect } from 'vitest';
33import { prettifySmallcaps } from './prettify-smallcaps.ts' ;
44
55describe ( 'prettifySmallcaps' , ( ) => {
6- it ( 'decodes a plain string' , ( ) => {
7- expect ( prettifySmallcaps ( { body : '#"0xca46b9"' , slots : [ ] } ) ) . toBe (
8- '0xca46b9' ,
9- ) ;
10- } ) ;
11-
12- it ( 'decodes a number' , ( ) => {
13- expect ( prettifySmallcaps ( { body : '#42' , slots : [ ] } ) ) . toBe ( 42 ) ;
14- } ) ;
15-
16- it ( 'decodes null' , ( ) => {
17- expect ( prettifySmallcaps ( { body : '#null' , slots : [ ] } ) ) . toBeNull ( ) ;
18- } ) ;
19-
20- it ( 'decodes a boolean' , ( ) => {
21- expect ( prettifySmallcaps ( { body : '#true' , slots : [ ] } ) ) . toBe ( true ) ;
22- } ) ;
23-
24- it ( 'replaces a remotable slot reference' , ( ) => {
25- expect ( prettifySmallcaps ( { body : '#"$0"' , slots : [ 'ko12' ] } ) ) . toBe (
26- '<ko12>' ,
27- ) ;
28- } ) ;
29-
30- it ( 'replaces a remotable slot reference with interface name' , ( ) => {
31- expect (
32- prettifySmallcaps ( { body : '#"$0.Alleged: MyObj"' , slots : [ 'ko12' ] } ) ,
33- ) . toBe ( '<ko12> (Alleged: MyObj)' ) ;
34- } ) ;
35-
36- it ( 'replaces a promise slot reference' , ( ) => {
37- expect ( prettifySmallcaps ( { body : '#"&0"' , slots : [ 'kp42' ] } ) ) . toBe (
38- '<kp42>' ,
39- ) ;
6+ it . each ( [
7+ [ 'plain string' , '#"0xca46b9"' , [ ] , '0xca46b9' ] ,
8+ [ 'number' , '#42' , [ ] , 42 ] ,
9+ [ 'null' , '#null' , [ ] , null ] ,
10+ [ 'boolean' , '#true' , [ ] , true ] ,
11+ ] ) ( 'decodes a %s' , ( _label , body , slots , expected ) => {
12+ expect ( prettifySmallcaps ( { body, slots } ) ) . toStrictEqual ( expected ) ;
13+ } ) ;
14+
15+ it . each ( [
16+ [ 'remotable' , '#"$0"' , [ 'ko12' ] , '<ko12>' ] ,
17+ [
18+ 'remotable with iface' ,
19+ '#"$0.Alleged: MyObj"' ,
20+ [ 'ko12' ] ,
21+ '<ko12> (Alleged: MyObj)' ,
22+ ] ,
23+ [ 'promise' , '#"&0"' , [ 'kp42' ] , '<kp42>' ] ,
24+ [ 'missing slot index' , '#"$5"' , [ 'ko1' ] , '<?5>' ] ,
25+ ] ) ( 'replaces a %s slot reference' , ( _label , body , slots , expected ) => {
26+ expect ( prettifySmallcaps ( { body, slots } ) ) . toBe ( expected ) ;
27+ } ) ;
28+
29+ it . each ( [
30+ [ 'escaped string (!)' , '#"!$0"' , '$0' ] ,
31+ [ 'double escape (!!)' , '#"!!hello"' , '!hello' ] ,
32+ [ 'non-negative bigint (+)' , '#"+7"' , '7n' ] ,
33+ [ 'negative bigint (-)' , '#"-7"' , '-7n' ] ,
34+ [ '#undefined' , '#"#undefined"' , '[undefined]' ] ,
35+ [ '#NaN' , '#"#NaN"' , '[NaN]' ] ,
36+ [ '#Infinity' , '#"#Infinity"' , '[Infinity]' ] ,
37+ [ '#-Infinity' , '#"#-Infinity"' , '[-Infinity]' ] ,
38+ [ 'symbol (%)' , '#"%foo"' , '[Symbol: foo]' ] ,
39+ ] ) ( 'decodes %s' , ( _label , body , expected ) => {
40+ expect ( prettifySmallcaps ( { body, slots : [ ] } ) ) . toBe ( expected ) ;
4041 } ) ;
4142
4243 it ( 'decodes an object with mixed values' , ( ) => {
@@ -60,66 +61,12 @@ describe('prettifySmallcaps', () => {
6061 } ) ;
6162 } ) ;
6263
63- it ( 'strips smallcaps escape prefix from strings' , ( ) => {
64- expect ( prettifySmallcaps ( { body : '#"!$0"' , slots : [ 'ko1' ] } ) ) . toBe ( '$0' ) ;
65- } ) ;
66-
67- it ( 'strips double escape prefix' , ( ) => {
68- expect ( prettifySmallcaps ( { body : '#"!!hello"' , slots : [ ] } ) ) . toBe ( '!hello' ) ;
69- } ) ;
70-
7164 it ( 'leaves non-slot strings unchanged' , ( ) => {
7265 expect (
7366 prettifySmallcaps ( { body : '#{"text":"hello world"}' , slots : [ ] } ) ,
7467 ) . toStrictEqual ( { text : 'hello world' } ) ;
7568 } ) ;
7669
77- it ( 'handles missing slot index gracefully' , ( ) => {
78- expect ( prettifySmallcaps ( { body : '#"$5"' , slots : [ 'ko1' ] } ) ) . toBe ( '<?5>' ) ;
79- } ) ;
80-
81- it ( 'throws if body does not start with #' , ( ) => {
82- expect ( ( ) => prettifySmallcaps ( { body : '"hello"' , slots : [ ] } ) ) . toThrow (
83- "Expected body to start with '#'" ,
84- ) ;
85- } ) ;
86-
87- it ( 'decodes a non-negative bigint' , ( ) => {
88- expect ( prettifySmallcaps ( { body : '#"+7"' , slots : [ ] } ) ) . toBe ( '7n' ) ;
89- } ) ;
90-
91- it ( 'decodes a negative bigint' , ( ) => {
92- expect ( prettifySmallcaps ( { body : '#"-7"' , slots : [ ] } ) ) . toBe ( '-7n' ) ;
93- } ) ;
94-
95- it ( 'decodes #undefined' , ( ) => {
96- expect ( prettifySmallcaps ( { body : '#"#undefined"' , slots : [ ] } ) ) . toBe (
97- '[undefined]' ,
98- ) ;
99- } ) ;
100-
101- it ( 'decodes #NaN' , ( ) => {
102- expect ( prettifySmallcaps ( { body : '#"#NaN"' , slots : [ ] } ) ) . toBe ( '[NaN]' ) ;
103- } ) ;
104-
105- it ( 'decodes #Infinity' , ( ) => {
106- expect ( prettifySmallcaps ( { body : '#"#Infinity"' , slots : [ ] } ) ) . toBe (
107- '[Infinity]' ,
108- ) ;
109- } ) ;
110-
111- it ( 'decodes #-Infinity' , ( ) => {
112- expect ( prettifySmallcaps ( { body : '#"#-Infinity"' , slots : [ ] } ) ) . toBe (
113- '[-Infinity]' ,
114- ) ;
115- } ) ;
116-
117- it ( 'decodes a symbol' , ( ) => {
118- expect ( prettifySmallcaps ( { body : '#"%foo"' , slots : [ ] } ) ) . toBe (
119- '[Symbol: foo]' ,
120- ) ;
121- } ) ;
122-
12370 it ( 'decodes a tagged value' , ( ) => {
12471 expect (
12572 prettifySmallcaps ( {
@@ -129,22 +76,15 @@ describe('prettifySmallcaps', () => {
12976 ) . toStrictEqual ( { '[Tagged: match:any]' : '[undefined]' } ) ;
13077 } ) ;
13178
132- it ( 'decodes an error' , ( ) => {
133- expect (
134- prettifySmallcaps ( {
135- body : '#{"#error":"boom","name":"TypeError"}' ,
136- slots : [ ] ,
137- } ) ,
138- ) . toBe ( '[TypeError: boom]' ) ;
139- } ) ;
140-
141- it ( 'decodes an error without name' , ( ) => {
142- expect (
143- prettifySmallcaps ( {
144- body : '#{"#error":"something broke"}' ,
145- slots : [ ] ,
146- } ) ,
147- ) . toBe ( '[Error: something broke]' ) ;
79+ it . each ( [
80+ [ 'with name' , '#{"#error":"boom","name":"TypeError"}' , '[TypeError: boom]' ] ,
81+ [
82+ 'without name' ,
83+ '#{"#error":"something broke"}' ,
84+ '[Error: something broke]' ,
85+ ] ,
86+ ] ) ( 'decodes an error %s' , ( _label , body , expected ) => {
87+ expect ( prettifySmallcaps ( { body, slots : [ ] } ) ) . toBe ( expected ) ;
14888 } ) ;
14989
15090 it ( 'unescapes record keys' , ( ) => {
@@ -155,4 +95,10 @@ describe('prettifySmallcaps', () => {
15595 } ) ,
15696 ) . toStrictEqual ( { '#foo' : 'bar' , normal : 'baz' } ) ;
15797 } ) ;
98+
99+ it ( 'throws if body does not start with #' , ( ) => {
100+ expect ( ( ) => prettifySmallcaps ( { body : '"hello"' , slots : [ ] } ) ) . toThrow (
101+ "Expected body to start with '#'" ,
102+ ) ;
103+ } ) ;
158104} ) ;
0 commit comments