count-words-in-file.test.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { readFile } from 'node:fs/promises'
  2. import path from 'node:path'
  3. import { countWordsInFile } from '@/features/word-count-modal/utils/count-words-in-file'
  4. import { WordCountData } from '@/features/word-count-modal/components/word-count-data'
  5. import { createSegmenters } from '@/features/word-count-modal/utils/segmenters'
  6. import { expect } from 'chai'
  7. describe('word count', function () {
  8. beforeEach(async function () {
  9. this.data = {
  10. encode: '',
  11. textWords: 0,
  12. headWords: 0,
  13. outside: 0,
  14. headers: 0,
  15. elements: 0,
  16. mathInline: 0,
  17. mathDisplay: 0,
  18. errors: 0,
  19. messages: '',
  20. textCharacters: 0,
  21. headCharacters: 0,
  22. captionWords: 0,
  23. captionCharacters: 0,
  24. footnoteWords: 0,
  25. footnoteCharacters: 0,
  26. abstractWords: 0,
  27. abstractCharacters: 0,
  28. otherWords: 0,
  29. otherCharacters: 0,
  30. } satisfies WordCountData
  31. const content = {
  32. 'word-count.tex': await readFile(
  33. path.join(__dirname, 'word-count.tex'),
  34. 'utf-8'
  35. ),
  36. }
  37. this.projectSnapshot = {
  38. getDocContents(path: keyof typeof content) {
  39. return content[path]
  40. },
  41. }
  42. this.segmenters = createSegmenters('en_US')
  43. })
  44. it('produces correct counts', function () {
  45. countWordsInFile(
  46. this.data,
  47. this.projectSnapshot,
  48. 'word-count.tex',
  49. this.segmenters
  50. )
  51. expect(this.data).to.deep.include({
  52. abstractCharacters: 8,
  53. abstractWords: 2,
  54. captionCharacters: 16,
  55. captionWords: 4,
  56. footnoteCharacters: 8,
  57. footnoteWords: 2,
  58. headCharacters: 296,
  59. headWords: 52,
  60. otherCharacters: 10,
  61. otherWords: 2,
  62. textCharacters: 193,
  63. textWords: 42,
  64. })
  65. })
  66. })