cNumber.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. let _ = require('lodash');
  3. let hljs = require('../../build');
  4. let utility = require('../utility');
  5. const pattern = new RegExp(`${hljs.C_NUMBER_RE}$`);
  6. let numberToString = utility.numberToString;
  7. describe('.C_NUMBER_RE', function() {
  8. it('should match regular numbers', function() {
  9. const numbers = _.map(_.range(0, 1001), numberToString);
  10. numbers.should.matchEach(pattern);
  11. });
  12. it('should match decimals', function() {
  13. const decimal = _.map(_.range(0, 1.001, 0.001), numberToString);
  14. const noLeadingZero = ['.1234', '.5206', '.0002', '.9998'];
  15. const numbers = [].concat(decimal, noLeadingZero);
  16. numbers.should.matchEach(pattern);
  17. });
  18. it('should match hex numbers', function() {
  19. const numbers = [ '0xbada55', '0xfa1755', '0x45362e', '0xfedcba'
  20. , '0x123456', '0x00000f', '0xfff000', '0xf0e1d2'
  21. ];
  22. numbers.should.matchEach(pattern);
  23. });
  24. it('should not match hex numbers greater than "f"', function() {
  25. const numbers = ['0xgada55', '0xfh1755', '0x45i62e'];
  26. numbers.should.not.matchEach(pattern);
  27. });
  28. it('should not match binary numbers', function() {
  29. const numbers = ['0b0101', '0b1100', '0b1001'];
  30. numbers.should.not.matchEach(pattern);
  31. });
  32. });