test.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*!
  2. * hex-color-regex <https://github.com/regexps/hex-color-regex>
  3. *
  4. * Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
  5. * Released under the MIT license.
  6. */
  7. /* jshint asi:true */
  8. 'use strict'
  9. var test = require('mukla')
  10. var hexColorRegex = require('./index')
  11. var sixDigits = {
  12. pass: [
  13. '#afebe3',
  14. '#AFEBE3',
  15. '#3cb371',
  16. '#3CB371',
  17. '#556b2f',
  18. '#556B2F',
  19. '#708090',
  20. '#7b68ee',
  21. '#7B68EE',
  22. '#eeeeee',
  23. '#ffffff',
  24. '#123fff}',
  25. '#111111'
  26. ],
  27. fail: [
  28. 'afebe3',
  29. 'AFEBE3',
  30. '3cb371',
  31. 'ABC371',
  32. '556b2f',
  33. '5A6B2F',
  34. '708090',
  35. '7b68ee',
  36. '7B68EE',
  37. 'eeeeee',
  38. 'ffffff',
  39. '111111',
  40. 'afebef',
  41. '3c537f',
  42. '556B2f',
  43. '708135',
  44. 'EE3EF1',
  45. '7f68ZY',
  46. '#7f68ZY',
  47. '#7z68ZY',
  48. '#GR68',
  49. '#Z68',
  50. '#666EFR'
  51. ]
  52. }
  53. var threeDigits = {
  54. pass: [
  55. '#afe',
  56. '#AF3',
  57. '#3cb',
  58. '#3CB',
  59. '#b2f',
  60. '#5B2',
  61. '#708',
  62. '#68e',
  63. '#7AF',
  64. '#777',
  65. '#FFF',
  66. '#fff',
  67. '#f3f}',
  68. '#111'
  69. ],
  70. fail: [
  71. 'fff',
  72. '4zy',
  73. '4g1',
  74. '111',
  75. 'Ge3',
  76. 'zY1',
  77. '#ggg',
  78. '#4zy',
  79. '#4g1',
  80. '#Ge3',
  81. '#zY1'
  82. ]
  83. }
  84. var fourDigits = {
  85. pass: ['#afe0', '#AF31', '#3cba', '#3CBA', '#b2ff', '#5B2F'],
  86. fail: ['afe0', 'AF31', '#3cbg', '#3CBy', '#b2fz']
  87. }
  88. var eightDigits = {
  89. pass: ['#afebe300', '#AFEBE3AA', '#3cb371ff', '#3CB371CC', '#556b2f55'],
  90. fail: ['afebe300', 'AFEBE3AA', '#3cb371fg', '#3CB371xy', '#556b2fz9']
  91. }
  92. test('hex-color-regex:', function() {
  93. test('in no strict mode', function() {
  94. test('six digit hex', function() {
  95. test('should be `true`', function() {
  96. sixDigits.pass.forEach(function(hex) {
  97. test('when `' + hex + '` value', function() {
  98. test.equal(hexColorRegex().test(hex), true)
  99. })
  100. })
  101. test('when `foo #ae3f4c bar` value', function() {
  102. test.equal(hexColorRegex().test('foo #ae3f4c bar'), true)
  103. })
  104. })
  105. test('should be `false`', function() {
  106. sixDigits.fail.forEach(function(hex) {
  107. test('when `' + hex + '` value', function() {
  108. test.equal(hexColorRegex().test(hex), false)
  109. })
  110. })
  111. })
  112. })
  113. test('three digit hex', function() {
  114. test('should be `true`', function() {
  115. threeDigits.pass.forEach(function(hex) {
  116. test('when `' + hex + '` value', function() {
  117. test.equal(hexColorRegex().test(hex), true)
  118. })
  119. })
  120. test('when `foo #e4f bar` value', function() {
  121. test.equal(hexColorRegex().test('foo #e4f bar'), true)
  122. })
  123. })
  124. test('should be `false`', function() {
  125. threeDigits.fail.forEach(function(hex) {
  126. test('when `' + hex + '` value', function() {
  127. test.equal(hexColorRegex().test(hex), false)
  128. })
  129. })
  130. })
  131. })
  132. test('eight digit alpha channel hex', function() {
  133. test('should be `true`', function() {
  134. eightDigits.pass.forEach(function(hex) {
  135. test('when `' + hex + '` value', function() {
  136. test.equal(hexColorRegex().test(hex), true)
  137. })
  138. })
  139. test('when `foo #ae3f4c bar` value', function() {
  140. test.equal(hexColorRegex().test('foo #ae3f4c00 bar'), true)
  141. })
  142. })
  143. test('should be `false`', function() {
  144. eightDigits.fail.forEach(function(hex) {
  145. test('when `' + hex + '` value', function() {
  146. test.equal(hexColorRegex().test(hex), false)
  147. })
  148. })
  149. })
  150. })
  151. test('four digit alpha channel hex', function() {
  152. test('should be `true`', function() {
  153. fourDigits.pass.forEach(function(hex) {
  154. test('when `' + hex + '` value', function() {
  155. test.equal(hexColorRegex().test(hex), true)
  156. })
  157. })
  158. test('when `foo #ae3f4c bar` value', function() {
  159. test.equal(hexColorRegex().test('foo #ae3f bar'), true)
  160. })
  161. })
  162. test('should be `false`', function() {
  163. fourDigits.fail.forEach(function(hex) {
  164. test('when `' + hex + '` value', function() {
  165. test.equal(hexColorRegex().test(hex), false)
  166. })
  167. })
  168. })
  169. })
  170. test('using regex().exec(hex)', function() {
  171. sixDigits.pass.forEach(function(hex) {
  172. var hexed = hex.replace('}', '')
  173. test('should match `' + hexed + '` when `' + hex + '` hex', function() {
  174. var actual = hexColorRegex().exec(hex)[0]
  175. var expected = hexed
  176. test.equal(actual, expected)
  177. })
  178. })
  179. test('should match `#ae3f4c` when `foo #ae3f4c bar` string', function() {
  180. var actual = hexColorRegex().exec('foo #ae3f4c bar')[0]
  181. var expected = '#ae3f4c'
  182. test.equal(actual, expected)
  183. })
  184. threeDigits.pass.forEach(function(hex) {
  185. var hexed = hex.replace('}', '')
  186. test('should match `' + hexed + '` when `' + hex + '` hex', function() {
  187. var actual = hexColorRegex().exec(hex)[0]
  188. var expected = hexed
  189. test.equal(actual, expected)
  190. })
  191. })
  192. test('should match `#e7f` when `foo #e7f bar` string', function() {
  193. var actual = hexColorRegex().exec('foo #e7f bar')[0]
  194. var expected = '#e7f'
  195. test.equal(actual, expected)
  196. })
  197. eightDigits.pass.forEach(function(hex) {
  198. var hexed = hex.replace('}', '')
  199. test('should match `' + hexed + '` when `' + hex + '` hex', function() {
  200. var actual = hexColorRegex().exec(hex)[0]
  201. var expected = hexed
  202. test.equal(actual, expected)
  203. })
  204. })
  205. test('should match `#ae3f4c00` when `foo #ae3f4c00 bar` string', function() {
  206. var actual = hexColorRegex().exec('foo #ae3f4c00 bar')[0]
  207. var expected = '#ae3f4c00'
  208. test.equal(actual, expected)
  209. })
  210. fourDigits.pass.forEach(function(hex) {
  211. var hexed = hex.replace('}', '')
  212. test('should match `' + hexed + '` when `' + hex + '` hex', function() {
  213. var actual = hexColorRegex().exec(hex)[0]
  214. var expected = hexed
  215. test.equal(actual, expected)
  216. })
  217. })
  218. test('should match `#e7f0` when `foo #e7f0 bar` string', function() {
  219. var actual = hexColorRegex().exec('foo #e7f0 bar')[0]
  220. var expected = '#e7f0'
  221. test.equal(actual, expected)
  222. })
  223. })
  224. })
  225. test('in strict mode', function() {
  226. test('six digit hex `#123fff}` should return false', function() {
  227. test.equal(hexColorRegex({ strict: true }).test('#123fff}'), false)
  228. })
  229. test('string contain six digit hex `foo #ae3f4c bar` return false', function() {
  230. test.equal(hexColorRegex({ strict: true }).test('foo #ae3f4c bar'), false)
  231. })
  232. test('three digit hex `#f3f}` should return false', function() {
  233. test.equal(hexColorRegex({ strict: true }).test('#f3f}'), false)
  234. })
  235. test('string contain three digit hex `foo #e7f bar` return false', function() {
  236. test.equal(hexColorRegex({ strict: true }).test('foo #e7f bar'), false)
  237. })
  238. test('eight digit alpha channel hex `#123fff00}` should return false', function() {
  239. test.equal(hexColorRegex({ strict: true }).test('#123fff00}'), false)
  240. })
  241. test('string contain eight digit alpha channel hex `foo #ae3f4cff bar` return false', function() {
  242. test.equal(
  243. hexColorRegex({ strict: true }).test('foo #ae3f4cff bar'),
  244. false
  245. )
  246. })
  247. test('four digit alpha channel hex `#f3f0}` should return false', function() {
  248. test.equal(hexColorRegex({ strict: true }).test('#f3f0}'), false)
  249. })
  250. test('string contain four digit alpha channel hex `foo #e7ff bar` return false', function() {
  251. test.equal(hexColorRegex({ strict: true }).test('foo #e7ff bar'), false)
  252. })
  253. test('should not match when `foo #ae3f4c bar` string', function() {
  254. var actual = hexColorRegex({ strict: true }).exec('foo #ae3f4c bar')
  255. var expected = null
  256. test.equal(actual, expected)
  257. })
  258. test('should not match when `foo #e7f bar` string', function() {
  259. var actual = hexColorRegex({ strict: true }).exec('foo #e7f bar')
  260. var expected = null
  261. test.equal(actual, expected)
  262. })
  263. test('should not match when `foo #ae3f4cff bar` string', function() {
  264. var actual = hexColorRegex({ strict: true }).exec('foo #ae3f4cff bar')
  265. var expected = null
  266. test.equal(actual, expected)
  267. })
  268. test('should not match when `foo #e7ff bar` string', function() {
  269. var actual = hexColorRegex({ strict: true }).exec('foo #e7ff bar')
  270. var expected = null
  271. test.equal(actual, expected)
  272. })
  273. })
  274. })