@@ -18,6 +18,7 @@ import {
1818import * as path from "path" ;
1919import { documentToString } from "resolve-cascade" ;
2020
21+ import { OptiCSSOptions } from "../../src/OpticssOptions" ;
2122import {
2223 IdentGenerator ,
2324 IdentGenerators ,
@@ -27,9 +28,14 @@ import {
2728 testOptimizationCascade ,
2829} from "../util/assertCascade" ;
2930
30- function testRewriteIdents ( templateRewriteOpts : RewritableIdents , ...stylesAndTemplates : Array < string | TestTemplate > ) : Promise < CascadeTestResult > {
31+ function testRewriteIdents ( templateRewriteOpts : RewritableIdents & Pick < OptiCSSOptions , "identifiers" > , ...stylesAndTemplates : Array < string | TestTemplate > ) : Promise < CascadeTestResult > {
32+ let identifiers = templateRewriteOpts . identifiers ;
33+ delete templateRewriteOpts . identifiers ;
3134 return testOptimizationCascade (
32- { only : [ "rewriteIdents" ] } ,
35+ {
36+ only : [ "rewriteIdents" ] ,
37+ identifiers,
38+ } ,
3339 {
3440 rewriteIdents : templateRewriteOpts ,
3541 analyzedAttributes : [ ] ,
@@ -40,22 +46,51 @@ function testRewriteIdents(templateRewriteOpts: RewritableIdents, ...stylesAndTe
4046
4147@suite ( "Rewrite idents" )
4248export class RewriteIdentsTest {
49+ @test "Can select a starting value" ( ) {
50+ for ( let startValue = 1 ; startValue < 20_000_000 ; startValue += Math . round ( Math . random ( ) * 50000 ) ) {
51+ const idGen = new IdentGenerator ( false , startValue ) ;
52+ assert . equal ( idGen . currentValue , startValue ) ;
53+ idGen . nextIdent ( ) ;
54+ assert . equal ( idGen . currentValue , startValue + 1 ) ;
55+ }
56+ }
57+ @test "can specify a max number of idents to generate" ( ) {
58+ let startValue = Math . round ( Math . random ( ) * 20_000_000 ) ;
59+ let maxIdentCount = 100 ;
60+ const idGen = new IdentGenerator ( false , startValue , maxIdentCount ) ;
61+ for ( let i = 0 ; i < maxIdentCount ; i ++ ) {
62+ idGen . nextIdent ( ) ;
63+ }
64+ assert . throws ( ( ) => {
65+ idGen . nextIdent ( ) ;
66+ } ) ;
67+ }
4368 @test "has an ident generator" ( ) {
4469 const idGen = new IdentGenerator ( ) ;
70+ let currentValue = 1 ;
71+ assert . equal ( idGen . currentValue , currentValue ++ ) ;
4572 assert . equal ( idGen . nextIdent ( ) , "a" ) ;
73+ assert . equal ( idGen . currentValue , currentValue ++ ) ;
4674 assert . equal ( idGen . nextIdent ( ) , "b" ) ;
75+ assert . equal ( idGen . currentValue , currentValue ++ ) ;
4776 for ( let i = 2 ; i < 52 ; i ++ ) {
4877 idGen . nextIdent ( ) ;
78+ currentValue ++ ;
4979 }
5080 assert . equal ( idGen . nextIdent ( ) , "a0" ) ;
81+ assert . equal ( idGen . currentValue , currentValue ++ ) ;
5182 for ( let i = 1 ; i < 64 ; i ++ ) {
5283 idGen . nextIdent ( ) ;
84+ currentValue ++ ;
5385 }
5486 assert . equal ( idGen . nextIdent ( ) , "b0" ) ;
87+ assert . equal ( idGen . currentValue , currentValue ++ ) ;
5588 for ( let i = 1 ; i < 64 * 51 ; i ++ ) {
5689 idGen . nextIdent ( ) ;
90+ currentValue ++ ;
5791 }
5892 assert . equal ( idGen . nextIdent ( ) , "a00" ) ;
93+ assert . equal ( idGen . currentValue , currentValue ++ ) ;
5994 }
6095
6196 @test "has an case-insensitive ident generator" ( ) {
@@ -84,7 +119,7 @@ export class RewriteIdentsTest {
84119 }
85120
86121 @test "ident generator set" ( ) {
87- const idGens = new IdentGenerators ( false , "id" , "class" , "state" ) ;
122+ const idGens = new IdentGenerators ( { namespaces : [ "id" , "class" , "state" ] } ) ;
88123 assert . equal ( idGens . nextIdent ( "id" ) , "a" ) ;
89124 assert . equal ( idGens . nextIdent ( "class" ) , "a" ) ;
90125 assert . equal ( idGens . nextIdent ( "state" ) , "a" ) ;
@@ -93,7 +128,7 @@ export class RewriteIdentsTest {
93128 assert . equal ( idGens . nextIdent ( "class" ) , "b" ) ;
94129 assert . equal ( idGens . nextIdent ( "state" ) , "b" ) ;
95130 try {
96- const errorProneGen = new IdentGenerators < string > ( false , "id" , "class" , "state" ) ;
131+ const errorProneGen = new IdentGenerators < string > ( { namespaces : [ "id" , "class" , "state" ] } ) ;
97132 errorProneGen . nextIdent ( "foo" ) ;
98133 assert . fail ( "error expected" ) ;
99134 } catch ( e ) {
@@ -234,4 +269,55 @@ export class RewriteIdentsTest {
234269 }
235270 } ) ;
236271 }
272+ @test "can configure ident start value" ( ) {
273+ let css1 = `
274+ #id3 { border-width: 2px; }
275+ #a { color: blue; }
276+ .a { color: red; }
277+ #id2 { width: 50%; }
278+ .thing2 { border: 1px solid blue; }
279+ .thing3 { background: red; }
280+ div { background-color: white; }
281+ #id3.thing4 { border-color: black; }
282+ ` ;
283+ let template = new TestTemplate ( "test" , clean `
284+ <div class="(thing3 | a)" id="(a | id2)"></div>
285+ <div class="(--- | thing2 | thing4)" id="id3"></div>
286+ ` ) ;
287+ return testRewriteIdents ( { id : true , class : true , identifiers : { startValue : 100 , maxCount : 100 } } , css1 , template ) . then ( result => {
288+ // debugResult(css1, result);
289+ assert . equal ( result . optimization . output . content , `
290+ #aL { border-width: 2px; }
291+ #aM { color: blue; }
292+ .aL { color: red; }
293+ #aN { width: 50%; }
294+ .aM { border: 1px solid blue; }
295+ .aN { background: red; }
296+ div { background-color: white; }
297+ #aL.aO { border-color: black; }
298+ ` ) ;
299+ } ) ;
300+ }
301+ @test async "rejects if there's not enough idents" ( ) {
302+ let css1 = `
303+ #id3 { border-width: 2px; }
304+ #a { color: blue; }
305+ .a { color: red; }
306+ #id2 { width: 50%; }
307+ .thing2 { border: 1px solid blue; }
308+ .thing3 { background: red; }
309+ div { background-color: white; }
310+ #id3.thing4 { border-color: black; }
311+ ` ;
312+ let template = new TestTemplate ( "test" , clean `
313+ <div class="(thing3 | a)" id="(a | id2)"></div>
314+ <div class="(--- | thing2 | thing4)" id="id3"></div>
315+ ` ) ;
316+ try {
317+ await testRewriteIdents ( { id : true , class : true , identifiers : { startValue : 100 , maxCount : 2 } } , css1 , template ) ;
318+ throw new Error ( "Didn't reject promise" ) ;
319+ } catch ( e ) {
320+ assert . equal ( e . message , "Too many identifiers were generated (Max: 2)." ) ;
321+ }
322+ }
237323}
0 commit comments