2017-07-24 23:52:56 +03:00
const test = require ( "ava" )
const css = require ( process . env . PWD )
2017-07-26 00:52:18 +03:00
const fs = require ( "fs" )
const glob = require ( "glob" )
2017-07-24 23:52:56 +03:00
var selectors , classnames = null
2017-07-26 00:52:18 +03:00
const classRegex = /class="([^"]+)"/ig
2017-07-24 23:52:56 +03:00
2017-07-26 01:25:30 +03:00
// Find unique selectors from the cssstats selector list
2017-07-24 23:52:56 +03:00
function uniqueSelectors ( s ) {
2017-07-26 01:25:30 +03:00
s = s . map ( s => {
2017-07-24 23:52:56 +03:00
// split multi-selectors into last class used .foo .bar .baz
return s . split ( " " ) . pop ( )
2017-07-26 01:25:30 +03:00
} ) . filter ( s => {
// remove any selector that aren't just regular classnames eg. ::hover [type]
return s . match ( /^\.[a-z\-_]+$/ig )
2017-07-24 23:52:56 +03:00
} )
2017-07-26 01:25:30 +03:00
// return only the unique selectors
return [ ... new Set ( s ) ]
2017-07-24 23:52:56 +03:00
}
2017-07-26 01:25:30 +03:00
// From the given glob sources array, read the files and return found classnames
2017-07-24 23:52:56 +03:00
function documentedClassnames ( sources ) {
2017-07-26 00:52:18 +03:00
var cn = [ ]
sources . forEach ( f => {
glob . sync ( f ) . forEach ( g => {
var match = null
2017-07-26 01:25:30 +03:00
// While we match a classRegex in the source
2017-07-26 21:30:00 +03:00
while ( ( match = classRegex . exec ( fs . readFileSync ( g , "utf8" ) ) ) != null ) {
2017-07-26 01:25:30 +03:00
// Get the matched classnames "..." and split by space into classes
2017-07-26 00:52:18 +03:00
cn = cn . concat ( match [ 1 ] . split ( " " ) )
}
} )
} )
2017-07-26 01:25:30 +03:00
// return only the unique classnames
2017-07-26 00:52:18 +03:00
return [ ... new Set ( cn ) ]
2017-07-24 23:52:56 +03:00
}
2017-07-26 01:25:30 +03:00
// Before all the tests get the selectors and classnames
2017-07-26 21:26:41 +03:00
test . before ( t => {
selectors = uniqueSelectors ( css . cssstats . selectors . values )
classnames = documentedClassnames ( [
2017-07-24 23:52:56 +03:00
'docs/*.md' ,
'README.md'
] )
} )
2017-07-26 00:52:18 +03:00
test ( "Every selector class is documented" , t => {
var undocumented = [ ]
selectors . forEach ( selector => {
if ( ! classnames . includes ( selector . replace ( "." , "" ) ) ) {
undocumented . push ( selector )
}
} )
t . is ( undocumented . length , 0 , ` I did not find documentation for the " ${ undocumented . join ( ", " ) } " selector(s) in the ${ process . env . npm _package _name } module. ` ) ;
2017-07-24 23:52:56 +03:00
} )