fixed-column.spec.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import FixedColumn from '../text/column/fixed'
  8. import { ColumnType } from '../../common/column'
  9. const lines = [
  10. '1.123 abc',
  11. '1.00 a',
  12. '1.1 bcd ',
  13. '',
  14. ' 5'
  15. ]
  16. const data = lines.join('\n');
  17. const linesTokens = (function () {
  18. const tokens: number[] = [];
  19. let last = 0;
  20. for (const l of lines) {
  21. tokens.push(last, last + l.length);
  22. last += l.length + 1;
  23. }
  24. if (tokens[tokens.length - 1] > data.length) tokens[tokens.length - 1] = data.length;
  25. return tokens;
  26. }());
  27. describe('fixed text column', () => {
  28. const col = FixedColumn({ data, lines: linesTokens, rowCount: lines.length });
  29. const col1 = col(0, 5, ColumnType.float);
  30. const col2 = col(5, 4, ColumnType.str);
  31. it('number', () => {
  32. expect(col1.value(0)).toBe(1.123);
  33. expect(col1.value(1)).toBe(1.0);
  34. expect(col1.value(2)).toBe(1.1);
  35. expect(col1.value(3)).toBe(0);
  36. expect(col1.value(4)).toBe(5);
  37. })
  38. it('str', () => {
  39. expect(col2.value(0)).toBe('abc');
  40. expect(col2.value(1)).toBe('a');
  41. expect(col2.value(2)).toBe('bc');
  42. expect(col2.value(3)).toBe('');
  43. expect(col2.value(4)).toBe('');
  44. })
  45. });